Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CPU performance counter for a piece of code

As we all know, perf is the tool to get the CPU performance counter for a program, such as cache-miss, cache-reference, instruction executed etc.

Question : How to get those performance counters for just a piece of code (such as a function) in one program in c or c++.
For example, my program firstly do some initializing, then do the work, then finalize, i just want to get the performance counter for the work, such as function do_something_1 .

int main(int argc, char ** argv) {
    do_initialize();
    for (int i = 0;i < 100 ;i ++) {
        /* begin profile code */
        do_something_1();
        /* end profile code */
        do_something_2();
    } 
    do_finalize();
}
like image 244
pgplus1628 Avatar asked Jun 08 '15 12:06

pgplus1628


1 Answers

Finally, i found a library to get those counter for a piece of code.

PAPI

For example, if you want to measure L3 data cache read for some piece of code.

#include "papi.h"
#include <iostream>
#include <glog/logging.h>

#define ASIZE 2684354560

#define event_count (1) // the number of event you want to trace

int main(int argc, char ** argv) {

  int events[event_count] = {PAPI_L3_DCR}; // L3 Data Cache Read
  int ret;
  long long int values[event_count]; // result  

  int* array = new int [ASIZE ];

  /* start counters */
  ret = PAPI_start_counters(events, event_count);
  CHECK_EQ(ret, PAPI_OK);

  size_t tot_cnt = 1;
  for(size_t cnt = 0; cnt < tot_cnt; cnt ++) {
    for(size_t i = 0;i < ASIZE ;i ++) {
      array[i] = i;
    }
  }

  /* read counters */
  ret = PAPI_read_counters(values, event_count);
  CHECK_EQ(ret, PAPI_OK);

  for(size_t i = 0;i < event_count ;i ++) {
    LOG(INFO) << " " << values[i];
  }
  return 0;
}

Makefile :

CXX?=g++
INC?=-I<path to where papi is installed>/include/
LIB?=-L<path to where papi is installed>/lib/ -lpapi -lglog

main : main.cpp
  ${CXX} -O3 ${INC} -o $@ $< ${LIB}

all : main

.PHONY:
clean :
  rm -f main
like image 117
pgplus1628 Avatar answered Oct 05 '22 23:10

pgplus1628