PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pthrtough2.c
Go to the documentation of this file.
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include "papi_test.h"
5 
6 #define NITER 2000
7 
8 void *
9 Thread( void *data )
10 {
11  int ret, evtset;
12 
13  ( void ) data;
14 
15  if ( ( ret = PAPI_register_thread( ) ) != PAPI_OK )
16  test_fail( __FILE__, __LINE__, "PAPI_thread_init", ret );
17 
18  evtset = PAPI_NULL;
19  if ( ( ret = PAPI_create_eventset( &evtset ) ) != PAPI_OK )
20  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", ret );
21 
22  if ( ( ret = PAPI_destroy_eventset( &evtset ) ) != PAPI_OK )
23  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", ret );
24 
25  if ( ( ret = PAPI_unregister_thread( ) ) != PAPI_OK )
26  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", ret );
27 
28  return ( NULL );
29 }
30 
31 int
32 main( int argc, char *argv[] )
33 {
34  int j;
35  pthread_t *th = NULL;
36  pthread_attr_t attr;
37  int ret;
38  long nthr;
39 
40  tests_quiet( argc, argv ); /*Set TESTS_QUIET variable */
41 
43  if ( ret != PAPI_VER_CURRENT )
44  test_fail( __FILE__, __LINE__, "PAPI_library_init", ret );
45 
46  if ( ( ret =
47  PAPI_thread_init( ( unsigned
48  long ( * )( void ) ) ( pthread_self ) ) ) !=
49  PAPI_OK )
50  test_fail( __FILE__, __LINE__, "PAPI_thread_init", ret );
51 
52  pthread_attr_init( &attr );
53 #ifdef PTHREAD_CREATE_UNDETACHED
54  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
55 #endif
56 #ifdef PTHREAD_SCOPE_SYSTEM
57  ret = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
58  if ( ret != 0 )
59  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", ret );
60 
61 #endif
62 
63  nthr = NITER;
64 
65  if ( !TESTS_QUIET ) {
66  printf( "Creating %d threads for %d iterations each of:\n",
67  ( int ) nthr, 1 );
68  printf( "\tregister\n" );
69  printf( "\tcreate_eventset\n" );
70  printf( "\tdestroy_eventset\n" );
71  printf( "\tunregister\n" );
72  }
73  th = ( pthread_t * ) malloc( ( size_t ) nthr * sizeof ( pthread_t ) );
74  if ( th == NULL )
75  test_fail( __FILE__, __LINE__, "malloc", PAPI_ESYS );
76 
77  for ( j = 0; j < nthr; j++ ) {
78  ret = pthread_create( &th[j], &attr, &Thread, NULL );
79  if ( ret ) {
80  printf( "Failed to create thread: %d\n", j );
81  if ( j < 10 )
82  test_fail( __FILE__, __LINE__, "pthread_create", PAPI_ESYS );
83  printf( "Continuing test with %d threads.\n", j - 1 );
84  nthr = j - 1;
85  th = ( pthread_t * ) realloc( th,
86  ( size_t ) nthr *
87  sizeof ( pthread_t ) );
88  break;
89  }
90  }
91 
92  for ( j = 0; j < nthr; j++ ) {
93  pthread_join( th[j], NULL );
94  }
95 
96  test_pass( __FILE__, NULL, 0 );
97  exit( 1 );
98 }
#define PAPI_NULL
Definition: fpapi.h:13
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
int PAPI_register_thread(void)
Definition: papi.c:203
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
void
Definition: iozone.c:18627
#define printf
Definition: papi_test.h:125
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
long long ret
Definition: iozone.c:1346
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:118
int PAPI_library_init(int version)
Definition: papi.c:495
#define NITER
Definition: pthrtough2.c:6
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
void * Thread(void *arg)
pthread_t
Definition: iozone.c:18557
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1406
#define PAPI_ESYS
Definition: fpapi.h:108
int PAPI_unregister_thread(void)
Definition: papi.c:239
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1944
long j
Definition: iozone.c:19135
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
pthread_attr_t attr
Definition: iozone.c:18466