|
PAPI
5.0.1.0
|
00001 /***************************************************************************** 00002 * This example shows how to use PAPI_set_domain * 00003 *****************************************************************************/ 00004 00005 #include <stdio.h> 00006 #include <stdlib.h> 00007 #include <sys/types.h> 00008 #include <sys/stat.h> 00009 #include <fcntl.h> 00010 00011 #include "papi.h" /* This needs to be included every time you use PAPI */ 00012 00013 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); } 00014 00015 int poorly_tuned_function() 00016 { 00017 float tmp; 00018 int i; 00019 00020 for(i=1; i<2000; i++) 00021 { 00022 tmp=(tmp+100)/i; 00023 } 00024 return 0; 00025 } 00026 00027 int main() 00028 { 00029 00030 int num, retval, EventSet = PAPI_NULL; 00031 long long values[2]; 00032 PAPI_option_t options; 00033 int fd; 00034 00035 00036 /**************************************************************************** 00037 * This part initializes the library and compares the version number of the * 00038 * header file, to the version of the library, if these don't match then it * 00039 * is likely that PAPI won't work correctly.If there is an error, retval * 00040 * keeps track of the version number. * 00041 ****************************************************************************/ 00042 00043 if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT ) 00044 { 00045 printf("Library initialization error! \n"); 00046 exit(1); 00047 } 00048 00049 /* Set the domain of this EventSet to counter user mode. The domain 00050 will be valid for all the eventset created after this function call 00051 unless you call PAPI_set_domain again */ 00052 if ((retval=PAPI_set_domain(PAPI_DOM_USER)) != PAPI_OK) 00053 ERROR_RETURN(retval); 00054 00055 if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK) 00056 ERROR_RETURN(retval); 00057 00058 /* Add Total Instructions Executed event to the EventSet */ 00059 if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK) 00060 ERROR_RETURN(retval); 00061 00062 /* Add Total Cycles Executed event to the EventSet */ 00063 if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK) 00064 ERROR_RETURN(retval); 00065 00066 /* Start counting */ 00067 if((retval=PAPI_start(EventSet)) != PAPI_OK) 00068 ERROR_RETURN(retval); 00069 00070 poorly_tuned_function(); 00071 /* add some system calls */ 00072 fd = open("/dev/zero", O_RDONLY); 00073 if (fd == -1) 00074 { 00075 perror("open(/dev/zero)"); 00076 exit(1); 00077 } 00078 close(fd); 00079 00080 00081 /* Stop counting */ 00082 if((retval=PAPI_stop(EventSet, values)) != PAPI_OK) 00083 ERROR_RETURN(retval); 00084 00085 printf(" Total instructions: %lld Total Cycles: %lld \n", values[0], 00086 values[1]); 00087 00088 /* Set the domain of this EventSet to counter user and kernel modes */ 00089 if ((retval=PAPI_set_domain(PAPI_DOM_ALL)) != PAPI_OK) 00090 ERROR_RETURN(retval); 00091 00092 EventSet = PAPI_NULL; 00093 if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK) 00094 ERROR_RETURN(retval); 00095 00096 /* Add Total Instructions Executed to our EventSet */ 00097 if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK) 00098 ERROR_RETURN(retval); 00099 00100 /* Add Total Instructions Executed to our EventSet */ 00101 if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK) 00102 ERROR_RETURN(retval); 00103 /* Start counting */ 00104 if((retval=PAPI_start(EventSet)) != PAPI_OK) 00105 ERROR_RETURN(retval); 00106 00107 poorly_tuned_function(); 00108 /* add some system calls */ 00109 fd = open("/dev/zero", O_RDONLY); 00110 if (fd == -1) 00111 { 00112 perror("open(/dev/zero)"); 00113 exit(1); 00114 } 00115 close(fd); 00116 00117 /* Stop counting */ 00118 if((retval=PAPI_stop(EventSet, values)) != PAPI_OK) 00119 ERROR_RETURN(retval); 00120 00121 printf(" Total instructions: %lld Total Cycles: %lld \n", values[0], 00122 values[1]); 00123 00124 /* clean up */ 00125 PAPI_shutdown(); 00126 00127 exit(0); 00128 }