PAPI 7.1.0.0
Loading...
Searching...
No Matches
krentel_pthreads_race.c
Go to the documentation of this file.
1/*
2 * Test PAPI with multiple threads.
3 * This code is a modification of krentel_pthreads.c by William Cohen
4 * <wcohen@redhat.com>, on Sep 10 2019, to exercise and test for the race
5 * condition in papi_internal.c involving the formerly static variables
6 * papi_event_code and papi_event_code_changed. This code should be run with
7 * "valgrind --tool=helgrind" to show any data races. If run with:
8 * "valgrind --tool=helgrind --log-file=helgrind_out.txt"
9 * The output will be captured in helgrind_out.txt and can then be processed
10 * with the program filter_helgrind.c; see commentary at the top of that file.
11 */
12
13#define MAX_THREADS 256
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <pthread.h>
18#include <sys/time.h>
19
20#include "papi.h"
21#include "papi_test.h"
22
23#define EVENT PAPI_TOT_CYC
24
25static int program_time = 5;
26static int threshold = 20000000;
27static int num_threads = 3;
28
29static long count[MAX_THREADS];
30static long iter[MAX_THREADS];
31static struct timeval last[MAX_THREADS];
32
34
35static struct timeval start;
36
37static void
38my_handler( int EventSet, void *pc, long long ovec, void *context )
39{
40 ( void ) EventSet;
41 ( void ) pc;
42 ( void ) ovec;
43 ( void ) context;
44
45 long num = ( long ) pthread_getspecific( key );
46
47 if ( num < 0 || num > num_threads )
48 test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
49 count[num]++;
50}
51
52static void
53print_rate( long num )
54{
55 struct timeval now;
56 long st_secs;
57 double last_secs;
58
59 gettimeofday( &now, NULL );
60 st_secs = now.tv_sec - start.tv_sec;
61 last_secs = ( double ) ( now.tv_sec - last[num].tv_sec )
62 + ( ( double ) ( now.tv_usec - last[num].tv_usec ) ) / 1000000.0;
63 if ( last_secs <= 0.001 )
64 last_secs = 0.001;
65
66 if (!TESTS_QUIET) {
67 printf( "[%ld] time = %ld, count = %ld, iter = %ld, "
68 "rate = %.1f/Kiter\n",
69 num, st_secs, count[num], iter[num],
70 ( 1000.0 * ( double ) count[num] ) / ( double ) iter[num] );
71 }
72
73 count[num] = 0;
74 iter[num] = 0;
75 last[num] = now;
76}
77
78static void
79do_cycles( long num, int len )
80{
81 struct timeval start, now;
82 double x, sum;
83
84 gettimeofday( &start, NULL );
85
86 for ( ;; ) {
87 sum = 1.0;
88 for ( x = 1.0; x < 250000.0; x += 1.0 )
89 sum += x;
90 if ( sum < 0.0 )
91 printf( "==>> SUM IS NEGATIVE !! <<==\n" );
92
93 iter[num]++;
94
95 gettimeofday( &now, NULL );
96 if ( now.tv_sec >= start.tv_sec + len )
97 break;
98 }
99}
100
101static void *
102my_thread( void *v )
103{
104 long num = ( long ) v;
105 int n;
106 int EventSet = PAPI_NULL;
107 int event_code;
108 long long value;
109
110 int retval;
111
113 if ( retval != PAPI_OK ) {
114 test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
115 }
116 pthread_setspecific( key, v );
117
118 count[num] = 0;
119 iter[num] = 0;
120 last[num] = start;
121
123 if ( retval != PAPI_OK ) {
124 test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", retval );
125 }
126
127 retval = PAPI_event_name_to_code("PAPI_TOT_CYC", &event_code);
128 if (retval != PAPI_OK ) {
129 if (!TESTS_QUIET) printf("Trouble creating event name\n");
130 test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code failed", retval );
131 }
132
134 if (retval != PAPI_OK ) {
135 if (!TESTS_QUIET) printf("Trouble adding event\n");
136 test_fail( __FILE__, __LINE__, "PAPI_add_event failed", retval );
137 }
138
140 test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );
141
142 if ( PAPI_start( EventSet ) != PAPI_OK )
143 test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
144
145 if (!TESTS_QUIET) printf( "launched timer in thread %ld\n", num );
146
147 for ( n = 1; n <= program_time; n++ ) {
148 do_cycles( num, 1 );
149 print_rate( num );
150 }
151
152 PAPI_stop( EventSet, &value );
153
155 if ( retval != PAPI_OK )
156 test_fail( __FILE__, __LINE__, "PAPI_overflow failed to reset the overflow handler", retval );
157
159 test_fail( __FILE__, __LINE__, "PAPI_remove_event", 1 );
160
162 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", 1 );
163
164 if ( PAPI_unregister_thread( ) != PAPI_OK )
165 test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", 1 );
166
167 return ( NULL );
168}
169
170int
171main( int argc, char **argv )
172{
173 pthread_t *td = NULL;
174 long n;
175 int quiet,retval;
176
177 /* Set TESTS_QUIET variable */
178 quiet=tests_quiet( argc, argv );
179
180 if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
181 program_time = 6;
182 if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
183 threshold = 20000000;
184 if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
185 num_threads = 32;
186
187 td = malloc((num_threads+1) * sizeof(pthread_t));
188 if (!td) {
189 test_fail( __FILE__, __LINE__, "td malloc failed", 1 );
190 }
191
192 if (!quiet) {
193 printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
195 }
196
198 test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
199
200 /* Test to be sure we can add events */
202 if (retval!=PAPI_OK) {
203 if (!quiet) printf("Trouble finding event\n");
204 test_skip(__FILE__,__LINE__,"Event not available",1);
205 }
206
207 if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
208 PAPI_OK )
209 test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
210
211 if ( pthread_key_create( &key, NULL ) != 0 )
212 test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
213
214 gettimeofday( &start, NULL );
215
216 for ( n = 1; n <= num_threads; n++ ) {
217 if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
218 test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
219 }
220
221 my_thread( ( void * ) 0 );
222
223 /* wait for all the threads */
224 for ( n = 1; n <= num_threads; n++ ) {
225 if ( pthread_join( td[n], NULL))
226 test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
227 }
228
229 free(td);
230
231 if (!quiet) printf( "done\n" );
232
233 test_pass( __FILE__ );
234
235 return 0;
236}
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
Convert a name to a numeric hardware event code.
initialize the PAPI library.
Set up an event set to begin registering overflows.
Query if PAPI event exists.
removes a hardware event from a PAPI event set.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Initialize thread support in the PAPI library.
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
int PAPI_register_thread(void)
Definition: papi.c:750
int PAPI_unregister_thread(void)
Definition: papi.c:786
static int EventSet
Definition: init_fini.c:8
static int num_threads
static long iter[MAX_THREADS]
static void * my_thread(void *v)
static int threshold
#define EVENT
static long count[MAX_THREADS]
static int program_time
#define MAX_THREADS
static struct timeval last[MAX_THREADS]
static void print_rate(long num)
static pthread_key_t key
static struct timeval start
static void my_handler(int EventSet, void *pc, long long ovec, void *context)
static void do_cycles(long num, int len)
int TESTS_QUIET
Definition: test_utils.c:18
Return codes and api definitions.
unsigned long int pthread_t
unsigned int pthread_key_t
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
long long int long long
Definition: sde_internal.h:85
__time_t tv_sec
__suseconds_t tv_usec
volatile double x
int retval
Definition: zero_fork.c:53