PAPI  5.6.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 /* No it doesn't, that description is *completely* wrong */
5 
6 /* I think this is trying to test the pthread thread-specific */
7 /* implementation but it is unclear and the git commit history */
8 /* does not help at all here */
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <pthread.h>
14 
15 #include "papi.h"
16 #include "papi_test.h"
17 
18 static volatile int processing = 1;
19 
20 void *
21 Thread( void *arg )
22 {
23  int retval;
24  void *arg2;
25  int i;
26 
27  retval = PAPI_register_thread( );
28  if ( retval != PAPI_OK ) {
29  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
30  }
31 
32  if (!TESTS_QUIET) {
33  printf( "Thread %#x started, specific data is at %p\n",
34  ( int ) pthread_self( ), arg );
35  }
36 
37  retval = PAPI_set_thr_specific( PAPI_USR1_TLS, arg );
38  if ( retval != PAPI_OK ) {
39  test_fail( __FILE__, __LINE__, "PAPI_set_thr_specific", retval );
40  }
41 
42  retval = PAPI_get_thr_specific( PAPI_USR1_TLS, &arg2 );
43  if ( retval != PAPI_OK ) {
44  test_fail( __FILE__, __LINE__, "PAPI_get_thr_specific", retval );
45  }
46 
47  if ( arg != arg2 ) {
48  test_fail( __FILE__, __LINE__, "set vs get specific", 0 );
49  }
50 
51  while ( processing ) {
52  if ( *( ( int * ) arg ) == 500000 ) {
53  sleep( 1 );
54 
56  data.num = 10;
57  data.id = ( unsigned long * ) malloc( ( size_t ) data.num *
58  sizeof ( unsigned long ) );
59  data.data = ( void ** ) malloc( ( size_t ) data.num * sizeof ( void * ) );
60 
62  ( void ** ) &data );
63  if ( retval != PAPI_OK ) {
64  test_fail( __FILE__, __LINE__, "PAPI_get_thr_specific",
65  retval );
66  }
67 
68  if ( data.num != 5 ) {
69  test_fail( __FILE__, __LINE__, "data.num != 5", 0 );
70  }
71 
72  if (!TESTS_QUIET) for ( i = 0; i < data.num; i++ ) {
73  printf( "Entry %d, Thread %#lx, Data Pointer %p, Value %d\n",
74  i, data.id[i], data.data[i], *( int * ) data.data[i] );
75  }
76  processing = 0;
77  }
78  }
79 
80  retval = PAPI_unregister_thread( );
81  if ( retval != PAPI_OK ) {
82  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
83  }
84 
85  return NULL;
86 }
87 
88 int
89 main( int argc, char **argv )
90 {
91  pthread_t e_th, f_th, g_th, h_th;
92  int flops1, flops2, flops3, flops4, flops5;
93  int retval, rc;
94  pthread_attr_t attr;
95  int quiet;
96 
97  /* Set TESTS_QUIET variable */
98  quiet = tests_quiet( argc, argv );
99 
100  if (!quiet) printf("Testing threads\n");
101 
103  if ( retval != PAPI_VER_CURRENT ) {
104  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
105  }
106 
107  retval = PAPI_thread_init( ( unsigned long ( * )( void ) )
108  ( pthread_self ) );
109  if ( retval != PAPI_OK ) {
110  if ( retval == PAPI_ECMP ) {
111  test_skip( __FILE__, __LINE__,
112  "PAPI_thread_init", retval );
113  }
114  else {
115  test_fail( __FILE__, __LINE__,
116  "PAPI_thread_init", retval );
117  }
118  }
119 
120  pthread_attr_init( &attr );
121 
122 #ifdef PTHREAD_CREATE_UNDETACHED
123  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
124 #endif
125 
126 #ifdef PTHREAD_SCOPE_SYSTEM
127  retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
128  if ( retval != 0 ) {
129  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
130  }
131 #endif
132 
133  flops1 = 1000000;
134  rc = pthread_create( &e_th, &attr, Thread, ( void * ) &flops1 );
135  if ( rc ) {
136  retval = PAPI_ESYS;
137  test_fail( __FILE__, __LINE__, "pthread_create", retval );
138  }
139 
140  flops2 = 2000000;
141  rc = pthread_create( &f_th, &attr, Thread, ( void * ) &flops2 );
142  if ( rc ) {
143  retval = PAPI_ESYS;
144  test_fail( __FILE__, __LINE__, "pthread_create", retval );
145  }
146 
147  flops3 = 4000000;
148  rc = pthread_create( &g_th, &attr, Thread, ( void * ) &flops3 );
149  if ( rc ) {
150  retval = PAPI_ESYS;
151  test_fail( __FILE__, __LINE__, "pthread_create", retval );
152  }
153 
154  flops4 = 8000000;
155  rc = pthread_create( &h_th, &attr, Thread, ( void * ) &flops4 );
156  if ( rc ) {
157  retval = PAPI_ESYS;
158  test_fail( __FILE__, __LINE__, "pthread_create", retval );
159  }
160 
161  pthread_attr_destroy( &attr );
162 
163  flops5 = 500000;
164  Thread( &flops5 );
165 
166  pthread_join( h_th, NULL );
167  pthread_join( g_th, NULL );
168  pthread_join( f_th, NULL );
169  pthread_join( e_th, NULL );
170 
171  test_pass( __FILE__ );
172 
173  pthread_exit( NULL );
174 
175  return 1;
176 }
void test_pass(const char *filename)
Definition: test_utils.c:432
sleep(1)
void ** data
Definition: papi.h:563
int PAPI_register_thread(void)
Definition: papi.c:208
return PAPI_OK
Definition: linux-nvml.c:497
static volatile int processing
Definition: thrspecific.c:18
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
int rc
Definition: iozone.c:20281
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_get_thr_specific(int tag, void **ptr)
Definition: papi.c:362
#define PAPI_TLS_ALL_THREADS
Definition: papi.h:327
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:123
int PAPI_library_init(int version)
Definition: papi.c:500
int i
Definition: fileop.c:140
int quiet
Definition: rapl_overflow.c:18
#define PAPI_USR1_TLS
Definition: papi.h:319
#define PAPI_ESYS
Definition: papi.h:255
long long
Definition: iozone.c:19827
#define PAPI_ECMP
Definition: papi.h:256
pthread_t
Definition: iozone.c:18557
PAPI_thread_id_t * id
Definition: papi.h:562
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 PAPI_set_thr_specific(int tag, void *ptr)
Definition: papi.c:438
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
static void * Thread(void *arg)
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.
pthread_attr_t attr
Definition: iozone.c:18466