32 #include <sys/types.h>
44 #include <sys/ptrace.h>
48 #include <sys/resource.h>
64 #define MAX_EVT_NAME_LEN 256
65 #define NUM_PMCS PFMLIB_MAX_PMCS
66 #define NUM_PMDS PFMLIB_MAX_PMDS
67 #define FMT_NAME PFM_DFL_SMPL_NAME
68 #define BPL (sizeof(uint64_t)<<3)
71 #define SYM_NAME_MAX_LENGTH 10000
72 #define MAX_OUTPUT_FILENAME_LENGTH 1024
73 #define MAX_EVENT_NAME_LENGTH 500
74 #define MAX_PREFIX_NAME_LENGTH 1024
75 #define FILENAME_MAX_LENGTH 1024
77 #define MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS 4
79 #define cpuid(func,ax,bx,cx,dx) __asm__ __volatile__ ("cpuid": "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
102 template <
typename T>
103 inline T function_cast(
void *p) {
109 return caster.function;
114 typedef void (*pfm_stop_t)(
int);
116 typedef void (*pfm_self_stop_t)(
int);
117 pfm_self_stop_t pfm_self_stop;
138 typedef char* (*pfm_strerror_t)(
int);
142 typedef pfm_err_t (*pfm_get_num_counters_t)(
unsigned int *);
144 static PFMon &instance() {
153 handle = dlopen(
"libpfm.so", RTLD_NOW);
154 if (handle) { loaded =
true; }
else { loaded =
false; }
156 pfm_start = function_cast<pfm_start_t>(dlsym(handle,
"pfm_start"));
157 pfm_stop = function_cast<pfm_stop_t>(dlsym(handle,
"pfm_stop"));
158 pfm_self_stop = function_cast<pfm_self_stop_t>(dlsym(handle,
"pfm_stop"));
159 pfm_restart = function_cast<pfm_restart_t>(dlsym(handle,
"pfm_restart"));
160 pfm_read_pmds = function_cast<pfm_read_pmds_t>(dlsym(handle,
"pfm_read_pmds"));
161 pfm_initialize = function_cast<pfm_initialize_t>(dlsym(handle,
"pfm_initialize"));
162 pfm_find_full_event = function_cast<pfm_find_full_event_t>(dlsym(handle,
"pfm_find_full_event"));
163 pfm_dispatch_events = function_cast<pfm_dispatch_events_t>(dlsym(handle,
"pfm_dispatch_events"));
164 pfm_create_context = function_cast<pfm_create_context_t>(dlsym(handle,
"pfm_create_context"));
165 pfm_write_pmcs = function_cast<pfm_write_pmcs_t>(dlsym(handle,
"pfm_write_pmcs"));
166 pfm_write_pmds = function_cast<pfm_write_pmds_t>(dlsym(handle,
"pfm_write_pmds"));
167 pfm_load_context = function_cast<pfm_load_context_t>(dlsym(handle,
"pfm_load_context"));
168 pfm_strerror = function_cast<pfm_strerror_t>(dlsym(handle,
"pfm_strerror"));
169 pfm_set_options = function_cast<pfm_set_options_t>(dlsym(handle,
"pfm_set_options"));
170 pfm_get_num_counters = function_cast<pfm_get_num_counters_t>(dlsym(handle,
"pfm_get_num_counters"));
176 if (handle) dlclose(handle);
179 static PFMon s_instance;
182 PFMon PFMon::s_instance;
220 #pragma warning(push)
221 #pragma warning(disable:593)
225 int sse4_2_mask = 1 << 20;
226 if(c & sse4_2_mask)
return 1;
else return 0;
274 m_pfm(PFMon::instance()),
396 void pfm_bv_copy(uint64_t *d, uint64_t *j, uint16_t
n){
if(n<=
BPL) *d = *j;
else {memcpy(d, j, (n>>
LBPL)*
sizeof(uint64_t));}}
398 static void sigio_handler(
int,
struct siginfo *,
struct sigcontext *);
423 memset(&
ctx,0,
sizeof(
ctx));
424 memset(&
inp,0,
sizeof(
inp));
426 memset(
pd, 0,
sizeof(
pd));
427 memset(
pc, 0,
sizeof(
pc));
477 fd =
m_pfm.pfm_create_context(&
ctx, NULL, 0, 0);
493 log <<
MSG::ERROR <<
"ERROR: Could not load context. Aborting..." <<
endmsg;
557 for(
int j=0; j<(
int)strlen(filename); j++)
567 strcpy(to_cat,
"_INV_1");
571 sprintf(to_cat,
"%s_CMASK_%d", to_cat,
cmask[i]);
573 sprintf(filename,
"%s%s.txt", filename, to_cat);
575 outfile = fopen(filename,
"w");
578 fprintf(outfile,
"NHM ");
582 fprintf(outfile,
"WSM ");
586 fprintf(outfile,
"CORE ");
591 fprintf(outfile,
"%s\n", (it->first).c_str());
594 fprintf(outfile,
"%lu\n", *j);
661 printf(
"skipping identical set of samples...\n");
666 pos = (
unsigned long)ent;
697 PFMon& pfm = PFMon::instance();
705 if(pfm.pfm_read_pmds(fd, pd_smpl+1, 1) == -1)
711 r = read(fd, &msg,
sizeof(msg));
714 if(r==-1 &&
errno==EINTR)
716 printf(
"read interrupted, retrying\n");
728 if(pfm.pfm_restart(fd))
736 printf(
"pfm_restart: task probably terminated \n");
772 struct sigaction act;
773 memset(&act, 0,
sizeof(act));
776 sigaction(SIGIO, &act, 0);
777 memset(&
ctx, 0,
sizeof(
ctx));
779 memset(&
inp,0,
sizeof(
inp));
781 memset(pd_smpl, 0,
sizeof(pd_smpl));
782 memset(
pc, 0,
sizeof(
pc));
833 pfm_bv_set(pd_smpl[0].reg_smpl_pmds, pd_smpl[
i].reg_num);
859 log <<
MSG::ERROR <<
"ERROR: Your kernel does not have performance monitoring support! Aborting..." <<
endmsg;
861 log <<
MSG::ERROR <<
"ERROR: Can't create PFM context " << strerror(
errno) <<
". Aborting..." <<
endmsg;
866 log <<
MSG::ERROR <<
"ERROR: cannot mmap sampling buffer: " << strerror(
errno) <<
". Aborting..." <<
endmsg;
871 log <<
MSG::ERROR <<
"ERROR: invalid buffer format version. Aborting..." <<
endmsg;
875 log <<
MSG::ERROR <<
"ERROR: pfm_write_pmcs error errno " << strerror(
errno) <<
". Aborting..." <<
endmsg;
879 log <<
MSG::ERROR <<
"ERROR: pfm_write_pmds error errno " << strerror(
errno) <<
". Aborting..." <<
endmsg;
884 log <<
MSG::ERROR <<
"ERROR: pfm_load_context error errno " << strerror(
errno) <<
". Aborting..." <<
endmsg;
934 for(
int j=0; j<(
int)strlen(filename); j++)
944 strcpy(to_cat,
"_INV_1");
948 sprintf(to_cat,
"%s_CMASK_%d", to_cat,
cmask[i]);
950 sprintf(filename,
"%s%s.txt.gz", filename, to_cat);
951 outfile = gzopen(filename,
"wb");
956 gzprintf(outfile,
"NHM ");
960 gzprintf(outfile,
"WSM ");
964 gzprintf(outfile,
"CORE ");
968 log <<
MSG::ERROR <<
"ERROR: gzputs err: " << gzerror(outfile, &err) <<
". Aborting..." <<
endmsg;
972 unsigned long long sum = 0;
977 if(gzprintf(outfile,
"%s%%%llu\n", (it->first).c_str(),
sum) < (
int)((it->first).length()))
979 log <<
MSG::ERROR <<
"ERROR: gzputs err: " << gzerror(outfile, &err) <<
". Aborting..." <<
endmsg;
986 const char *symbolName;
995 if(symbolName!=NULL && strlen(symbolName)>0)
997 strcpy(sym_name, symbolName);
998 strcat(sym_name,
" ");
1002 strcpy(sym_name,
"??? ");
1004 if(libName!=NULL && strlen(libName)>0)
1006 strcat(sym_name, libName);
1007 strcat(sym_name,
" ");
1011 strcat(sym_name,
"??? ");
1013 sprintf(sym_name,
"%s%d ", sym_name, libOffset);
1014 if(strlen(sym_name)<=0)
1016 log <<
MSG::ERROR <<
"ERROR: Symbol name length is zero. Aborting..." <<
endmsg;
1021 strcpy(sym_name,
"??? ??? 0 ");
1026 strcpy(sym_name,
"??? ??? 0 ");
1028 if(gzprintf(outfile,
"%s %d\n", sym_name, jt->second) < (
int)strlen(sym_name))
1030 log <<
MSG::ERROR <<
"ERROR: gzputs err: " << gzerror(outfile, &err) <<
endmsg;
1037 log <<
MSG::ERROR <<
"ERROR: Could not open file: " << filename <<
". Aborting..." <<
endmsg;
1134 if(!alg_stack.
empty())
1164 if(!alg_stack.
empty())