|
PAPI
5.1.0.2
|
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 15 00015 00016 main(int argc, char *argv[]) { 00017 int Events[NUM_EVENTS]; 00018 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"}; 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) 00031 printf("This program will listen on port 3490, and write data received to standard output AND socket\n" 00032 "In the output ensure that the following identities hold:\n" 00033 "READ_* == SOCK_READ_*\n" 00034 "WRITE_{CALLS,BYTES} = 2 * SOCK_WRITE_{CALLS,BYTES}\n" 00035 "SOCK_READ_BYTES == SOCK_WRITE_BYTES\n"); 00036 int retval; 00037 int e; 00038 for (e=0; e<NUM_EVENTS; e++) { 00039 retval = PAPI_event_name_to_code((char*)names[e], &Events[e]); 00040 if (retval != PAPI_OK) { 00041 fprintf(stderr, "Error getting code for %s\n", names[e]); 00042 exit(2); 00043 } 00044 } 00045 00046 int bytes = 0; 00047 char buf[1024]; 00048 00049 int sockfd, n_sockfd, sin_size, len; 00050 char *host_addr; 00051 struct sockaddr_in my_addr; 00052 struct sockaddr_in their_addr; 00053 my_addr.sin_family = AF_INET; 00054 my_addr.sin_port = htons(PORT); 00055 my_addr.sin_addr.s_addr = INADDR_ANY; 00056 00057 sockfd = socket(AF_INET, SOCK_STREAM, 0); 00058 if (sockfd < 0) { 00059 perror("socket"); 00060 exit(1); 00061 } 00062 if ((bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1) { 00063 perror("bind"); 00064 exit(1); 00065 } 00066 listen(sockfd, 10); 00067 if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { 00068 perror("accept"); 00069 exit(1); 00070 } 00071 close(sockfd); 00072 00073 /* Start counting events */ 00074 if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) { 00075 fprintf(stderr, "Error in PAPI_start_counters\n"); 00076 exit(1); 00077 } 00078 00079 while ((bytes = read(n_sockfd, buf, 1024)) > 0) { 00080 write(1, buf, bytes); 00081 write(n_sockfd, buf, bytes); 00082 } 00083 00084 close(n_sockfd); 00085 00086 /* Stop counting events */ 00087 if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) { 00088 fprintf(stderr, "Error in PAPI_stop_counters\n"); 00089 } 00090 00091 if (!TESTS_QUIET) { 00092 printf("----\n"); 00093 for (e=0; e<NUM_EVENTS; e++) 00094 printf("%s: %lld\n", names[e], values[e]); 00095 } 00096 test_pass( __FILE__, NULL, 0 ); 00097 return 0; 00098 }