PAPI 7.1.0.0
Loading...
Searching...
No Matches
host_micpower_plot.c
Go to the documentation of this file.
1
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10#include "papi.h"
11
12#define MAX_DEVICES (32)
13#define EVENTS_PER_DEVICE 10
14
15#define MAX_EVENTS (MAX_DEVICES*EVENTS_PER_DEVICE)
16
17char events[MAX_EVENTS][BUFSIZ];
18char filenames[MAX_EVENTS][BUFSIZ];
19
21
22static int num_events=0;
23
24int main (int argc, char **argv)
25{
26
27 int retval,cid,host_micpower_cid=-1,numcmp;
28 int EventSet = PAPI_NULL;
29 long long values[MAX_EVENTS];
30 int i,code,enum_retval;
31 const PAPI_component_info_t *cmpinfo = NULL;
32 long long start_time,before_time,after_time;
33 double elapsed_time,total_time;
34 double energy = 0.0;
35 char event_name[BUFSIZ];
36
37 /* PAPI Initialization */
39 if ( retval != PAPI_VER_CURRENT ) {
40 fprintf(stderr,"PAPI_library_init failed\n");
41 exit(1);
42 }
43
44 numcmp = PAPI_num_components();
45
46 for(cid=0; cid<numcmp; cid++) {
47
48 if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
49 fprintf(stderr,"PAPI_get_component_info failed\n");
50 exit(1);
51 }
52
53 if (strstr(cmpinfo->name,"host_micpower")) {
54 host_micpower_cid=cid;
55 printf("Found host_micpower component at cid %d\n", host_micpower_cid);
56
57 if (cmpinfo->disabled) {
58 fprintf(stderr,"No host_micpower events found: %s\n",
59 cmpinfo->disabled_reason);
60 exit(1);
61 }
62 break;
63 }
64 }
65
66 /* Component not found */
67 if (cid==numcmp) {
68 fprintf(stderr,"No host_micpower component found\n");
69 exit(1);
70 }
71
72 /* Find Events */
73 code = PAPI_NATIVE_MASK;
74
75 enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
76
77 while ( enum_retval == PAPI_OK ) {
78
80 if ( retval != PAPI_OK ) {
81 printf("Error translating %#x\n",code);
82 exit(1);
83 }
84
85 printf("Found: %s\n",event_name);
86 strncpy(events[num_events],event_name,BUFSIZ);
87 sprintf(filenames[num_events],"results.%s",event_name);
88 num_events++;
89
91 printf("Too many events! %d\n",num_events);
92 exit(1);
93 }
94
95 enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
96
97 }
98
99 if (num_events==0) {
100 printf("Error! No host_micpower events found!\n");
101 exit(1);
102 }
103
104 /* Open output files */
105 for(i=0;i<num_events;i++) {
106 fff[i]=fopen(filenames[i],"w");
107 if (fff[i]==NULL) {
108 fprintf(stderr,"Could not open %s\n",filenames[i]);
109 exit(1);
110 }
111 }
112
113
114 /* Create EventSet */
116 if (retval != PAPI_OK) {
117 fprintf(stderr,"Error creating eventset!\n");
118 }
119
120 for(i=0;i<num_events;i++) {
121
123 if (retval != PAPI_OK) {
124 fprintf(stderr,"Error adding event %s\n",events[i]);
125 }
126 }
127
128
129
130 start_time=PAPI_get_real_nsec();
131
132 while(1) {
133
134 /* Start Counting */
137 if (retval != PAPI_OK) {
138 fprintf(stderr,"PAPI_start() failed\n");
139 exit(1);
140 }
141
142
143 usleep(100000);
144
145 /* Stop Counting */
148 if (retval != PAPI_OK) {
149 fprintf(stderr, "PAPI_start() failed\n");
150 }
151
152 total_time=((double)(after_time-start_time))/1.0e9;
153 elapsed_time=((double)(after_time-before_time))/1.0e9;
154
155 for(i=0;i<num_events;i++) {
156 if( (strstr(events[i],"vccp") != NULL) ||
157 (strstr(events[i],"vddg") != NULL) ||
158 (strstr(events[i],"vddq") != NULL) ) {
159
160 fprintf(fff[i],"%.4f %.1f (* Average Voltage (Volt) for %s *)\n",
161 total_time,
162 ((double)values[i]/1.0e6),
163 events[i]);
164 } else {
165 if( strstr(events[i],"tot0") != NULL ){
166 energy += elapsed_time*((double)values[i]/1.0e6);
167 fprintf(fff[i],"%.4f %.1f %.1f (* Average Power (Watt) and Energy consumption (kWs) for %s *)\n",
168 total_time,
169 ((double)values[i]/1.0e6),
170 energy/1.0e3,
171 events[i]);
172 } else {
173 fprintf(fff[i],"%.4f %.1f (* Average Power (Watt) for %s *)\n",
174 total_time,
175 ((double)values[i]/1.0e6),
176 events[i]);
177 }
178 }
179 fflush(fff[i]);
180 }
181 }
182
183 return 0;
184}
185
int i
add PAPI preset or native hardware event by name to an EventSet
Create a new empty PAPI EventSet.
Enumerate PAPI preset or native events for a given component.
Convert a numeric hardware event code to a name.
get information about a specific software component
Get real time counter value in nanoseconds.
initialize the PAPI library.
Get the number of components available on the system.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_NULL
Definition: f90papi.h:78
FILE * fff[MAX_EVENTS]
static int num_events
char events[MAX_EVENTS][BUFSIZ]
char filenames[MAX_EVENTS][BUFSIZ]
#define MAX_EVENTS
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
#define PAPI_NATIVE_MASK
Return codes and api definitions.
FILE * stderr
int main()
Definition: pernode.c:20
static long long after_time
Definition: rapl_overflow.c:15
static long long before_time
Definition: rapl_overflow.c:15
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627
char disabled_reason[PAPI_HUGE_STR_LEN]
Definition: papi.h:634
int retval
Definition: zero_fork.c:53