PAPI  5.7.0.0
testPCP.c File Reference
Include dependency graph for testPCP.c:

Go to the source code of this file.

Data Structures

union  convert_64_t
 

Macros

#define mConvertUsec(timeval_)   ((double) (timeval_.tv_sec*1000000+timeval_.tv_usec)) /* avoid typos, make it a double. */
 

Functions

unsigned long dummyThreadId (void)
 
int main (int argc, char **argv)
 

Variables

static struct timeval t1 t2
 

Macro Definition Documentation

◆ mConvertUsec

#define mConvertUsec (   timeval_)    ((double) (timeval_.tv_sec*1000000+timeval_.tv_usec)) /* avoid typos, make it a double. */

Definition at line 13 of file testPCP.c.

Function Documentation

◆ dummyThreadId()

unsigned long dummyThreadId ( void  )

Definition at line 16 of file testPCP.c.

16 { return(0); } // dummy return to give a thread id.

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 31 of file testPCP.c.

31  { // args to allow quiet flags.
32  int i,k,m,code=0;
33  int eventSetCount, ret;
34  PAPI_event_info_t info;
35  int cid=-1; // signal for not found.
36  int EventSet = PAPI_NULL;
37  int quiet=0;
38  char errMsg[1024]; // space for an error message with more info.
39  quiet=tests_quiet( argc, argv ); // From papi_test.h.
40  int firstTime = 1; // some things we want to run once.
41  int retlen;
42 
43  gettimeofday(&t1, NULL);
45  gettimeofday(&t2, NULL);
46  if (!quiet) fprintf(stderr, "PAPI_Library_init run time = %9.1f uS\n", (mConvertUsec(t2)-mConvertUsec(t1)));
47 
48  if (ret != PAPI_VER_CURRENT) { // if we failed,
49  test_fail(__FILE__, __LINE__, "PAPI_library_init failed\n", ret); // report.
50  }
51 
52 
53  if (!quiet) {
54  fprintf(stderr, "Testing PCP Component with PAPI %d.%d.%d\n",
58  }
59 
60  // PAPI_init_thread should be run only once, immediately after
61  // library init.
62 // ret = PAPI_thread_init(dummyThreadId); // PCP doesn't do anything, but should not err or crash on thread init.
63 // if (ret != PAPI_OK) { // If we get an error, this tester needs updating; pcp code has changed.
64 // test_fail(__FILE__, __LINE__, "PAPI_thread_init failed; this tester needs to be updated.\n", ret); // report.
65 // }
66 
67  // Find our component, pcp;
68  k = PAPI_num_components(); // get number of components.
69  for (i=0; i<k && cid<0; i++) { // while not found,
70  PAPI_component_info_t *aComponent =
71  (PAPI_component_info_t*) PAPI_get_component_info(i); // get the component info.
72  if (aComponent == NULL) { // if we failed,
73  sprintf(errMsg, "PAPI_get_component_info(%i) failed, "
74  "returned NULL. %i components reported.\n", i,k);
75  test_fail(__FILE__, __LINE__, errMsg, 0);
76 
77  }
78 
79  if (strcmp("pcp", aComponent->name) == 0) cid=i; // If we found our match, record it.
80  } // end search components.
81 
82  if (cid < 0) { // if no PCP component found,
83  sprintf(errMsg, "Failed to find pcp component among %i "
84  "reported components.\n", k);
85  test_fail(__FILE__, __LINE__, errMsg, 0); // report it.
86  }
87 
88  if (!quiet) {
89  fprintf(stderr, "Found PCP Component at id %d\n",cid);
90  }
91 
92  // Library is initialized and pcp component is identified.
93 
94  // Set up to exercise the code for _pcp_ctl; it does nothing
95  // but prove it doesn't crash if called. The actual call to
96  // PAPI_set_opt is below; it requires an eventset.
97 
98  PAPI_option_t aDomOpt; // make a domain option.
99  aDomOpt.domain.def_cidx = cid; // fill it out.
100  aDomOpt.domain.domain = PAPI_DOM_ALL; // ..
101 
102  // Begin enumeration of all events.
103 
104  m=PAPI_NATIVE_MASK; // Get the PAPI NATIVE mask.
105  ret=PAPI_enum_cmp_event(&m,PAPI_ENUM_FIRST,cid); // Begin enumeration of ALL papi counters.
106  if (ret != PAPI_OK) fprintf(stderr, "PAPI_enum_cmp_event returned %i [%s].\n", ret, PAPI_strerror(ret));
107  if (ret != PAPI_OK) { // If that failed, report and exit.
108  test_fail(__FILE__, __LINE__, "PAPI_enum_cmp_event failed.\n",
109  ret);
110  }
111 
112  //----------------------------------------------
113  // Setups are done, we begin the work work here.
114  //----------------------------------------------
115  int count = 0;
116  if (!quiet) printf("Component Idx, Symbol, Units, Description, HexCode (this run only), Time Scope, PAPI_TYPE, Sample Value\n");
117 
118  do{ // Enumerate all events.
119  memset(&info,0,sizeof(PAPI_event_info_t)); // Clear event info.
120  k=m; // Make a copy of current code.
121 
122  // enumerate sub-events, with masks. For this test, we do not
123  // have any! But we do this to test our enumeration works as
124  // expected. First time through is guaranteed, of course.
125 
126  do{ // enumerate masked events.
127  ret=PAPI_get_event_info(k,&info); // get name of k symbol.
128  if (ret != PAPI_OK) { // If that failed, report and exit.
129  sprintf(errMsg, "PAPI_get_event_info(%i) failed.\n", k); // build message.
130  test_fail(__FILE__, __LINE__, errMsg, ret);
131  }
132 
133  // Test creating an event set.
134  ret = PAPI_create_eventset(&EventSet); // Try it.
135  if (ret != PAPI_OK) { // If that failed, report and exit.
136  test_fail(__FILE__, __LINE__, "PAPI_enum_create_eventset failed.\n", ret);
137  }
138 
139  // Test adding and removing the event by name.
140  ret=PAPI_add_named_event(EventSet,info.symbol); // Try to add it for counting.
141  if (ret != PAPI_OK) { // If that failed, report it.
142  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_add_named_event('%s') failed.\n", info.symbol);
143  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
144  continue;
145  test_fail( __FILE__, __LINE__, errMsg, ret);
146  }
147 
148  // test _pcp_ctl function. Just need to do this once.
149 
150  if (firstTime) {
151  aDomOpt.domain.eventset = EventSet; // ..
152  ret = PAPI_set_opt(PAPI_DOMAIN, &aDomOpt); // force call to pcp_ctl.
153  if (ret != PAPI_OK) { // If that failed, report and exit.
154  test_fail(__FILE__, __LINE__, "PAPI_set_opt failed.\n",
155  ret);
156  }
157  }
158 
159  ret=PAPI_remove_named_event(EventSet,info.symbol); // Try to remove it.
160  if (ret != PAPI_OK) { // If that failed, report it.
161  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_remove_named_event('%s') failed.\n", info.symbol);
162  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
163  continue;
164  test_fail( __FILE__, __LINE__, errMsg, ret);
165  }
166 
167  // Test getting code for name, consistency with enumeration.
168  ret=PAPI_event_name_to_code(info.symbol, &code); // Try to read the code from the name.
169  if (ret != PAPI_OK) { // If that failed, report it.
170  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_name_to_code('%s') failed.\n", info.symbol);
171  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
172  continue;
173  test_fail( __FILE__, __LINE__, errMsg, ret);
174  }
175 
176  // Papi can report a different code; k incremented by 1.
177  // I am not clear on why it does that.
178  if (code != k && code != (k+1)) { // If code retrieved is not the same, fail and report it.
179  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_name_to_code('%s') "
180  "returned code 0x%08X, expected 0x%08X. failure.\n",
181  info.symbol, code, k);
182  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
183  continue;
184  test_fail( __FILE__, __LINE__, errMsg, 0); // report and fail.
185  }
186 
187  // Test getting name from code, consistency with info.
188  char testName[PAPI_MAX_STR_LEN] = ""; // needed for test.
189  ret=PAPI_event_code_to_name(code, testName); // turn code back into a name.
190  if (ret != PAPI_OK) { // If that failed, report it.
191  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_code_to_name(('0x%08X') failed.\n", code);
192  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
193  continue;
194  test_fail( __FILE__, __LINE__, errMsg, ret);
195  }
196 
197  if (strcmp(info.symbol, testName) != 0) { // If name retrieved is not the same, fail and report it.
198  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_code_to_name(('0x%08X') "
199  "returned name=\"'%s'\", expected \"%s\". failure.\n",
200  code, testName, info.symbol);
201  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
202  continue;
203  test_fail( __FILE__, __LINE__, errMsg, 0); // Report and exit.
204  }
205 
206  ret = PAPI_add_event(EventSet, code); // Try to add the event.
207  if (ret != PAPI_OK) { // If that failed, report it.
208  sprintf(errMsg, "PAPI_add_event('0x%08X') failed.\n", code);
209  test_fail( __FILE__, __LINE__, errMsg, ret); // report and exit.
210  }
211 
212  ret = PAPI_start(EventSet); // start counting.
213  if (ret != PAPI_OK) { // If that failed, report it.
214  sprintf(errMsg, "PAPI_start_event('0x%08X') failed.\n", code);
215  test_fail( __FILE__, __LINE__, errMsg, ret); // report and exit.
216  }
217 
218  long long *values = NULL; // pointer for us to malloc next.
219 
220  eventSetCount = PAPI_num_events(EventSet); // get the number of events in set.
221  values = calloc(eventSetCount, sizeof(long long)); // make zeroed space for it.
222 
223  ret = PAPI_read(EventSet, values); // read without a stop.
224  if (ret != PAPI_OK) { // If that failed, report it.
225  test_fail( __FILE__, __LINE__, "PAPI_read(EventSet) failed.\n", ret);
226  }
227 
228  // Test doing something with it.
229  count++; // bump count of items seen and added.
230  if (!quiet) {
231  printf("%i, %s, %s, %s, 0x%08x,",
232  info.component_index, info.symbol,info.units,
233  info.long_descr, info.event_code);
234  convert_64_t cvt;
235  cvt.ll = values[0]; // copy the value returned.
236 
237  if (info.timescope == PAPI_TIMESCOPE_SINCE_START) {
238  printf("SINCE START,");
239  } else {
240  printf("POINT,");
241  }
242 
243  switch (info.data_type) { // based on type,
244  case PAPI_DATATYPE_INT64:
245  printf("INT64, %lli", cvt.ll);
246  break; // END CASE.
247 
249  printf("UINT64, %llu", cvt.ull);
250  break; // END CASE.
251 
252  case PAPI_DATATYPE_FP64:
253  printf("FP64, %f", cvt.d);
254  break; // END CASE.
255 
256  default:
257  printf("UNKNOWN TYPE, %p", cvt.vp);
258  break; // END CASE.
259  }
260 
261  printf("\n");
262  }
263 
264  ret = PAPI_reset(EventSet); // Reset the event.
265  if (ret != PAPI_OK) { // If that failed, report and exit.
266  test_fail( __FILE__, __LINE__, "PAPI_reset_event() failed\n", ret);
267  }
268 
269  ret = PAPI_stop(EventSet, values); // stop counting, get final values.
270  if (ret != PAPI_OK) { // If that failed, report it.
271  test_fail( __FILE__, __LINE__, "PAPI_stop_event(EventSet, values) failed.\n", ret);
272  }
273 
274  free(values); // free alloc memory.
275  values = NULL; // prevent double free.
276 
277  ret = PAPI_cleanup_eventset(EventSet); // Try a cleanup.
278  if (ret != PAPI_OK) { // If that failed, report it.
279  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset(EventSet) failed.\n", ret);
280  }
281 
282  ret = PAPI_destroy_eventset(&EventSet); // Deallocate. No memory leaks!
283  if (ret != PAPI_OK) { // If that failed, report it.
284  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset(EventSet) failed.\n", ret);
285  }
286 
287  firstTime = 0; // Don't test the one-time functions again.
288 
289  } while(PAPI_enum_cmp_event(&k,PAPI_NTV_ENUM_UMASKS,cid)==PAPI_OK); // Get next umask entry (bits different) (should return PAPI_NOEVNT).
290  } while(PAPI_enum_cmp_event(&m,PAPI_ENUM_EVENTS,cid)==PAPI_OK); // Get next event code.
291 
292  // Round 2: Try to load all events into one Event Set.
293 
294  // Create an event set.
295  ret = PAPI_create_eventset(&EventSet); // Try it.
296  if (ret != PAPI_OK) { // If that failed, report and exit.
297  test_fail(__FILE__, __LINE__, "PAPI_enum_create_eventset failed.\n", ret);
298  }
299 
300  m=PAPI_NATIVE_MASK; // Get the PAPI NATIVE mask.
301  ret=PAPI_enum_cmp_event(&m,PAPI_ENUM_FIRST,cid); // Begin enumeration of ALL papi counters.
302  if (ret != PAPI_OK) { // If that failed, report and exit.
303  test_fail(__FILE__, __LINE__, "PAPI_enum_cmp_event failed.\n",
304  ret);
305  }
306 
307  i = 0; // To count successful adds.
308  do{ // Enumerate all events.
309  memset(&info,0,sizeof(PAPI_event_info_t)); // Clear event info.
310  ret=PAPI_get_event_info(m,&info); // get name of k symbol.
311  if (ret != PAPI_OK) { // If that failed, report and exit.
312  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_get_event_info(%i) failed.\n", k); // build message.
313  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
314  continue;
315  test_fail(__FILE__, __LINE__, errMsg, ret);
316  }
317 
318  // Add it in by name.
319  ret=PAPI_add_named_event(EventSet,info.symbol); // Try to add it for counting.
320  if (ret != PAPI_OK) { // If that failed, report it.
321  retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_add_named_event('%s') failed.\n", info.symbol);
322  if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
323  continue;
324  test_fail( __FILE__, __LINE__, errMsg, ret);
325  } else {
326  i++; // success.
327  }
328  } while(PAPI_enum_cmp_event(&m,PAPI_ENUM_EVENTS,cid)==PAPI_OK); // Get next event code.
329 
330 
331  if (i != count) { // If we failed to add them all,
332  sprintf(errMsg, "Test should have been able to add all %i events; failed after %i.\n", count, i); // build message.
333  test_fail(__FILE__, __LINE__, errMsg, 0);
334  }
335 
336  ret = PAPI_cleanup_eventset(EventSet); // Try a cleanup.
337  if (ret != PAPI_OK) { // If that failed, report it.
338  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset(EventSet) failed.\n", ret);
339  }
340 
341  ret = PAPI_destroy_eventset(&EventSet); // Deallocate. No memory leaks!
342  if (ret != PAPI_OK) { // If that failed, report it.
343  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset(EventSet) failed.\n", ret);
344  }
345 
346  if (!quiet) fprintf(stderr, "PCP discovered %i events; added " // Say what we learned.
347  "%i.\n", count, i); // ..
348  PAPI_shutdown(); // get out of papi.
349  if (!quiet) fprintf(stderr, "Shutdown completed.\n"); // If we are verbose,
350  test_pass( __FILE__ ); // Note the test passed.
351  return 0; // Exit with all okay.
352 } // END main.
static struct timeval t1 t2
Definition: testPCP.c:14
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define PAPI_NATIVE_MASK
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:796
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
int PAPI_reset(int EventSet)
Definition: papi.c:2459
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:976
int PAPI_num_components(void)
Definition: papi.c:4387
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:970
int EventSet
int PAPI_event_name_to_code(const char *in, int *out)
Definition: papi.c:1004
A pointer to the following is passed to PAPI_set/get_opt()
Definition: papi.h:849
#define mConvertUsec(timeval_)
Definition: testPCP.c:13
volatile double t1
int PAPI_set_opt(int option, PAPI_option_t *ptr)
Definition: papi.c:3465
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
#define PAPI_VERSION
Definition: fpapi.h:15
int PAPI_library_init(int version)
Definition: papi.c:500
#define PAPI_VERSION_MAJOR(x)
Definition: papi.h:217
void PAPI_shutdown(void)
Definition: papi.c:4461
int quiet
Definition: rapl_overflow.c:18
int PAPI_num_events(int EventSet)
Definition: papi.c:4426
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:967
#define PAPI_VERSION_REVISION(x)
Definition: papi.h:219
#define PAPI_NULL
Definition: fpapi.h:13
long long ret
Definition: iozone.c:1346
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:630
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1357
#define PAPI_VERSION_MINOR(x)
Definition: papi.h:218
unsigned long long ull
Definition: benchSANVML.c:138
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:915
int gettimeofday(void *ptr1, void *ptr2)
char * PAPI_strerror(int errorCode)
Definition: papi.c:4603
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
int component_index
Definition: papi.h:975
long long ll
Definition: benchSANVML.c:137
#define PAPI_DOMAIN
Definition: fpapi.h:50
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
int PAPI_remove_named_event(int EventSet, const char *EventName)
Definition: papi.c:1961
PAPI_domain_option_t domain
Definition: papi.h:856
int PAPI_read(int EventSet, long long *values)
Definition: papi.c:2559
int PAPI_start(int EventSet)
Definition: papi.c:2096
unsigned int event_code
Definition: papi.h:965
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
#define PAPI_DOM_ALL
Definition: fpapi.h:25
static long count
int i
Definition: fileop.c:140
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
Here is the call graph for this function:

Variable Documentation

◆ t2

struct timeval t1 t2
static

Definition at line 14 of file testPCP.c.