PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ctests/overflow_pthreads.c
Go to the documentation of this file.
1 /* This file performs the following test: overflow dispatch with pthreads
2 
3  - This tests the dispatch of overflow calls from PAPI. These are counted
4  in the default counting domain and default granularity, depending on
5  the platform. Usually this is the user domain (PAPI_DOM_USER) and
6  thread context (PAPI_GRN_THR).
7 
8  The Eventset contains:
9  + PAPI_FP_INS (overflow monitor)
10  + PAPI_TOT_CYC
11 
12  - Set up overflow
13  - Start eventset 1
14  - Do flops
15  - Stop eventset 1
16 */
17 
18 #include <pthread.h>
19 #include "papi_test.h"
20 
21 static const PAPI_hw_info_t *hw_info = NULL;
22 static int total[NUM_THREADS];
23 static int expected[NUM_THREADS];
25 
26 void
27 handler( int EventSet, void *address, long long overflow_vector, void *context )
28 {
29 #if 0
30  printf( "handler(%d,%#lx,%llx) Overflow %d in thread %lx\n",
31  EventSet, ( unsigned long ) address, overflow_vector,
32  total[EventSet], PAPI_thread_id( ) );
33  printf( "%lx vs %lx\n", myid[EventSet], PAPI_thread_id( ) );
34 #else /* eliminate unused parameter warning message */
35  ( void ) address;
36  ( void ) overflow_vector;
37  ( void ) context;
38 #endif
39  total[EventSet]++;
40 }
41 
42 long long mythreshold=0;
43 
44 void *
45 Thread( void *arg )
46 {
47  int retval, num_tests = 1;
48  int EventSet1 = PAPI_NULL;
49  int mask1, papi_event;
50  int num_events1;
51  long long **values;
52  long long elapsed_us, elapsed_cyc;
54 
55  retval = PAPI_register_thread( );
56  if ( retval != PAPI_OK )
57  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
58 
59  /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
60  PAPI_TOT_INS, depends on the availability of the event on the
61  platform */
62  EventSet1 =
63  add_two_nonderived_events( &num_events1, &papi_event, &mask1 );
64 
65  if (EventSet1 < 0) return NULL;
66 
67  /* Wait, we're indexing a per-thread array with the EventSet number? */
68  /* does that make any sense at all???? -- vmw */
69  expected[EventSet1] = *( int * ) arg / mythreshold;
71 
72  values = allocate_test_space( num_tests, num_events1 );
73 
74  elapsed_us = PAPI_get_real_usec( );
75 
76  elapsed_cyc = PAPI_get_real_cyc( );
77 
78  if ((retval = PAPI_overflow( EventSet1, papi_event,
79  mythreshold, 0, handler ) ) != PAPI_OK ) {
80  test_fail( __FILE__, __LINE__, "PAPI_overflow", retval );
81  }
82 
83  /* start_timer(1); */
84  if ( ( retval = PAPI_start( EventSet1 ) ) != PAPI_OK )
85  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
86 
87  do_stuff( );
88 
89  if ( ( retval = PAPI_stop( EventSet1, values[0] ) ) != PAPI_OK )
90  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
91 
92  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
93 
94  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
95 
96  if ( ( retval =
97  PAPI_overflow( EventSet1, papi_event, 0, 0, NULL ) ) != PAPI_OK )
98  test_fail( __FILE__, __LINE__, "PAPI_overflow", retval );
99 
100  remove_test_events( &EventSet1, mask1 );
101 
102  if ( ( retval =
103  PAPI_event_code_to_name( papi_event, event_name ) ) != PAPI_OK )
104  test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
105 
106  if ( !TESTS_QUIET ) {
107  printf( "Thread %#x %s : \t%lld\n", ( int ) pthread_self( ),
108  event_name, ( values[0] )[0] );
109  printf( "Thread %#x PAPI_TOT_CYC: \t%lld\n", ( int ) pthread_self( ),
110  ( values[0] )[1] );
111  printf( "Thread %#x Real usec : \t%lld\n", ( int ) pthread_self( ),
112  elapsed_us );
113  printf( "Thread %#x Real cycles : \t%lld\n", ( int ) pthread_self( ),
114  elapsed_cyc );
115  }
116  free_test_space( values, num_tests );
117  retval = PAPI_unregister_thread( );
118  if ( retval != PAPI_OK )
119  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
120  return ( NULL );
121 }
122 
123 int
124 main( int argc, char **argv )
125 {
127  int flops[NUM_THREADS];
128  int i, rc, retval;
129  pthread_attr_t attr;
130  float ratio;
131 
132  memset( total, 0x0, NUM_THREADS * sizeof ( *total ) );
133  memset( expected, 0x0, NUM_THREADS * sizeof ( *expected ) );
134  memset( myid, 0x0, NUM_THREADS * sizeof ( *myid ) );
135  tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */
136 
137  if ( ( retval =
139  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
140 
141  hw_info = PAPI_get_hardware_info( );
142  if ( hw_info == NULL )
143  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
144 
145  if ( ( retval =
146  PAPI_thread_init( ( unsigned
147  long ( * )( void ) ) ( pthread_self ) ) ) !=
148  PAPI_OK ) {
149  if ( retval == PAPI_ECMP )
150  test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
151  else
152  test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
153  }
154 #if defined(linux)
155  mythreshold = hw_info->cpu_max_mhz * 10000 * 2;
156 #else
157  mythreshold = THRESHOLD * 2;
158 #endif
159 
160  pthread_attr_init( &attr );
161 #ifdef PTHREAD_CREATE_UNDETACHED
162  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
163 #endif
164 #ifdef PTHREAD_SCOPE_SYSTEM
165  retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
166  if ( retval != 0 )
167  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
168 #endif
169 
170  for ( i = 0; i < NUM_THREADS; i++ ) {
171  flops[i] = NUM_FLOPS * ( i + 1 );
172  rc = pthread_create( &id[i], &attr, Thread, ( void * ) &flops[i] );
173  if ( rc )
174  test_fail( __FILE__, __LINE__, "pthread_create", PAPI_ESYS );
175  }
176  for ( i = 0; i < NUM_THREADS; i++ )
177  pthread_join( id[i], NULL );
178 
179  pthread_attr_destroy( &attr );
180 
181  {
182  long long t = 0, r = 0;
183  for ( i = 0; i < NUM_THREADS; i++ ) {
184  t += ( NUM_FLOPS * ( i + 1 ) ) / mythreshold;
185  r += total[i];
186  }
187  printf( "Expected total overflows: %lld\n", t );
188  printf( "Received total overflows: %lld\n", r );
189  }
190 
191 /* ratio = (float)total[0] / (float)expected[0]; */
192 /* printf("Ratio of total to expected: %f\n",ratio); */
193  ratio = 1.0;
194  for ( i = 0; i < NUM_THREADS; i++ ) {
195  printf( "Overflows thread %d: %d, expected %d\n",
196  i, total[i], ( int ) ( ratio * ( float ) expected[i] ) );
197  }
198 
199  for ( i = 0; i < NUM_THREADS; i++ ) {
200  if ( total[i] < ( int ) ( ( ratio * ( float ) expected[i] ) / 2.0 ) )
201  test_fail( __FILE__, __LINE__, "not enough overflows", PAPI_EMISC );
202  }
203 
204  test_pass( __FILE__, NULL, 0 );
205  pthread_exit( NULL );
206  exit( 1 );
207 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
memset(eventId, 0, size)
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
#define NUM_FLOPS
int mask1
Definition: zero_fork.c:41
#define PAPI_EMISC
Definition: fpapi.h:119
static int expected[NUM_THREADS]
Hardware info structure.
Definition: papi.h:775
#define PAPI_NULL
Definition: fpapi.h:13
unsigned long PAPI_thread_id(void)
Definition: papi.c:157
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
long long myid
Definition: iozone.c:1335
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
int num_events1
Definition: zero_fork.c:42
void do_stuff(void)
Definition: do_loops.c:249
int PAPI_register_thread(void)
Definition: papi.c:203
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
int PAPI_overflow(int EventSet, int EventCode, int threshold, int flags, PAPI_overflow_handler_t handler)
Definition: papi.c:4668
void
Definition: iozone.c:18627
#define printf
Definition: papi_test.h:125
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
t
Definition: iozone.c:23562
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:118
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
long long elapsed_cyc
Definition: zero_fork.c:43
#define PAPI_ECMP
Definition: fpapi.h:109
#define NUM_THREADS
Definition: test_utils.h:8
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
void free_test_space(long long **values, int num_tests)
Definition: test_utils.c:131
void * Thread(void *arg)
void handler(int EventSet, void *address, long long overflow_vector, void *context)
Definition: rapl_overflow.c:16
pthread_t
Definition: iozone.c:18557
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:924
#define PAPI_ESYS
Definition: fpapi.h:108
int num_tests
Definition: zero_fork.c:46
int EventSet
long long PAPI_get_real_usec(void)
Definition: papi.c:6138
int PAPI_unregister_thread(void)
Definition: papi.c:239
static int total
Definition: rapl_overflow.c:5
int cpu_max_mhz
Definition: papi.h:791
int rc
Definition: iozone.c:20281
int add_two_nonderived_events(int *num_events, int *papi_event, int *mask)
Definition: test_utils.c:723
long long PAPI_get_real_cyc(void)
Definition: papi.c:6091
long long mythreshold
int PAPI_start(int EventSet)
Definition: papi.c:2026
#define THRESHOLD
Definition: earprofile.c:31
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6059
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long elapsed_us
Definition: zero_fork.c:43
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:107
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:23
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int remove_test_events(int *EventSet, int mask)
Definition: test_utils.c:277
pthread_attr_t attr
Definition: iozone.c:18466
int EventSet1
Definition: zero_fork.c:40