PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
example.c File Reference

This is an example component, it demos the component interface and implements three example counters. More...

Include dependency graph for example.c:

Go to the source code of this file.

Data Structures

struct  example_register_t
 
struct  example_native_event_entry_t
 
struct  example_reg_alloc_t
 
struct  example_control_state_t
 
struct  example_context_t
 

Macros

#define EXAMPLE_MAX_SIMULTANEOUS_COUNTERS   3
 
#define EXAMPLE_MAX_MULTIPLEX_COUNTERS   4
 
#define EXAMPLE_ZERO_REG   0
 
#define EXAMPLE_CONSTANT_REG   1
 
#define EXAMPLE_AUTOINC_REG   2
 
#define EXAMPLE_GLOBAL_AUTOINC_REG   3
 
#define EXAMPLE_TOTAL_EVENTS   4
 

Functions

static void example_hardware_reset (example_context_t *ctx)
 
static long long example_hardware_read (int which_one, example_context_t *ctx)
 
static int example_hardware_write (int which_one, example_context_t *ctx, long long value)
 
static int detect_example (void)
 
int _example_init_component (int cidx)
 
int _example_init_thread (hwd_context_t *ctx)
 
int _example_init_control_state (hwd_control_state_t *ctl)
 
int _example_update_control_state (hwd_control_state_t *ctl, NativeInfo_t *native, int count, hwd_context_t *ctx)
 
