PAPI 7.1.0.0
Loading...
Searching...
No Matches
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 <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <pthread.h>
22
23#include "papi.h"
24#include "do_loops.h"
25#include "papi_test.h"
26
27static const PAPI_hw_info_t *hw_info = NULL;
28static int total[NUM_THREADS];
31
32static void
33handler( int EventSet, void *address, long long overflow_vector, void *context )
34{
35#if 0
36 printf( "handler(%d,%#lx,%llx) Overflow %d in thread %lx\n",
37 EventSet, ( unsigned long ) address, overflow_vector,
39 printf( "%lx vs %lx\n", myid[EventSet], PAPI_thread_id( ) );
40#else /* eliminate unused parameter warning message */
41 ( void ) address;
42 ( void ) overflow_vector;
43 ( void ) context;
44#endif
45 total[EventSet]++;
46}
47
48static long long mythreshold=0;
49
50static void *
51Thread( void *arg )
52{
53 int retval, num_tests = 1;
54 int EventSet1 = PAPI_NULL;
55 int mask1, papi_event;
56 int num_events1;
57 long long **values;
58 long long elapsed_us, elapsed_cyc;
60
62 if ( retval != PAPI_OK )
63 test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
64
65 /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
66 PAPI_TOT_INS, depends on the availability of the event on the
67 platform */
68 EventSet1 =
70
71 if (EventSet1 < 0) return NULL;
72
73 /* Wait, we're indexing a per-thread array with the EventSet number? */
74 /* does that make any sense at all???? -- vmw */
75 expected[EventSet1] = *( int * ) arg / mythreshold;
77
79
81
83
84 if ((retval = PAPI_overflow( EventSet1, papi_event,
85 mythreshold, 0, handler ) ) != PAPI_OK ) {
86 test_fail( __FILE__, __LINE__, "PAPI_overflow", retval );
87 }
88
89 /* start_timer(1); */
90 if ( ( retval = PAPI_start( EventSet1 ) ) != PAPI_OK )
91 test_fail( __FILE__, __LINE__, "PAPI_start", retval );
92
93 do_stuff( );
94
95 if ( ( retval = PAPI_stop( EventSet1, values[0] ) ) != PAPI_OK )
96 test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
97
99
101
102 retval = PAPI_overflow( EventSet1, papi_event, 0, 0, NULL );
103 if (retval != PAPI_OK ) {
104 test_fail( __FILE__, __LINE__, "PAPI_overflow", retval );
105 }
106
108
110 if (retval != PAPI_OK ) {
111 test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
112 }
113
114 if ( !TESTS_QUIET ) {
115 printf( "Thread %#x %s : \t%lld\n", ( int ) pthread_self( ),
116 event_name, ( values[0] )[0] );
117 printf( "Thread %#x PAPI_TOT_CYC: \t%lld\n", ( int ) pthread_self( ),
118 ( values[0] )[1] );
119 printf( "Thread %#x Real usec : \t%lld\n", ( int ) pthread_self( ),
120 elapsed_us );
121 printf( "Thread %#x Real cycles : \t%lld\n", ( int ) pthread_self( ),
122 elapsed_cyc );
123 }
126 if ( retval != PAPI_OK )
127 test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
128 return ( NULL );
129}
130
131int
132main( int argc, char **argv )
133{
135 int flops[NUM_THREADS];
136 int i, rc, retval;
137 pthread_attr_t attr;
138 float ratio;
139 int quiet;
140
141 /* Set TESTS_QUIET variable */
142 quiet = tests_quiet( argc, argv );
143
144 memset( total, 0x0, NUM_THREADS * sizeof ( *total ) );
145 memset( expected, 0x0, NUM_THREADS * sizeof ( *expected ) );
146 memset( myid, 0x0, NUM_THREADS * sizeof ( *myid ) );
147
149 if (retval != PAPI_VER_CURRENT ) {
150 test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
151 }
152
154 if ( hw_info == NULL ) {
155 test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
156 }
157
158 retval = PAPI_thread_init( ( unsigned long ( * )( void ) )
159 ( pthread_self ) );
160 if (retval != PAPI_OK ) {
161 if ( retval == PAPI_ECMP )
162 test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
163 else
164 test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
165 }
166#if defined(linux)
167 mythreshold = ((long long)hw_info->cpu_max_mhz) * 10000 * 2;
168#else
170#endif
171
172 pthread_attr_init( &attr );
173#ifdef PTHREAD_CREATE_UNDETACHED
174 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
175#endif
176#ifdef PTHREAD_SCOPE_SYSTEM
177 retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
178 if ( retval != 0 )
179 test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
180#endif
181
182 for ( i = 0; i < NUM_THREADS; i++ ) {
183 flops[i] = NUM_FLOPS * ( i + 1 );
184 rc = pthread_create( &id[i], &attr, Thread, ( void * ) &flops[i] );
185 if ( rc )
186 test_fail( __FILE__, __LINE__, "pthread_create", PAPI_ESYS );
187 }
188 for ( i = 0; i < NUM_THREADS; i++ )
189 pthread_join( id[i], NULL );
190
191 pthread_attr_destroy( &attr );
192
193 {
194 long long t = 0, r = 0;
195 for ( i = 0; i < NUM_THREADS; i++ ) {
196 t += ( NUM_FLOPS * ( i + 1 ) ) / mythreshold;
197 r += total[i];
198 }
199 if (!quiet) {
200 printf( "Expected total overflows: %lld\n", t );
201 printf( "Received total overflows: %lld\n", r );
202 }
203 }
204// FIXME: are we actually testing this properly?
205
206/* ratio = (float)total[0] / (float)expected[0]; */
207/* printf("Ratio of total to expected: %f\n",ratio); */
208 ratio = 1.0;
209 for ( i = 0; i < NUM_THREADS; i++ ) {
210 if (!quiet) printf( "Overflows thread %d: %d, expected %d\n",
211 i, total[i], ( int ) ( ratio * ( float ) expected[i] ) );
212 }
213
214 for ( i = 0; i < NUM_THREADS; i++ ) {
215 if ( total[i] < ( int ) ( ( ratio * ( float ) expected[i] ) / 2.0 ) )
216 test_fail( __FILE__, __LINE__, "not enough overflows", PAPI_EMISC );
217 }
218
219 test_pass( __FILE__ );
220
221 pthread_exit( NULL );
222
223 return 0;
224
225}
int i
Convert a numeric hardware event code to a name.
get information about the system hardware
get real time counter value in clock cycles Returns the total real time passed since some arbitrary s...
get real time counter value in microseconds
initialize the PAPI library.
Set up an event set to begin registering overflows.
Notify PAPI that a thread has 'appeared'.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Get the thread identifier of the current thread.
Initialize thread support in the PAPI library.
Notify PAPI that a thread has 'disappeared'.
static int total[NUM_THREADS]
static const PAPI_hw_info_t * hw_info
static int expected[NUM_THREADS]
static void handler(int EventSet, void *address, long long overflow_vector, void *context)
static void * Thread(void *arg)
static long long mythreshold
static pthread_t myid[NUM_THREADS]
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
void do_stuff(void)
Definition: do_loops.c:256
#define THRESHOLD
Definition: earprofile.c:37
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
#define PAPI_EMISC
Definition: f90papi.h:122
#define PAPI_ESYS
Definition: f90papi.h:136
#define PAPI_ECMP
Definition: f90papi.h:214
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
int TESTS_QUIET
Definition: test_utils.c:18
Return codes and api definitions.
unsigned long int pthread_t
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
int add_two_nonderived_events(int *num_events, int *papi_event, int *mask)
Definition: test_utils.c:671
void free_test_space(long long **values, int num_tests)
Definition: test_utils.c:70
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:46
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 remove_test_events(int *EventSet, int mask)
Definition: test_utils.c:201
int main()
Definition: pernode.c:20
rc
Definition: pscanf.h:23
#define NUM_THREADS
Definition: pthread_hl.c:9
int quiet
Definition: rapl_overflow.c:19
long long int long long
Definition: sde_internal.h:85
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
Hardware info structure.
Definition: papi.h:774
int cpu_max_mhz
Definition: papi.h:790
int EventSet1
Definition: zero_fork.c:47
int num_events1
Definition: zero_fork.c:49
int mask1
Definition: zero_fork.c:48
long long elapsed_cyc
Definition: zero_fork.c:50
long long elapsed_us
Definition: zero_fork.c:50
int num_tests
Definition: zero_fork.c:53
int retval
Definition: zero_fork.c:53