PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
appio_test_recv.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 6
15 
16 main(int argc, char *argv[]) {
17  int Events[NUM_EVENTS];
18  const char* names[NUM_EVENTS] = {"RECV_CALLS", "RECV_BYTES", "RECV_USEC", "RECV_ERR", "RECV_INTERRUPTED", "RECV_WOULD_BLOCK"};
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) printf("This program will listen on port 3490, and write data received to standard output\n");
31  int retval;
32  int e;
33  for (e=0; e<NUM_EVENTS; e++) {
34  retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
35  if (retval != PAPI_OK) {
36  fprintf(stderr, "Error getting code for %s\n", names[e]);
37  exit(2);
38  }
39  }
40 
41  int bytes = 0;
42  char buf[1024];
43 
44  int sockfd, n_sockfd, sin_size, len;
45  char *host_addr, *recv_msg;
46  struct sockaddr_in my_addr;
47  struct sockaddr_in their_addr;
48  my_addr.sin_family = AF_INET;
49  my_addr.sin_port = htons(PORT);
50  my_addr.sin_addr.s_addr = INADDR_ANY;
51 
52  sockfd = socket(AF_INET, SOCK_STREAM, 0);
53  if (sockfd < 0) {
54  perror("socket");
55  exit(1);
56  }
57  if ((bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1) {
58  perror("bind");
59  exit(1);
60  }
61  listen(sockfd, 10);
62  if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
63  perror("accept");
64  exit(1);
65  }
66  close(sockfd);
67 
68  /* Start counting events */
69  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
70  fprintf(stderr, "Error in PAPI_start_counters\n");
71  exit(1);
72  }
73 
74  while ((bytes = recv(n_sockfd, buf, 1024, 0)) > 0) {
75  write(1, buf, bytes);
76  }
77 
78  close(n_sockfd);
79 
80  /* Stop counting events */
81  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
82  fprintf(stderr, "Error in PAPI_stop_counters\n");
83  }
84 
85  if (!TESTS_QUIET) {
86  printf("----\n");
87  for (e=0; e<NUM_EVENTS; e++)
88  printf("%s: %lld\n", names[e], values[e]);
89  }
90  test_pass( __FILE__, NULL, 0 );
91  return 0;
92 }
int close(int fd)
Definition: appio.c:175
static int Events[NUM_EVENTS]
Definition: init_fini.c:8
#define PORT
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_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
#define NUM_EVENTS
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()