PAPI  5.7.0.0
libmsr_write_test.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 
11 #include "papi.h"
12 #include "msr/msr_core.h"
13 #include "msr/msr_rapl.h"
14 
15 #define MAX_EVENTS 128
16 
17 char events[MAX_EVENTS][BUFSIZ];
18 char filenames[MAX_EVENTS][BUFSIZ];
19 
20 int ompcpuloadprimes( int limit )
21 {
22  int num, primes=0;
23 #pragma omp parallel for schedule(dynamic) reduction(+ : primes)
24  for (num = 1; num <= limit; num++) {
25  int i = 2;
26  while(i <= num) {
27  if(num % i == 0)
28  break;
29  i++;
30  }
31  if(i == num)
32  primes++;
33  }
34  return primes;
35 }
36 
37 
38 int main (int argc, char **argv)
39 {
40  int retval,cid,rapl_cid=-1,numcmp;
41  int EventSet = PAPI_NULL;
42  long long values[MAX_EVENTS];
43  int i,code,enum_retval;
44  const PAPI_component_info_t *cmpinfo = NULL;
45  long long start_time,write_start_time,write_end_time,read_start_time,read_end_time;
46  char event_name[BUFSIZ];
47  union { long long ll; double dbl; } event_value_union;
48  static int num_events=0;
49  FILE *fileout;
50 
51  /* PAPI Initialization */
53  if ( retval != PAPI_VER_CURRENT ) {
54  fprintf(stderr,"PAPI_library_init failed\n");
55  exit(1);
56  }
57 
58  /* Find the libmsr component */
59  numcmp = PAPI_num_components();
60  for(cid=0; cid<numcmp; cid++) {
61  if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
62  fprintf(stderr,"PAPI_get_component_info failed\n");
63  exit(1);
64  }
65  if (strstr(cmpinfo->name,"libmsr")) {
66  rapl_cid=cid;
67  printf("Found libmsr component at cid %d\n", rapl_cid);
68  if (cmpinfo->disabled) {
69  fprintf(stderr,"No libmsr events found: %s\n", cmpinfo->disabled_reason);
70  exit(1);
71  }
72  break;
73  }
74  }
75 
76  /* Component not found */
77  if (cid==numcmp) {
78  fprintf(stderr,"No libmsr component found\n");
79  exit(1);
80  }
81 
82  /* Find events in the component */
83  code = PAPI_NATIVE_MASK;
84  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
85  while ( enum_retval == PAPI_OK ) {
87  if ( retval != PAPI_OK ) {
88  printf("Error translating %#x\n",code);
89  exit(1);
90  }
91  printf("Found: %s\n",event_name);
92  strncpy(events[num_events],event_name,BUFSIZ);
93  sprintf(filenames[num_events],"results.%s",event_name);
94  num_events++;
95  if (num_events==MAX_EVENTS) {
96  printf("Too many events! %d\n",num_events);
97  exit(1);
98  }
99  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
100  }
101  if (num_events==0) {
102  printf("Error! No libmsr events found!\n");
103  exit(1);
104  }
105 
106  /* Open output file */
107  char fileoutname[]="libmsr_write_test_output.txt";
108  fileout=fopen( fileoutname ,"w" );
109  if ( fileout==NULL) { fprintf( stderr,"Could not open %s\n",fileoutname ); exit(1); }
110 
111  /* Create EventSet */
113  if (retval != PAPI_OK) {
114  fprintf(stderr,"Error creating eventset!\n");
115  }
116 
117  for(i=0;i<num_events;i++) {
119  if (retval != PAPI_OK) fprintf(stderr,"Error adding event %s\n",events[i]);
120  }
121 
122  start_time=PAPI_get_real_nsec();
123 
124  /* Grab the initial values for the events */
126  if (retval != PAPI_OK) { fprintf(stderr,"PAPI_start() failed\n"); exit(1); }
127  /* Initial checking read */
129  if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
130 
131  /* Write a header line */
132  fprintf( fileout, "ACTION TIME-STAMP TIME-FOR-UNIT-WORK TIME-OVERHEAD-RW\t" );
133  for(i=0; i<num_events; i++)
134  fprintf( fileout, "%s ", events[i]+9 );
135  fprintf( fileout, "\n" );
136 
137  /* Read the initial values */
139  if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
140  fprintf( fileout, "INIT %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 );
141  fprintf( fileout, "%8.3e ", 0.0);
142  for(i=0; i<num_events; i++) {
143  event_value_union.ll = values[i];
144  fprintf( fileout, "%8.3f ", event_value_union.dbl );
145  }
146  fprintf( fileout, "\n" );
147 
148  int rpt=0;
149  int limit1base=10;
150  int limit2base=10;
151  while(rpt++<200) {
152  //printf("rpt %d\n", rpt);
153 
154  if ( rpt % 10 == 0 ) {
155  for (i=0; i<num_events; i++) {
156  event_value_union.ll = values[i];
157  if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=limit1base+(rpt/2);
158  else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=1.0;
159  else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=limit2base+(rpt/2);
160  else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=1.0;
161  else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=limit1base+(rpt/2);
162  else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=1.0;
163  else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=limit2base+(rpt/2);
164  else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=1.0;
165  else event_value_union.dbl=PAPI_NULL;
166  values[i]=event_value_union.ll;
167  }
168 
169  write_start_time=PAPI_get_real_nsec();
171  write_end_time=PAPI_get_real_nsec();
172  if (retval != PAPI_OK) { fprintf(stderr,"PAPI_write() failed\n"); exit(1); }
173 
174  fprintf( fileout, "SET %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 );
175  fprintf( fileout, "%8.3e ", ((double)(write_end_time-write_start_time))/1.0e9 );
176  for(i=0; i<num_events; i++) {
177  event_value_union.ll = values[i];
178  fprintf( fileout, "%8.3f ", event_value_union.dbl );
179  }
180  fprintf( fileout, "\n" );
181  }
182 
183  /* DO SOME WORK TO USE ENERGY */
184  //usleep(100000);
185  double work_start_time=PAPI_get_real_nsec();
186  ompcpuloadprimes( 100000 );
187  double work_time=PAPI_get_real_nsec()-work_start_time;
188  //printf("primescount %d\n", primescount);
189 
190  /* Read and output the values */
191  read_start_time=PAPI_get_real_nsec();
193  read_end_time=PAPI_get_real_nsec();
194  if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
195  fprintf( fileout, "READ %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, work_time/1.0e9 );
196  fprintf( fileout, "%8.3e ", ((double)(read_end_time-read_start_time))/1.0e9 );
197  for(i=0; i<num_events; i++) {
198  event_value_union.ll = values[i];
199  fprintf( fileout, "%8.3f ", event_value_union.dbl );
200  }
201  fprintf( fileout, "\n" );
202  }
203 
205  return 0;
206 }
207 
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define PAPI_NATIVE_MASK
char disabled_reason[PAPI_MAX_STR_LEN]
Definition: papi.h:637
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:796
int PAPI_write(int EventSet, long long *values)
Definition: papi.c:2813
if(file==NULL) goto out
static int num_events
int PAPI_num_components(void)
Definition: papi.c:4387
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int EventSet
int retval
Definition: zero_fork.c:53
Return codes and api definitions.
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
int PAPI_library_init(int version)
Definition: papi.c:500
char filenames[MAX_EVENTS][BUFSIZ]
#define PAPI_NULL
Definition: fpapi.h:13
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:630
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1357
int ompcpuloadprimes(int limit)
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:915
long long PAPI_get_real_nsec(void)
Definition: papi.c:6237
int main(int argc, char **argv)
int PAPI_read(int EventSet, long long *values)
Definition: papi.c:2559
int PAPI_start(int EventSet)
Definition: papi.c:2096
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
void exit()
#define MAX_EVENTS
char events[MAX_EVENTS][BUFSIZ]
int i
Definition: fileop.c:140