|
PAPI
5.0.1.0
|
00001 #include <stdio.h> 00002 #include <string.h> 00003 #include <stdlib.h> /* exit() */ 00004 #include <errno.h> /* herror() */ 00005 #include <netdb.h> /* gethostbyname() */ 00006 #include <sys/types.h> /* bind() accept() */ 00007 #include <sys/socket.h> /* bind() accept() */ 00008 #include <unistd.h> 00009 00010 #include <papi.h> 00011 #include "papi_test.h" 00012 00013 #define PORT 3490 00014 #define NUM_EVENTS 6 00015 00016 main(int argc, char *argv[]) { 00017 int Events[NUM_EVENTS]; 00018 const char* names[NUM_EVENTS] = {"RECV_CALLS", "RECV_BYTES", "RECV_USEC", "RECV_ERR", "RECV_INTERRUPTED", "RECV_WOULD_BLOCK"}; 00019 long long values[NUM_EVENTS]; 00020 00021 /* Set TESTS_QUIET variable */ 00022 tests_quiet( argc, argv ); 00023 00024 int version = PAPI_library_init (PAPI_VER_CURRENT); 00025 if (version != PAPI_VER_CURRENT) { 00026 fprintf(stderr, "PAPI_library_init version mismatch\n"); 00027 exit(1); 00028 } 00029 00030 if (!TESTS_QUIET) printf("This program will listen on port 3490, and write data received to standard output\n"); 00031 int retval; 00032 int e; 00033 for (e=0; e<NUM_EVENTS; e++) { 00034 retval = PAPI_event_name_to_code((char*)names[e], &Events[e]); 00035 if (retval != PAPI_OK) { 00036 fprintf(stderr, "Error getting code for %s\n", names[e]); 00037 exit(2); 00038 } 00039 } 00040 00041 int bytes = 0; 00042 char buf[1024]; 00043 00044 int sockfd, n_sockfd, sin_size, len; 00045 char *host_addr, *recv_msg; 00046 struct sockaddr_in my_addr; 00047 struct sockaddr_in their_addr; 00048 my_addr.sin_family = AF_INET; 00049 my_addr.sin_port = htons(PORT); 00050 my_addr.sin_addr.s_addr = INADDR_ANY; 00051 00052 sockfd = socket(AF_INET, SOCK_STREAM, 0); 00053 if (sockfd < 0) { 00054 perror("socket"); 00055 exit(1); 00056 } 00057 if ((bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1) { 00058 perror("bind"); 00059 exit(1); 00060 } 00061 listen(sockfd, 10); 00062 if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { 00063 perror("accept"); 00064 exit(1); 00065 } 00066 close(sockfd); 00067 00068 /* Start counting events */ 00069 if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) { 00070 fprintf(stderr, "Error in PAPI_start_counters\n"); 00071 exit(1); 00072 } 00073 00074 while ((bytes = recv(n_sockfd, buf, 1024, 0)) > 0) { 00075 write(1, buf, bytes); 00076 } 00077 00078 close(n_sockfd); 00079 00080 /* Stop counting events */ 00081 if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) { 00082 fprintf(stderr, "Error in PAPI_stop_counters\n"); 00083 } 00084 00085 if (!TESTS_QUIET) { 00086 printf("----\n"); 00087 for (e=0; e<NUM_EVENTS; e++) 00088 printf("%s: %lld\n", names[e], values[e]); 00089 } 00090 test_pass( __FILE__, NULL, 0 ); 00091 return 0; 00092 }