PAPI 7.1.0.0
Loading...
Searching...
No Matches
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 HANDLE_STRING_ERROR   {fprintf(stderr,"%s:%i unexpected string function error.\n",__FILE__,__LINE__); exit(-1);}
 
#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)
 
static int _example_init_component (int cidx)
 
static int _example_init_thread (hwd_context_t *ctx)
 
static int _example_init_control_state (hwd_control_state_t *ctl)
 
static int _example_update_control_state (hwd_control_state_t *ctl, NativeInfo_t *native, int count, hwd_context_t *ctx)
 
static int _example_start (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
static int _example_stop (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
static int _example_read (hwd_context_t *ctx, hwd_control_state_t *ctl, long long **events, int flags)
 
static int _example_write (hwd_context_t *ctx, hwd_control_state_t *ctl, long long *events)
 
static int _example_reset (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
static int _example_shutdown_component (void)
 
static int _example_shutdown_thread (hwd_context_t *ctx)
 
static int _example_ctl (hwd_context_t *ctx, int code, _papi_int_option_t *option)
 
static int _example_set_domain (hwd_control_state_t *cntrl, int domain)
 
static int _example_ntv_enum_events (unsigned int *EventCode, int modifier)
 
static int _example_ntv_code_to_name (unsigned int EventCode, char *name, int len)
 
static int _example_ntv_code_to_descr (unsigned int EventCode, char *descr, int len)
 

Variables

papi_vector_t _example_vector
 
static example_native_event_entry_texample_native_table
 
static int num_events = 0
 
static long long example_global_autoinc_value = 0
 

Detailed Description

Macro Definition Documentation

◆ EXAMPLE_AUTOINC_REG

#define EXAMPLE_AUTOINC_REG   2

Definition at line 104 of file example.c.

◆ EXAMPLE_CONSTANT_REG

#define EXAMPLE_CONSTANT_REG   1

Definition at line 103 of file example.c.

◆ EXAMPLE_GLOBAL_AUTOINC_REG

#define EXAMPLE_GLOBAL_AUTOINC_REG   3

Definition at line 105 of file example.c.

◆ EXAMPLE_MAX_MULTIPLEX_COUNTERS

#define EXAMPLE_MAX_MULTIPLEX_COUNTERS   4

Definition at line 29 of file example.c.

◆ EXAMPLE_MAX_SIMULTANEOUS_COUNTERS

#define EXAMPLE_MAX_SIMULTANEOUS_COUNTERS   3

This driver supports three counters counting at once

Definition at line 28 of file example.c.

◆ EXAMPLE_TOTAL_EVENTS

#define EXAMPLE_TOTAL_EVENTS   4

Definition at line 107 of file example.c.

◆ EXAMPLE_ZERO_REG

#define EXAMPLE_ZERO_REG   0

Definition at line 102 of file example.c.

◆ HANDLE_STRING_ERROR

#define HANDLE_STRING_ERROR   {fprintf(stderr,"%s:%i unexpected string function error.\n",__FILE__,__LINE__); exit(-1);}

Definition at line 34 of file example.c.

Function Documentation

◆ _example_ctl()

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

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 477 of file example.c.

478{
479
480 (void) ctx;
481 (void) code;
482 (void) option;
483
484 SUBDBG( "example_ctl..." );
485
486 return PAPI_OK;
487}
#define PAPI_OK
Definition: f90papi.h:73
#define SUBDBG(format, args...)
Definition: papi_debug.h:64

◆ _example_init_component()

static int _example_init_component ( int  cidx)
static

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 192 of file example.c.

193{
194 int retval = PAPI_OK;
195
196 SUBDBG( "_example_init_component..." );
197
198 /* First, detect that our hardware is available */
199 if (detect_example()!=PAPI_OK) {
201 "Example Hardware not present.");
202 _example_vector.cmp_info.disabled_reason[PAPI_MAX_STR_LEN-1]=0; // force null termination.
205 goto fn_fail;
206 }
207
208 /* we know in advance how many events we want */
209 /* for actual hardware this might have to be determined dynamically */
211
212 /* Allocate memory for the our native event table */
216 if ( example_native_table == NULL ) {
218 "Could not allocate %lu bytes of memory for EXAMPLE device structure.", num_events*sizeof(example_native_event_entry_t));
219 _example_vector.cmp_info.disabled_reason[PAPI_MAX_STR_LEN-1]=0; // force null termination.
222 goto fn_fail;
223 }
224
225 /* fill in the event table parameters */
226 /* for complicated components this will be done dynamically */
227 /* or by using an external library */
228
229 strcpy( example_native_table[0].name, "EXAMPLE_ZERO" );
230 strcpy( example_native_table[0].description,
231 "This is an example counter, that always returns 0" );
233
234 strcpy( example_native_table[1].name, "EXAMPLE_CONSTANT" );
235 strcpy( example_native_table[1].description,
236 "This is an example counter, that always returns a constant value of 42" );
238
239 strcpy( example_native_table[2].name, "EXAMPLE_AUTOINC" );
240 strcpy( example_native_table[2].description,
241 "This is an example counter, that reports a per-thread auto-incrementing value" );
243
244 strcpy( example_native_table[3].name, "EXAMPLE_GLOBAL_AUTOINC" );
245 strcpy( example_native_table[3].description,
246 "This is an example counter, that reports a global auto-incrementing value" );
248
249 /* Export the total number of events available */
251
252 /* Export the component id */
254
255 fn_exit:
256 _papi_hwd[cidx]->cmp_info.disabled = retval;
257 return retval;
258 fn_fail:
259 goto fn_exit;
260}
struct papi_vectors * _papi_hwd[]
static int detect_example(void)
Definition: example.c:177
static example_native_event_entry_t * example_native_table
Definition: example.c:92
papi_vector_t _example_vector
Definition: example.c:38
static int num_events
Definition: example.c:95
#define EXAMPLE_TOTAL_EVENTS
Definition: example.c:107
#define HANDLE_STRING_ERROR
Definition: example.c:34
#define PAPI_ENOSUPP
Definition: f90papi.h:244
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
#define PAPI_ENOMEM
Definition: f90papi.h:16
#define papi_calloc(a, b)
Definition: papi_memory.h:37
static int cidx
const char * name
Definition: rocs.c:225
char disabled_reason[PAPI_HUGE_STR_LEN]
Definition: papi.h:634
Definition: example.c:53
int writable
Definition: example.c:57
PAPI_component_info_t cmp_info
Definition: papi_vector.h:20
int retval
Definition: zero_fork.c:53
Here is the call graph for this function:

◆ _example_init_control_state()

static int _example_init_control_state ( hwd_control_state_t ctl)
static

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

Definition at line 284 of file example.c.

285{
286 SUBDBG( "example_init_control_state... %p\n", ctl );
287
288 example_control_state_t *example_ctl = ( example_control_state_t * ) ctl;
289 memset( example_ctl, 0, sizeof ( example_control_state_t ) );
290
291 return PAPI_OK;
292}

◆ _example_init_thread()

static int _example_init_thread ( hwd_context_t ctx)
static

This is called whenever a thread is initialized

Definition at line 264 of file example.c.

265{
266
267 example_context_t *example_context = (example_context_t *)ctx;
268
269 example_context->autoinc_value=0;
270
271 SUBDBG( "_example_init_thread %p...", ctx );
272
273 return PAPI_OK;
274}
long long autoinc_value
Definition: example.c:88

◆ _example_ntv_code_to_descr()

static int _example_ntv_code_to_descr ( unsigned int  EventCode,
char *  descr,
int  len 
)
static

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 604 of file example.c.

605{
606 int index;
607 index = EventCode;
608
609 /* make sure event is in range */
610 if (index >= 0 && index < num_events) {
611 strncpy( descr, example_native_table[index].description, len );
612 return PAPI_OK;
613 }
614
615 return PAPI_ENOEVNT;
616}
#define PAPI_ENOEVNT
Definition: f90papi.h:139
char * descr

◆ _example_ntv_code_to_name()

static int _example_ntv_code_to_name ( unsigned int  EventCode,
char *  name,
int  len 
)
static

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 583 of file example.c.

584{
585 int index;
586
587 index = EventCode;
588
589 /* Make sure we are in range */
590 if (index >= 0 && index < num_events) {
591 strncpy( name, example_native_table[index].name, len );
592 return PAPI_OK;
593 }
594
595 return PAPI_ENOEVNT;
596}

◆ _example_ntv_enum_events()

static int _example_ntv_enum_events ( unsigned int EventCode,
int  modifier 
)
static

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 541 of file example.c.

542{
543 int index;
544
545
546 switch ( modifier ) {
547
548 /* return EventCode of first event */
549 case PAPI_ENUM_FIRST:
550 /* return the first event that we support */
551
552 *EventCode = 0;
553 return PAPI_OK;
554
555 /* return EventCode of next available event */
556 case PAPI_ENUM_EVENTS:
557 index = *EventCode;
558
559 /* Make sure we have at least 1 more event after us */
560 if ( index < num_events - 1 ) {
561
562 /* This assumes a non-sparse mapping of the events */
563 *EventCode = *EventCode + 1;
564 return PAPI_OK;
565 } else {
566 return PAPI_ENOEVNT;
567 }
568 break;
569
570 default:
571 return PAPI_EINVAL;
572 }
573
574 return PAPI_EINVAL;
575}
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_EINVAL
Definition: f90papi.h:115

◆ _example_read()

static int _example_read ( hwd_context_t ctx,
hwd_control_state_t ctl,
long long **  events,
int  flags 
)
static

Triggered by PAPI_read()

Definition at line 372 of file example.c.

374{
375
376 (void) flags;
377
378 example_context_t *example_ctx = (example_context_t *) ctx;
379 example_control_state_t *example_ctl = ( example_control_state_t *) ctl;
380
381 SUBDBG( "example_read... %p %d", ctx, flags );
382
383 int i;
384
385 /* Read counters into expected slot */
386 for(i=0;i<example_ctl->num_events;i++) {
387 example_ctl->counter[i] =
388 example_hardware_read( example_ctl->which_counter[i],
389 example_ctx );
390 }
391
392 /* return pointer to the values we read */
393 *events = example_ctl->counter;
394
395 return PAPI_OK;
396}
int i
static long long example_hardware_read(int which_one, example_context_t *ctx)
Definition: example.c:126
char events[MAX_EVENTS][BUFSIZ]
long long counter[EXAMPLE_MAX_MULTIPLEX_COUNTERS]
Definition: example.c:82
int which_counter[EXAMPLE_MAX_SIMULTANEOUS_COUNTERS]
Definition: example.c:81
Here is the call graph for this function:

◆ _example_reset()

static int _example_reset ( hwd_context_t ctx,
hwd_control_state_t ctl 
)
static

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

Definition at line 427 of file example.c.

428{
429 example_context_t *event_ctx = (example_context_t *)ctx;
430 (void) ctl;
431
432 SUBDBG( "example_reset ctx=%p ctrl=%p...", ctx, ctl );
433
434 /* Reset the hardware */
435 example_hardware_reset( event_ctx );
436
437 return PAPI_OK;
438}
static void example_hardware_reset(example_context_t *ctx)
Definition: example.c:113
Here is the call graph for this function:

◆ _example_set_domain()

static int _example_set_domain ( hwd_control_state_t cntrl,
int  domain 
)
static

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 499 of file example.c.

500{
501 (void) cntrl;
502
503 int found = 0;
504 SUBDBG( "example_set_domain..." );
505
506 if ( PAPI_DOM_USER & domain ) {
507 SUBDBG( " PAPI_DOM_USER " );
508 found = 1;
509 }
510 if ( PAPI_DOM_KERNEL & domain ) {
511 SUBDBG( " PAPI_DOM_KERNEL " );
512 found = 1;
513 }
514 if ( PAPI_DOM_OTHER & domain ) {
515 SUBDBG( " PAPI_DOM_OTHER " );
516 found = 1;
517 }
518 if ( PAPI_DOM_ALL & domain ) {
519 SUBDBG( " PAPI_DOM_ALL " );
520 found = 1;
521 }
522 if ( !found )
523 return ( PAPI_EINVAL );
524
525 return PAPI_OK;
526}
#define PAPI_DOM_USER
Definition: f90papi.h:174
#define PAPI_DOM_OTHER
Definition: f90papi.h:21
#define PAPI_DOM_KERNEL
Definition: f90papi.h:254
#define PAPI_DOM_ALL
Definition: f90papi.h:261

◆ _example_shutdown_component()

static int _example_shutdown_component ( void  )
static

Triggered by PAPI_shutdown()

Definition at line 442 of file example.c.

443{
444
445 SUBDBG( "example_shutdown_component..." );
446
447 /* Free anything we allocated */
448
450
451 return PAPI_OK;
452}
#define papi_free(a)
Definition: papi_memory.h:35

◆ _example_shutdown_thread()

static int _example_shutdown_thread ( hwd_context_t ctx)
static

Called at thread shutdown

Definition at line 456 of file example.c.

457{
458
459 (void) ctx;
460
461 SUBDBG( "example_shutdown_thread... %p", ctx );
462
463 /* Last chance to clean up thread */
464
465 return PAPI_OK;
466}

◆ _example_start()

static int _example_start ( hwd_context_t ctx,
hwd_control_state_t ctl 
)
static

Triggered by PAPI_start()

Definition at line 331 of file example.c.

332{
333
334 (void) ctx;
335 (void) ctl;
336
337 SUBDBG( "example_start %p %p...", ctx, ctl );
338
339 /* anything that would need to be set at counter start time */
340
341 /* reset counters? */
342 /* For hardware that cannot reset counters, store initial */
343 /* counter state to the ctl and subtract it off at read time */
344
345 /* start the counting ?*/
346
347 return PAPI_OK;
348}

◆ _example_stop()

static int _example_stop ( hwd_context_t ctx,
hwd_control_state_t ctl 
)
static

Triggered by PAPI_stop()

Definition at line 353 of file example.c.

354{
355
356 (void) ctx;
357 (void) ctl;
358
359 SUBDBG( "example_stop %p %p...", ctx, ctl );
360
361 /* anything that would need to be done at counter stop time */
362
363
364
365 return PAPI_OK;
366}

◆ _example_update_control_state()

static int _example_update_control_state ( hwd_control_state_t ctl,
NativeInfo_t native,
int  count,
hwd_context_t ctx 
)
static

Triggered by eventset operations like add or remove

Definition at line 297 of file example.c.

301{
302
303 (void) ctx;
304 int i, index;
305
306 example_control_state_t *example_ctl = ( example_control_state_t * ) ctl;
307
308 SUBDBG( "_example_update_control_state %p %p...", ctl, ctx );
309
310 /* if no events, return */
311 if (count==0) return PAPI_OK;
312
313 for( i = 0; i < count; i++ ) {
314 index = native[i].ni_event;
315
316 /* Map counter #i to Measure Event "index" */
317 example_ctl->which_counter[i]=index;
318
319 /* We have no constraints on event position, so any event */
320 /* can be in any slot. */
321 native[i].ni_position = i;
322 }
323
324 example_ctl->num_events=count;
325
326 return PAPI_OK;
327}
static long count
static int native

◆ _example_write()

static int _example_write ( hwd_context_t ctx,
hwd_control_state_t ctl,
long long events 
)
static

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

Definition at line 401 of file example.c.

403{
404
405 example_context_t *example_ctx = (example_context_t *) ctx;
406 example_control_state_t *example_ctl = ( example_control_state_t *) ctl;
407
408 int i;
409
410 SUBDBG( "example_write... %p %p", ctx, ctl );
411
412 /* Write counters into expected slot */
413 for(i=0;i<example_ctl->num_events;i++) {
415 example_ctx,
416 events[i] );
417 }
418
419 return PAPI_OK;
420}
static int example_hardware_write(int which_one, example_context_t *ctx, long long value)
Definition: example.c:153
Here is the call graph for this function:

