PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
appio_test_pthreads.c
Go to the documentation of this file.
1 /*
2  * Test case for appio
3  * Author: Tushar Mohan
4  * tusharmohan@gmail.com
5  *
6  * Description: This test case reads from standard linux /etc files in
7  * four separate threads and copies the output to /dev/null
8  * READ and WRITE statistics for each of the threads is
9  * summarized at the end.
10  */
11 #include <pthread.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <malloc.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 
21 #include "papi.h"
22 #include "papi_test.h"
23 
24 #define NUM_EVENTS 6
25 const char* names[NUM_EVENTS] = {"READ_CALLS", "READ_BYTES","READ_USEC","WRITE_CALLS","WRITE_BYTES","WRITE_USEC"};
26 
27 #define NUM_INFILES 4
28 static const char* files[NUM_INFILES] = {"/etc/passwd", "/etc/group", "/etc/protocols", "/etc/nsswitch.conf"};
29 
30 void *ThreadIO(void *arg) {
31  unsigned long tid = (unsigned long)pthread_self();
32  if (!TESTS_QUIET) printf("\nThread 0x%lx: will read %s and write it to /dev/null\n", tid,(const char*) arg);
33  int Events[NUM_EVENTS];
34  long long values[NUM_EVENTS];
35  int retval;
36  int e;
37  for (e=0; e<NUM_EVENTS; e++) {
38  retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
39  if (retval != PAPI_OK) {
40  fprintf(stderr, "Error getting code for %s\n", names[e]);
41  exit(2);
42  }
43  }
44 
45  /* Start counting events */
46  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
47  fprintf(stderr, "Error in PAPI_start_counters\n");
48  exit(1);
49  }
50 
51 //if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK)
52 // handle_error(1);
53 //printf("After reading the counters: %lld\n",values[0]);
54 
55  int fdin = open((const char*)arg, O_RDONLY);
56  if (fdin < 0) perror("Could not open file for reading: \n");
57 
58  int bytes = 0;
59  char buf[1024];
60 
61  int fdout = open("/dev/null", O_WRONLY);
62  if (fdout < 0) perror("Could not open /dev/null for writing: \n");
63  while ((bytes = read(fdin, buf, 1024)) > 0) {
64  write(fdout, buf, bytes);
65  }
66  close(fdout);
67 
68  /* Stop counting events */
69  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
70  fprintf(stderr, "Error in PAPI_stop_counters\n");
71  }
72 
73  if (!TESTS_QUIET) {
74  for (e=0; e<NUM_EVENTS; e++)
75  printf("Thread 0x%lx: %s: %lld\n", tid, names[e], values[e]);
76  }
77  return(NULL);
78 }
79 
80 int main(int argc, char** argv) {
81  pthread_t *callThd;
82  int i, numthrds;
83  int retval;
84  pthread_attr_t attr;
85 
86  /* Set TESTS_QUIET variable */
87  tests_quiet( argc, argv );
88 
90  if (version != PAPI_VER_CURRENT) {
91  fprintf(stderr, "PAPI_library_init version mismatch\n");
92  exit(1);
93  }
94 
95 
96  pthread_attr_init(&attr);
97  if (PAPI_thread_init(pthread_self) != PAPI_OK) {
98  fprintf(stderr, "PAPI_thread_init returned an error\n");
99  exit(1);
100  }
101 #ifdef PTHREAD_CREATE_UNDETACHED
102  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
103 #endif
104 #ifdef PTHREAD_SCOPE_SYSTEM
105  retval = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
106  if (retval != 0) {
107  fprintf(stderr,"This system does not support kernel scheduled pthreads.\n");
108  exit(1);
109  }
110 #endif
111 
112  numthrds = NUM_INFILES;
113  if (!TESTS_QUIET) printf("%d threads\n",numthrds);
114  callThd = (pthread_t *)malloc(numthrds*sizeof(pthread_t));
115 
116  int rc ;
117  for (i=0;i<(numthrds-1);i++) {
118  rc = pthread_create(callThd+i, &attr, ThreadIO, (void *) files[i]);
119  if (rc != 0) perror("Error creating thread using pthread_create()");
120  }
121  ThreadIO((void *)files[numthrds-1]);
122  pthread_attr_destroy(&attr);
123 
124  for (i=0;i<(numthrds-1);i++)
125  pthread_join(callThd[i], NULL);
126 
127  test_pass( __FILE__, NULL, 0 );
128  return 0;
129 }
ssize_t read(int fd, void *buf, size_t count)
Definition: appio.c:225
int close(int fd)
Definition: appio.c:175
static int Events[NUM_EVENTS]
Definition: init_fini.c:8
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
Return codes and api definitions.
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:118
int open(const char *pathname, int flags, mode_t mode)
Definition: appio.c:184
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
char buf[200]
Definition: iozone.c:19609
ssize_t write(int fd, const void *buf, size_t count)
Definition: appio.c:298
int PAPI_stop_counters(long long *values, int array_len)
Definition: papi_hl.c:803
void *long long tid
Definition: iozone.c:18586
#define NUM_INFILES
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
long long
Definition: iozone.c:19827
void * ThreadIO(void *arg)
pthread_t
Definition: iozone.c:18557
char version[]
Definition: fileop.c:134
int PAPI_event_name_to_code(char *in, int *out)
Definition: papi.c:1003
#define NUM_EVENTS
int PAPI_start_counters(int *events, int array_len)
Definition: papi_hl.c:593
int rc
Definition: iozone.c:20281
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
int main(int argc, char **argv)
List all appio events codes and names.
const char * names[NUM_EVENTS]
void exit()
static const char * files[NUM_INFILES]
pthread_attr_t attr
Definition: iozone.c:18466