PAPI  5.6.0.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 */
2 /* on the platform. */
3 /* Platforms where the locking mechanisms are not implemented or are */
4 /* incorrectly implemented will fail. -KSL */
5 
6 #define MAX_THREADS 256
7 #define APPR_TOTAL_ITER 1000000
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <pthread.h>
13 #include <math.h>
14 
15 #include "papi.h"
16 #include "papi_test.h"
17 
18 volatile long long count = 0;
19 volatile long long tmpcount = 0;
20 volatile long long thread_iter = 0;
21 
22 static int quiet=0;
23 
24 void
25 lockloop( int iters, volatile long long *mycount )
26 {
27  int i;
28  for ( i = 0; i < iters; i++ ) {
30  *mycount = *mycount + 1;
32  }
33 }
34 
35 void *
36 Slave( void *arg )
37 {
38  long long duration;
39 
40  duration = PAPI_get_real_usec( );
42  duration = PAPI_get_real_usec( ) - duration;
43 
44  if (!quiet) {
45  printf("%f lock/unlocks per us\n",
46  (float)thread_iter/(float)duration);
47  }
48  pthread_exit( arg );
49 }
50 
51 
52 int
53 main( int argc, char **argv )
54 {
55  pthread_t slaves[MAX_THREADS];
56  int rc, i, nthr;
57  int retval;
58  const PAPI_hw_info_t *hwinfo = NULL;
59 
60  /* Set TESTS_QUIET variable */
61  quiet = tests_quiet( argc, argv );
62 
64  if (retval != PAPI_VER_CURRENT ) {
65  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
66  }
67 
68  hwinfo = PAPI_get_hardware_info( );
69  if (hwinfo == NULL ) {
70  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
71  }
72 
73  retval = PAPI_thread_init((unsigned long (*)(void)) ( pthread_self ) );
74  if ( retval != PAPI_OK ) {
75  if ( retval == PAPI_ECMP ) {
76  test_skip( __FILE__, __LINE__,
77  "PAPI_thread_init", retval );
78  }
79  else {
80  test_fail( __FILE__, __LINE__,
81  "PAPI_thread_init", retval );
82  }
83  }
84 
85  if ( hwinfo->ncpu > MAX_THREADS ) {
86  nthr = MAX_THREADS;
87  }
88  else {
89  nthr = hwinfo->ncpu;
90  }
91 
92  /* Scale the per thread work to keep the serial runtime about the same. */
93  thread_iter = APPR_TOTAL_ITER/sqrt(nthr);
94 
95  if (!quiet) {
96  printf( "Creating %d threads, %lld lock/unlock\n",
97  nthr , thread_iter);
98  }
99 
100  for ( i = 0; i < nthr; i++ ) {
101  rc = pthread_create( &slaves[i], NULL, Slave, NULL );
102  if ( rc ) {
103  retval = PAPI_ESYS;
104  test_fail( __FILE__, __LINE__,
105  "pthread_create", retval );
106  }
107  }
108 
109  for ( i = 0; i < nthr; i++ ) {
110  pthread_join( slaves[i], NULL );
111  }
112 
113  if (!quiet) {
114  printf( "Expected: %lld Received: %lld\n",
115  ( long long ) nthr * thread_iter,
116  count );
117  }
118 
119  if ( nthr * thread_iter != count ) {
120  test_fail( __FILE__, __LINE__, "Thread Locks", 1 );
121  }
122 
123  test_pass( __FILE__ );
124 
125  return 0;
126 
127 }
void lockloop(int iters, volatile long long *mycount)
void test_pass(const char *filename)
Definition: test_utils.c:432
Hardware info structure.
Definition: papi.h:780
#define APPR_TOTAL_ITER
return PAPI_OK
Definition: linux-nvml.c:497
int count
Definition: iozone.c:22422
#define MAX_THREADS
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
void * Slave(void *arg)
char ** argv
Definition: iozone.c:1610
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_ESYS
Definition: papi.h:255
#define PAPI_ECMP
Definition: papi.h:256
pthread_t
Definition: iozone.c:18557
long long PAPI_get_real_usec(void)
Definition: papi.c:6264
printf("\tTry: -i 0 -i 1 \n\n")
#define PAPI_USR1_LOCK
Definition: papi.h:333
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
int PAPI_unlock(int lck)
Definition: papi.c:6421
volatile long long thread_iter
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6185
ssize_t retval
Definition: libasync.c:338
int PAPI_lock(int lck)
Definition: papi.c:6401
#define PAPI_VER_CURRENT
Definition: papi.h:225
volatile long long tmpcount
int main(int argc, char **argv)
List all appio events codes and names.
int ncpu
Definition: papi.h:781