|
PAPI
5.0.1.0
|
00001 /* 00002 * File: overflow3_pthreads.c 00003 * CVS: $Id$ 00004 * Author: Philip Mucci 00005 * mucci@cs.utk.edu 00006 * Mods: 00007 * 00008 */ 00009 00010 /* This file tests the overflow functionality when there are 00011 * threads in which the application isn't calling PAPI (and only 00012 * one thread that is calling PAPI.) 00013 */ 00014 00015 #include <pthread.h> 00016 #include "papi_test.h" 00017 00018 int total = 0; 00019 00020 void * 00021 thread_fn( void *dummy ) 00022 { 00023 ( void ) dummy; 00024 while ( 1 ) { 00025 do_stuff( ); 00026 } 00027 return ( NULL ); 00028 } 00029 00030 void 00031 handler( int EventSet, void *address, long long overflow_vector, void *context ) 00032 { 00033 ( void ) overflow_vector; 00034 ( void ) context; 00035 if ( !TESTS_QUIET ) { 00036 fprintf( stderr, "handler(%d ) Overflow at %p, thread 0x%lux!\n", 00037 EventSet, address, PAPI_thread_id( ) ); 00038 } 00039 total++; 00040 } 00041 00042 void 00043 mainloop( int arg ) 00044 { 00045 int retval, num_tests = 1; 00046 int EventSet1 = PAPI_NULL; 00047 int mask1 = 0x0; 00048 int num_events1; 00049 long long **values; 00050 int PAPI_event; 00051 char event_name[PAPI_MAX_STR_LEN]; 00052 00053 ( void ) arg; 00054 00055 if ( ( retval = 00056 PAPI_library_init( PAPI_VER_CURRENT ) ) != PAPI_VER_CURRENT ) 00057 test_fail( __FILE__, __LINE__, "PAPI_library_init", retval ); 00058 00059 00060 /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or 00061 PAPI_TOT_INS, depending on the availability of the event on the 00062 platform */ 00063 EventSet1 = 00064 add_two_nonderived_events( &num_events1, &PAPI_event, &mask1 ); 00065 00066 values = allocate_test_space( num_tests, num_events1 ); 00067 00068 if ( ( retval = 00069 PAPI_overflow( EventSet1, PAPI_event, THRESHOLD, 0, 00070 handler ) ) != PAPI_OK ) 00071 test_fail( __FILE__, __LINE__, "PAPI_overflow", retval ); 00072 00073 do_stuff( ); 00074 00075 if ( ( retval = PAPI_start( EventSet1 ) ) != PAPI_OK ) 00076 test_fail( __FILE__, __LINE__, "PAPI_start", retval ); 00077 00078 do_stuff( ); 00079 00080 if ( ( retval = PAPI_stop( EventSet1, values[0] ) ) != PAPI_OK ) 00081 test_fail( __FILE__, __LINE__, "PAPI_stop", retval ); 00082 00083 /* clear the papi_overflow event */ 00084 if ( ( retval = 00085 PAPI_overflow( EventSet1, PAPI_event, 0, 0, NULL ) ) != PAPI_OK ) 00086 test_fail( __FILE__, __LINE__, "PAPI_overflow", retval ); 00087 00088 if ( ( retval = 00089 PAPI_event_code_to_name( PAPI_event, event_name ) ) != PAPI_OK ) 00090 test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval ); 00091 00092 if ( !TESTS_QUIET ) { 00093 printf( "Thread 0x%x %s : \t%lld\n", ( int ) pthread_self( ), 00094 event_name, ( values[0] )[0] ); 00095 printf( "Thread 0x%x PAPI_TOT_CYC: \t%lld\n", ( int ) pthread_self( ), 00096 ( values[0] )[1] ); 00097 } 00098 00099 retval = PAPI_cleanup_eventset( EventSet1 ); 00100 if ( retval != PAPI_OK ) 00101 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval ); 00102 00103 retval = PAPI_destroy_eventset( &EventSet1 ); 00104 if ( retval != PAPI_OK ) 00105 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval ); 00106 00107 free_test_space( values, num_tests ); 00108 PAPI_shutdown( ); 00109 } 00110 00111 int 00112 main( int argc, char **argv ) 00113 { 00114 int i, rc, retval; 00115 pthread_t id[NUM_THREADS]; 00116 pthread_attr_t attr; 00117 00118 tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */ 00119 00120 printf( "%s: Using %d threads\n\n", argv[0], NUM_THREADS ); 00121 printf 00122 ( "Does non-threaded overflow work with extraneous threads present?\n" ); 00123 00124 pthread_attr_init( &attr ); 00125 #ifdef PTHREAD_CREATE_UNDETACHED 00126 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED ); 00127 #endif 00128 #ifdef PTHREAD_SCOPE_SYSTEM 00129 retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); 00130 if ( retval != 0 ) 00131 test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval ); 00132 #endif 00133 00134 for ( i = 0; i < NUM_THREADS; i++ ) { 00135 rc = pthread_create( &id[i], &attr, thread_fn, NULL ); 00136 if ( rc ) 00137 test_fail( __FILE__, __LINE__, "pthread_create", rc ); 00138 } 00139 pthread_attr_destroy( &attr ); 00140 00141 mainloop( NUM_ITERS ); 00142 00143 test_pass( __FILE__, NULL, 0 ); 00144 exit( 1 ); 00145 }