PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
krentel_pthreads.c
Go to the documentation of this file.
1 /*
2  * Test PAPI with multiple threads.
3  */
4 
5 #define MAX_THREADS 256
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <pthread.h>
10 #include <sys/time.h>
11 
12 #include "papi.h"
13 #include "papi_test.h"
14 
15 #define EVENT PAPI_TOT_CYC
16 
17 static int program_time = 5;
18 static int threshold = 20000000;
19 static int num_threads = 3;
20 
21 static long count[MAX_THREADS];
22 static long iter[MAX_THREADS];
23 static struct timeval last[MAX_THREADS];
24 
25 static pthread_key_t key;
26 
27 static struct timeval start;
28 
29 static void
30 my_handler( int EventSet, void *pc, long long ovec, void *context )
31 {
32  ( void ) EventSet;
33  ( void ) pc;
34  ( void ) ovec;
35  ( void ) context;
36 
37  long num = ( long ) pthread_getspecific( key );
38 
39  if ( num < 0 || num > num_threads )
40  test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
41  count[num]++;
42 }
43 
44 static void
45 print_rate( long num )
46 {
47  struct timeval now;
48  long st_secs;
49  double last_secs;
50 
51  gettimeofday( &now, NULL );
52  st_secs = now.tv_sec - start.tv_sec;
53  last_secs = ( double ) ( now.tv_sec - last[num].tv_sec )
54  + ( ( double ) ( now.tv_usec - last[num].tv_usec ) ) / 1000000.0;
55  if ( last_secs <= 0.001 )
56  last_secs = 0.001;
57 
58  if (!TESTS_QUIET) {
59  printf( "[%ld] time = %ld, count = %ld, iter = %ld, "
60  "rate = %.1f/Kiter\n",
61  num, st_secs, count[num], iter[num],
62  ( 1000.0 * ( double ) count[num] ) / ( double ) iter[num] );
63  }
64 
65  count[num] = 0;
66  iter[num] = 0;
67  last[num] = now;
68 }
69 
70 static void
71 do_cycles( long num, int len )
72 {
73  struct timeval start, now;
74  double x, sum;
75 
76  gettimeofday( &start, NULL );
77 
78  for ( ;; ) {
79  sum = 1.0;
80  for ( x = 1.0; x < 250000.0; x += 1.0 )
81  sum += x;
82  if ( sum < 0.0 )
83  printf( "==>> SUM IS NEGATIVE !! <<==\n" );
84 
85  iter[num]++;
86 
87  gettimeofday( &now, NULL );
88  if ( now.tv_sec >= start.tv_sec + len )
89  break;
90  }
91 }
92 
93 static void *
94 my_thread( void *v )
95 {
96  long num = ( long ) v;
97  int n;
98  int EventSet = PAPI_NULL;
99  long long value;
100 
101  int retval;
102 
103  retval = PAPI_register_thread( );
104  if ( retval != PAPI_OK ) {
105  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
106  }
107  pthread_setspecific( key, v );
108 
109  count[num] = 0;
110  iter[num] = 0;
111  last[num] = start;
112 
113  retval = PAPI_create_eventset( &EventSet );
114  if ( retval != PAPI_OK ) {
115  test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", retval );
116  }
117 
118  retval = PAPI_add_event( EventSet, EVENT );
119  if (retval != PAPI_OK ) {
120  if (!TESTS_QUIET) printf("Trouble adding event\n");
121  test_fail( __FILE__, __LINE__, "PAPI_add_event failed", retval );
122  }
123 
124  if ( PAPI_overflow( EventSet, EVENT, threshold, 0, my_handler ) != PAPI_OK )
125  test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );
126 
127  if ( PAPI_start( EventSet ) != PAPI_OK )
128  test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
129 
130  if (!TESTS_QUIET) printf( "launched timer in thread %ld\n", num );
131 
132  for ( n = 1; n <= program_time; n++ ) {
133  do_cycles( num, 1 );
134  print_rate( num );
135  }
136 
137  PAPI_stop( EventSet, &value );
138 
139  retval = PAPI_overflow( EventSet, EVENT, 0, 0, my_handler);
140  if ( retval != PAPI_OK )
141  test_fail( __FILE__, __LINE__, "PAPI_overflow failed to reset the overflow handler", retval );
142 
143  if ( PAPI_remove_event( EventSet, EVENT ) != PAPI_OK )
144  test_fail( __FILE__, __LINE__, "PAPI_remove_event", 1 );
145 
146  if ( PAPI_destroy_eventset( &EventSet ) != PAPI_OK )
147  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", 1 );
148 
149  if ( PAPI_unregister_thread( ) != PAPI_OK )
150  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", 1 );
151 
152  return ( NULL );
153 }
154 
155 int
156 main( int argc, char **argv )
157 {
158  pthread_t *td = NULL;
159  long n;
160  int quiet,retval;
161 
162  /* Set TESTS_QUIET variable */
163  quiet=tests_quiet( argc, argv );
164 
165  if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
166  program_time = 6;
167  if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
168  threshold = 20000000;
169  if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
170  num_threads = 3;
171 
172  td = malloc((num_threads+1) * sizeof(pthread_t));
173  if (!td) {
174  test_fail( __FILE__, __LINE__, "td malloc failed", 1 );
175  }
176 
177  if (!quiet) {
178  printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
180  }
181 
183  test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
184 
185  /* Test to be sure we can add events */
186  retval = PAPI_query_event( EVENT );
187  if (retval!=PAPI_OK) {
188  if (!quiet) printf("Trouble finding event\n");
189  test_skip(__FILE__,__LINE__,"Event not available",1);
190  }
191 
192  if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
193  PAPI_OK )
194  test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
195 
196  if ( pthread_key_create( &key, NULL ) != 0 )
197  test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
198 
199  gettimeofday( &start, NULL );
200 
201  for ( n = 1; n <= num_threads; n++ ) {
202  if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
203  test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
204  }
205 
206  my_thread( ( void * ) 0 );
207 
208  /* wait for all the threads */
209  for ( n = 1; n <= num_threads; n++ ) {
210  if ( pthread_join( td[n], NULL))
211  test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
212  }
213 
214  free(td);
215 
216  if (!quiet) printf( "done\n" );
217 
218  test_pass( __FILE__ );
219 
220  return 0;
221 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
start
Definition: iozone.c:22736
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1758
#define PAPI_NULL
Definition: papi.h:292
static void print_rate(long num)
int EventSet
int PAPI_register_thread(void)
Definition: papi.c:208
return PAPI_OK
Definition: linux-nvml.c:497
int count
Definition: iozone.c:22422
int PAPI_overflow(int EventSet, int EventCode, int threshold, int flags, PAPI_overflow_handler_t handler)
Definition: papi.c:4788
void
Definition: iozone.c:18627
static void * my_thread(void *v)
void double value
Definition: iozone.c:18781
static double
Definition: fileop.c:1281
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
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
static long iter[MAX_THREADS]
static int program_time
#define EVENT
int quiet
Definition: rapl_overflow.c:18
free(dummyfile[xx])
static struct timeval last[MAX_THREADS]
static int num_threads
void int num
Definition: iozone.c:22151
static pthread_key_t key
#define MAX_THREADS
long long
Definition: iozone.c:19827
static void my_handler(int EventSet, void *pc, long long ovec, void *context)
pthread_t
Definition: iozone.c:18557
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
static int threshold
static void do_cycles(int program_time)
Definition: fork_overflow.c:95
int PAPI_query_event(int EventCode)
Definition: papi.c:684
int gettimeofday(void *ptr1, void *ptr2)
sscanf(mnc->m_child_port,"%d",&mc.m_child_port)
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
int x
Definition: fileop.c:78
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
int PAPI_start(int EventSet)
Definition: papi.c:2096
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.