int _example_start (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
int _example_stop (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
int _example_read (hwd_context_t *ctx, hwd_control_state_t *ctl, long long **events, int flags)
 
int _example_write (hwd_context_t *ctx, hwd_control_state_t *ctl, long long *events)
 
int _example_reset (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
int _example_shutdown_component (void)
 
int _example_shutdown_thread (hwd_context_t *ctx)
 
int _example_ctl (hwd_context_t *ctx, int code, _papi_int_option_t *option)
 
int _example_set_domain (hwd_control_state_t *cntrl, int domain)
 
int _example_ntv_enum_events (unsigned int *EventCode, int modifier)
 
int _example_ntv_code_to_name (unsigned int EventCode, char *name, int len)
 
int _example_ntv_code_to_descr (unsigned int EventCode, char *descr, int len)
 

Variables

papi_vector_t _example_vector
 
static
example_native_event_entry_t
example_native_table
 
static int num_events = 0
 
static long long example_global_autoinc_value = 0
 

Detailed Description

Macro Definition Documentation

#define EXAMPLE_AUTOINC_REG   2

Definition at line 99 of file example.c.

#define EXAMPLE_CONSTANT_REG   1

Definition at line 98 of file example.c.

#define EXAMPLE_GLOBAL_AUTOINC_REG   3

Definition at line 100 of file example.c.

#define EXAMPLE_MAX_MULTIPLEX_COUNTERS   4

Definition at line 29 of file example.c.

#define EXAMPLE_MAX_SIMULTANEOUS_COUNTERS   3

This driver supports three counters counting at once

Definition at line 28 of file example.c.

#define EXAMPLE_TOTAL_EVENTS   4

Definition at line 102 of file example.c.

#define EXAMPLE_ZERO_REG   0

Definition at line 97 of file example.c.

Function Documentation

int _example_ctl ( hwd_context_t ctx,
int  code,
_papi_int_option_t option 
)

This function sets various options in the component

Parameters
[in]ctx– hardware context
[in]codevalid are PAPI_SET_DEFDOM, PAPI_SET_DOMAIN, PAPI_SETDEFGRN, PAPI_SET_GRANUL and PAPI_SET_INHERIT
[in]option– options to be set

Definition at line 461 of file example.c.

462 {
463 
464  (void) ctx;
465  (void) code;
466  (void) option;
467 
468  SUBDBG( "example_ctl..." );
469 
470  return PAPI_OK;
471 }
return PAPI_OK
Definition: linux-nvml.c:458
void
Definition: iozone.c:18627
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
int _example_init_component ( int  cidx)

Initialize hardware counters, setup the function vector table and get hardware information, this routine is called when the PAPI process is initialized (IE PAPI_library_init)

Definition at line 187 of file example.c.

188 {
189 
190  SUBDBG( "_example_init_component..." );
191 
192 
193  /* First, detect that our hardware is available */
194  if (detect_example()!=PAPI_OK) {
195  return PAPI_ECMP;
196  }
197 
198  /* we know in advance how many events we want */
199  /* for actual hardware this might have to be determined dynamically */
201 
202  /* Allocate memory for the our native event table */
206  if ( example_native_table == NULL ) {
207  PAPIERROR( "malloc():Could not get memory for events table" );
208  return PAPI_ENOMEM;
209  }
210 
211  /* fill in the event table parameters */
212  /* for complicated components this will be done dynamically */
213  /* or by using an external library */
214 
215  strcpy( example_native_table[0].name, "EXAMPLE_ZERO" );
216  strcpy( example_native_table[0].description,
217  "This is an example counter, that always returns 0" );
219 
220  strcpy( example_native_table[1].name, "EXAMPLE_CONSTANT" );
221  strcpy( example_native_table[1].description,
222  "This is an example counter, that always returns a constant value of 42" );
224 
225  strcpy( example_native_table[2].name, "EXAMPLE_AUTOINC" );
226  strcpy( example_native_table[2].description,
227  "This is an example counter, that reports a per-thread auto-incrementing value" );
229 
230  strcpy( example_native_table[3].name, "EXAMPLE_GLOBAL_AUTOINC" );
231  strcpy( example_native_table[3].description,
232  "This is an example counter, that reports a global auto-incrementing value" );
234 
235  /* Export the total number of events available */
237 
238  /* Export the component id */
240 
241 
242 
243  return PAPI_OK;
244 }
#define PAPI_ENOMEM
Definition: fpapi.h:107
static int detect_example(void)
Definition: example.c:172
static int num_events
return PAPI_OK
Definition: linux-nvml.c:458
Definition: example.c:47
int writable
Definition: example.c:52
PAPI_component_info_t cmp_info
Definition: papi_vector.h:20
papi_vector_t _example_vector
Definition: example.c:33
#define EXAMPLE_TOTAL_EVENTS
Definition: example.c:102
static int cidx
Definition: event_info.c:40
#define PAPI_ECMP
Definition: fpapi.h:109
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
void PAPIERROR(char *format,...)
static example_native_event_entry_t * example_native_table
Definition: example.c:87
strcpy(filename, default_filename)
char * name
Definition: iozone.c:23648
#define papi_calloc(a, b)
Definition: papi_memory.h:37

Here is the call graph for this function:

int _example_init_control_state ( hwd_control_state_t ctl)

Setup a counter control state. In general a control state holds the hardware info for an EventSet.

Definition at line 268 of file example.c.

269 {
270  SUBDBG( "example_init_control_state... %p\n", ctl );
271 
272  example_control_state_t *example_ctl = ( example_control_state_t * ) ctl;
273  memset( example_ctl, 0, sizeof ( example_control_state_t ) );
274 
275  return PAPI_OK;
276 }
memset(eventId, 0, size)
return PAPI_OK
Definition: linux-nvml.c:458
#define SUBDBG(format, args...)
Definition: papi_debug.h:63

Here is the call graph for this function:

int _example_init_thread ( hwd_context_t ctx)

This is called whenever a thread is initialized

Definition at line 248 of file example.c.

249 {
250 
251  example_context_t *example_context = (example_context_t *)ctx;
252 
253  example_context->autoinc_value=0;
254 
255  SUBDBG( "_example_init_thread %p...", ctx );
256 
257  return PAPI_OK;
258 }
return PAPI_OK
Definition: linux-nvml.c:458
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
long long autoinc_value
Definition: example.c:83
int _example_ntv_code_to_descr ( unsigned int  EventCode,
char *  descr,
int  len 
)

Takes a native event code and passes back the event description

Parameters
EventCodeis the native event code
descris a pointer for the description to be copied to
lenis the size of the descr string

Definition at line 588 of file example.c.

589 {
590  int index;
591  index = EventCode;
592 
593  /* make sure event is in range */
594  if (index >= 0 && index < num_events) {
595  strncpy( descr, example_native_table[index].description, len );
596  return PAPI_OK;
597  }
598 
599  return PAPI_ENOEVNT;
600 }
#define PAPI_ENOEVNT
Definition: fpapi.h:112
static int num_events
return PAPI_OK
Definition: linux-nvml.c:458
static example_native_event_entry_t * example_native_table
Definition: example.c:87
int _example_ntv_code_to_name ( unsigned int  EventCode,
char *  name,
int  len 
)

Takes a native event code and passes back the name

Parameters
EventCodeis the native event code
nameis a pointer for the name to be copied to
lenis the size of the name string

Definition at line 567 of file example.c.

568 {
569  int index;
570 
571  index = EventCode;
572 
573  /* Make sure we are in range */
574  if (index >= 0 && index < num_events) {
575  strncpy( name, example_native_table[index].name, len );
576  return PAPI_OK;
577  }
578 
579  return PAPI_ENOEVNT;
580 }
#define PAPI_ENOEVNT
Definition: fpapi.h:112
static int num_events
return PAPI_OK
Definition: linux-nvml.c:458
static example_native_event_entry_t * example_native_table
Definition: example.c:87
char * name
Definition: iozone.c:23648
int _example_ntv_enum_events ( unsigned int EventCode,
int  modifier 
)

Enumerate Native Events

Parameters
EventCodeis the event of interest
modifieris one of PAPI_ENUM_FIRST, PAPI_ENUM_EVENTS If your component has attribute masks then these need to be handled here as well.

Definition at line 525 of file example.c.

526 {
527  int index;
528 
529 
530  switch ( modifier ) {
531 
532  /* return EventCode of first event */
533  case PAPI_ENUM_FIRST:
534  /* return the first event that we support */
535 
536  *EventCode = 0;
537  return PAPI_OK;
538 
539  /* return EventCode of next available event */
540  case PAPI_ENUM_EVENTS:
541  index = *EventCode;
542 
543  /* Make sure we have at least 1 more event after us */
544  if ( index < num_events - 1 ) {
545 
546  /* This assumes a non-sparse mapping of the events */
547  *EventCode = *EventCode + 1;
548  return PAPI_OK;
549  } else {
550  return PAPI_ENOEVNT;
551  }
552  break;
553 
554  default:
555  return PAPI_EINVAL;
556  }
557 
558  return PAPI_EINVAL;
559 }
#define PAPI_ENOEVNT
Definition: fpapi.h:112
static int num_events
return PAPI_OK
Definition: linux-nvml.c:458
return PAPI_EINVAL
Definition: linux-nvml.c:408
int _example_read ( hwd_context_t ctx,
hwd_control_state_t ctl,
long long **  events,
int  flags 
)

Triggered by PAPI_read()

Definition at line 356 of file example.c.

358 {
359 
360  (void) flags;
361 
362  example_context_t *example_ctx = (example_context_t *) ctx;
363  example_control_state_t *example_ctl = ( example_control_state_t *) ctl;
364 
365  SUBDBG( "example_read... %p %d", ctx, flags );
366 
367  int i;
368 
369  /* Read counters into expected slot */
370  for(i=0;i<example_ctl->num_events;i++) {
371  example_ctl->counter[i] =
372  example_hardware_read( example_ctl->which_counter[i],
373  example_ctx );
374  }
375 
376  /* return pointer to the values we read */
377  *events = example_ctl->counter;
378 
379  return PAPI_OK;
380 }
long long flags
Definition: iozone.c:12330
return PAPI_OK
Definition: linux-nvml.c:458
void
Definition: iozone.c:18627
int which_counter[EXAMPLE_MAX_SIMULTANEOUS_COUNTERS]
Definition: example.c:76
static long long example_hardware_read(int which_one, example_context_t *ctx)
Definition: example.c:121
int i
Definition: fileop.c:140
long long counter[EXAMPLE_MAX_MULTIPLEX_COUNTERS]
Definition: example.c:77
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
char events[MAX_EVENTS][BUFSIZ]

Here is the call graph for this function:

int _example_reset ( hwd_context_t ctx,
hwd_control_state_t ctl 
)

Triggered by PAPI_reset() but only if the EventSet is currently running

Definition at line 411 of file example.c.

412 {
413  example_context_t *event_ctx = (example_context_t *)ctx;
414  (void) ctl;
415 
416  SUBDBG( "example_reset ctx=%p ctrl=%p...", ctx, ctl );
417 
418  /* Reset the hardware */
419  example_hardware_reset( event_ctx );
420 
421  return PAPI_OK;
422 }
return PAPI_OK
Definition: linux-nvml.c:458
void
Definition: iozone.c:18627
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
static void example_hardware_reset(example_context_t *ctx)
Definition: example.c:108

Here is the call graph for this function:

int _example_set_domain ( hwd_control_state_t cntrl,
int  domain 
)

This function has to set the bits needed to count different domains In particular: PAPI_DOM_USER, PAPI_DOM_KERNEL PAPI_DOM_OTHER By default return PAPI_EINVAL if none of those are specified and PAPI_OK with success PAPI_DOM_USER is only user context is counted PAPI_DOM_KERNEL is only the Kernel/OS context is counted PAPI_DOM_OTHER is Exception/transient mode (like user TLB misses) PAPI_DOM_ALL is all of the domains

Definition at line 483 of file example.c.

484 {
485  (void) cntrl;
486 
487  int found = 0;
488  SUBDBG( "example_set_domain..." );
489 
490  if ( PAPI_DOM_USER & domain ) {
491  SUBDBG( " PAPI_DOM_USER " );
492  found = 1;
493  }
494  if ( PAPI_DOM_KERNEL & domain ) {
495  SUBDBG( " PAPI_DOM_KERNEL " );
496  found = 1;
497  }
498  if ( PAPI_DOM_OTHER & domain ) {
499  SUBDBG( " PAPI_DOM_OTHER " );
500  found = 1;
501  }
502  if ( PAPI_DOM_ALL & domain ) {
503  SUBDBG( " PAPI_DOM_ALL " );
504  found = 1;
505  }
506  if ( !found )
507  return ( PAPI_EINVAL );
508 
509  return PAPI_OK;
510 }
#define PAPI_DOM_ALL
Definition: fpapi.h:25
return PAPI_OK
Definition: linux-nvml.c:458
#define PAPI_DOM_OTHER
Definition: fpapi.h:23
#define PAPI_DOM_KERNEL
Definition: fpapi.h:22
void
Definition: iozone.c:18627
return PAPI_EINVAL
Definition: linux-nvml.c:408
long long found
Definition: libasync.c:735
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
#define PAPI_DOM_USER
Definition: fpapi.h:21
int _example_shutdown_component ( void  )

Triggered by PAPI_shutdown()

Definition at line 426 of file example.c.

427 {
428 
429  SUBDBG( "example_shutdown_component..." );
430 
431  /* Free anything we allocated */
432 
434 
435  return PAPI_OK;
436 }
#define papi_free(a)
Definition: papi_memory.h:35
return PAPI_OK
Definition: linux-nvml.c:458
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
static example_native_event_entry_t * example_native_table
Definition: example.c:87
int _example_shutdown_thread ( hwd_context_t ctx)

Called at thread shutdown

Definition at line 440 of file example.c.

441 {
442 
443  (void) ctx;
444 
445  SUBDBG( "example_shutdown_thread... %p", ctx );
446 
447  /* Last chance to clean up thread */
448 
449  return PAPI_OK;
450 }
return PAPI_OK
Definition: linux-nvml.c:458
void
Definition: iozone.c:18627
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
int _example_start ( hwd_context_t ctx,
hwd_control_state_t ctl 
)

Triggered by PAPI_start()

Definition at line 315 of file example.c.

316 {
317 
318  (void) ctx;
319  (void) ctl;
320 
321  SUBDBG( "example_start %p %p...", ctx, ctl );
322 
323  /* anything that would need to be set at counter start time */
324 
325  /* reset counters? */
326  /* For hardware that cannot reset counters, store initial */
327  /* counter state to the ctl and subtract it off at read time */
328 
329  /* start the counting ?*/
330 
331  return PAPI_OK;
332 }
return PAPI_OK
Definition: linux-nvml.c:458
void
Definition: iozone.c:18627
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
int _example_stop ( hwd_context_t ctx,
hwd_control_state_t ctl 
)

Triggered by PAPI_stop()

Definition at line 337 of file example.c.

338 {
339 
340  (void) ctx;
341  (void) ctl;
342 
343  SUBDBG( "example_stop %p %p...", ctx, ctl );
344 
345  /* anything that would need to be done at counter stop time */
346 
347 
348 
349  return PAPI_OK;
350 }
return PAPI_OK
Definition: linux-nvml.c:458
void
Definition: iozone.c:18627
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
int _example_update_control_state ( hwd_control_state_t ctl,
NativeInfo_t native,
int  count,
hwd_context_t ctx 
)

Triggered by eventset operations like add or remove

Definition at line 281 of file example.c.

285 {
286 
287  (void) ctx;
288  int i, index;
289 
290  example_control_state_t *example_ctl = ( example_control_state_t * ) ctl;
291 
292  SUBDBG( "_example_update_control_state %p %p...", ctl, ctx );
293 
294  /* if no events, return */
295  if (count==0) return PAPI_OK;
296 
297  for( i = 0; i < count; i++ ) {
298  index = native[i].ni_event;
299 
300  /* Map counter #i to Measure Event "index" */
301  example_ctl->which_counter[i]=index;
302 
303  /* We have no constraints on event position, so any event */
304  /* can be in any slot. */
305  native[i].ni_position = i;
306  }
307 
308  example_ctl->num_events=count;
309 
310  return PAPI_OK;
311 }
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
void
Definition: iozone.c:18627
int which_counter[EXAMPLE_MAX_SIMULTANEOUS_COUNTERS]
Definition: example.c:76
int i
Definition: fileop.c:140
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
int _example_write ( hwd_context_t ctx,
hwd_control_state_t ctl,
long long events 
)

Triggered by PAPI_write(), but only if the counters are running

Definition at line 385 of file example.c.

387 {
388 
389  example_context_t *example_ctx = (example_context_t *) ctx;
390  example_control_state_t *example_ctl = ( example_control_state_t *) ctl;
391 
392  int i;
393 
394  SUBDBG( "example_write... %p %p", ctx, ctl );
395 
396  /* Write counters into expected slot */
397  for(i=0;i<example_ctl->num_events;i++) {
398  example_hardware_write( example_ctl->which_counter[i],
399  example_ctx,
400  events[i] );
401  }
402 
403  return PAPI_OK;
404 }
return PAPI_OK
Definition: linux-nvml.c:458
int which_counter[EXAMPLE_MAX_SIMULTANEOUS_COUNTERS]
Definition: example.c:76
static int example_hardware_write(int which_one, example_context_t *ctx, long long value)
Definition: example.c:148
int i
Definition: fileop.c:140
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
char events[MAX_EVENTS][BUFSIZ]

Here is the call graph for this function:

static int detect_example ( void  )
static

Definition at line 172 of file example.c.

172  {
173 
174  return PAPI_OK;
175 }
return PAPI_OK
Definition: linux-nvml.c:458

Here is the caller graph for this function:

static long long example_hardware_read ( int  which_one,
example_context_t ctx 
)
static

Code that reads event values.

Definition at line 121 of file example.c.

122 {
123  long long old_value;
124 
125  switch ( which_one ) {
126  case EXAMPLE_ZERO_REG:
127  return 0;
129  return 42;
130  case EXAMPLE_AUTOINC_REG:
131  old_value = ctx->autoinc_value;
132  ctx->autoinc_value++;
133  return old_value;
135  old_value = example_global_autoinc_value;
137  return old_value;
138  default:
139  fprintf(stderr,"Invalid counter read %#x\n",which_one );
140  return -1;
141  }
142 
143  return 0;
144 }
static long long example_global_autoinc_value
Definition: example.c:104
#define EXAMPLE_GLOBAL_AUTOINC_REG
Definition: example.c:100
#define EXAMPLE_AUTOINC_REG
Definition: example.c:99
long long autoinc_value
Definition: example.c:83
#define EXAMPLE_ZERO_REG
Definition: example.c:97
#define EXAMPLE_CONSTANT_REG
Definition: example.c:98

Here is the caller graph for this function:

static void example_hardware_reset ( example_context_t ctx)
static

Code that resets the hardware.

Definition at line 108 of file example.c.

109 {
110  /* reset per-thread count */
111  ctx->autoinc_value=0;
112  /* reset global count */
114 
115 }
static long long example_global_autoinc_value
Definition: example.c:104
long long autoinc_value
Definition: example.c:83

Here is the caller graph for this function:

static int example_hardware_write ( int  which_one,
example_context_t ctx,
long long  value 
)
static

Code that writes event values.

Definition at line 148 of file example.c.

151 {
152 
153  switch ( which_one ) {
154  case EXAMPLE_ZERO_REG:
156  return PAPI_OK; /* can't be written */
157  case EXAMPLE_AUTOINC_REG:
158  ctx->autoinc_value=value;
159  return PAPI_OK;
162  return PAPI_OK;
163  default:
164  perror( "Invalid counter write" );
165  return -1;
166  }
167 
168  return 0;
169 }
static long long example_global_autoinc_value
Definition: example.c:104
return PAPI_OK
Definition: linux-nvml.c:458
void double value
Definition: iozone.c:18781
#define EXAMPLE_GLOBAL_AUTOINC_REG
Definition: example.c:100
#define EXAMPLE_AUTOINC_REG
Definition: example.c:99
long long autoinc_value
Definition: example.c:83
#define EXAMPLE_ZERO_REG
Definition: example.c:97
#define EXAMPLE_CONSTANT_REG
Definition: example.c:98

Here is the caller graph for this function:

Variable Documentation

papi_vector_t _example_vector

Vector that points to entry points for our component

Definition at line 33 of file example.c.

long long example_global_autoinc_value = 0
static

Definition at line 104 of file example.c.

example_native_event_entry_t* example_native_table
static

This table contains the native events

Definition at line 87 of file example.c.

int num_events = 0
static

number of events in the table

Definition at line 90 of file example.c.