PAPI  5.7.0.0
powercap_basic.c
Go to the documentation of this file.
1 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 
13 #include "papi.h"
14 #include "papi_test.h"
15 
16 #define MAX_powercap_EVENTS 64
17 
18 #ifdef BASIC_TEST
19 
20 void run_test( int quiet )
21 {
22  if ( !quiet ) {
23  printf( "Sleeping 1 second...\n" );
24  }
25  sleep( 1 );
26 }
27 
28 #else /* NOT BASIC_TEST */
29 
30 #define MATRIX_SIZE 1024
31 static double a[MATRIX_SIZE][MATRIX_SIZE];
32 static double b[MATRIX_SIZE][MATRIX_SIZE];
33 static double c[MATRIX_SIZE][MATRIX_SIZE];
34 
35 /* Naive matrix multiply */
36 void run_test( int quiet )
37 {
38  double s;
39  int i,j,k;
40 
41  if ( !quiet ) printf( "Doing a naive %dx%d MMM...\n",MATRIX_SIZE,MATRIX_SIZE );
42 
43  for( i=0; i<MATRIX_SIZE; i++ ) {
44  for( j=0; j<MATRIX_SIZE; j++ ) {
45  a[i][j]=( double )i*( double )j;
46  b[i][j]=( double )i/( double )( j+5 );
47  }
48  }
49 
50  for( j=0; j<MATRIX_SIZE; j++ ) {
51  for( i=0; i<MATRIX_SIZE; i++ ) {
52  s=0;
53  for( k=0; k<MATRIX_SIZE; k++ ) {
54  s+=a[i][k]*b[k][j];
55  }
56  c[i][j] = s;
57  }
58  }
59 
60  s=0.0;
61  for( i=0; i<MATRIX_SIZE; i++ ) {
62  for( j=0; j<MATRIX_SIZE; j++ ) {
63  s+=c[i][j];
64  }
65  }
66 
67  if ( !quiet ) printf( "Matrix multiply sum: s=%lf\n",s );
68 }
69 
70 #endif
71 
72 int main ( int argc, char **argv )
73 {
74  (void) argv;
75  (void) argc;
76  int retval,cid,powercap_cid=-1,numcmp;
77  int EventSet = PAPI_NULL;
78  long long *values;
79  int num_events=0;
80  int code;
81  char event_names[MAX_powercap_EVENTS][PAPI_MAX_STR_LEN];
82  char event_descrs[MAX_powercap_EVENTS][PAPI_MAX_STR_LEN];
85  int r,i;
86 
87  const PAPI_component_info_t *cmpinfo = NULL;
88  PAPI_event_info_t evinfo;
89  long long before_time,after_time;
90  double elapsed_time;
91 
92  /* Set TESTS_QUIET variable */
93  tests_quiet( argc, argv );
94 
95  /* Currently unimplemented? */
96 #if 0
97  int do_wrap = 0;
98  if ( argc > 1 ) {
99  if ( strstr( argv[1], "-w" ) ) {
100  do_wrap = 1;
101  }
102  }
103 #endif
104 
105  /* PAPI Initialization */
107  if ( retval != PAPI_VER_CURRENT )
108  test_fail( __FILE__, __LINE__,"PAPI_library_init failed\n",retval );
109 
110  if ( !TESTS_QUIET ) printf( "Trying all powercap events\n" );
111 
112  numcmp = PAPI_num_components();
113 
114  for( cid=0; cid<numcmp; cid++ ) {
115 
116  if ( ( cmpinfo = PAPI_get_component_info( cid ) ) == NULL )
117  test_fail( __FILE__, __LINE__,"PAPI_get_component_info failed\n", 0 );
118 
119  if ( strstr( cmpinfo->name,"powercap" ) ) {
120  powercap_cid=cid;
121  if ( !TESTS_QUIET ) printf( "Found powercap component at cid %d\n",powercap_cid );
122  if ( cmpinfo->disabled ) {
123  if ( !TESTS_QUIET ) {
124  printf( "powercap component disabled: %s\n",
125  cmpinfo->disabled_reason );
126  }
127  test_skip( __FILE__,__LINE__,"powercap component disabled",0 );
128  }
129  break;
130  }
131  }
132 
133  /* Component not found */
134  if ( cid==numcmp )
135  test_skip( __FILE__,__LINE__,"No powercap component found\n",0 );
136 
137  /* Skip if component has no counters */
138  if ( cmpinfo->num_cntrs==0 )
139  test_skip( __FILE__,__LINE__,"No counters in the powercap component\n",0 );
140 
141  /* Create EventSet */
143  if ( retval != PAPI_OK )
144  test_fail( __FILE__, __LINE__, "PAPI_create_eventset()",retval );
145 
146  /* Add all events */
147  code = PAPI_NATIVE_MASK;
148  r = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, powercap_cid );
149  while ( r == PAPI_OK ) {
150  retval = PAPI_event_code_to_name( code, event_names[num_events] );
151  if ( retval != PAPI_OK )
152  test_fail( __FILE__, __LINE__,"Error from PAPI_event_code_to_name", retval );
153 
154  retval = PAPI_get_event_info( code,&evinfo );
155  if ( retval != PAPI_OK )
156  test_fail( __FILE__, __LINE__, "Error getting event info\n",retval );
157 
158  strncpy( event_descrs[num_events],evinfo.long_descr,sizeof( event_descrs[0] )-1 );
159  strncpy( units[num_events],evinfo.units,sizeof( units[0] )-1 );
160  // buffer must be null terminated to safely use strstr operation on it below
161  units[num_events][sizeof( units[0] )-1] = '\0';
162  data_type[num_events] = evinfo.data_type;
163  retval = PAPI_add_event( EventSet, code );
164 
165  if ( retval != PAPI_OK )
166  break; /* We've hit an event limit */
167  num_events++;
168 
169  r = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, powercap_cid );
170  }
171 
172  values=calloc( num_events,sizeof( long long ) );
173  if ( values==NULL )
174  test_fail( __FILE__, __LINE__,"No memory",retval );
175 
176  if ( !TESTS_QUIET ) printf( "\nStarting measurements...\n\n" );
177 
178  /* Start Counting */
179  before_time=PAPI_get_real_nsec();
181  if ( retval != PAPI_OK )
182  test_fail( __FILE__, __LINE__, "PAPI_start()",retval );
183 
184  /* Run test */
186 
187  /* Stop Counting */
188  after_time=PAPI_get_real_nsec();
190  if ( retval != PAPI_OK )
191  test_fail( __FILE__, __LINE__, "PAPI_stop()",retval );
192 
193  elapsed_time=( ( double )( after_time-before_time ) )/1.0e9;
194 
195  if ( !TESTS_QUIET ) {
196  printf( "\nStopping measurements, took %.3fs, gathering results...\n\n", elapsed_time );
197 
198  printf( "\n" );
199  printf( "scaled energy measurements:\n" );
200  for( i=0; i<num_events; i++ ) {
201  if ( strstr( event_names[i],"ENERGY_UJ" ) ) {
202  if ( data_type[i] == PAPI_DATATYPE_UINT64 ) {
203  printf( "%-45s%-20s%4.6f J (Average Power %.1fW)\n",
204  event_names[i], event_descrs[i],
205  ( double )values[i]/1.0e6,
206  ( ( double )values[i]/1.0e6 )/elapsed_time );
207  }
208  }
209  }
210 
211  printf( "\n" );
212  printf( "energy counts:\n" );
213  for( i=0; i<num_events; i++ ) {
214  if ( strstr( event_names[i],"ENERGY_UJ" ) ) {
215  if ( data_type[i] == PAPI_DATATYPE_UINT64 ) {
216  printf( "%-45s%-20s%12lld\t%#08llx\n", event_names[i],
217  event_descrs[i],
218  values[i], values[i] );
219  }
220  }
221  }
222 
223  printf( "\n" );
224  printf( "long term time window values:\n" );
225  for( i=0; i<num_events; i++ ) {
226  if ( strstr( event_names[i],"TIME_WINDOW_A_US" ) ) {
227  if ( data_type[i] == PAPI_DATATYPE_UINT64 ) {
228  printf( "%-45s%-20s%4f (secs)\n",
229  event_names[i], event_descrs[i],
230  ( double )values[i]/1.0e6 );
231  }
232  }
233  }
234 
235  printf( "\n" );
236  printf( "short term time window values:\n" );
237  for( i=0; i<num_events; i++ ) {
238  if ( strstr( event_names[i],"TIME_WINDOW_B_US" ) ) {
239  if ( data_type[i] == PAPI_DATATYPE_UINT64 ) {
240  printf( "%-45s%-20s%4f (secs)\n",
241  event_names[i], event_descrs[i],
242  ( double )values[i]/1.0e6 );
243  }
244  }
245  }
246 
247  printf( "\n" );
248  printf( "long term power limit:\n" );
249  for( i=0; i<num_events; i++ ) {
250  if ( strstr( event_names[i],"POWER_LIMIT_A_UW" ) ) {
251  if ( data_type[i] == PAPI_DATATYPE_UINT64 ) {
252  printf( "%-45s%-20s%4f (watts)\n",
253  event_names[i], event_descrs[i],
254  ( double )values[i]/1.0e6 );
255  }
256  }
257  }
258 
259  printf( "\n" );
260  printf( "short term power limit:\n" );
261  for( i=0; i<num_events; i++ ) {
262  if ( strstr( event_names[i],"POWER_LIMIT_B_UW" ) ) {
263  if ( data_type[i] == PAPI_DATATYPE_UINT64 ) {
264  printf( "%-45s%-20s%4f (watts)\n",
265  event_names[i], event_descrs[i],
266  ( double )values[i]/1.0e6 );
267  }
268  }
269  }
270 
271  }
272 
273  /* Done, clean up */
275  if ( retval != PAPI_OK )
276  test_fail( __FILE__, __LINE__,"PAPI_cleanup_eventset()",retval );
277 
279  if ( retval != PAPI_OK )
280  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset()",retval );
281 
282  test_pass( __FILE__ );
283 
284  return 0;
285 }
286 
#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
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:976
static int num_events
int PAPI_num_components(void)
Definition: papi.c:4387
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:970
int EventSet
static double b[MATRIX_SIZE][MATRIX_SIZE]
int retval
Definition: zero_fork.c:53
char units[MAX_EVENTS][BUFSIZ]
Definition: powercap_plot.c:15
Return codes and api definitions.
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
int PAPI_library_init(int version)
Definition: papi.c:500
void run_test(int quiet)
int quiet
Definition: rapl_overflow.c:18
#define PAPI_MIN_STR_LEN
Definition: fpapi.h:41
#define MAX_powercap_EVENTS
Tests basic functionality of powercap component.
#define PAPI_NULL
Definition: fpapi.h:13
double s
Definition: byte_profile.c:36
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 PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:915
static double c[MATRIX_SIZE][MATRIX_SIZE]
#define MATRIX_SIZE
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
long long PAPI_get_real_nsec(void)
Definition: papi.c:6237
int data_type[MAX_EVENTS]
Definition: powercap_plot.c:16
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
static double a[MATRIX_SIZE][MATRIX_SIZE]
int PAPI_start(int EventSet)
Definition: papi.c:2096
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
int main(int argc, char **argv)
int i
Definition: fileop.c:140
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43