-
Notifications
You must be signed in to change notification settings - Fork 2
/
measure-util.h
192 lines (165 loc) · 5.08 KB
/
measure-util.h
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
/*
* Energy and performance measurement utility functions
*
* Author: Mikael Hirki <mikael.hirki@gmail.com>
*
* Copyright (c) 2015 Helsinki Institute of Physics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#define millisleep(x) (usleep((x) * 1000))
/* Assuming that RAND_MAX is 2^31 - 1 */
#define rand64() (((unsigned long long)rand() << 62) | ((unsigned long long)rand() << 31) | rand())
#define rand32() (((unsigned int)rand() << 31) | rand())
#if __x86_64__ || __i386__
#define HAVE_RDTSC
#define RDTSC(v) \
do { unsigned lo, hi; \
__asm__ volatile("rdtsc" : "=a" (lo), "=d" (hi)); \
(v) = ((uint64_t) lo) | ((uint64_t) hi << 32); \
} while (0)
#else
#define RDTSC(v) (v = 0)
#endif
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
/* PAPI gives energy in nanojoules */
#define ENERGY_SCALE_FACTOR (1e-9)
/* Flags for measure_init_v2 and measure_stop_v2 */
#define MEASURE_FLAG_NO_PRINT 0x01
#define MEASURE_FLAG_NO_ENERGY 0x02
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
/* PAPI event sets */
int papi_energy_events;
int papi_perf_events;
/* Number of events in each set */
int num_energy_events;
int num_perf_events;
/* Nanosecond timing information */
struct timespec begin_time;
struct timespec end_time;
/* TSC timing information */
unsigned long long begin_tsc;
unsigned long long end_tsc;
/* Core temperatures */
double begin_temp_pkg;
double begin_temp0;
double begin_temp1;
double begin_temp2;
double begin_temp3;
double end_temp_pkg;
double end_temp0;
double end_temp1;
double end_temp2;
double end_temp3;
/* Core voltages */
double begin_voltage0;
double begin_voltage1;
double begin_voltage2;
double begin_voltage3;
double end_voltage0;
double end_voltage1;
double end_voltage2;
double end_voltage3;
/* Buffers for PAPI_read() */
long long *papi_energy_values;
long long *papi_perf_values;
/* For storing computed RAPL power consumption */
double pkg_power_before;
double pp0_power_before;
double pp1_power_before;
double dram_power_before;
double time_elapsed_before;
double event_1_before;
double event_2_before;
double event_3_before;
double event_4_before;
/* Indices for PAPI event sets */
int idx_pkg_energy;
int idx_pp0_energy;
int idx_pp1_energy;
int idx_dram_energy;
int idx_cycles;
int idx_ref_cycles;
int idx_instructions;
int idx_event_1;
int idx_event_2;
int idx_event_3;
int idx_event_4;
/* Flags */
char energy_started;
char perf_started;
char have_rapl;
} measure_state_t;
/*
* Some PAPI functions don't seem to be thread safe...
*/
extern pthread_mutex_t papi_mutex;
/*
* Cache event codes for faster performance.
*/
extern int papi_code_uops_issued;
extern int papi_code_idq_mite;
extern int papi_code_idq_dsb;
extern int papi_code_idq_ms;
int measure_init_papi(int flags);
int measure_init_thread(measure_state_t *state, int flags);
int measure_start(measure_state_t *s, int flags);
int measure_stop(measure_state_t *state, int flags);
int measure_combine_perf_results(measure_state_t *this, measure_state_t *other);
int measure_print(measure_state_t *state, int flags);
int measure_cleanup(measure_state_t *state);
void *measure_alloc(size_t size);
void *measure_aligned_alloc(size_t size, size_t alignment);
/*
* New higher level interface
*/
typedef struct {
const char *name;
const char *desc;
} perf_counter_t;
typedef struct {
int (*init)(void **benchdata);
int (*normal)(void *benchdata, long ntimes);
int (*extreme)(void *benchdata, long ntimes);
int (*cleanup)(void *benchdata);
perf_counter_t counters[4]; /* Not yet implemented */
long ntimes;
} measure_benchmark_t;
/*
* Parsed command line parameters
*/
extern char arg_do_measure;
extern char arg_use_64bit_numbers;
extern int arg_benchmark_phase;
extern int arg_num_threads;
extern int arg_num_repeat;
extern int arg_warmup_time;
extern char arg_force_affinity;
int measure_main(int argc, char **argv, measure_benchmark_t *bench);
#ifdef __cplusplus
} /* extern "C" */
#endif