|
PAPI
5.0.1.0
|
00001 /* 00002 * Test case for appio 00003 * Author: Tushar Mohan 00004 * tusharmohan@gmail.com 00005 * 00006 * Description: This test case reads from standard linux /etc files in 00007 * four separate threads and copies the output to /dev/null 00008 * READ and WRITE statistics for each of the threads is 00009 * summarized at the end. 00010 */ 00011 #include <pthread.h> 00012 #include <stdlib.h> 00013 #include <stdio.h> 00014 #include <malloc.h> 00015 #include <unistd.h> 00016 #include <errno.h> 00017 #include <sys/types.h> 00018 #include <sys/stat.h> 00019 #include <fcntl.h> 00020 00021 #include "papi.h" 00022 #include "papi_test.h" 00023 00024 #define NUM_EVENTS 6 00025 const char* names[NUM_EVENTS] = {"READ_CALLS", "READ_BYTES","READ_USEC","WRITE_CALLS","WRITE_BYTES","WRITE_USEC"}; 00026 00027 #define NUM_INFILES 4 00028 static const char* files[NUM_INFILES] = {"/etc/passwd", "/etc/group", "/etc/protocols", "/etc/nsswitch.conf"}; 00029 00030 void *ThreadIO(void *arg) { 00031 unsigned long tid = (unsigned long)pthread_self(); 00032 if (!TESTS_QUIET) printf("\nThread 0x%lx: will read %s and write it to /dev/null\n", tid,(const char*) arg); 00033 int Events[NUM_EVENTS]; 00034 long long values[NUM_EVENTS]; 00035 int retval; 00036 int e; 00037 for (e=0; e<NUM_EVENTS; e++) { 00038 retval = PAPI_event_name_to_code((char*)names[e], &Events[e]); 00039 if (retval != PAPI_OK) { 00040 fprintf(stderr, "Error getting code for %s\n", names[e]); 00041 exit(2); 00042 } 00043 } 00044 00045 /* Start counting events */ 00046 if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) { 00047 fprintf(stderr, "Error in PAPI_start_counters\n"); 00048 exit(1); 00049 } 00050 00051 //if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK) 00052 // handle_error(1); 00053 //printf("After reading the counters: %lld\n",values[0]); 00054 00055 int fdin = open((const char*)arg, O_RDONLY); 00056 if (fdin < 0) perror("Could not open file for reading: \n"); 00057 00058 int bytes = 0; 00059 char buf[1024]; 00060 00061 int fdout = open("/dev/null", O_WRONLY); 00062 if (fdout < 0) perror("Could not open /dev/null for writing: \n"); 00063 while ((bytes = read(fdin, buf, 1024)) > 0) { 00064 write(fdout, buf, bytes); 00065 } 00066 close(fdout); 00067 00068 /* Stop counting events */ 00069 if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) { 00070 fprintf(stderr, "Error in PAPI_stop_counters\n"); 00071 } 00072 00073 if (!TESTS_QUIET) { 00074 for (e=0; e<NUM_EVENTS; e++) 00075 printf("Thread 0x%lx: %s: %lld\n", tid, names[e], values[e]); 00076 } 00077 return(NULL); 00078 } 00079 00080 int main(int argc, char** argv) { 00081 pthread_t *callThd; 00082 int i, numthrds; 00083 int retval; 00084 pthread_attr_t attr; 00085 00086 /* Set TESTS_QUIET variable */ 00087 tests_quiet( argc, argv ); 00088 00089 int version = PAPI_library_init (PAPI_VER_CURRENT); 00090 if (version != PAPI_VER_CURRENT) { 00091 fprintf(stderr, "PAPI_library_init version mismatch\n"); 00092 exit(1); 00093 } 00094 00095 00096 pthread_attr_init(&attr); 00097 if (PAPI_thread_init(pthread_self) != PAPI_OK) { 00098 fprintf(stderr, "PAPI_thread_init returned an error\n"); 00099 exit(1); 00100 } 00101 #ifdef PTHREAD_CREATE_UNDETACHED 00102 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED); 00103 #endif 00104 #ifdef PTHREAD_SCOPE_SYSTEM 00105 retval = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 00106 if (retval != 0) { 00107 fprintf(stderr,"This system does not support kernel scheduled pthreads.\n"); 00108 exit(1); 00109 } 00110 #endif 00111 00112 numthrds = NUM_INFILES; 00113 if (!TESTS_QUIET) printf("%d threads\n",numthrds); 00114 callThd = (pthread_t *)malloc(numthrds*sizeof(pthread_t)); 00115 00116 int rc ; 00117 for (i=0;i<(numthrds-1);i++) { 00118 rc = pthread_create(callThd+i, &attr, ThreadIO, (void *) files[i]); 00119 if (rc != 0) perror("Error creating thread using pthread_create()"); 00120 } 00121 ThreadIO((void *)files[numthrds-1]); 00122 pthread_attr_destroy(&attr); 00123 00124 for (i=0;i<(numthrds-1);i++) 00125 pthread_join(callThd[i], NULL); 00126 00127 test_pass( __FILE__, NULL, 0 ); 00128 return 0; 00129 }