Use VTuneAPI in your code to profile without launching VTune™ Analyzer

Intel® VTune™ Performance Analyzer provides VTune APIs, allow profiling which is controlled by the user. The objective is to collect samples in interest of user's code when data collecting

The article An Introduction to Sampling and Time introduces how to use Pause and Resume API at page 8. However it also informed the user to check on "Start with Data Collection Paused" on GUI at page 9.

Believe me that VTune APIs can work without VTune™ Performance Analyzer. I mean that you can get result file by running your application, and analyze data in VTune™ Analyzer later.

How smart it is! How simple it is! You don't need to run VTune™ Analyzer to get performance data. Everything is controlled by the user, including Sampling Configuration.  

See below example -

// testVTuneAPI.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "VtuneApi.h"

int _tmain(int argc, _TCHAR* argv[])
{
 U32 u32Return;

 VTUNE_EVENT events[] = {
         { 10000, 0, 0, 0, "CPU_CLK_UNHALTED.CORE" },  // Sample After Value, Reserved, Reserved, Return Status, Event Name - my system is Intel® Core™ 2 Duo
         { 10000, 0, 0, 0, "INST_RETIRED.ANY" },             //  Use "vtl query -c sampling" to know all supported events in your system, in command prompt
     };
 
     U32 numEvents = sizeof(events) / sizeof(VTUNE_EVENT);
     VTUNE_SAMPLING_PARAMS params = {
         sizeof(VTUNE_SAMPLING_PARAMS),
         sizeof(VTUNE_EVENT),
         0, // sampling options
         0, // reserved
         0, // If 1, start sampling in pause mode
         0, // maximum number of samples to be collected. 
         1000, // number of samples per buffer
         40,  // the sampling interval in milliseconds  
         1,  // "1" for event based sampling
         numEvents,  // number of events
         events, // event list array
         "results.tb5", // result file with or without full path
         0, // reserved
         0, // "1" - generate a count file "tb5Filename.txt"
         "" // CPU mask
     };
 
     u32Return = VTStartSampling(&params); // start sampling since it is not in pause mode
     if (u32Return) {
         printf("Can't start sampling\n");
         exit(0);
     }
     for (int i=0; i<10000000L; i++);  // do some work, data is captured

     VTPauseSampling();  // pause data collection

     for (int i=0; i<10000000L; i++); // do some work, data is NOT captured

     VTResumeSampling(); // resume data collection
     
     for (int i=0; i<10000000L; i++); // do some work, data is captured again 


     u32Return = VTStopSampling(1); // stop collecting data and write result into TB5 file, use VTune™ Analyzer to import it!
    
     if (u32Return) {
         printf("Can't stop sampling\n");
         exit(0);
     }


     return 0;
}

For more complete information about compiler optimizations, see our Optimization Notice.