PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
appio_test_read_write.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/group
7  * and writes the output to stdout.
8  * Statistics are printed at the end of the run.,
9  */
10 #include <papi.h>
11 #include <errno.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 
19 #include "papi_test.h"
20 
21 #define NUM_EVENTS 12
22 
23 int main(int argc, char** argv) {
24  int Events[NUM_EVENTS];
25  const char* names[NUM_EVENTS] = {"OPEN_CALLS", "OPEN_FDS", "READ_CALLS", "READ_BYTES", "READ_USEC", "READ_ERR", "READ_INTERRUPTED", "READ_WOULD_BLOCK", "WRITE_CALLS","WRITE_BYTES","WRITE_USEC","WRITE_WOULD_BLOCK"};
26  long long values[NUM_EVENTS];
27 
28  char *infile = "/etc/group";
29 
30  /* Set TESTS_QUIET variable */
31  tests_quiet( argc, argv );
32 
34  if (version != PAPI_VER_CURRENT) {
35  fprintf(stderr, "PAPI_library_init version mismatch\n");
36  exit(1);
37  }
38 
39  int fdin;
40  if (!TESTS_QUIET) printf("This program will read %s and write it to /dev/null\n", infile);
41  int retval;
42  int e;
43  for (e=0; e<NUM_EVENTS; e++) {
44  retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
45  if (retval != PAPI_OK) {
46  fprintf(stderr, "Error getting code for %s\n", names[e]);
47  exit(2);
48  }
49  }
50 
51  /* Start counting events */
52  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
53  fprintf(stderr, "Error in PAPI_start_counters\n");
54  exit(1);
55  }
56 
57  fdin=open(infile, O_RDONLY);
58  if (fdin < 0) perror("Could not open file for reading: \n");
59  int fdout;
60  fdout=open("/dev/null", O_WRONLY);
61  if (fdout < 0) perror("Could not open file for writing: \n");
62  int bytes = 0;
63  char buf[1024];
64 
65 
66 //if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK)
67 // handle_error(1);
68 //printf("After reading the counters: %lld\n",values[0]);
69 
70  while ((bytes = read(fdin, buf, 1024)) > 0) {
71  write(fdout, buf, bytes);
72  }
73 
74  /* Closing the descriptors before doing the PAPI_stop
75  means, OPEN_FDS will be reported as zero, which is
76  right, since at the time of PAPI_stop, the descriptors
77  we opened have been closed */
78  close (fdin);
79  close (fdout);
80 
81  /* Stop counting events */
82  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
83  fprintf(stderr, "Error in PAPI_stop_counters\n");
84  }
85 
86  if (!TESTS_QUIET) {
87  printf("----\n");
88  for (e=0; e<NUM_EVENTS; e++)
89  printf("%s: %lld\n", names[e], values[e]);
90  }
91  test_pass( __FILE__, NULL, 0 );
92  return 0;
93 }
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 open(const char *pathname, int flags, mode_t mode)
Definition: appio.c:184
int PAPI_library_init(int version)
Definition: papi.c:495
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
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
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
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()