The Gaudi Framework  v36r1 (3e2fb5a8)
PfmCodeAnalyser.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef PfmCodeAnalyserH
12 #define PfmCodeAnalyserH 1
13 
14 #include <perfmon/perfmon.h>
16 #include <perfmon/pfmlib.h>
17 #include <perfmon/pfmlib_core.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #define MAX_EVT_NAME_LEN 256
26 #define NUM_PMCS PFMLIB_MAX_PMCS
27 #define NUM_PMDS PFMLIB_MAX_PMDS
28 #define FMT_NAME PFM_DFL_SMPL_NAME
29 #define MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS 4
30 #define cpuid( func, eax, ebx, ecx, edx ) \
31  __asm__ __volatile__( "cpuid" : "=a"( eax ), "=b"( ebx ), "=c"( ecx ), "=d"( edx ) : "a"( func ) );
32 
34 private:
36  int nehalem;
43  int fd;
49  int ret;
50  unsigned i;
52  unsigned count;
54 
55 private:
56  PfmCodeAnalyser( const char* event0, unsigned int cmask_v0, bool inv_v0, const char* event1, unsigned int cmask_v1,
57  bool inv_v1, const char* event2, unsigned int cmask_v2, bool inv_v2, const char* event3,
58  unsigned int cmask_v3, bool inv_v3 ); // constructor hidden
59  PfmCodeAnalyser( PfmCodeAnalyser const& ); // copy constructor hidden
60  PfmCodeAnalyser& operator=( PfmCodeAnalyser const& ); // assign operator hidden
61  ~PfmCodeAnalyser(); // destructor hidden
62 
63 public:
64  static PfmCodeAnalyser& Instance( const char* event0 = "UNHALTED_CORE_CYCLES", unsigned int cmask_v0 = 0,
65  bool inv_v0 = false, const char* event1 = "", unsigned int cmask_v1 = 0,
66  bool inv_v1 = false, const char* event2 = "", unsigned int cmask_v2 = 0,
67  bool inv_v2 = false, const char* event3 = "", unsigned int cmask_v3 = 0,
68  bool inv_v3 = false );
69  void start();
70  void stop();
71  void stop_init();
72 };
73 
74 PfmCodeAnalyser::PfmCodeAnalyser( const char* event0, unsigned int cmask_v0, bool inv_v0, const char* event1,
75  unsigned int cmask_v1, bool inv_v1, const char* event2, unsigned int cmask_v2,
76  bool inv_v2, const char* event3, unsigned int cmask_v3, bool inv_v3 ) {
77  int ax, bx, cx, dx;
78  cpuid( 1, ax, bx, cx, dx );
79  int sse4_2_mask = 1 << 20;
80  nehalem = cx & sse4_2_mask;
81  strcpy( event_str[0], event0 );
82  strcpy( event_str[1], event1 );
83  strcpy( event_str[2], event2 );
84  strcpy( event_str[3], event3 );
85  cmask[0] = cmask_v0;
86  cmask[1] = cmask_v1;
87  cmask[2] = cmask_v2;
88  cmask[3] = cmask_v3;
89  inv[0] = inv_v0;
90  inv[1] = inv_v1;
91  inv[2] = inv_v2;
92  inv[3] = inv_v3;
94  for ( int i = 0; i < MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS; i++ ) {
95  if ( strlen( event_str[i] ) > 0 ) used_counters_number++;
96  }
97  for ( int i = 0; i < used_counters_number; i++ ) {
98  sum[i] = 0;
99  overhead_avg[i] = 0;
100  }
101  count = 0;
102  if ( pfm_initialize() != PFMLIB_SUCCESS ) {
103  printf( "Cannot initialize perfmon!!\nExiting...\n" );
104  exit( 0 );
105  }
106  while ( count < 3 ) {
107  start();
108  stop_init();
109  }
110  for ( int i = 0; i < used_counters_number; i++ ) { sum[i] = 0; }
111  count = 0;
112  while ( count < 10 ) {
113  start();
114  stop_init();
115  }
116  for ( int i = 0; i < used_counters_number; i++ ) {
117  overhead_avg[i] = sum[i] / count;
118  sum[i] = 0;
119  }
120  count = 0;
121 }
122 
123 PfmCodeAnalyser& PfmCodeAnalyser::Instance( const char* event0, unsigned int cmask_v0, bool inv_v0, const char* event1,
124  unsigned int cmask_v1, bool inv_v1, const char* event2,
125  unsigned int cmask_v2, bool inv_v2, const char* event3,
126  unsigned int cmask_v3, bool inv_v3 ) {
127  static PfmCodeAnalyser theSingleton( event0, cmask_v0, inv_v0, event1, cmask_v1, inv_v1, event2, cmask_v2, inv_v2,
128  event3, cmask_v3, inv_v3 );
129  return theSingleton;
130 }
131 
132 // start()
133 // initializes all the necessary structures to start the actual counting, calling pfm_start()
135  memset( &ctx, 0, sizeof( ctx ) );
136  memset( &inp, 0, sizeof( inp ) );
137  memset( &outp, 0, sizeof( outp ) );
138  memset( pd, 0, sizeof( pd ) );
139  memset( pc, 0, sizeof( pc ) );
140  memset( &load_arg, 0, sizeof( load_arg ) );
141  memset( &params, 0, sizeof( params ) );
142  for ( int i = 0; i < used_counters_number; i++ ) {
144  if ( ret != PFMLIB_SUCCESS ) {
145  fprintf( stderr, "ERROR: cannot find event: %s\naborting...\n", event_str[i] );
146  exit( 1 );
147  }
148  }
151  for ( int i = 0; i < used_counters_number; i++ ) {
152  if ( inv[i] ) {
155  }
156  if ( cmask[i] > 0 ) {
157  ( params.pfp_core_counters[i] ).cnt_mask = cmask[i];
158  ( nhm_params.pfp_nhm_counters[i] ).cnt_mask = cmask[i];
159  }
160  }
161  if ( nehalem ) {
162  ret = pfm_dispatch_events( &inp, &nhm_params, &outp, NULL );
163  } else {
164  ret = pfm_dispatch_events( &inp, &params, &outp, NULL );
165  }
166  if ( ret != PFMLIB_SUCCESS ) {
167  fprintf( stderr, "ERROR: cannot dispatch events: %s\naborting...\n", pfm_strerror( ret ) );
168  exit( 1 );
169  }
170  for ( unsigned int i = 0; i < outp.pfp_pmc_count; i++ ) {
173  }
174  for ( unsigned int i = 0; i < outp.pfp_pmd_count; i++ ) {
176  pd[i].reg_value = 0;
177  }
178  fd = pfm_create_context( &ctx, NULL, 0, 0 );
179  if ( fd == -1 ) {
180  fprintf( stderr, "ERROR: Context not created\naborting...\n" );
181  exit( 1 );
182  }
183  if ( pfm_write_pmcs( fd, pc, outp.pfp_pmc_count ) == -1 ) {
184  fprintf( stderr, "ERROR: Could not write pmcs\naborting...\n" );
185  exit( 1 );
186  }
187  if ( pfm_write_pmds( fd, pd, outp.pfp_pmd_count ) == -1 ) {
188  fprintf( stderr, "ERROR: Could not write pmds\naborting...\n" );
189  exit( 1 );
190  }
191  load_arg.load_pid = getpid();
192  if ( pfm_load_context( fd, &load_arg ) == -1 ) {
193  fprintf( stderr, "ERROR: Could not load context\naborting...\n" );
194  exit( 1 );
195  }
196  pfm_start( fd, NULL );
197 }
198 
199 // stop()
200 // const ModuleDescription& desc : description of the module that just finished its execution (we are only interested in
201 // its name)
202 // stops the counting calling pfm_stop() and stores the counting results into the "results" map
204  pfm_stop( fd );
205  if ( pfm_read_pmds( fd, pd, inp.pfp_event_count ) == -1 ) {
206  fprintf( stderr, "ERROR: Could not read pmds\naborting...\n" );
207  exit( 1 );
208  }
209 
210  for ( int i = 0; i < used_counters_number; i++ ) { sum[i] += ( pd[i].reg_value - overhead_avg[i] ); }
211  count++;
212  close( fd );
213 }
214 
216  pfm_stop( fd );
217  if ( pfm_read_pmds( fd, pd, inp.pfp_event_count ) == -1 ) {
218  fprintf( stderr, "ERROR: Could not read pmds\naborting...\n" );
219  exit( 1 );
220  }
221 
222  for ( int i = 0; i < used_counters_number; i++ ) { sum[i] += ( pd[i].reg_value ); }
223  count++;
224  close( fd );
225 }
226 
228  for ( int i = 0; i < used_counters_number; i++ ) {
229  printf( "Event: %s\nTotal count:%lu\nNumber of counts:%u\nAverage count:%f\nOverhead removed:%u\n", event_str[i],
230  sum[i], count, (double)sum[i] / count, overhead_avg[i] );
231  }
232 }
233 
234 #endif // PfmCodeAnalyserH
pfm_start
os_err_t pfm_start(int fd, pfarg_start_t *start)
pfmlib_nhm_input_param_t::pfp_nhm_counters
pfmlib_nhm_counter_t pfp_nhm_counters[PMU_NHM_NUM_COUNTERS]
Definition: pfmlib_intel_nhm.h:139
pfarg_pmd_t
Definition: perfmon_v2.h:38
pfmlib_core_input_param_t::pfp_core_counters
pfmlib_core_counter_t pfp_core_counters[PMU_CORE_NUM_COUNTERS]
Definition: pfmlib_core.h:79
PfmCodeAnalyser::pc
pfarg_pmc_t pc[NUM_PMCS]
Definition: PfmCodeAnalyser.h:41
pfm_write_pmds
os_err_t pfm_write_pmds(int fd, pfarg_pmd_t *pmds, int count)
pfarg_ctx_t
Definition: perfmon_v2.h:18
PfmCodeAnalyser::~PfmCodeAnalyser
~PfmCodeAnalyser()
Definition: PfmCodeAnalyser.h:227
pfmlib_output_param_t::pfp_pmd_count
unsigned int pfp_pmd_count
Definition: pfmlib.h:127
pfmlib_input_param_t::pfp_events
pfmlib_event_t pfp_events[PFMLIB_MAX_PMCS]
Definition: pfmlib.h:112
pfarg_pmc_t::reg_num
uint16_t reg_num
Definition: perfmon_v2.h:28
PfmCodeAnalyser::sum
unsigned long sum[MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS]
Definition: PfmCodeAnalyser.h:51
pfmlib_core_input_param_t
Definition: pfmlib_core.h:78
pfarg_pmc_t
Definition: perfmon_v2.h:27
PfmCodeAnalyser::inv
bool inv[MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS]
Definition: PfmCodeAnalyser.h:45
PfmCodeAnalyser::overhead_avg
unsigned overhead_avg[MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS]
Definition: PfmCodeAnalyser.h:53
PfmCodeAnalyser::outp
pfmlib_output_param_t outp
Definition: PfmCodeAnalyser.h:38
pfarg_pmd_t::reg_value
uint64_t reg_value
Definition: perfmon_v2.h:42
PfmCodeAnalyser::cmask
unsigned int cmask[MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS]
Definition: PfmCodeAnalyser.h:46
pfarg_pmc_t::reg_value
uint64_t reg_value
Definition: perfmon_v2.h:31
PfmCodeAnalyser::pd
pfarg_pmd_t pd[NUM_PMDS]
Definition: PfmCodeAnalyser.h:40
PfmCodeAnalyser
Definition: PfmCodeAnalyser.h:33
pfm_read_pmds
os_err_t pfm_read_pmds(int fd, pfarg_pmd_t *pmds, int count)
NUM_PMDS
#define NUM_PMDS
Definition: PfmCodeAnalyser.h:27
pfm_initialize
pfm_err_t pfm_initialize(void)
pfm_strerror
char * pfm_strerror(int code)
pfarg_load_t
Definition: perfmon_v2.h:68
pfmlib_nhm_input_param_t
Definition: pfmlib_intel_nhm.h:138
PfmCodeAnalyser::stop_init
void stop_init()
Definition: PfmCodeAnalyser.h:215
pfmlib_reg_t::reg_value
unsigned long long reg_value
Definition: pfmlib.h:97
PfmCodeAnalyser::i
unsigned i
Definition: PfmCodeAnalyser.h:50
MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS
#define MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS
Definition: PfmCodeAnalyser.h:29
PfmCodeAnalyser::inp
pfmlib_input_param_t inp
Definition: PfmCodeAnalyser.h:37
PFM_PLM3
#define PFM_PLM3
Definition: pfmlib.h:53
pfmlib_core.h
PfmCodeAnalyser::stop
void stop()
Definition: PfmCodeAnalyser.h:203
pfm_create_context
os_err_t pfm_create_context(pfarg_ctx_t *ctx, char *smpl_name, void *smpl_arg, size_t smpl_size)
pfmlib_intel_nhm.h
PfmCodeAnalyser::load_arg
pfarg_load_t load_arg
Definition: PfmCodeAnalyser.h:42
pfarg_pmd_t::reg_num
uint16_t reg_num
Definition: perfmon_v2.h:39
NUM_PMCS
#define NUM_PMCS
Definition: PfmCodeAnalyser.h:26
perfmon.h
PfmCodeAnalyser::Instance
static PfmCodeAnalyser & Instance(const char *event0="UNHALTED_CORE_CYCLES", unsigned int cmask_v0=0, bool inv_v0=false, const char *event1="", unsigned int cmask_v1=0, bool inv_v1=false, const char *event2="", unsigned int cmask_v2=0, bool inv_v2=false, const char *event3="", unsigned int cmask_v3=0, bool inv_v3=false)
Definition: PfmCodeAnalyser.h:123
PFM_CORE_SEL_INV
#define PFM_CORE_SEL_INV
Definition: pfmlib_core.h:68
pfmlib_reg_t::reg_num
unsigned int reg_num
Definition: pfmlib.h:99
pfmlib_output_param_t::pfp_pmcs
pfmlib_reg_t pfp_pmcs[PFMLIB_MAX_PMCS]
Definition: pfmlib.h:128
PfmCodeAnalyser::event_str
char event_str[MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS][MAX_EVT_NAME_LEN]
Definition: PfmCodeAnalyser.h:44
pfmlib_input_param_t::pfp_dfl_plm
unsigned int pfp_dfl_plm
Definition: pfmlib.h:109
PfmCodeAnalyser::start
void start()
Definition: PfmCodeAnalyser.h:134
pfmlib_input_param_t::pfp_event_count
unsigned int pfp_event_count
Definition: pfmlib.h:108
pfm_find_full_event
pfm_err_t pfm_find_full_event(const char *str, pfmlib_event_t *e)
PFM_NHM_SEL_INV
#define PFM_NHM_SEL_INV
Definition: pfmlib_intel_nhm.h:103
PfmCodeAnalyser::nehalem
int nehalem
Definition: PfmCodeAnalyser.h:36
PfmCodeAnalyser::count
unsigned count
Definition: PfmCodeAnalyser.h:52
cpuid
#define cpuid(func, eax, ebx, ecx, edx)
Definition: PfmCodeAnalyser.h:30
PFMLIB_SUCCESS
#define PFMLIB_SUCCESS
Definition: pfmlib.h:267
pfmlib.h
PfmCodeAnalyser::used_counters_number
int used_counters_number
Definition: PfmCodeAnalyser.h:35
PfmCodeAnalyser::ret
int ret
Definition: PfmCodeAnalyser.h:49
PfmCodeAnalyser::ctx
pfarg_ctx_t ctx
Definition: PfmCodeAnalyser.h:39
PfmCodeAnalyser::nhm_params
pfmlib_nhm_input_param_t nhm_params
Definition: PfmCodeAnalyser.h:48
PfmCodeAnalyser::params
pfmlib_core_input_param_t params
Definition: PfmCodeAnalyser.h:47
PfmCodeAnalyser::fd
int fd
Definition: PfmCodeAnalyser.h:43
pfm_stop
os_err_t pfm_stop(int fd)
perfmon_dfl_smpl.h
MAX_EVT_NAME_LEN
#define MAX_EVT_NAME_LEN
Definition: PfmCodeAnalyser.h:25
pfm_load_context
os_err_t pfm_load_context(int fd, pfarg_load_t *load)
pfmlib_output_param_t
Definition: pfmlib.h:125
pfmlib_output_param_t::pfp_pmds
pfmlib_reg_t pfp_pmds[PFMLIB_MAX_PMDS]
Definition: pfmlib.h:129
pfmlib_input_param_t
Definition: pfmlib.h:107
PfmCodeAnalyser::operator=
PfmCodeAnalyser & operator=(PfmCodeAnalyser const &)
pfmlib_output_param_t::pfp_pmc_count
unsigned int pfp_pmc_count
Definition: pfmlib.h:126
pfm_write_pmcs
os_err_t pfm_write_pmcs(int fd, pfarg_pmc_t *pmcs, int count)
pfarg_load_t::load_pid
uint32_t load_pid
Definition: perfmon_v2.h:69
PfmCodeAnalyser::PfmCodeAnalyser
PfmCodeAnalyser(PfmCodeAnalyser const &)
pfm_dispatch_events
pfm_err_t pfm_dispatch_events(pfmlib_input_param_t *p, void *model_in, pfmlib_output_param_t *q, void *model_out)
PfmCodeAnalyser::PfmCodeAnalyser
PfmCodeAnalyser(const char *event0, unsigned int cmask_v0, bool inv_v0, const char *event1, unsigned int cmask_v1, bool inv_v1, const char *event2, unsigned int cmask_v2, bool inv_v2, const char *event3, unsigned int cmask_v3, bool inv_v3)
Definition: PfmCodeAnalyser.h:74