PAPI 7.1.0.0
Loading...
Searching...
No Matches
testPCP.c
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// This test program exercises the functions within the linux-pcp.c component.
3//-----------------------------------------------------------------------------
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <math.h>
9#include <papi.h>
10#include "papi_test.h"
11#include <sys/time.h>
12
13#define mConvertUsec(timeval_) ((double) (timeval_.tv_sec*1000000+timeval_.tv_usec)) /* avoid typos, make it a double. */
14static struct timeval t1, t2;
15
16unsigned long dummyThreadId(void) { return(0); } // dummy return to give a thread id.
17
18typedef union
19{
20 long long ll;
21 unsigned long long ull;
22 double d;
23 void *vp;
24 unsigned char ch[8];
26
27//-----------------------------------------------------------------------------
28// MAIN.
29//-----------------------------------------------------------------------------
30
31int main(int argc, char **argv) { // args to allow quiet flags.
32 int i,k,m,code=0;
33 int eventSetCount, ret;
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 PAPI_component_info_t *aComponent;
69 k = PAPI_num_components(); // get number of components.
70 for (i=0; i<k && cid<0; i++) { // while not found,
71 aComponent = (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 // If the component is disabled, skip the test.
93 if (aComponent->disabled) {
94 test_skip(__FILE__,__LINE__,"Component pcp is disabled", 0);
96 return 0;
97 }
98
99 // Library is initialized and pcp component is identified.
100
101 // Set up to exercise the code for _pcp_ctl; it does nothing
102 // but prove it doesn't crash if called. The actual call to
103 // PAPI_set_opt is below; it requires an eventset.
104
105 PAPI_option_t aDomOpt; // make a domain option.
106 aDomOpt.domain.def_cidx = cid; // fill it out.
107 aDomOpt.domain.domain = PAPI_DOM_ALL; // ..
108
109 // Begin enumeration of all events.
110
111 m=PAPI_NATIVE_MASK; // Get the PAPI NATIVE mask.
112 ret=PAPI_enum_cmp_event(&m,PAPI_ENUM_FIRST,cid); // Begin enumeration of ALL papi counters.
113 if (ret != PAPI_OK) fprintf(stderr, "PAPI_enum_cmp_event returned %i [%s].\n", ret, PAPI_strerror(ret));
114 if (ret != PAPI_OK) { // If that failed, report and exit.
115 test_fail(__FILE__, __LINE__, "PAPI_enum_cmp_event failed.\n",
116 ret);
117 }
118
119 //----------------------------------------------
120 // Setups are done, we begin the work work here.
121 //----------------------------------------------
122 int count = 0;
123 if (!quiet) printf("Component Idx, Symbol, Units, Description, HexCode (this run only), Time Scope, PAPI_TYPE, Sample Value\n");
124
125 do{ // Enumerate all events.
126 memset(&info,0,sizeof(PAPI_event_info_t)); // Clear event info.
127 k=m; // Make a copy of current code.
128
129 // enumerate sub-events, with masks. For this test, we do not
130 // have any! But we do this to test our enumeration works as
131 // expected. First time through is guaranteed, of course.
132
133 do{ // enumerate masked events.
134 ret=PAPI_get_event_info(k,&info); // get name of k symbol.
135 if (ret != PAPI_OK) { // If that failed, report and exit.
136 sprintf(errMsg, "PAPI_get_event_info(%i) failed.\n", k); // build message.
137 test_fail(__FILE__, __LINE__, errMsg, ret);
138 }
139
140 // Test creating an event set.
141 ret = PAPI_create_eventset(&EventSet); // Try it.
142 if (ret != PAPI_OK) { // If that failed, report and exit.
143 test_fail(__FILE__, __LINE__, "PAPI_enum_create_eventset failed.\n", ret);
144 }
145
146 // Test adding and removing the event by name.
147 ret=PAPI_add_named_event(EventSet,info.symbol); // Try to add it for counting.
148 if (ret != PAPI_OK) { // If that failed, report it.
149 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_add_named_event('%s') failed.\n", info.symbol);
150 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
151 continue;
152 test_fail( __FILE__, __LINE__, errMsg, ret);
153 }
154
155 // test _pcp_ctl function. Just need to do this once.
156
157 if (firstTime) {
158 aDomOpt.domain.eventset = EventSet; // ..
159 ret = PAPI_set_opt(PAPI_DOMAIN, &aDomOpt); // force call to pcp_ctl.
160 if (ret != PAPI_OK) { // If that failed, report and exit.
161 test_fail(__FILE__, __LINE__, "PAPI_set_opt failed.\n",
162 ret);
163 }
164 }
165
166 ret=PAPI_remove_named_event(EventSet,info.symbol); // Try to remove it.
167 if (ret != PAPI_OK) { // If that failed, report it.
168 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_remove_named_event('%s') failed.\n", info.symbol);
169 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
170 continue;
171 test_fail( __FILE__, __LINE__, errMsg, ret);
172 }
173
174 // Test getting code for name, consistency with enumeration.
175 ret=PAPI_event_name_to_code(info.symbol, &code); // Try to read the code from the name.
176 if (ret != PAPI_OK) { // If that failed, report it.
177 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_name_to_code('%s') failed.\n", info.symbol);
178 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
179 continue;
180 test_fail( __FILE__, __LINE__, errMsg, ret);
181 }
182
183 // Papi can report a different code; k incremented by 1.
184 // I am not clear on why it does that.
185 if (code != k && code != (k+1)) { // If code retrieved is not the same, fail and report it.
186 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_name_to_code('%s') "
187 "returned code 0x%08X, expected 0x%08X. failure.\n",
188 info.symbol, code, k);
189 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
190 continue;
191 test_fail( __FILE__, __LINE__, errMsg, 0); // report and fail.
192 }
193
194 // Test getting name from code, consistency with info.
195 char testName[PAPI_MAX_STR_LEN] = ""; // needed for test.
196 ret=PAPI_event_code_to_name(code, testName); // turn code back into a name.
197 if (ret != PAPI_OK) { // If that failed, report it.
198 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_code_to_name(('0x%08X') failed.\n", code);
199 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
200 continue;
201 test_fail( __FILE__, __LINE__, errMsg, ret);
202 }
203
204 if (strcmp(info.symbol, testName) != 0) { // If name retrieved is not the same, fail and report it.
205 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_event_code_to_name(('0x%08X') "
206 "returned name=\"'%s'\", expected \"%s\". failure.\n",
207 code, testName, info.symbol);
208 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
209 continue;
210 test_fail( __FILE__, __LINE__, errMsg, 0); // Report and exit.
211 }
212
213 ret = PAPI_add_event(EventSet, code); // Try to add the event.
214 if (ret != PAPI_OK) { // If that failed, report it.
215 sprintf(errMsg, "PAPI_add_event('0x%08X') failed.\n", code);
216 test_fail( __FILE__, __LINE__, errMsg, ret); // report and exit.
217 }
218
219 ret = PAPI_start(EventSet); // start counting.
220 if (ret != PAPI_OK) { // If that failed, report it.
221 sprintf(errMsg, "PAPI_start_event('0x%08X') failed.\n", code);
222 test_fail( __FILE__, __LINE__, errMsg, ret); // report and exit.
223 }
224
225 long long *values = NULL; // pointer for us to malloc next.
226
227 eventSetCount = PAPI_num_events(EventSet); // get the number of events in set.
228 if (eventSetCount < 1) {
229 test_fail( __FILE__, __LINE__, "PAPI_num_events(EventSet) failed.\n", ret);
230 }
231
232 values = calloc(eventSetCount, sizeof(long long)); // make zeroed space for it.
233
234 ret = PAPI_read(EventSet, values); // read without a stop.
235 if (ret != PAPI_OK) { // If that failed, report it.
236 free(values);
237 test_fail( __FILE__, __LINE__, "PAPI_read(EventSet) failed.\n", ret);
238 }
239
240 // Test doing something with it.
241 count++; // bump count of items seen and added.
242 if (!quiet) {
243 printf("%i, %s, %s, %s, 0x%08x,",
244 info.component_index, info.symbol,info.units,
245 info.long_descr, info.event_code);
246 convert_64_t cvt;
247 cvt.ll = values[0]; // copy the value returned.
248
250 printf("SINCE START,");
251 } else {
252 printf("POINT,");
253 }
254
255 switch (info.data_type) { // based on type,
257 printf("INT64, %lli", cvt.ll);
258 break; // END CASE.
259
261 printf("UINT64, %llu", cvt.ull);
262 break; // END CASE.
263
265 printf("FP64, %f", cvt.d);
266 break; // END CASE.
267
268 default:
269 printf("UNKNOWN TYPE, %p", cvt.vp);
270 break; // END CASE.
271 }
272
273 printf("\n");
274 }
275
276 ret = PAPI_reset(EventSet); // Reset the event.
277 if (ret != PAPI_OK) { // If that failed, report and exit.
278 free(values);
279 test_fail( __FILE__, __LINE__, "PAPI_reset_event() failed\n", ret);
280 }
281
282 ret = PAPI_stop(EventSet, values); // stop counting, get final values.
283 if (ret != PAPI_OK) { // If that failed, report it.
284 free(values);
285 test_fail( __FILE__, __LINE__, "PAPI_stop_event(EventSet, values) failed.\n", ret);
286 }
287
288 free(values); // free alloc memory.
289 values = NULL; // prevent double free.
290
291 ret = PAPI_cleanup_eventset(EventSet); // Try a cleanup.
292 if (ret != PAPI_OK) { // If that failed, report it.
293 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset(EventSet) failed.\n", ret);
294 }
295
296 ret = PAPI_destroy_eventset(&EventSet); // Deallocate. No memory leaks!
297 if (ret != PAPI_OK) { // If that failed, report it.
298 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset(EventSet) failed.\n", ret);
299 }
300
301 firstTime = 0; // Don't test the one-time functions again.
302
303 } while(PAPI_enum_cmp_event(&k,PAPI_NTV_ENUM_UMASKS,cid)==PAPI_OK); // Get next umask entry (bits different) (should return PAPI_NOEVNT).
304 } while(PAPI_enum_cmp_event(&m,PAPI_ENUM_EVENTS,cid)==PAPI_OK); // Get next event code.
305
306 // Round 2: Try to load all events into one Event Set.
307
308 // Create an event set.
309 ret = PAPI_create_eventset(&EventSet); // Try it.
310 if (ret != PAPI_OK) { // If that failed, report and exit.
311 test_fail(__FILE__, __LINE__, "PAPI_enum_create_eventset failed.\n", ret);
312 }
313
314 m=PAPI_NATIVE_MASK; // Get the PAPI NATIVE mask.
315 ret=PAPI_enum_cmp_event(&m,PAPI_ENUM_FIRST,cid); // Begin enumeration of ALL papi counters.
316 if (ret != PAPI_OK) { // If that failed, report and exit.
317 test_fail(__FILE__, __LINE__, "PAPI_enum_cmp_event failed.\n",
318 ret);
319 }
320
321 i = 0; // To count successful adds.
322 do{ // Enumerate all events.
323 memset(&info,0,sizeof(PAPI_event_info_t)); // Clear event info.
324 ret=PAPI_get_event_info(m,&info); // get name of k symbol.
325 if (ret != PAPI_OK) { // If that failed, report and exit.
326 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_get_event_info(%i) failed.\n", k); // build message.
327 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
328 continue;
329 test_fail(__FILE__, __LINE__, errMsg, ret);
330 }
331
332 // Add it in by name.
333 ret=PAPI_add_named_event(EventSet,info.symbol); // Try to add it for counting.
334 if (ret != PAPI_OK) { // If that failed, report it.
335 retlen = snprintf(errMsg, PAPI_MAX_STR_LEN, "PAPI_add_named_event('%s') failed.\n", info.symbol);
336 if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen)
337 continue;
338 test_fail( __FILE__, __LINE__, errMsg, ret);
339 } else {
340 i++; // success.
341 }
342 } while(PAPI_enum_cmp_event(&m,PAPI_ENUM_EVENTS,cid)==PAPI_OK); // Get next event code.
343
344
345 if (i != count) { // If we failed to add them all,
346 sprintf(errMsg, "Test should have been able to add all %i events; failed after %i.\n", count, i); // build message.
347 test_fail(__FILE__, __LINE__, errMsg, 0);
348 }
349
350 ret = PAPI_cleanup_eventset(EventSet); // Try a cleanup.
351 if (ret != PAPI_OK) { // If that failed, report it.
352 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset(EventSet) failed.\n", ret);
353 }
354
355 ret = PAPI_destroy_eventset(&EventSet); // Deallocate. No memory leaks!
356 if (ret != PAPI_OK) { // If that failed, report it.
357 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset(EventSet) failed.\n", ret);
358 }
359
360 if (!quiet) fprintf(stderr, "PCP discovered %i events; added " // Say what we learned.
361 "%i.\n", count, i); // ..
362 PAPI_shutdown(); // get out of papi.
363 if (!quiet) fprintf(stderr, "Shutdown completed.\n"); // If we are verbose,
364 test_pass( __FILE__ ); // Note the test passed.
365 return 0; // Exit with all okay.
366} // END main.
int i
static long count
add PAPI preset or native hardware event to an event set
add PAPI preset or native hardware event by name to an EventSet
Empty and destroy an EventSet.
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
Enumerate PAPI preset or native events for a given component.
Convert a numeric hardware event code to a name.
Convert a name to a numeric hardware event code.
get information about a specific software component
Get the event's name and description info.
initialize the PAPI library.
Return the number of events in an event set.
Read hardware counters from an event set.
removes a named hardware event from a PAPI event set.
Reset the hardware event counts in an event set.
Set PAPI library or event set options.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Returns a string describing the PAPI error code.
volatile double t1
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_VERSION
Definition: f90papi.h:193
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_DATATYPE_FP64
Definition: f90papi.h:171
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
#define PAPI_DATATYPE_UINT64
Definition: f90papi.h:278
#define PAPI_DOMAIN
Definition: f90papi.h:159
#define PAPI_NTV_ENUM_UMASKS
Definition: f90papi.h:66
#define PAPI_TIMESCOPE_SINCE_START
Definition: f90papi.h:124
#define PAPI_DATATYPE_INT64
Definition: f90papi.h:227
#define PAPI_DOM_ALL
Definition: f90papi.h:261
int PAPI_num_components(void)
Definition: papi.c:4947
void PAPI_shutdown(void)
Definition: papi.c:5021
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
#define PAPI_NATIVE_MASK
Return codes and api definitions.
#define PAPI_VERSION_REVISION(x)
Definition: papi.h:221
#define PAPI_VERSION_MAJOR(x)
Definition: papi.h:219
#define PAPI_VERSION_MINOR(x)
Definition: papi.h:220
FILE * stderr
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
void PAPI_NORETURN test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:584
int main()
Definition: pernode.c:20
int quiet
Definition: rapl_overflow.c:19
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627
int component_index
Definition: papi.h:968
unsigned int event_code
Definition: papi.h:958
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:969
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:960
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:963
#define mConvertUsec(timeval_)
Definition: testPCP.c:13
unsigned long dummyThreadId(void)
Definition: testPCP.c:16
static struct timeval t1 t2
Definition: testPCP.c:14
A pointer to the following is passed to PAPI_set/get_opt()
Definition: papi.h:843
PAPI_domain_option_t domain
Definition: papi.h:849
unsigned long long ull
Definition: benchSANVML.c:138
long long ll
Definition: benchSANVML.c:137