PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ctests/locks_pthreads.c
Go to the documentation of this file.
1 /* This file checks to make sure the locking mechanisms work correctly on the platform.
2  * Platforms where the locking mechanisms are not implemented or are incorrectly implemented
3  * will fail. -KSL
4  */
5 
6 #include <pthread.h>
7 #include "papi_test.h"
8 
9 volatile long long count = 0;
10 volatile long long tmpcount = 0;
11 volatile int num_iters = 0;
12 
13 void
14 lockloop( int iters, volatile long long *mycount )
15 {
16  int i;
17  for ( i = 0; i < iters; i++ ) {
19  *mycount = *mycount + 1;
21  }
22 }
23 
24 void *
25 Slave( void *arg )
26 {
27  long long duration;
28 
29  ( void ) arg;
30 
31  sleep( 1 );
32  duration = PAPI_get_real_usec( );
33  lockloop( 10000, &tmpcount );
34  duration = PAPI_get_real_usec( ) - duration;
35 
36  /* First one here set's the number */
38  if ( num_iters == 0 ) {
39  printf( "10000 iterations took %lld us.\n", duration );
40  num_iters = ( int ) ( 10 * ( TIME_LIMIT_IN_US / duration ) );
41  printf( "Running %d iterations\n", num_iters );
42  }
44 
46  pthread_exit( NULL );
47 }
48 
49 
50 int
51 main( int argc, char **argv )
52 {
53  pthread_t slaves[MAX_THREADS];
54  int rc, i, nthr;
55  int retval;
56  const PAPI_hw_info_t *hwinfo = NULL;
57 
58  /* Set TESTS_QUIET variable */
59  tests_quiet( argc, argv );
60 
61  if ( ( retval =
63  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
64 
65  if ( ( hwinfo = PAPI_get_hardware_info( ) ) == NULL )
66  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
67 
68  retval =
69  PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) );
70  if ( retval != PAPI_OK ) {
71  if ( retval == PAPI_ECMP )
72  test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
73  else
74  test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
75  }
76 
77  if ( hwinfo->ncpu > MAX_THREADS )
78  nthr = MAX_THREADS;
79  else
80  nthr = hwinfo->ncpu;
81 
82  printf( "Creating %d threads\n", nthr );
83 
84  for ( i = 0; i < nthr; i++ ) {
85  rc = pthread_create( &slaves[i], NULL, Slave, NULL );
86  if ( rc ) {
87  retval = PAPI_ESYS;
88  test_fail( __FILE__, __LINE__, "pthread_create", retval );
89  }
90  }
91 
92  for ( i = 0; i < nthr; i++ ) {
93  pthread_join( slaves[i], NULL );
94  }
95 
96  printf( "Expected: %lld Received: %lld\n", ( long long ) nthr * num_iters,
97  count );
98  if ( nthr * num_iters != count )
99  test_fail( __FILE__, __LINE__, "Thread Locks", 1 );
100 
101  test_pass( __FILE__, NULL, 0 );
102  exit( 1 );
103 }
void lockloop(int iters, volatile long long *mycount)
sleep(1)
Hardware info structure.
Definition: papi.h:775
#define TIME_LIMIT_IN_US
Definition: test_utils.h:16
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
volatile int num_iters
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
#define PAPI_USR2_LOCK
Definition: fpapi.h:83
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
void * Slave(void *arg)
char ** argv
Definition: iozone.c:1610
#define PAPI_USR1_LOCK
Definition: fpapi.h:82
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
int i
Definition: fileop.c:140
#define PAPI_ECMP
Definition: fpapi.h:109
#define MAX_THREADS
Definition: test_utils.h:7
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
pthread_t
Definition: iozone.c:18557
#define PAPI_ESYS
Definition: fpapi.h:108
long long PAPI_get_real_usec(void)
Definition: papi.c:6138
int PAPI_unlock(int lck)
Definition: papi.c:6295
int
Definition: iozone.c:18528
int rc
Definition: iozone.c:20281
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6059
ssize_t retval
Definition: libasync.c:338
int PAPI_lock(int lck)
Definition: papi.c:6275
volatile long long tmpcount
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int ncpu
Definition: papi.h:776