PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
appio_test_fread_fwrite.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 /dev/null
8  * Fread and fwrite are used for I/O.
9  * Statistics are printed at the end of the run.,
10  */
11 #include <papi.h>
12 #include <errno.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 
20 #include "papi.h"
21 #include "papi_test.h"
22 
23 
24 #define NUM_EVENTS 8
25 
26 int main(int argc, char** argv) {
27  int Events[NUM_EVENTS];
28  const char* names[NUM_EVENTS] = {"READ_CALLS", "READ_BYTES","READ_USEC","READ_ERR", "READ_EOF", "WRITE_CALLS","WRITE_BYTES","WRITE_USEC"};
29  long long values[NUM_EVENTS];
30 
31  char *infile = "/etc/group";
32 
33  /* Set TESTS_QUIET variable */
34  tests_quiet( argc, argv );
35 
37  if (version != PAPI_VER_CURRENT) {
38  fprintf(stderr, "PAPI_library_init version mismatch\n");
39  exit(1);
40  }
41  if (!TESTS_QUIET) printf("This program will read %s and write it to /dev/null\n", infile);
42  FILE* fdin=fopen(infile, "r");
43  if (fdin == NULL) perror("Could not open file for reading: \n");
44  FILE* fout=fopen("/dev/null", "w");
45  if (fout == NULL) perror("Could not open file for writing: \n");
46  int bytes = 0;
47  char buf[1024];
48 
49  int retval;
50  int e;
51  for (e=0; e<NUM_EVENTS; e++) {
52  retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
53  if (retval != PAPI_OK) {
54  fprintf(stderr, "Error getting code for %s\n", names[e]);
55  exit(2);
56  }
57  }
58 
59  /* Start counting events */
60  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
61  fprintf(stderr, "Error in PAPI_start_counters\n");
62  exit(1);
63  }
64 
65 //if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK)
66 // handle_error(1);
67 //printf("After reading the counters: %lld\n",values[0]);
68 
69  while ((bytes = fread(buf, 1, 1024, fdin)) > 0) {
70  fwrite(buf, 1, bytes, fout);
71  }
72 
73  fclose(fdin);
74  fclose(fout);
75 
76  /* Stop counting events */
77  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
78  fprintf(stderr, "Error in PAPI_stop_counters\n");
79  }
80 
81  if (!TESTS_QUIET) {
82  printf("----\n");
83  for (e=0; e<NUM_EVENTS; e++)
84  printf("%s: %lld\n", names[e], values[e]);
85  }
86  test_pass( __FILE__ );
87  return 0;
88 }
void test_pass(const char *filename)
Definition: test_utils.c:432
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: appio.c:387
static int Events[NUM_EVENTS]
Definition: init_fini.c:8
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: appio.c:275
return PAPI_OK
Definition: linux-nvml.c:497
int PAPI_event_name_to_code(const char *in, int *out)
Definition: papi.c:1004
fclose(thread_wqfd)
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
char ** argv
Definition: iozone.c:1610
int PAPI_library_init(int version)
Definition: papi.c:500
char buf[200]
Definition: iozone.c:19609
int PAPI_stop_counters(long long *values, int array_len)
Definition: papi_hl.c:803
char version[]
Definition: fileop.c:134
printf("\tTry: -i 0 -i 1 \n\n")
int TESTS_QUIET
Definition: test_utils.c:18
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
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
#define PAPI_VER_CURRENT
Definition: papi.h:225
int main(int argc, char **argv)
List all appio events codes and names.
const char * names[NUM_EVENTS]
void exit()
#define NUM_EVENTS