PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
multiplex1_pthreads.c
Go to the documentation of this file.
1 /*
2 * File: multiplex1_pthreads.c
3 * Author: Philip Mucci
4 * mucci@cs.utk.edu
5 */
6 
7 /* This file tests the multiplex pthread functionality */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <pthread.h>
13 
14 #include "papi.h"
15 #include "papi_test.h"
16 
17 #include "do_loops.h"
18 
19 #define TOTAL_EVENTS 10
20 
23 };
24 
27 };
28 
31 };
32 
33 static int PAPI_events[TOTAL_EVENTS] = { 0, };
34 static int PAPI_events_len = 0;
35 
36 static void
37 init_papi_pthreads( int *out_events, int *len )
38 {
39  int retval;
40  int i, real_len = 0;
41  int *in_events = preset_PAPI_events;
42  const PAPI_hw_info_t *hw_info;
43 
44  /* Initialize the library */
46  if ( retval != PAPI_VER_CURRENT ) {
47  test_fail(__FILE__, __LINE__, "PAPI_library_init", retval );
48  }
49 
50  hw_info = PAPI_get_hardware_info( );
51  if ( hw_info == NULL ) {
52  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
53  }
54 
55  if ( strstr( hw_info->model_string, "UltraSPARC" ) ) {
56  in_events = solaris_preset_PAPI_events;
57  }
58 
59  if ( strcmp( hw_info->model_string, "POWER6" ) == 0 ) {
60  in_events = power6_preset_PAPI_events;
61  retval = PAPI_set_domain( PAPI_DOM_ALL );
62  if ( retval != PAPI_OK ) {
63  test_fail(__FILE__, __LINE__,
64  "PAPI_set_domain", retval );
65  }
66  }
67 
68  retval = PAPI_multiplex_init( );
69  if ( retval == PAPI_ENOSUPP) {
70  test_skip(__FILE__, __LINE__, "Multiplex not supported", 1);
71  }
72  else if ( retval != PAPI_OK ) {
73  test_fail(__FILE__, __LINE__, "PAPI_multiplex_init", retval );
74  }
75 
76  retval = PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) );
77  if (retval != PAPI_OK ) {
78  if ( retval == PAPI_ECMP )
79  test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
80  else
81  test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
82  }
83 
84  for ( i = 0; in_events[i] != 0; i++ ) {
85  char out[PAPI_MAX_STR_LEN];
86  /* query and set up the right instruction to monitor */
87  retval = PAPI_query_event( in_events[i] );
88  if ( retval == PAPI_OK ) {
89  out_events[real_len++] = in_events[i];
90  PAPI_event_code_to_name( in_events[i], out );
91  if ( real_len == *len )
92  break;
93  } else {
94  PAPI_event_code_to_name( in_events[i], out );
95  if ( !TESTS_QUIET )
96  printf( "%s does not exist\n", out );
97  }
98  }
99  if ( real_len < 1 ) {
100  if (!TESTS_QUIET) printf("No counters available\n");
101  test_skip(__FILE__, __LINE__, "No counters available", 0 );
102  }
103  *len = real_len;
104 }
105 
106 static int
107 do_pthreads( void *( *fn ) ( void * ) )
108 {
109  int i, rc, retval;
110  pthread_attr_t attr;
112 
113  pthread_attr_init( &attr );
114 #ifdef PTHREAD_CREATE_UNDETACHED
115  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
116 #endif
117 #ifdef PTHREAD_SCOPE_SYSTEM
118  retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
119  if ( retval != 0 )
120  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
121 #endif
122 
123  for ( i = 0; i < NUM_THREADS; i++ ) {
124  rc = pthread_create( &id[i], &attr, fn, NULL );
125  if ( rc )
126  return ( FAILURE );
127  }
128  for ( i = 0; i < NUM_THREADS; i++ )
129  pthread_join( id[i], NULL );
130 
131  pthread_attr_destroy( &attr );
132 
133  return ( SUCCESS );
134 }
135 
136 /* Tests that PAPI_multiplex_init does not mess with normal operation. */
137 
138 static void *
139 case1_pthreads( void *arg )
140 {
141  ( void ) arg; /*unused */
142  int retval, i, EventSet = PAPI_NULL;
143  long long values[2];
144 
145  if ( ( retval = PAPI_register_thread( ) ) != PAPI_OK ) {
146  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
147  }
148 
149  if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK ) {
150  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
151  }
152 
153  for ( i = 0; i < PAPI_events_len; i++ ) {
154  char out[PAPI_MAX_STR_LEN];
155 
156  retval = PAPI_add_event( EventSet, PAPI_events[i] );
157  if ( retval != PAPI_OK )
158  test_fail(__FILE__, __LINE__, "PAPI_add_event", retval );
160  if ( !TESTS_QUIET )
161  printf( "Added %s\n", out );
162  }
163 
164  do_stuff( );
165 
166  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
167  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
168 
169  do_stuff( );
170 
171  if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK )
172  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
173 
174  if ( !TESTS_QUIET ) {
175  printf( "case1 thread %4x:", ( unsigned ) pthread_self( ) );
176  test_print_event_header( "", EventSet );
177  printf( "case1 thread %4x:", ( unsigned ) pthread_self( ) );
178  printf( TAB2, "", values[0], values[1] );
179  }
180 
181  if ( ( retval = PAPI_cleanup_eventset( EventSet ) ) != PAPI_OK ) /* JT */
182  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
183 
184  if ( ( retval = PAPI_destroy_eventset( &EventSet) ) != PAPI_OK )
185  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
186 
187  if ( ( retval = PAPI_unregister_thread( ) ) != PAPI_OK )
188  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
189 
190  return ( ( void * ) SUCCESS );
191 }
192 
193 /* Tests that PAPI_set_multiplex() works before adding events */
194 
195 static void *
196 case2_pthreads( void *arg )
197 {
198  ( void ) arg; /*unused */
199  int retval, i, EventSet = PAPI_NULL;
200  long long values[2];
201 
202  if ( ( retval = PAPI_register_thread( ) ) != PAPI_OK ) {
203  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
204  }
205 
206  if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK ) {
207  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
208  }
209 
210  /* In Component PAPI, EventSets must be assigned a component index
211  before you can fiddle with their internals.
212  0 is always the cpu component */
213  retval = PAPI_assign_eventset_component( EventSet, 0 );
214  if ( retval != PAPI_OK ) {
215  test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component", retval );
216  }
217 
218  if ( ( retval = PAPI_set_multiplex( EventSet ) ) != PAPI_OK ) {
219  if ( retval == PAPI_ENOSUPP) {
220  test_skip(__FILE__, __LINE__, "Multiplex not supported", 1);
221  }
222  test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );
223  }
224 
225  if (!TESTS_QUIET) {
226  printf( "++case2 thread %4x:", ( unsigned ) pthread_self( ) );
227  }
228 
229  for ( i = 0; i < PAPI_events_len; i++ ) {
230  char out[PAPI_MAX_STR_LEN];
231 
232  retval = PAPI_add_event( EventSet, PAPI_events[i] );
233  if ( retval != PAPI_OK )
234  test_fail(__FILE__, __LINE__, "PAPI_add_event", retval );
236  if ( !TESTS_QUIET )
237  printf( "Added %s\n", out );
238  }
239 
240  do_stuff( );
241 
242  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
243  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
244 
245  do_stuff( );
246 
247  if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK )
248  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
249 
250  if ( !TESTS_QUIET ) {
251  printf( "case2 thread %4x:", ( unsigned ) pthread_self( ) );
252  test_print_event_header( "", EventSet );
253  printf( "case2 thread %4x:", ( unsigned ) pthread_self( ) );
254  printf( TAB2, "", values[0], values[1] );
255  }
256 
257  /* JT */
258  if ( ( retval = PAPI_cleanup_eventset( EventSet ) ) != PAPI_OK ) {
259  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
260  }
261 
262  if ( ( retval = PAPI_destroy_eventset( &EventSet) ) != PAPI_OK ) {
263  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
264  }
265 
266  if ( ( retval = PAPI_unregister_thread( ) ) != PAPI_OK ) {
267  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
268  }
269 
270  return ( ( void * ) SUCCESS );
271 }
272 
273 /* Tests that PAPI_set_multiplex() works after adding events */
274 
275 static void *
276 case3_pthreads( void *arg )
277 {
278  ( void ) arg; /*unused */
279  int retval, i, EventSet = PAPI_NULL;
280  long long values[2];
281 
282  if ( ( retval = PAPI_register_thread( ) ) != PAPI_OK ) {
283  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
284  }
285 
286  if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK ) {
287  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
288  }
289 
290  for ( i = 0; i < PAPI_events_len; i++ ) {
291  char out[PAPI_MAX_STR_LEN];
292 
293  retval = PAPI_add_event( EventSet, PAPI_events[i] );
294  if ( retval != PAPI_OK )
295  test_fail(__FILE__, __LINE__, "PAPI_add_event", retval );
297  if ( !TESTS_QUIET )
298  printf( "Added %s\n", out );
299  }
300 
301  if ( ( retval = PAPI_set_multiplex( EventSet ) ) != PAPI_OK ) {
302  if ( retval == PAPI_ENOSUPP) {
303  test_skip(__FILE__, __LINE__, "Multiplex not supported", 1);
304  }
305  test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );
306  }
307  do_stuff( );
308 
309  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
310  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
311  }
312 
313  do_stuff( );
314 
315  if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK ) {
316  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
317  }
318 
319  if ( !TESTS_QUIET ) {
320  printf( "case3 thread %4x:", ( unsigned ) pthread_self( ) );
321  test_print_event_header( "", EventSet );
322  printf( "case3 thread %4x:", ( unsigned ) pthread_self( ) );
323  printf( TAB2, "", values[0], values[1] );
324  }
325 
326  if ( ( retval = PAPI_cleanup_eventset( EventSet ) ) != PAPI_OK ) /* JT */
327  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
328 
329  if ( ( retval = PAPI_destroy_eventset( &EventSet) ) != PAPI_OK )
330  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
331 
332  if ( ( retval = PAPI_unregister_thread( ) ) != PAPI_OK )
333  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
334 
335  return ( ( void * ) SUCCESS );
336 }
337 
338 /* Tests that PAPI_set_multiplex() works before/after adding events */
339 
340 static void *
341 case4_pthreads( void *arg )
342 {
343  ( void ) arg; /*unused */
344  int retval, i, EventSet = PAPI_NULL;
345  long long values[4];
346  char out[PAPI_MAX_STR_LEN];
347 
348  if ( ( retval = PAPI_register_thread( ) ) != PAPI_OK ) {
349  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
350  }
351 
352  if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK ) {
353  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
354  }
355 
356  i = 0;
357  retval = PAPI_add_event( EventSet, PAPI_events[i] );
358  if ( retval != PAPI_OK )
359  test_fail(__FILE__, __LINE__, "PAPI_add_event", retval );
361  if (!TESTS_QUIET) printf( "Added %s\n", out );
362 
363  if ( ( retval = PAPI_set_multiplex( EventSet ) ) != PAPI_OK ) {
364  if ( retval == PAPI_ENOSUPP) {
365  test_skip(__FILE__, __LINE__, "Multiplex not supported", 1);
366  }
367 
368  test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );
369  }
370  i = 1;
371  retval = PAPI_add_event( EventSet, PAPI_events[i] );
372  if ( retval != PAPI_OK )
373  test_fail(__FILE__, __LINE__, "PAPI_add_event", retval );
375  if (!TESTS_QUIET) printf( "Added %s\n", out );
376 
377  do_stuff( );
378 
379  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
380  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
381 
382  do_stuff( );
383 
384  if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK )
385  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
386 
387  if ( !TESTS_QUIET ) {
388  printf( "case4 thread %4x:", ( unsigned ) pthread_self( ) );
389  test_print_event_header( "", EventSet );
390  printf( "case4 thread %4x:", ( unsigned ) pthread_self( ) );
391  printf( TAB2, "", values[0], values[1] );
392  }
393 
394  if ( ( retval = PAPI_cleanup_eventset( EventSet ) ) != PAPI_OK ) /* JT */
395  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
396 
397  if ( ( retval = PAPI_destroy_eventset( &EventSet) ) != PAPI_OK )
398  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
399 
400  if ( ( retval = PAPI_unregister_thread( ) ) != PAPI_OK )
401  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
402 
403  return ( ( void * ) SUCCESS );
404 }
405 
406 static int
407 case1( void )
408 {
409  int retval;
410 
411  PAPI_events_len = 2;
413 
414  retval = do_pthreads( case1_pthreads );
415 
416  PAPI_shutdown( );
417 
418  return retval;
419 }
420 
421 static int
422 case2( void )
423 {
424  int retval;
425 
426  PAPI_events_len = 2;
428 
429  retval = do_pthreads( case2_pthreads );
430 
431  PAPI_shutdown( );
432 
433  return retval;
434 }
435 
436 static int
437 case3( void )
438 {
439  int retval;
440 
441  PAPI_events_len = 2;
443 
444  retval = do_pthreads( case3_pthreads );
445 
446  PAPI_shutdown( );
447 
448  return retval;
449 }
450 
451 static int
452 case4( void )
453 {
454  int retval;
455 
456  PAPI_events_len = 2;
458 
459  retval = do_pthreads( case4_pthreads );
460 
461  PAPI_shutdown( );
462 
463  return retval;
464 }
465 
466 int
467 main( int argc, char **argv )
468 {
469  int retval;
470  int quiet;
471 
472  /* Set TESTS_QUIET variable */
473  quiet = tests_quiet( argc, argv );
474 
475  if (!quiet) {
476  printf( "%s: Using %d threads\n\n", argv[0], NUM_THREADS );
477  }
478 
479  /* Case1 */
480  if (!quiet) {
481  printf ( "case1: Does PAPI_multiplex_init() "
482  "not break regular operation?\n" );
483  }
484 
485  if ( case1() != SUCCESS ) {
486  test_fail( __FILE__, __LINE__, "case1", PAPI_ESYS );
487  }
488 
489  /* Case2 */
490  if (!quiet) {
491  printf( "case2: Does setmpx/add work?\n" );
492  }
493  if ( case2( ) != SUCCESS ) {
494  test_fail( __FILE__, __LINE__, "case2", PAPI_ESYS );
495  }
496 
497  /* Case3 */
498  if (!quiet) {
499  printf( "case3: Does add/setmpx work?\n" );
500  }
501  if ( case3( ) != SUCCESS ) {
502  test_fail( __FILE__, __LINE__, "case3", PAPI_ESYS );
503  }
504 
505  /* Case4 */
506  if (!quiet) {
507  printf( "case4: Does add/setmpx/add work?\n" );
508  }
509  if ( case4( ) != SUCCESS ) {
510  test_fail( __FILE__, __LINE__, "case4", PAPI_ESYS );
511  }
512 
513  /* Finally init PAPI? */
515  if ( retval != PAPI_VER_CURRENT ) {
516  test_fail(__FILE__, __LINE__, "PAPI_library_init", retval );
517  }
518 
519  test_pass( __FILE__ );
520 
521  return 0;
522 
523 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define NUM_THREADS
Definition: do_loops.h:8
void test_pass(const char *filename)
Definition: test_utils.c:432
#define PAPI_L2_TCM
#define FAILURE
Definition: do_loops.h:6
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
Hardware info structure.
Definition: papi.h:780
#define PAPI_L1_DCM
int preset_PAPI_events[TOTAL_EVENTS]
Definition: multiplex1.c:31
static int PAPI_events_len
static void * case3_pthreads(void *arg)
#define PAPI_ENOSUPP
Definition: papi.h:271
#define PAPI_NULL
Definition: papi.h:292
void do_stuff(void)
Definition: do_loops.c:256
void test_print_event_header(const char *call, int evset)
Definition: test_utils.c:580
static int do_pthreads(void *(*fn)(void *))
#define PAPI_TOT_INS
#define PAPI_DOM_ALL
Definition: papi.h:303
int EventSet
int PAPI_register_thread(void)
Definition: papi.c:208
return PAPI_OK
Definition: linux-nvml.c:497
#define SUCCESS
Definition: do_loops.h:5
static void * case1_pthreads(void *arg)
#define PAPI_BR_MSP
void
Definition: iozone.c:18627
int power6_preset_PAPI_events[TOTAL_EVENTS]
Definition: multiplex1.c:28
int int argc
Definition: iozone.c:1609
int case2(void)
Definition: multiplex1.c:151
Return codes and api definitions.
int rc
Definition: iozone.c:20281
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
char ** argv
Definition: iozone.c:1610
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:123
int PAPI_library_init(int version)
Definition: papi.c:500
int i
Definition: fileop.c:140
static void init_papi_pthreads(int *out_events, int *len)
void PAPI_shutdown(void)
Definition: papi.c:4461
int quiet
Definition: rapl_overflow.c:18
#define PAPI_TOT_CYC
#define TAB2
Definition: papi_test.h:89
int solaris_preset_PAPI_events[TOTAL_EVENTS]
Definition: multiplex1.c:25
#define PAPI_ESYS
Definition: papi.h:255
#define TOTAL_EVENTS
#define PAPI_ECMP
Definition: papi.h:256
pthread_t
Definition: iozone.c:18557
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_assign_eventset_component(int EventSet, int cidx)
Definition: papi.c:1526
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
#define PAPI_L1_ICM
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:915
goto out
Definition: pscanf.h:20
int case4(void)
Definition: multiplex1.c:274
int PAPI_query_event(int EventCode)
Definition: papi.c:684
int PAPI_multiplex_init(void)
Definition: papi.c:2982
printf("\tTry: -i 0 -i 1 \n\n")
int PAPI_unregister_thread(void)
Definition: papi.c:244
int TESTS_QUIET
Definition: test_utils.c:18
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
unsigned int PAPI_events[EVENTSLEN]
Definition: derived.c:13
int PAPI_set_multiplex(int EventSet)
Definition: papi.c:3333
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
#define PAPI_MAX_STR_LEN
Definition: papi.h:465
int PAPI_set_domain(int domain)
Definition: papi.c:5688
static void * case4_pthreads(void *arg)
char model_string[PAPI_MAX_STR_LEN]
Definition: papi.h:790
int PAPI_start(int EventSet)
Definition: papi.c:2096
static void * case2_pthreads(void *arg)
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6185
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
#define PAPI_VER_CURRENT
Definition: papi.h:225
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:28
int main(int argc, char **argv)
List all appio events codes and names.
int case1(void)
Definition: multiplex1.c:102
pthread_attr_t attr
Definition: iozone.c:18466
int case3(void)
Definition: multiplex1.c:215
#define PAPI_FP_INS