PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PAPI_profil.c
Go to the documentation of this file.
1 /****************************************************************************
2  * PAPI_profil - generate PC histogram data *
3  ****************************************************************************/
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "papi.h" /* This needs to be included every time you use PAPI */
9 
10 #define FLOPS 1000000
11 #define THRESHOLD 100000
12 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
13 
15 {
16  int i;
17  double tmp=1.1;
18 
19  for(i=0; i < FLOPS; i++)
20  {
21  tmp=i+tmp;
22  tmp++;
23  }
24  i = (int) tmp;
25  return i;
26 }
27 
28 int main()
29 {
30 
31  unsigned long length;
32  caddr_t start, end;
33  PAPI_sprofil_t * prof;
34  int EventSet = PAPI_NULL;
35  /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
36  int PAPI_event,i,tmp = 0;
38  /*These are going to be used as buffers */
39  unsigned short *profbuf;
40  long long values[2];
41  const PAPI_exe_info_t *prginfo = NULL;
42 
43 
44  int retval;
45 
46 
47  /****************************************************************************
48  * This part initializes the library and compares the version number of the *
49  * header file, to the version of the library, if these don't match then it *
50  * is likely that PAPI won't work correctly.If there is an error, retval *
51  * keeps track of the version number. *
52  ****************************************************************************/
53 
55  {
56  printf("Library initialization error! \n");
57  exit(1);
58  }
59 
60 
61  if ((prginfo = PAPI_get_executable_info()) == NULL)
62  {
63  fprintf(stderr, "Error in get executable information \n");
64  exit(1);
65  }
66 
67  start = prginfo->address_info.text_start;
68  end = prginfo->address_info.text_end;
69  length = (end - start);
70 
71  /* for PAPI_PROFIL_BUCKET_16 and scale = 65536,
72  profile buffer length == program address length.
73  Larger bucket sizes would increase the buffer length.
74  Smaller scale factors would decrease it.
75  Handle with care...
76  */
77  profbuf = (unsigned short *)malloc(length);
78  if (profbuf == NULL)
79  {
80  fprintf(stderr, "Not enough memory \n");
81  exit(1);
82  }
83  memset(profbuf,0x00,length);
84 
85  /* Creating the eventset */
86  if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
87  ERROR_RETURN(retval);
88 
89  PAPI_event = PAPI_TOT_INS;
90  /* Add Total Instructions Executed to our EventSet */
91  if ( (retval = PAPI_add_event(EventSet, PAPI_event)) != PAPI_OK)
92  ERROR_RETURN(retval);
93 
94  /* Add Total Cycles Executed to our EventSet */
95  if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
96  ERROR_RETURN(retval);
97 
98  /* enable the collection of profiling information */
99  if ((retval = PAPI_profil(profbuf, length, start, 65536, EventSet,
101  ERROR_RETURN(retval);
102 
103  /* let's rock and roll */
104  if ((retval=PAPI_start(EventSet)) != PAPI_OK)
105  ERROR_RETURN(retval);
106 
107  code_to_monitor();
108 
109  if ((retval=PAPI_stop(EventSet, values)) != PAPI_OK)
110  ERROR_RETURN(retval);
111 
112  /* disable the collection of profiling information by setting threshold
113  to 0
114  */
115  if ((retval = PAPI_profil(profbuf, length, start, 65536, EventSet,
116  PAPI_event, 0, PAPI_PROFIL_POSIX)) != PAPI_OK)
117  ERROR_RETURN(retval);
118 
119  printf("-----------------------------------------------------------\n");
120  printf("Text start: %p, Text end: %p, \n",
121  prginfo->address_info.text_start,prginfo->address_info.text_end);
122  printf("Data start: %p, Data end: %p\n",
123  prginfo->address_info.data_start,prginfo->address_info.data_end);
124  printf("BSS start : %p, BSS end: %p\n",
125  prginfo->address_info.bss_start,prginfo->address_info.bss_end);
126 
127  printf("------------------------------------------\n");
128 
129  printf("Test type : \tPAPI_PROFIL_POSIX\n");
130  printf("------------------------------------------\n\n\n");
131  printf("PAPI_profil() hash table.\n");
132  printf("address\t\tflat \n");
133  for (i = 0; i < (int) length/2; i++)
134  {
135  if (profbuf[i])
136  printf("%#lx\t%d \n",
137  (unsigned long) start + (unsigned long) (2 * i), profbuf[i]);
138  }
139 
140  printf("-----------------------------------------\n");
141 
142  retval = 0;
143  for (i = 0; i < (int) length/2; i++)
144  retval = retval || (profbuf[i]);
145  if (retval)
146  printf("Test succeeds! \n");
147  else
148  printf( "No information in buffers\n");
149  /* clean up */
150  PAPI_shutdown();
151 
152  exit(0);
153 }
154 
155 
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
memset(eventId, 0, size)
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
unsigned long long length
Definition: iozone.c:17106
int PAPI_event[2]
Definition: data_range.c:24
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1622
caddr_t text_end
Definition: papi.h:695
start
Definition: iozone.c:22736
#define THRESHOLD
Definition: PAPI_profil.c:11
#define PAPI_NULL
Definition: papi.h:290
#define PAPI_PROFIL_BUCKET_16
Definition: papi.h:398
int EventSet
Definition: data_range.c:25
#define PAPI_TOT_INS
get the executable&#39;s info
Definition: papi.h:704
return PAPI_OK
Definition: linux-nvml.c:458
caddr_t bss_start
Definition: papi.h:698
#define printf
Definition: papi_test.h:125
Return codes and api definitions.
int code_to_monitor()
Definition: PAPI_profil.c:14
int PAPI_library_init(int version)
Definition: papi.c:497
int PAPI_profil(void *buf, unsigned bufsiz, caddr_t offset, unsigned scale, int EventSet, int EventCode, int threshold, int flags)
Definition: papi.c:5364
int i
Definition: fileop.c:140
void PAPI_shutdown(void)
Definition: papi.c:4400
#define PAPI_TOT_CYC
const PAPI_exe_info_t * PAPI_get_executable_info(void)
Definition: papi.c:6040
caddr_t text_start
Definition: papi.h:694
PAPI_address_map_t address_info
Definition: papi.h:706
void * profbuf[5]
Definition: prof_utils.c:28
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
#define ERROR_RETURN(retval)
Definition: PAPI_profil.c:12
caddr_t data_start
Definition: papi.h:696
#define PAPI_PROFIL_POSIX
Definition: papi.h:394
int
Definition: iozone.c:18528
caddr_t bss_end
Definition: papi.h:699
caddr_t data_end
Definition: papi.h:697
#define PAPI_MAX_STR_LEN
Definition: papi.h:463
int PAPI_start(int EventSet)
Definition: papi.c:2053
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long tmp
Definition: iozone.c:12031
#define PAPI_VER_CURRENT
Definition: papi.h:223
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
#define FLOPS
Definition: PAPI_profil.c:10