-
Notifications
You must be signed in to change notification settings - Fork 1
/
perf.c
392 lines (305 loc) · 10.8 KB
/
perf.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// version14:
// using global variables to distribute energy to threads
// ENERGY-THREAD = m_i/m_sum * ENERGY-CORES
/* Comparision to previous versions:
*
* version 12: contains false sharing
* version 14: fix false sharing
*
*/
// m_i : estimation equation: Joules.power.energy.cores = 1.602416e-9*instructions-9.779874e-11 *cpu.cycles + 6.437730e-08*cache.misses +2.418160e+03
///////////////////////////////// start: headers///////////////////////////////////////////
// check if there exists VT/SCOREP,then decide which header to include.
// in my case, when building: -DSCOREP_DIR=~/install/scorep, so scorep detected->scorep_MetricPlugin.h
#ifdef SCOREP
#include <scorep/SCOREP_MetricPlugins.h>
#else
#error "You need Score-P to compile this plugin"
#endif /* SCOREP*/
#include <omp.h>
#include <inttypes.h> //PRIu64
#include <stdlib.h> // strtoll(); malloc();
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h> //uint64_t
#include <sys/syscall.h>
#include <sys/types.h>
#include <linux/perf_event.h>
////////////////////////////// end: headers////////////////////////////////////////////
/* number of threads */
#define N_THREADS 4 //must be constant, compiler needs to assign space for arrays.
////////////////////////////// start : global variables. //////////////////////////////
#define N 4 // how many counters I use for estimation. CTNAME_FD ctname_fd[N]
/* define the unique id for metrics (add_counter) */
#define ENERGY_THREAD 1 //first metric, m_i --> the portion of E for each thread
#define POWER_ENERGY_CORES 2 // second metric. E-cores --> total E.
/* define type, config value for RAPL attr*/
#define PERF_TYPE_RAPL 10
#define PERF_COUNT_ENERGY_COERS 1
#define PERF_COUNT_ENERGY_PKG 2
#define PERF_COUNT_ENERGY_RAM 3
#define PERF_COUNT_ENERGY_GPU 4
//#define PERF_RAPL_SCALE 2.3283064365386962890625e-10 // unit: Joules
#define PERF_RAPL_SCALE 2.3283064365386962890625e-1 // unit: Nanojoules
#define CACHE_LINE_SIZE 64
struct thread_local {
uint64_t m_i;
uint32_t round_num; // sometimes can use it to keep track which time a thread measures (not always useful)
} __attribute__ ((aligned(CACHE_LINE_SIZE)));
struct thread_local local_val[N_THREADS];
// mnemonic for base perf event-counters.
enum perf_event{
cpu_cycles=1,
cycles=1,
instructions=2,
cache_references=3,
cache_misses=4,
branch_instructions=5,
branches=5,
branch_misses=6,
power_energy_cores=21 // need sudo
};
typedef struct
{
int event_num;
int fd;
}CTNAME_FD;
// global array for fd setter and getter.
// store those that are useful to calculate required metric result.
static CTNAME_FD ctname_fd[N]={
{instructions,-1},
{cpu_cycles,-1},
{cache_misses,-1},
{power_energy_cores,-1}
};
////////////////////////////// end : global variables. //////////////////////////////
/* init() is intended, executed only once at the beginning before getting any result.*/
int32_t init(){
int i=0;
for(i=0;i<N_THREADS;i++){
local_val[i].m_i=0;
local_val[i].round_num=0;
}
return 0;
}
//This functions is called once per process to clean up all resources used by the metric plugin.
void fini(){
/* we do not close perfs file descriptors */
/* as we do not store this information */
}
/* This function writes the attr definitions for a given event name
* If the event has not been found, attr->type is PERF_TYPE_MAX
* */
void build_perf_attr(struct perf_event_attr * attr, int event_num)
{
memset( attr, 0, sizeof( struct perf_event_attr ) );
//The first n bits of attr will be replaced by n 0s. n = sizeof( struct perf_event_attr).
attr->config1 = 0;
attr->config2 = 0;
attr->type = PERF_TYPE_MAX;
// those that will be used.
switch(event_num){
case cpu_cycles:
attr->type = PERF_TYPE_HARDWARE;
attr->config = PERF_COUNT_HW_CPU_CYCLES;
break;
case instructions:
attr->type = PERF_TYPE_HARDWARE;
attr->config = PERF_COUNT_HW_INSTRUCTIONS;
break;
case cache_references:
attr->type = PERF_TYPE_HARDWARE;
attr->config = PERF_COUNT_HW_CACHE_REFERENCES;
break;
case cache_misses:
attr->type = PERF_TYPE_HARDWARE;
attr->config = PERF_COUNT_HW_CACHE_MISSES;
break;
case branch_instructions:
attr->type = PERF_TYPE_HARDWARE;
attr->config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
break;
case branch_misses:
attr->type = PERF_TYPE_HARDWARE;
attr->config = PERF_COUNT_HW_BRANCH_MISSES;
break;
case power_energy_cores:
attr->type = PERF_TYPE_RAPL;
attr->config = PERF_COUNT_ENERGY_COERS;
break;
default:
break;
}
}
/* syscall to gather performance counters. */
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
return ret;
}
void set_fd(int event_num)
{
int i;
int fd;
struct perf_event_attr attr;
build_perf_attr(&attr, event_num);
if(event_num == power_energy_cores){ //rapl-read
fd = perf_event_open(&attr, -1,0,-1,0);
}else{ //others
fd = perf_event_open(&attr, 0,-1,-1,0);
}
for(i=0;i<N;i++){
if(ctname_fd[i].event_num == event_num){
ctname_fd[i].fd = fd;
}
}
}
/* registers perf event , it provides a unique ID for each metric.*/
int32_t add_counter(char * event_name) // callback, fixed parameter : metric name(string)--> not possible to use switch.
{
int id=0; //unique id.
int i=0; // for loop.
if(strstr( event_name, "energy-thread" ) == event_name)
{
id = ENERGY_THREAD;
set_fd(instructions);
set_fd(cpu_cycles);
set_fd(cache_misses);
set_fd(power_energy_cores);
}else if(strstr( event_name, "power-energy-cores" ) == event_name){
id = POWER_ENERGY_CORES;
set_fd(power_energy_cores);
}
return id;
}
/* reads value repeatedly */
int get_fd(int event_num)
{
int i=0;
struct perf_event_attr attr;
int fd=-1;
build_perf_attr(&attr, event_num);
for(i=0;i<N;i++){
if(ctname_fd[i].event_num == event_num){
fd=ctname_fd[i].fd;
}
}
if(fd<=-1){
fprintf(stderr, "Error: Failed to get counter %d!\n", event_num);
return -1;
}
return fd;
}
uint64_t get_counterValue(int event_num){
int fd;
size_t ret;
uint64_t count;
fd = get_fd(event_num);
if(fd <= -1){
fprintf(stderr, "Unable to get event for event_num=%d!\n",event_num);
return -1;
}
ret =read(fd, &count, sizeof(uint64_t));
if (ret!=sizeof(uint64_t)){
return -1;
}
return count;
}
uint64_t get_value(int id){
int i;
uint64_t result;
uint64_t count1=0;
uint64_t count2=0;
uint64_t count3=0;
uint64_t energy_cores_value=0;
uint64_t m_sum=0;
int tid;
tid=omp_get_thread_num();
if(id == ENERGY_THREAD){
// 0. read all values.
count1=get_counterValue(instructions);
count2=get_counterValue(cpu_cycles);
count3=get_counterValue(cache_misses);
energy_cores_value=get_counterValue(power_energy_cores) * PERF_RAPL_SCALE;
if (count1<0|| count2<0 || count3<0 || energy_cores_value<0){
return !0;
}
local_val[tid].m_i = 1.602416e-9*count1 - 9.779874e-11*count2 + 6.437730e-08*count3 + 2.418160e+03; // record the current measurement.
for(i=0;i<N_THREADS;i++){
m_sum += local_val[tid].m_i;
}
result = local_val[tid].m_i*(1.0)/m_sum * energy_cores_value;
}else if(id == POWER_ENERGY_CORES) {
energy_cores_value = get_counterValue(power_energy_cores) * PERF_RAPL_SCALE;
if(energy_cores_value < 0){
return !0;
}
result = energy_cores_value ;
}
local_val[tid].round_num++;
//printf("tid=%d, No.=%"PRIu32", id=%d, result=%"PRIu64".\n", tid, local_val[tid].round_num, id, result);
return result;
}
#ifdef SCOREP
SCOREP_Metric_Plugin_MetricProperties * get_event_info(char * event_name)
{
SCOREP_Metric_Plugin_MetricProperties * return_values;
uint64_t id = add_counter(event_name);
/* wrong metric */
if (id < 0){
fprintf(stderr, "PERF metric not recognized: %s", event_name );
return NULL;
}
return_values= malloc(2 * sizeof(SCOREP_Metric_Plugin_MetricProperties) ); /// why 2????
if (return_values==NULL){
fprintf(stderr, "Score-P Perf Plugin: "
"failed to allocate memory for passing information to Score-P.\n");
return NULL;
}
return_values[0].name = strdup(event_name); //strdup == duplicate.
return_values[0].unit = "Nanojoules";
return_values[0].description = NULL;
return_values[0].mode = SCOREP_METRIC_MODE_ACCUMULATED_START;
return_values[0].value_type = SCOREP_METRIC_VALUE_UINT64;
return_values[0].base = SCOREP_METRIC_BASE_DECIMAL;
return_values[0].exponent = 0;
return_values[1].name=NULL;
return return_values;
}
bool get_optional_value( int32_t id, uint64_t* value ){
*value=get_value(id);
return true;
}
/**
* This function get called to give some informations about the plugin to scorep
*/
// define the name of this plugin, with its inner structure.
SCOREP_METRIC_PLUGIN_ENTRY( perf_plugin )
{
/* Initialize info data (with zero) */
//SCOREP_Metric_Plugin_Info : defined as a struct in SCOREP_MetricPlugins.h
SCOREP_Metric_Plugin_Info info;
memset( &info, 0, sizeof( SCOREP_Metric_Plugin_Info ) );
/* Set up the structure */
info.plugin_version = SCOREP_METRIC_PLUGIN_VERSION; // uint32_t
info.run_per = SCOREP_METRIC_PER_THREAD; // <SCOREP_MetricTypes.h>: SCOREP_MetricPer, int enum
info.sync = SCOREP_METRIC_SYNC; //<SCOREP_MetricTypes.h>:SCOREP_MetricSynchronicity, int enum
info.delta_t = 10*1000*1000; //uint64_t, default=0; Set a specific interval for reading metric values.
info.initialize = init; // function as a member: int32_t(void)
info.finalize = fini; // function as a member: void(void)
info.get_event_info = get_event_info; // ditto: SCOREP_Metric_Plugin_MetricProperties(char* token)
info.add_counter = add_counter; // ditto: int32_t(char* metric_name)
info.get_current_value = get_value; // ditto: uint64_t(int32_t id)
info.get_optional_value = get_optional_value; // ditto: bool(int32_t id, uint_64* value)
//some other members in this SCOREP_Metric_Plugin_Info Struct but useless here
//info.set_clock_function; // ditto: void(uint64_t)
//info.get_all_values; //ditto: uint64_t(int32_t, SCOREP_MetricTimeValuePair**)
//info.synchronize; //ditto: void(bool,SCOREP_MetricSynchronizationMode)
//info.reserved; //Reserved space for future features, should be zeroed
return info;
}
#endif /* SCOREP */