PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
byte_profile.c
Go to the documentation of this file.
1 /*
2 * File: byte_profile.c
3 * Author: Dan Terpstra
4 * terpstra@cs.utk.edu
5 * Mods: Maynard Johnson
6 * maynardj@us.ibm.com
7 */
8 
9 /* This file profiles multiple events with byte level address resolution.
10  It's patterned after code suggested by John Mellor-Crummey, Rob Fowler,
11  and Nathan Tallent.
12  It is intended to illustrate the use of Multiprofiling on a very tight
13  block of code at byte level resolution of the instruction addresses.
14 */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "papi.h"
21 #include "papi_test.h"
22 #include "prof_utils.h"
23 
24 #include "do_loops.h"
25 
26 #define PROFILE_ALL
27 
28 static const PAPI_hw_info_t *hw_info;
29 
30 static int num_events = 0;
31 
32 #define N (1 << 23)
33 #define T (10)
34 
35 double aa[N], bb[N];
36 double s = 0, s2 = 0;
37 
38 static void
39 cleara( double a[N] )
40 {
41  int i;
42 
43  for ( i = 0; i < N; i++ ) {
44  a[i] = 0;
45  }
46 }
47 
48 static int
49 my_dummy( int i )
50 {
51  return ( i + 1 );
52 }
53 
54 static void
55 my_main( void )
56 {
57  int i, j;
58 
59  for ( j = 0; j < T; j++ ) {
60  for ( i = 0; i < N; i++ ) {
61  bb[i] = 0;
62  }
63  cleara( aa );
64  memset( aa, 0, sizeof ( aa ) );
65  for ( i = 0; i < N; i++ ) {
66  s += aa[i] * bb[i];
67  s2 += aa[i] * aa[i] + bb[i] * bb[i];
68  }
69  }
70 }
71 
72 static int
73 do_profile( caddr_t start, unsigned long plength, unsigned scale, int thresh,
74  int bucket, unsigned int mask ) {
75 
76  int i, retval;
77  unsigned long blength;
78  int num_buckets,j=0;
79 
80  int num_bufs = num_events;
81  int event = num_events;
82 
84  char header[BUFSIZ];
85 
86  strncpy(header,"address\t\t",BUFSIZ);
87 
88  //= "address\t\t\tcyc\tins\tfp_ins\n";
89 
90  for(i=0;i<MAX_TEST_EVENTS;i++) {
91  if (mask & test_events[i].mask) {
92  events[j]=test_events[i].event;
93 
94  if (events[j]==PAPI_TOT_CYC) {
95  strncat(header,"\tcyc",BUFSIZ-1);
96  }
97  if (events[j]==PAPI_TOT_INS) {
98  strncat(header,"\tins",BUFSIZ-1);
99  }
100  if (events[j]==PAPI_FP_INS) {
101  strncat(header,"\tfp_ins",BUFSIZ-1);
102  }
103  if (events[j]==PAPI_FP_OPS) {
104  strncat(header,"\tfp_ops",BUFSIZ-1);
105  }
106  if (events[j]==PAPI_L2_TCM) {
107  strncat(header,"\tl2_tcm",BUFSIZ-1);
108  }
109 
110  j++;
111 
112  }
113  }
114 
115  strncat(header,"\n",BUFSIZ-1);
116 
117 
118 
119  blength = prof_size( plength, scale, bucket, &num_buckets );
120  prof_alloc( num_bufs, blength );
121 
122  if ( !TESTS_QUIET ) printf( "Overall event counts:\n" );
123 
124  for ( i = 0; i < num_events; i++ ) {
125  if ( ( retval =
126  PAPI_profil( profbuf[i], ( unsigned int ) blength, start, scale,
127  EventSet, events[i], thresh,
128  PAPI_PROFIL_POSIX | bucket ) ) != PAPI_OK ) {
129  if (retval == PAPI_EINVAL) {
130  test_warn( __FILE__, __LINE__, "Trying to profile with derived event", 1);
131  num_events=i;
132  break;
133  }
134  else {
135  printf("Failed with event %d %#x\n",i,events[i]);
136  test_fail( __FILE__, __LINE__, "PAPI_profil", retval );
137  }
138  }
139  }
140 
141  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
142  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
143 
144  my_main( );
145 
146  if ( ( retval = PAPI_stop( EventSet, values[0] ) ) != PAPI_OK )
147  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
148 
149  if ( !TESTS_QUIET ) {
150  printf( TAB1, "PAPI_TOT_CYC:", ( values[0] )[--event] );
151  if ( strcmp( hw_info->model_string, "POWER6" ) != 0 ) {
152  printf( TAB1, "PAPI_TOT_INS:", ( values[0] )[--event] );
153  }
154 #if defined(__powerpc__)
155  printf( TAB1, "PAPI_FP_INS", ( values[0] )[--event] );
156 #else
157  if ( strcmp( hw_info->model_string, "Intel Pentium III" ) != 0 ) {
158  printf( TAB1, "PAPI_FP_OPS:", ( values[0] )[--event] );
159  printf( TAB1, "PAPI_L2_TCM:", ( values[0] )[--event] );
160  }
161 #endif
162  }
163 
164  for ( i = 0; i < num_events; i++ ) {
165  if ( ( retval =
166  PAPI_profil( profbuf[i], ( unsigned int ) blength, start, scale,
167  EventSet, events[i], 0,
168  PAPI_PROFIL_POSIX ) ) != PAPI_OK )
169  test_fail( __FILE__, __LINE__, "PAPI_profil", retval );
170  }
171 
172  if (!TESTS_QUIET) {
173  prof_head( blength, bucket, num_buckets, header );
174  prof_out( start, num_events, bucket, num_buckets, scale );
175  }
176  retval = prof_check( num_bufs, bucket, num_buckets );
177  for ( i = 0; i < num_bufs; i++ ) {
178  free( profbuf[i] );
179  }
180  return retval;
181 }
182 
183 
184 
185 int
186 main( int argc, char **argv )
187 {
188  long length;
189  int mask;
190  int retval;
191  const PAPI_exe_info_t *prginfo;
192  caddr_t start, end;
193  int quiet;
194 
195  /* Set TESTS_QUIET variable */
196  quiet=tests_quiet( argc, argv );
197 
199  if (retval != PAPI_VER_CURRENT) {
200  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
201  }
202 
203  if ( ( prginfo = PAPI_get_executable_info( ) ) == NULL ) {
204  test_fail( __FILE__, __LINE__, "PAPI_get_executable_info", 1 );
205  }
206 
207  hw_info = PAPI_get_hardware_info( );
208  if ( hw_info == NULL ) {
209  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
210  }
211 
213 
214 #if defined(__powerpc__)
215  if ( strcmp( hw_info->model_string, "POWER6" ) == 0 )
216  mask = MASK_TOT_CYC | MASK_FP_INS;
217  else
219 #endif
220 
221 #if defined(ITANIUM2)
223 #endif
224  EventSet = add_test_events( &num_events, &mask, 0 );
225  if (num_events==0) {
226  if (!quiet) printf("Trouble adding events\n");
227  test_skip(__FILE__,__LINE__,"add_test_events",2);
228  }
230 
231 /* profile the cleara and my_main address space */
232  start = ( caddr_t ) cleara;
233  end = ( caddr_t ) my_dummy;
234 
235 /* Itanium and PowerPC64 processors return function descriptors instead
236  * of function addresses. You must dereference the descriptor to get the address.
237 */
238 #if defined(ITANIUM1) || defined(ITANIUM2) \
239  || (defined(__powerpc64__) && (_CALL_ELF != 2))
240  start = ( caddr_t ) ( ( ( struct fdesc * ) start )->ip );
241  end = ( caddr_t ) ( ( ( struct fdesc * ) end )->ip );
242  /* PPC64 Big Endian is ELF version 1 which uses function descriptors.
243  * PPC64 Little Endian is ELF version 2 which does not use
244  * function descriptors
245  */
246 #endif
247 
248  /* call dummy so it doesn't get optimized away */
249  retval = my_dummy( 1 );
250 
251  length = end - start;
252  if ( length < 0 )
253  test_fail( __FILE__, __LINE__, "Profile length < 0!", ( int ) length );
254 
255  if (!quiet) {
256  prof_print_address( "Test case byte_profile: "
257  "Multi-event profiling at byte resolution.\n",
258  prginfo );
259  prof_print_prof_info( start, end, THRESHOLD, event_name );
260  }
261 
262  retval = do_profile( start, ( unsigned ) length,
263  FULL_SCALE * 2, THRESHOLD,
264  PAPI_PROFIL_BUCKET_32, mask );
265 
266  remove_test_events( &EventSet, mask );
267 
268  if (retval == 0) {
269  test_fail( __FILE__, __LINE__, "No information in buffers", 1 );
270  }
271 
272  test_pass( __FILE__ );
273 
274  return 0;
275 }
276 
277 
278 
279 
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
unsigned long long length
Definition: iozone.c:17106
void test_pass(const char *filename)
Definition: test_utils.c:432
#define PAPI_L2_TCM
Hardware info structure.
Definition: papi.h:780
#define PAPI_FP_OPS
struct in_addr * ip
Definition: iozone.c:20416
start
Definition: iozone.c:22736
#define T
Definition: byte_profile.c:33
#define TAB1
Definition: papi_test.h:88
#define PAPI_TOT_INS
static void my_main(void)
Definition: byte_profile.c:55
#define MAX_TEST_EVENTS
Definition: papi_test.h:42
int EventSet
get the executable&#39;s info
Definition: papi.h:707
return PAPI_OK
Definition: linux-nvml.c:497
double aa[N]
Definition: byte_profile.c:35
return PAPI_EINVAL
Definition: linux-nvml.c:436
void test_warn(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:522
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
char events[MAX_EVENTS][BUFSIZ]
char ** argv
Definition: iozone.c:1610
unsigned long prof_size(unsigned long plength, unsigned scale, int bucket, int *num_buckets)
Definition: prof_utils.c:310
#define MASK_FP_INS
Definition: papi_test.h:38
int PAPI_library_init(int version)
Definition: papi.c:500
int PAPI_profil(void *buf, unsigned bufsiz, caddr_t offset, unsigned scale, int EventSet, int EventCode, int threshold, int flags)
Definition: papi.c:5438
int i
Definition: fileop.c:140
void prof_out(caddr_t start, int n, int bucket, int num_buckets, unsigned int scale)
Definition: prof_utils.c:202
static void cleara(double a[N])
Definition: byte_profile.c:39
#define FULL_SCALE
Definition: prof_utils.h:20
int quiet
Definition: rapl_overflow.c:18
#define PAPI_TOT_CYC
const PAPI_exe_info_t * PAPI_get_executable_info(void)
Definition: papi.c:6114
free(dummyfile[xx])
void * profbuf[5]
Definition: prof_utils.c:33
s
Definition: iozone.c:20289
static int my_dummy(int i)
Definition: byte_profile.c:49
#define PAPI_PROFIL_BUCKET_32
Definition: papi.h:401
double bb[N]
Definition: byte_profile.c:35
#define MASK_TOT_CYC
Definition: papi_test.h:40
double s2
Definition: byte_profile.c:36
#define MASK_FP_OPS
Definition: papi_test.h:24
#define N
Definition: byte_profile.c:32
#define MASK_TOT_INS
Definition: papi_test.h:39
void prof_head(unsigned long blength, int bucket, int num_buckets, const char *header)
Definition: prof_utils.c:183
struct test_events_t test_events[]
Definition: test_utils.c:130
#define PAPI_PROFIL_POSIX
Definition: papi.h:396
printf("\tTry: -i 0 -i 1 \n\n")
int TESTS_QUIET
Definition: test_utils.c:18
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
void prof_print_address(const char *title, const PAPI_exe_info_t *prginfo)
Definition: prof_utils.c:69
void prof_print_prof_info(caddr_t start, caddr_t end, int threshold, char *event_name)
Definition: prof_utils.c:93
#define MASK_L2_TCM
Definition: papi_test.h:34
static int num_events
Definition: byte_profile.c:30
#define MASK_L1_DCM
Definition: papi_test.h:35
int prof_check(int n, int bucket, int num_buckets)
Definition: prof_utils.c:272
unsigned int event
Definition: papi_test.h:46
char model_string[PAPI_MAX_STR_LEN]
Definition: papi.h:790
static int do_profile(caddr_t start, unsigned long plength, unsigned scale, int thresh, int bucket, unsigned int mask)
Definition: byte_profile.c:73
int PAPI_start(int EventSet)
Definition: papi.c:2096
#define THRESHOLD
Definition: earprofile.c:37
long j
Definition: iozone.c:19135
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6185
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:46
#define PAPI_VER_CURRENT
Definition: papi.h:225
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:28
int main(int argc, char **argv)
List all appio events codes and names.
int remove_test_events(int *EventSet, int mask)
Definition: test_utils.c:201
void prof_alloc(int num, unsigned long blength)
Definition: prof_utils.c:140
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:38
int add_test_events(int *number, int *mask, int allow_derived)
Definition: test_utils.c:152
#define PAPI_FP_INS