PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
multiplex3_pthreads.c
Go to the documentation of this file.
1 /*
2 * File: multiplex3_pthreads.c
3 * Author: Philip Mucci
4 * mucci@cs.utk.edu
5 * Mods: John May
6 * johnmay@llnl.gov
7 */
8 
9 /* This file tests the multiplex functionality when there are
10  * threads in which the application isn't calling PAPI (and only
11  * one thread that is calling PAPI.)
12  */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <pthread.h>
17 
18 #include "papi.h"
19 #include "papi_test.h"
20 
21 #include "do_loops.h"
22 
23 #define MAX_TO_ADD 5
24 
25 /* A thread function that does nothing forever, while the other
26  * tests are running.
27  */
28 void *
29 thread_fn( void *dummy )
30 {
31  ( void ) dummy;
32  while ( 1 ) {
33  do_stuff( );
34  }
35  return NULL;
36 }
37 
38 /* Runs a bunch of multiplexed events */
39 
40 static void
41 mainloop( int arg )
42 {
43  int allvalid;
44  long long *values;
45  int EventSet = PAPI_NULL;
46  int retval, i, j = 2, skipped_counters=0;
47  PAPI_event_info_t pset;
48 
49  ( void ) arg;
50 
51  /* Initialize the library */
52 
54  if ( retval != PAPI_VER_CURRENT ) {
55  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
56  }
57 
58  retval = PAPI_multiplex_init( );
59  if ( retval != PAPI_OK ) {
60  test_fail( __FILE__, __LINE__, "PAPI multiplex init fail\n", retval );
61  }
62 
63  retval = PAPI_create_eventset( &EventSet );
64  if ( retval != PAPI_OK ) {
65  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
66  }
67 
68  /* In Component PAPI, EventSets must be assigned a component index
69  before you can fiddle with their internals.
70  0 is always the cpu component */
71  retval = PAPI_assign_eventset_component( EventSet, 0 );
72  if ( retval != PAPI_OK ) {
73  test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
74  retval );
75  }
76 
77  retval = PAPI_set_multiplex( EventSet );
78  if ( retval == PAPI_ENOSUPP) {
79  test_skip(__FILE__, __LINE__, "Multiplex not supported", 1);
80  } else if ( retval != PAPI_OK ) {
81  test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );
82  }
83 
84  retval = PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) );
85  if (retval != PAPI_OK ) {
86  if ( retval == PAPI_ECMP )
87  test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
88  else
89  test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
90  }
91 
92  retval = PAPI_add_event( EventSet, PAPI_TOT_INS );
93  if ( ( retval != PAPI_OK ) && ( retval != PAPI_ECNFLCT ) ) {
94  if (!TESTS_QUIET) printf("Trouble adding PAPI_TOT_INS\n");
95  test_skip( __FILE__, __LINE__, "PAPI_add_event", retval );
96  }
97 
98  if ( !TESTS_QUIET ) {
99  printf( "Added %s\n", "PAPI_TOT_INS" );
100  }
101 
102  retval = PAPI_add_event( EventSet, PAPI_TOT_CYC );
103  if ( ( retval != PAPI_OK ) && ( retval != PAPI_ECNFLCT ) )
104  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
105  if ( !TESTS_QUIET ) {
106  printf( "Added %s\n", "PAPI_TOT_CYC" );
107  }
108 
109  values = ( long long * ) malloc( MAX_TO_ADD * sizeof ( long long ) );
110  if ( values == NULL )
111  test_fail( __FILE__, __LINE__, "malloc", 0 );
112 
113  for ( i = 0; i < PAPI_MAX_PRESET_EVENTS; i++ ) {
114  retval = PAPI_get_event_info( i | PAPI_PRESET_MASK, &pset );
115  if ( retval != PAPI_OK )
116  test_fail( __FILE__, __LINE__, "PAPI_get_event_info", retval );
117 
118  if ( pset.count ) {
119  if (!TESTS_QUIET) printf( "Adding %s\n", pset.symbol );
120 
121  retval = PAPI_add_event( EventSet, ( int ) pset.event_code );
122  if ( ( retval != PAPI_OK ) && ( retval != PAPI_ECNFLCT ) )
123  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
124 
125  if ( retval == PAPI_OK ) {
126  if (!TESTS_QUIET) printf( "Added %s\n", pset.symbol );
127  } else {
128  if (!TESTS_QUIET) printf( "Could not add %s\n", pset.symbol );
129  }
130 
131  do_stuff( );
132 
133  if ( retval == PAPI_OK ) {
134  retval = PAPI_start( EventSet );
135  if ( retval != PAPI_OK )
136  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
137 
138  do_stuff( );
139 
140  retval = PAPI_stop( EventSet, values );
141  if ( retval != PAPI_OK )
142  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
143 
144  if ( values[j] ) {
145  if ( ++j >= MAX_TO_ADD )
146  break;
147  } else {
148  retval =
149  PAPI_remove_event( EventSet, ( int ) pset.event_code );
150  if ( retval == PAPI_OK )
151  if (!TESTS_QUIET) printf( "Removed %s\n", pset.symbol );
152  /* This added because the test */
153  /* can take a long time if mplexing */
154  /* is broken and all values are 0 */
155  skipped_counters++;
156  if (skipped_counters>MAX_TO_ADD) break;
157 
158  }
159  }
160  }
161  }
162 
163  retval = PAPI_start( EventSet );
164  if ( retval != PAPI_OK )
165  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
166 
167  do_stuff( );
168 
169  retval = PAPI_stop( EventSet, values );
170  if ( retval != PAPI_OK )
171  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
172 
173  if (!TESTS_QUIET) {
174  test_print_event_header( "multiplex3_pthreads:\n", EventSet );
175  }
176  allvalid = 0;
177  for ( i = 0; i < MAX_TO_ADD; i++ ) {
178  if (!TESTS_QUIET) printf( ONENUM, values[i] );
179  if ( values[i] != 0 )
180  allvalid++;
181  }
182  if (!TESTS_QUIET) printf( "\n" );
183  if ( !allvalid )
184  test_fail( __FILE__, __LINE__, "all counter registered no counts", 1 );
185 
186  retval = PAPI_cleanup_eventset( EventSet ); /* JT */
187  if ( retval != PAPI_OK )
188  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
189 
190  retval = PAPI_destroy_eventset( &EventSet );
191  if ( retval != PAPI_OK )
192  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
193 
194  free( values );
195  PAPI_shutdown( );
196 }
197 
198 int
199 main( int argc, char **argv )
200 {
201  int i, rc, retval;
203  pthread_attr_t attr;
204  int quiet;
205 
206  /* Set TESTS_QUIET variable */
207  quiet = tests_quiet( argc, argv );
208 
209  if (!quiet) {
210  printf( "%s: Using %d threads\n\n", argv[0], NUM_THREADS );
211  printf( "Does non-threaded multiplexing work "
212  "with extraneous threads present?\n" );
213  }
214 
215  /* Create a bunch of unused pthreads, to simulate threads created
216  * by the system that the user doesn't know about.
217  */
218  pthread_attr_init( &attr );
219 #ifdef PTHREAD_CREATE_UNDETACHED
220  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
221 #endif
222 #ifdef PTHREAD_SCOPE_SYSTEM
223  retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
224  if ( retval != 0 )
225  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
226 #endif
227 
228 #ifdef PPC64
229  sigset_t sigprof;
230  sigemptyset( &sigprof );
231  sigaddset( &sigprof, SIGPROF );
232  retval = sigprocmask( SIG_BLOCK, &sigprof, NULL );
233  if ( retval != 0 )
234  test_fail( __FILE__, __LINE__, "sigprocmask SIG_BLOCK", retval );
235 #endif
236 
237  for ( i = 0; i < NUM_THREADS; i++ ) {
238  rc = pthread_create( &id[i], &attr, thread_fn, NULL );
239  if ( rc )
240  test_fail( __FILE__, __LINE__, "pthread_create", rc );
241  }
242  pthread_attr_destroy( &attr );
243 
244 #ifdef PPC64
245  retval = sigprocmask( SIG_UNBLOCK, &sigprof, NULL );
246  if ( retval != 0 )
247  test_fail( __FILE__, __LINE__, "sigprocmask SIG_UNBLOCK", retval );
248 #endif
249 
250  mainloop( NUM_ITERS );
251 
252  test_pass( __FILE__ );
253 
254  return 0;
255 
256 }
unsigned int count
Definition: papi.h:987
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
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
#define NUM_ITERS
Definition: multiplex.c:19
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1758
#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
#define ONENUM
Definition: papi_test.h:87
#define PAPI_MAX_PRESET_EVENTS
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:966
#define PAPI_TOT_INS
int EventSet
return PAPI_OK
Definition: linux-nvml.c:497
#define PAPI_PRESET_MASK
void
Definition: iozone.c:18627
int int argc
Definition: iozone.c:1609
int dummy
Definition: iozone.c:19741
Return codes and api definitions.
int rc
Definition: iozone.c:20281
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
char ** argv
Definition: iozone.c:1610
void * thread_fn(void *dummy)
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
void PAPI_shutdown(void)
Definition: papi.c:4461
int quiet
Definition: rapl_overflow.c:18
#define PAPI_TOT_CYC
free(dummyfile[xx])
#define PAPI_ECNFLCT
Definition: papi.h:261
#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
int PAPI_multiplex_init(void)
Definition: papi.c:2982
printf("\tTry: -i 0 -i 1 \n\n")
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
int PAPI_set_multiplex(int EventSet)
Definition: papi.c:3333
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
int PAPI_start(int EventSet)
Definition: papi.c:2096
#define MAX_TO_ADD
long j
Definition: iozone.c:19135
unsigned int event_code
Definition: papi.h:964
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
int main(int argc, char **argv)
List all appio events codes and names.
pthread_attr_t attr
Definition: iozone.c:18466
static void mainloop(int arg)