PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
appio_test_socket.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h> /* exit() */
4 #include <errno.h> /* herror() */
5 #include <netdb.h> /* gethostbyname() */
6 #include <sys/types.h> /* bind() accept() */
7 #include <sys/socket.h> /* bind() accept() */
8 #include <unistd.h>
9 
10 #include <papi.h>
11 #include "papi_test.h"
12 
13 #define PORT 3490
14 #define NUM_EVENTS 15
15 
16 main(int argc, char *argv[]) {
17  int Events[NUM_EVENTS];
18  const char* names[NUM_EVENTS] = {"READ_CALLS", "READ_BYTES", "READ_USEC", "READ_WOULD_BLOCK", "SOCK_READ_CALLS", "SOCK_READ_BYTES", "SOCK_READ_USEC", "SOCK_READ_WOULD_BLOCK", "WRITE_BYTES", "WRITE_CALLS", "WRITE_WOULD_BLOCK", "WRITE_USEC", "SOCK_WRITE_BYTES", "SOCK_WRITE_CALLS", "SOCK_WRITE_USEC"};
19  long long values[NUM_EVENTS];
20 
21  /* Set TESTS_QUIET variable */
22  tests_quiet( argc, argv );
23 
25  if (version != PAPI_VER_CURRENT) {
26  fprintf(stderr, "PAPI_library_init version mismatch\n");
27  exit(1);
28  }
29 
30  if (!TESTS_QUIET)
31  printf("This program will listen on port 3490, and write data received to standard output AND socket\n"
32  "In the output ensure that the following identities hold:\n"
33  "READ_* == SOCK_READ_*\n"
34  "WRITE_{CALLS,BYTES} = 2 * SOCK_WRITE_{CALLS,BYTES}\n"
35  "SOCK_READ_BYTES == SOCK_WRITE_BYTES\n");
36  int retval;
37  int e;
38  for (e=0; e<NUM_EVENTS; e++) {
39  retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
40  if (retval != PAPI_OK) {
41  fprintf(stderr, "Error getting code for %s\n", names[e]);
42  exit(2);
43  }
44  }
45 
46  int bytes = 0;
47  char buf[1024];
48 
49  int sockfd, n_sockfd, sin_size, len;
50  char *host_addr;
51  struct sockaddr_in my_addr;
52  struct sockaddr_in their_addr;
53  my_addr.sin_family = AF_INET;
54  my_addr.sin_port = htons(PORT);
55  my_addr.sin_addr.s_addr = INADDR_ANY;
56 
57  sockfd = socket(AF_INET, SOCK_STREAM, 0);
58  if (sockfd < 0) {
59  perror("socket");
60  exit(1);
61  }
62  if ((bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1) {
63  perror("bind");
64  exit(1);
65  }
66  listen(sockfd, 10);
67  if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
68  perror("accept");
69  exit(1);
70  }
71  close(sockfd);
72 
73  /* Start counting events */
74  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
75  fprintf(stderr, "Error in PAPI_start_counters\n");
76  exit(1);
77  }
78 
79  while ((bytes = read(n_sockfd, buf, 1024)) > 0) {
80  write(1, buf, bytes);
81  write(n_sockfd, buf, bytes);
82  }
83 
84  close(n_sockfd);
85 
86  /* Stop counting events */
87  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
88  fprintf(stderr, "Error in PAPI_stop_counters\n");
89  }
90 
91  if (!TESTS_QUIET) {
92  printf("----\n");
93  for (e=0; e<NUM_EVENTS; e++)
94  printf("%s: %lld\n", names[e], values[e]);
95  }
96  test_pass( __FILE__, NULL, 0 );
97  return 0;
98 }
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.
#define PORT
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
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 NUM_EVENTS
#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:1008
listen(s, 10)
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()