PAPI 7.1.0.0
Loading...
Searching...
No Matches
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
25const char* names[NUM_EVENTS] = {"READ_CALLS", "READ_BYTES","READ_USEC","WRITE_CALLS","WRITE_BYTES","WRITE_USEC"};
26
27#define NUM_INFILES 4
28static const char* files[NUM_INFILES] = {"/etc/passwd", "/etc/group", "/etc/protocols", "/etc/nsswitch.conf"};
29
30void *ThreadIO(void *arg) {
31 unsigned long tid = (unsigned long)pthread_self();
32 if (!TESTS_QUIET) printf("\nThread %#lx: will read %s and write it to /dev/null\n", tid,(const char*) arg);
33 int EventSet = PAPI_NULL;
34 long long values[NUM_EVENTS];
35 int retval;
36 int e;
37 int event_code;
38
39 /* Create the Event Set */
41 fprintf(stderr, "Error creating event set\n");
42 exit(2);
43 }
44
45 for (e=0; e<NUM_EVENTS; e++) {
46 retval = PAPI_event_name_to_code((char*)names[e], &event_code);
47 if (retval != PAPI_OK) {
48 fprintf(stderr, "Error getting code for %s\n", names[e]);
49 exit(2);
50 }
51 retval = PAPI_add_event(EventSet, event_code);
52 if (retval != PAPI_OK) {
53 fprintf(stderr, "Error adding %s to event set\n", names[e]);
54 exit(2);
55 }
56 }
57
58 /* Start counting events */
59 if (PAPI_start(EventSet) != PAPI_OK) {
60 fprintf(stderr, "Error in PAPI_start\n");
61 exit(1);
62 }
63
64//if (PAPI_read_counters(EventSet, values) != PAPI_OK)
65// handle_error(1);
66//printf("After reading the counters: %lld\n",values[0]);
67
68 int fdin = open((const char*)arg, O_RDONLY);
69 if (fdin < 0) perror("Could not open file for reading: \n");
70
71 int bytes = 0;
72 char buf[1024];
73
74 int fdout = open("/dev/null", O_WRONLY);
75 if (fdout < 0) perror("Could not open /dev/null for writing: \n");
76 while ((bytes = read(fdin, buf, 1024)) > 0) {
77 write(fdout, buf, bytes);
78 }
79 close(fdout);
80
81 /* Stop counting events */
83 fprintf(stderr, "Error in PAPI_stop\n");
84 }
85
86 if (!TESTS_QUIET) {
87 for (e=0; e<NUM_EVENTS; e++)
88 printf("Thread %#lx: %s: %lld\n", tid, names[e], values[e]);
89 }
90 return(NULL);
91}
92
93int main(int argc, char** argv) {
94 pthread_t *callThd;
95 int i, numthrds;
96 int retval;
97 pthread_attr_t attr;
98
99 /* Set TESTS_QUIET variable */
100 tests_quiet( argc, argv );
101
103 if (version != PAPI_VER_CURRENT) {
104 fprintf(stderr, "PAPI_library_init version mismatch\n");
105 exit(1);
106 }
107
108
109 pthread_attr_init(&attr);
110 if (PAPI_thread_init(pthread_self) != PAPI_OK) {
111 fprintf(stderr, "PAPI_thread_init returned an error\n");
112 exit(1);
113 }
114#ifdef PTHREAD_CREATE_UNDETACHED
115 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
116#endif
117#ifdef PTHREAD_SCOPE_SYSTEM
118 retval = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
119 if (retval != 0) {
120 fprintf(stderr,"This system does not support kernel scheduled pthreads.\n");
121 exit(1);
122 }
123#endif
124
125 numthrds = NUM_INFILES;
126 if (!TESTS_QUIET) printf("%d threads\n",numthrds);
127 callThd = (pthread_t *)malloc(numthrds*sizeof(pthread_t));
128
129 int rc ;
130 for (i=0;i<(numthrds-1);i++) {
131 rc = pthread_create(callThd+i, &attr, ThreadIO, (void *) files[i]);
132 if (rc != 0) perror("Error creating thread using pthread_create()");
133 }
134 ThreadIO((void *)files[numthrds-1]);
135 pthread_attr_destroy(&attr);
136
137 for (i=0;i<(numthrds-1);i++)
138 pthread_join(callThd[i], NULL);
139
140 test_pass( __FILE__ );
141 return 0;
142}
int i
int open(const char *pathname, int flags, mode_t mode)
Definition: appio.c:188
int close(int fd)
Definition: appio.c:179
ssize_t write(int fd, const void *buf, size_t count)
Definition: appio.c:302
ssize_t read(int fd, void *buf, size_t count)
Definition: appio.c:229
static const char * files[NUM_INFILES]
const char * names[NUM_EVENTS]
void * ThreadIO(void *arg)
#define NUM_EVENTS
#define NUM_INFILES
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Convert a name to a numeric hardware event code.
initialize the PAPI library.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Initialize thread support in the PAPI library.
volatile int buf[CACHE_FLUSH_BUFFER_SIZE_INTS]
Definition: do_loops.c:12
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
uint8_t version
int TESTS_QUIET
Definition: test_utils.c:18
Return codes and api definitions.
unsigned long int pthread_t
FILE * stderr
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
int main()
Definition: pernode.c:20
rc
Definition: pscanf.h:23
long long int long long
Definition: sde_internal.h:85
int retval
Definition: zero_fork.c:53