PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
thrspecific.c
Go to the documentation of this file.
1 /* This file performs the following test: start, stop and timer
2 functionality for 2 slave pthreads */
3 
4 #include <pthread.h>
5 #include "papi_test.h"
6 
7 static int processing = 1;
8 
9 void *
10 Thread( void *arg )
11 {
12  int retval;
13  void *arg2;
14  retval = PAPI_register_thread( );
15  if ( retval != PAPI_OK )
16  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
17 
18  printf( "Thread %#x started, specific data is at %p\n",
19  ( int ) pthread_self( ), arg );
20 
21  retval = PAPI_set_thr_specific( PAPI_USR1_TLS, arg );
22  if ( retval != PAPI_OK )
23  test_fail( __FILE__, __LINE__, "PAPI_set_thr_specific", retval );
24 
25  retval = PAPI_get_thr_specific( PAPI_USR1_TLS, &arg2 );
26  if ( retval != PAPI_OK )
27  test_fail( __FILE__, __LINE__, "PAPI_get_thr_specific", retval );
28 
29  if ( arg != arg2 )
30  test_fail( __FILE__, __LINE__, "set vs get specific", 0 );
31 
32  while ( processing ) {
33  if ( *( ( int * ) arg ) == 500000 ) {
34  sleep( 1 );
35  int i;
37  data.num = 10;
38  data.id =
39  ( unsigned long * ) malloc( ( size_t ) data.num *
40  sizeof ( unsigned long ) );
41  data.data =
42  ( void ** ) malloc( ( size_t ) data.num * sizeof ( void * ) );
43 
44  retval =
46  ( void ** ) &data );
47  if ( retval != PAPI_OK )
48  test_fail( __FILE__, __LINE__, "PAPI_get_thr_specific",
49  retval );
50 
51  if ( data.num != 5 )
52  test_fail( __FILE__, __LINE__, "data.num != 5", 0 );
53 
54  for ( i = 0; i < data.num; i++ )
55  printf( "Entry %d, Thread %#lx, Data Pointer %p, Value %d\n",
56  i, data.id[i], data.data[i], *( int * ) data.data[i] );
57 
58  processing = 0;
59  }
60  }
61 
62  retval = PAPI_unregister_thread( );
63  if ( retval != PAPI_OK )
64  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
65  return ( NULL );
66 }
67 
68 int
69 main( int argc, char **argv )
70 {
71  pthread_t e_th, f_th, g_th, h_th;
72  int flops1, flops2, flops3, flops4, flops5;
73  int retval, rc;
74  pthread_attr_t attr;
75 
76  tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */
77 
79  if ( retval != PAPI_VER_CURRENT )
80  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
81 
82  retval =
83  PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) );
84  if ( retval != PAPI_OK ) {
85  if ( retval == PAPI_ECMP )
86  test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
87  else
88  test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
89  }
90 
91  pthread_attr_init( &attr );
92 #ifdef PTHREAD_CREATE_UNDETACHED
93  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
94 #endif
95 #ifdef PTHREAD_SCOPE_SYSTEM
96  retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
97  if ( retval != 0 )
98  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
99 #endif
100 
101  flops1 = 1000000;
102  rc = pthread_create( &e_th, &attr, Thread, ( void * ) &flops1 );
103  if ( rc ) {
104  retval = PAPI_ESYS;
105  test_fail( __FILE__, __LINE__, "pthread_create", retval );
106  }
107  flops2 = 2000000;
108  rc = pthread_create( &f_th, &attr, Thread, ( void * ) &flops2 );
109  if ( rc ) {
110  retval = PAPI_ESYS;
111  test_fail( __FILE__, __LINE__, "pthread_create", retval );
112  }
113 
114  flops3 = 4000000;
115  rc = pthread_create( &g_th, &attr, Thread, ( void * ) &flops3 );
116  if ( rc ) {
117  retval = PAPI_ESYS;
118  test_fail( __FILE__, __LINE__, "pthread_create", retval );
119  }
120 
121  flops4 = 8000000;
122  rc = pthread_create( &h_th, &attr, Thread, ( void * ) &flops4 );
123  if ( rc ) {
124  retval = PAPI_ESYS;
125  test_fail( __FILE__, __LINE__, "pthread_create", retval );
126  }
127 
128  pthread_attr_destroy( &attr );
129  flops5 = 500000;
130  Thread( &flops5 );
131  pthread_join( h_th, NULL );
132  pthread_join( g_th, NULL );
133  pthread_join( f_th, NULL );
134  pthread_join( e_th, NULL );
135 
136  test_pass( __FILE__, NULL, 0 );
137  pthread_exit( NULL );
138  exit( 1 );
139 }
sleep(1)
void ** data
Definition: papi.h:561
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
int PAPI_register_thread(void)
Definition: papi.c:205
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
#define printf
Definition: papi_test.h:125
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
int rc
Definition: iozone.c:20281
static int processing
Definition: thrspecific.c:7
char ** argv
Definition: iozone.c:1610
int PAPI_get_thr_specific(int tag, void **ptr)
Definition: papi.c:359
#define PAPI_TLS_ALL_THREADS
Definition: papi.h:325
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:120
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
#define PAPI_USR1_TLS
Definition: papi.h:317
#define PAPI_ESYS
Definition: papi.h:253
long long
Definition: iozone.c:19827
void * Thread(void *arg)
#define PAPI_ECMP
Definition: papi.h:254
pthread_t
Definition: iozone.c:18557
PAPI_thread_id_t * id
Definition: papi.h:560
int PAPI_unregister_thread(void)
Definition: papi.c:241
int PAPI_set_thr_specific(int tag, void *ptr)
Definition: papi.c:435
ssize_t retval
Definition: libasync.c:338
#define PAPI_VER_CURRENT
Definition: papi.h:223
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
pthread_attr_t attr
Definition: iozone.c:18466