◆ detect_example()

static int detect_example ( void  )
static

Definition at line 177 of file example.c.

177 {
178
179 return PAPI_OK;
180}
Here is the caller graph for this function:

◆ example_hardware_read()

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

Code that reads event values.

Definition at line 126 of file example.c.

127{
128 long long old_value;
129
130 switch ( which_one ) {
131 case EXAMPLE_ZERO_REG:
132 return 0;
134 return 42;
136 old_value = ctx->autoinc_value;
137 ctx->autoinc_value++;
138 return old_value;
142 return old_value;
143 default:
144 fprintf(stderr,"Invalid counter read %#x\n",which_one );
145 return -1;
146 }
147
148 return 0;
149}
#define EXAMPLE_ZERO_REG
Definition: example.c:102
#define EXAMPLE_GLOBAL_AUTOINC_REG
Definition: example.c:105
#define EXAMPLE_CONSTANT_REG
Definition: example.c:103
#define EXAMPLE_AUTOINC_REG
Definition: example.c:104
static long long example_global_autoinc_value
Definition: example.c:109
FILE * stderr
Here is the caller graph for this function:

◆ example_hardware_reset()

static void example_hardware_reset ( example_context_t ctx)
static

Code that resets the hardware.

Definition at line 113 of file example.c.

114{
115 /* reset per-thread count */
116 ctx->autoinc_value=0;
117 /* reset global count */
119
120}
Here is the caller graph for this function:

◆ example_hardware_write()

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

Code that writes event values.

Definition at line 153 of file example.c.

156{
157
158 switch ( which_one ) {
159 case EXAMPLE_ZERO_REG:
161 return PAPI_OK; /* can't be written */
163 ctx->autoinc_value=value;
164 return PAPI_OK;
167 return PAPI_OK;
168 default:
169 perror( "Invalid counter write" );
170 return -1;
171 }
172
173 return 0;
174}
Here is the caller graph for this function:

Variable Documentation

◆ _example_vector

papi_vector_t _example_vector

Vector that points to entry points for our component

Definition at line 38 of file example.c.

◆ example_global_autoinc_value

long long example_global_autoinc_value = 0
static

Definition at line 109 of file example.c.

◆ example_native_table

example_native_event_entry_t* example_native_table
static

This table contains the native events

Definition at line 92 of file example.c.

◆ num_events

int num_events = 0
static

number of events in the table

Definition at line 95 of file example.c.