-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpapi-poll-pkg.cc
129 lines (111 loc) · 3.24 KB
/
papi-poll-pkg.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* papi-poll-pkg.cc
* Compare two different PAPI RAPL counters:
* PACKAGE_ENERGY_CNT
* PACKAGE_ENERGY
*
* The second one gives the energy on nanojoules.
*
* Code based on IgProf energy profiling module by Filip Nybäck.
*
* Author: Mikael Hirki <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <papi.h>
#include <math.h>
#include <vector>
#include "util.h"
#define READ_ENERGY(a) PAPI_read(s_event_set, a)
bool do_rapl() {
int s_event_set = 0;
int s_num_events = 0;
long long *s_values = NULL;
int i = 0;
int idx_pkg_energy_cnt = -1;
int idx_pkg_energy = -1;
struct timespec one_ms = { 0, 1000000 };
if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT) {
fprintf(stderr, "PAPI library initialisation failed.\n");
return false;
}
// Find the RAPL component of PAPI.
int num_components = PAPI_num_components();
int component_id;
const PAPI_component_info_t *component_info = 0;
for (component_id = 0; component_id < num_components; ++component_id) {
component_info = PAPI_get_component_info(component_id);
if (component_info && strstr(component_info->name, "rapl")) {
break;
}
}
if (component_id == num_components) {
fprintf(stderr, "No RAPL component found in PAPI library.\n");
return false;
}
if (component_info->disabled) {
fprintf(stderr, "RAPL component of PAPI disabled: %s.\n",
component_info->disabled_reason);
return false;
}
// Create an event set.
s_event_set = PAPI_NULL;
if (PAPI_create_eventset(&s_event_set) != PAPI_OK) {
fprintf(stderr, "Could not create PAPI event set.\n");
return false;
}
int code = PAPI_NATIVE_MASK;
for (int retval = PAPI_enum_cmp_event(&code, PAPI_ENUM_FIRST, component_id); retval == PAPI_OK; retval = PAPI_enum_cmp_event(&code, PAPI_ENUM_EVENTS, component_id)) {
char event_name[PAPI_MAX_STR_LEN];
if (PAPI_event_code_to_name(code, event_name) != PAPI_OK) {
fprintf(stderr, "Could not get PAPI event name.\n");
return false;
}
PAPI_event_info_t event_info;
if (PAPI_get_event_info(code, &event_info) != PAPI_OK) {
fprintf(stderr, "Could not get PAPI event info.\n");
return false;
}
if (event_info.data_type != PAPI_DATATYPE_UINT64) {
continue;
}
if (strstr(event_name, "PACKAGE_ENERGY_CNT:")) {
idx_pkg_energy_cnt = s_num_events;
} else if (strstr(event_name, "PACKAGE_ENERGY:")) {
idx_pkg_energy = s_num_events;
} else {
continue; // Skip other counters
}
printf("Adding %s to event set.\n", event_name);
if (PAPI_add_event(s_event_set, code) != PAPI_OK) {
break;
}
++s_num_events;
}
if (s_num_events == 0) {
fprintf(stderr, "Could not find any RAPL events.\n");
return false;
}
// Allocate memory for reading the counters
s_values = (long long *)calloc(s_num_events, sizeof(long long));
// Activate the event set.
if (PAPI_start(s_event_set) != PAPI_OK) {
fprintf(stderr, "Could not activate the event set.\n");
return false;
}
int num_iterations = 1000;
for (i = 0; i < num_iterations; i++) {
READ_ENERGY(s_values);
printf("%lld, %lld\n", s_values[idx_pkg_energy_cnt], s_values[idx_pkg_energy]);
nanosleep(&one_ms, NULL);
}
return true;
}
int main() {
do_affinity(0);
do_rapl();
return 0;
}