|
PAPI
5.0.1.0
|
00001 /**************************************************************************** 00002 * PAPI_profil - generate PC histogram data * 00003 ****************************************************************************/ 00004 00005 #include <stdio.h> 00006 #include <stdlib.h> 00007 #include <string.h> 00008 #include "papi.h" /* This needs to be included every time you use PAPI */ 00009 00010 #define FLOPS 1000000 00011 #define THRESHOLD 100000 00012 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); } 00013 00014 int code_to_monitor() 00015 { 00016 int i; 00017 double tmp=1.1; 00018 00019 for(i=0; i < FLOPS; i++) 00020 { 00021 tmp=i+tmp; 00022 tmp++; 00023 } 00024 i = (int) tmp; 00025 return i; 00026 } 00027 00028 int main() 00029 { 00030 00031 unsigned long length; 00032 caddr_t start, end; 00033 PAPI_sprofil_t * prof; 00034 int EventSet = PAPI_NULL; 00035 /*must be initialized to PAPI_NULL before calling PAPI_create_event*/ 00036 int PAPI_event,i,tmp = 0; 00037 char event_name[PAPI_MAX_STR_LEN]; 00038 /*These are going to be used as buffers */ 00039 unsigned short *profbuf; 00040 long long values[2]; 00041 const PAPI_exe_info_t *prginfo = NULL; 00042 00043 00044 int retval; 00045 00046 00047 /**************************************************************************** 00048 * This part initializes the library and compares the version number of the * 00049 * header file, to the version of the library, if these don't match then it * 00050 * is likely that PAPI won't work correctly.If there is an error, retval * 00051 * keeps track of the version number. * 00052 ****************************************************************************/ 00053 00054 if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT ) 00055 { 00056 printf("Library initialization error! \n"); 00057 exit(1); 00058 } 00059 00060 00061 if ((prginfo = PAPI_get_executable_info()) == NULL) 00062 { 00063 fprintf(stderr, "Error in get executable information \n"); 00064 exit(1); 00065 } 00066 00067 start = prginfo->address_info.text_start; 00068 end = prginfo->address_info.text_end; 00069 length = (end - start); 00070 00071 /* for PAPI_PROFIL_BUCKET_16 and scale = 65536, 00072 profile buffer length == program address length. 00073 Larger bucket sizes would increase the buffer length. 00074 Smaller scale factors would decrease it. 00075 Handle with care... 00076 */ 00077 profbuf = (unsigned short *)malloc(length); 00078 if (profbuf == NULL) 00079 { 00080 fprintf(stderr, "Not enough memory \n"); 00081 exit(1); 00082 } 00083 memset(profbuf,0x00,length); 00084 00085 /* Creating the eventset */ 00086 if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK) 00087 ERROR_RETURN(retval); 00088 00089 PAPI_event = PAPI_TOT_INS; 00090 /* Add Total Instructions Executed to our EventSet */ 00091 if ( (retval = PAPI_add_event(EventSet, PAPI_event)) != PAPI_OK) 00092 ERROR_RETURN(retval); 00093 00094 /* Add Total Cycles Executed to our EventSet */ 00095 if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK) 00096 ERROR_RETURN(retval); 00097 00098 /* enable the collection of profiling information */ 00099 if ((retval = PAPI_profil(profbuf, length, start, 65536, EventSet, 00100 PAPI_event, THRESHOLD, PAPI_PROFIL_POSIX | PAPI_PROFIL_BUCKET_16)) != PAPI_OK) 00101 ERROR_RETURN(retval); 00102 00103 /* let's rock and roll */ 00104 if ((retval=PAPI_start(EventSet)) != PAPI_OK) 00105 ERROR_RETURN(retval); 00106 00107 code_to_monitor(); 00108 00109 if ((retval=PAPI_stop(EventSet, values)) != PAPI_OK) 00110 ERROR_RETURN(retval); 00111 00112 /* disable the collection of profiling information by setting threshold 00113 to 0 00114 */ 00115 if ((retval = PAPI_profil(profbuf, length, start, 65536, EventSet, 00116 PAPI_event, 0, PAPI_PROFIL_POSIX)) != PAPI_OK) 00117 ERROR_RETURN(retval); 00118 00119 printf("-----------------------------------------------------------\n"); 00120 printf("Text start: %p, Text end: %p, \n", 00121 prginfo->address_info.text_start,prginfo->address_info.text_end); 00122 printf("Data start: %p, Data end: %p\n", 00123 prginfo->address_info.data_start,prginfo->address_info.data_end); 00124 printf("BSS start : %p, BSS end: %p\n", 00125 prginfo->address_info.bss_start,prginfo->address_info.bss_end); 00126 00127 printf("------------------------------------------\n"); 00128 00129 printf("Test type : \tPAPI_PROFIL_POSIX\n"); 00130 printf("------------------------------------------\n\n\n"); 00131 printf("PAPI_profil() hash table.\n"); 00132 printf("address\t\tflat \n"); 00133 for (i = 0; i < (int) length/2; i++) 00134 { 00135 if (profbuf[i]) 00136 printf("0x%lx\t%d \n", 00137 (unsigned long) start + (unsigned long) (2 * i), profbuf[i]); 00138 } 00139 00140 printf("-----------------------------------------\n"); 00141 00142 retval = 0; 00143 for (i = 0; i < (int) length/2; i++) 00144 retval = retval || (profbuf[i]); 00145 if (retval) 00146 printf("Test succeeds! \n"); 00147 else 00148 printf( "No information in buffers\n"); 00149 /* clean up */ 00150 PAPI_shutdown(); 00151 00152 exit(0); 00153 } 00154 00155