PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
examples/sprofile.c
Go to the documentation of this file.
1 /* This program shows how to use PAPI_sprofil */
2 
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <string.h>
10 
11 #include "papi.h" /* This needs to be included every time you use PAPI */
12 
13 #define NUM_FLOPS 20000000
14 #define NUM_ITERS 100000
15 #define THRESHOLD 100000
16 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
17 
18 #if (defined(linux) && defined(__ia64__)) || (defined(_AIX))
19 #define DO_FLOPS1 (caddr_t)(*(void **)do_flops1)
20 #define DO_FLOPS2 (caddr_t)(*(void **)do_flops2)
21 #else
22 #define DO_FLOPS1 (caddr_t)(do_flops1)
23 #define DO_FLOPS2 (caddr_t)(do_flops2)
24 #endif
25 
26 void do_flops2(int);
27 volatile double t1 = 0.8, t2 = 0.9;
28 void do_flops1(int n)
29 {
30  int i;
31  double c = 22222.11;
32 
33  for (i = 0; i < n; i++)
34  c -= t1 * t2;
35 }
36 
37 void do_both(int n)
38 {
39  int i;
40  const int flops2 = NUM_FLOPS / n;
41  const int flops1 = NUM_FLOPS / n;
42 
43  for (i = 0; i < n; i++)
44  {
45  do_flops1(flops1);
46  do_flops2(flops2);
47  }
48 }
49 
50 int main(int argc, char **argv)
51 {
52  int i , PAPI_event;
53  int EventSet = PAPI_NULL;
54  unsigned short *profbuf;
55  unsigned short *profbuf2;
56  unsigned short *profbuf3;
57  unsigned long length;
58  caddr_t start, end;
59  long long values[2];
60  const PAPI_exe_info_t *prginfo = NULL;
61  PAPI_sprofil_t sprof[3];
62  int retval;
63 
64  /* initializaion */
66  {
67  printf("Library initialization error! \n");
68  exit(1);
69  }
70 
71  if ((prginfo = PAPI_get_executable_info()) == NULL)
72  ERROR_RETURN(1);
73 
74  start = prginfo->address_info.text_start;
75  end = prginfo->address_info.text_end;
76  length = (end - start)/sizeof(unsigned short) * sizeof(unsigned short);
77  printf("start= %p end =%p \n", start, end);
78 
79  profbuf = (unsigned short *) malloc(length);
80  if (profbuf == NULL)
82 
83  memset(profbuf, 0x00, length );
84 
85  profbuf2 = (unsigned short *) malloc(length);
86  if (profbuf2 == NULL)
88 
89  memset(profbuf2, 0x00, length );
90 
91  profbuf3 = (unsigned short *) malloc(1 * sizeof(unsigned short));
92  if (profbuf3 == NULL)
94 
95  memset(profbuf3, 0x00, 1 * sizeof(unsigned short));
96 
97  /* First half */
98  sprof[0].pr_base = profbuf;
99  sprof[0].pr_size = length / 2;
100  sprof[0].pr_off = DO_FLOPS2;
101  fprintf(stderr, "do_flops is at %p %lx\n", &do_flops2, strtoul(sprof[0].pr_off,NULL,0));
102 
103  sprof[0].pr_scale = 65536; /* constant needed by PAPI_sprofil */
104  /* Second half */
105  sprof[1].pr_base = profbuf2;
106  sprof[1].pr_size = length / 2;
107  sprof[1].pr_off = DO_FLOPS1;
108  fprintf(stderr, "do_flops1 is at %p %lx\n", &do_flops1, strtoul(sprof[1].pr_off,NULL,0));
109  sprof[1].pr_scale = 65536; /* constant needed by PAPI_sprofil */
110 
111  /* Overflow bin */
112  sprof[2].pr_base = profbuf3;
113  sprof[2].pr_size = 1;
114  sprof[2].pr_off = 0;
115  sprof[2].pr_scale = 0x2; /* constant needed by PAPI_sprofil */
116 
117  /* Creating the eventset */
118  if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
119  ERROR_RETURN(retval);
120 
121  PAPI_event = PAPI_TOT_CYC;
122  /* Add Total Instructions Executed to our EventSet */
123  if ( (retval = PAPI_add_event(EventSet, PAPI_event)) != PAPI_OK)
124  ERROR_RETURN(retval);
125 
126  /* Add Total Instructions Executed to our EventSet */
127  if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
128  ERROR_RETURN(retval);
129 
130  /* set profile flag */
131  if ((retval = PAPI_sprofil(sprof, 3, EventSet, PAPI_event, THRESHOLD,
133  ERROR_RETURN(retval);
134 
135  if ((retval = PAPI_start(EventSet)) != PAPI_OK)
136  ERROR_RETURN(retval);
137 
139 
140  if ((retval = PAPI_stop(EventSet, values)) != PAPI_OK)
141  ERROR_RETURN(retval);
142 
143  /* to clear the profile flag before removing the events */
144  if ((retval = PAPI_sprofil(sprof, 3, EventSet, PAPI_event, 0,
146  ERROR_RETURN(retval);
147 
148  /* free the resources hold by PAPI */
149  PAPI_shutdown();
150 
151  printf("Test case: PAPI_sprofil()\n");
152  printf("---------Buffer 1--------\n");
153  for (i = 0; i < length / 2; i++)
154  {
155  if (profbuf[i])
156  printf("%#lx\t%d\n", strtoul(DO_FLOPS2,NULL,0) + 2 * i, profbuf[i]);
157  }
158  printf("---------Buffer 2--------\n");
159  for (i = 0; i < length / 2; i++)
160  {
161  if (profbuf2[i])
162  printf("%#lx\t%d\n", strtoul(DO_FLOPS1,NULL,0) + 2 * i, profbuf2[i]);
163  }
164  printf("-------------------------\n");
165  printf("%u samples fell outside the regions.\n", *profbuf3);
166 
167  exit(0);
168 }
169 
170 /* Declare a and b to be volatile.
171  This is to try to keep the
172  compiler from optimizing the loop */
173 volatile double a = 0.5, b = 2.2;
174 void do_flops2(int n)
175 {
176  int i;
177  double c = 0.11;
178 
179  for (i = 0; i < n; i++)
180  c += a * b;
181 }
182 
memset(eventId, 0, size)
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
#define NUM_FLOPS
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 PAPI_NULL
Definition: papi.h:290
int EventSet
Definition: data_range.c:25
#define PAPI_TOT_INS
caddr_t pr_off
Definition: papi.h:581
get the executable&#39;s info
Definition: papi.h:704
return PAPI_OK
Definition: linux-nvml.c:458
volatile double t2
int PAPI_sprofil(PAPI_sprofil_t *prof, int profcnt, int EventSet, int EventCode, int threshold, int flags)
Definition: papi.c:4970
#define printf
Definition: papi_test.h:125
volatile double t1
double c
Definition: multiplex.c:22
int int argc
Definition: iozone.c:1609
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:37
unsigned pr_scale
Definition: papi.h:582
Return codes and api definitions.
char ** argv
Definition: iozone.c:1610
unsigned pr_size
Definition: papi.h:580
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
#define NUM_ITERS
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
#define PAPI_ESYS
Definition: papi.h:253
void do_flops2(int)
void * profbuf[5]
Definition: prof_utils.c:28
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
#define ERROR_RETURN(retval)
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:38
#define PAPI_PROFIL_POSIX
Definition: papi.h:394
void do_both(int n)
void do_flops1(int n)
#define DO_FLOPS1
void * pr_base
Definition: papi.h:579
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
#define PAPI_VER_CURRENT
Definition: papi.h:223
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
#define DO_FLOPS2
int n
Definition: mendes-alt.c:164
#define THRESHOLD