-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathetrace2.c
302 lines (259 loc) · 5.98 KB
/
etrace2.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
/*
An enery trace code
support spawn mode(e.g. strace) and polling mode that priodically
print power usage.
currently only support RAPL
Kazutomo Yoshii <[email protected]>
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <getopt.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "raplreader.h"
#define Z printf("%d\n",__LINE__)
#define ETRACE2_VERSION "0.1"
static char *program_name;
char *_basename(char *path)
{
char *base = strrchr(path, '/');
return base ? base+1 : path;
}
#if 0
/*
XXX: clean up this later
*/
#define LOCKFN "/var/tmp/etrace.lock"
static void touch_lock(void)
{
int hackfd;
hackfd = open(LOCKFN, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666);
close(hackfd);
}
static int access_lock(void)
{
return access(LOCKFN, R_OK);
}
static void unlink_lock(void)
{
unlink(LOCKFN);
}
static void sigh(int signum)
{
unlink(LOCKFN);
exit(1);
}
#endif
static double gettimesec(void)
{
struct timeval tv;
gettimeofday(&tv, 0);
return (double)tv.tv_sec + (double)tv.tv_usec/1000.0/1000.0;
}
/*
if child_pid > 0, it doesn't use timeout and waits until the child process is terminated
*/
static void sampling_loop(FILE *fp, double interval, double timeout, int child_pid, int verbose, int relative_time)
{
double ts, time_start, time_delta;
struct raplreader rr;
useconds_t usec;
int status;
int rc;
int i;
fprintf(fp, "# ETRACE2_VERSION=%s\n",ETRACE2_VERSION);
#if 0
rc = access_lock();
if( rc == 0 ) {
fprintf(stderr, "\n");
fprintf(stderr, "Failed to start etrace!\n");
fprintf(stderr, "\n");
fprintf(stderr, "Someone else is running etrace. RAPL is a sytem-wide resource.\n");
fprintf(stderr, "\n");
exit(1);
}
touch_lock();
signal(SIGINT, sigh);
#endif
rc = raplreader_init(&rr);
if (rc) {
fprintf(stderr, "Error: raplreader_init() failed\n");
exit(1);
}
raplreader_sample(&rr);
ts = raplreader_get_ts(&rr);
time_start = ts;
if (interval <= 0.0) {
/* just energy usage */
if (child_pid > 0) {
waitpid(child_pid, &status, 0);
} else {
usleep(timeout*1000.0*1000.0);
}
} else {
if (relative_time) {
fprintf(fp, "# START_TIME=%lf\n",gettimesec());
}
/* header */
fprintf(fp, "# Time [S] Power [W] Energy [J]");
fprintf(fp, "\n");
fflush(fp);
/* polling loop */
for (;;) {
time_delta = fmod(ts, interval);
usec = (useconds_t)((interval - time_delta)*1000.0*1000.0);
usleep(usec);
raplreader_sample(&rr);
ts = raplreader_get_ts(&rr);
if (child_pid > 0) {
if (waitpid(child_pid, &status, WNOHANG) != 0)
break;
} else if ((ts - time_start) > timeout) {
break;
}
if (relative_time)
fprintf(fp, "%.3lf ", ts - time_start);
else
fprintf(fp, "%.3lf ", ts);
fprintf(fp, "%.3lf ", raplreader_get_total_power(&rr));
fprintf(fp, "%.3lf ", raplreader_get_total_energy(&rr));
fprintf(fp, "\n");
fflush(fp);
}
}
raplreader_sample(&rr);
ts = raplreader_get_ts(&rr);
fprintf(fp, "# ELAPSED=%lf\n", ts - time_start);
fprintf(fp, "# ENERGY=%lf\n", raplreader_get_total_energy(&rr));
for (i = 0; i < raplreader_get_nsockets(&rr); i++) {
fprintf(fp, "# ENERGY_SOCKET%d=%lf\n", i, raplreader_get_energy_socket(&rr,i));
if (raplreader_is_dram_available(&rr))
fprintf(fp, "# ENERGY_DRAM%d=%lf\n", i, raplreader_get_energy_dram(&rr,i));
}
}
static void usage(void)
{
printf("\n");
printf("Usage: %s [options] [command]\n",program_name);
printf("\n");
printf("[Options]\n");
printf("\t-i sec : interval\n");
printf("\t-t sec : timeout\n");
printf("\t-o filename : output filename\n");
printf("\t-r : relative time, instead of epoch\n");
printf("\n");
#if 0
printf("\t--setplim1 watt : limit power\n");
printf("\t--setplim2 watt : limit power (turbo)\n");
printf("\n");
#endif
printf("[usage examples]\n");
printf("\n");
printf("# spawn your program and print energy consumption \n");
printf("$ ./%s your_program\n",program_name);
printf("\n");
printf("# also print power consumption every 0.5 sec\n");
printf("$ ./%s -i 0.5 your_program\n",program_name);
printf("\n");
printf("# just print power consumption every 1sec for 10sec\n");
printf("$ ./%s -i 1.0 -t 10\n",program_name);
printf("\n");
}
int main(int argc, char *argv[])
{
int opt;
int verbose=0;
int relative_time=0;
double timeout=-1.0, interval=-1.0;
char fn[1024];
FILE *fp = stdout;
// double plim1=-1.0, plim2=-1.0;
fn[0] = 0;
program_name = _basename(argv[0]);
while(1) {
int option_index = 0;
static struct option long_options[] = {
#if 0
// name, has_arg, flag, val
{"setplim1", 1, 0, 0 },
{"setplim2", 1, 0, 0 },
#endif
{0, 0, 0, 0 }
};
opt=getopt_long(argc, argv, "+i:t:o:rhv",
long_options, &option_index);
if(opt==-1) break;
switch(opt) {
#if 0
case 0:
switch( option_index ) {
case 0:
plim1 = strtod(optarg, (char**)0);
break;
case 1:
plim2 = strtod(optarg, (char**)0);
break;
}
break;
#endif
case 'i':
interval = strtod(optarg,(char**)0);
break;
case 't':
timeout = strtod(optarg,(char**)0);
break;
case 'o':
strncpy(fn,optarg,sizeof(fn)-1);
fn[sizeof(fn)-1] = 0;
break;
case 'r':
relative_time = 1;
break;
case 'v':
verbose = 1;
break;
case 'h':
usage();
exit(0);
}
}
if (strlen(fn) > 0) {
fp = fopen(fn, "w");
if (!fp) {
perror("fopen");
exit(1);
}
}
if (argc - optind) {
pid_t child_pid;
/* spawn mode */
child_pid = fork();
if (child_pid == 0) {
execvp(argv[optind], argv+optind);
} else {
sampling_loop(fp, interval, 0, child_pid, verbose, relative_time);
}
} else {
/* polling mode */
if (timeout < 0.0 || interval < 0.0) {
printf("Please specify timeout(-t) and interval(-i)\n");
usage();
exit(1);
}
sampling_loop(fp, interval, timeout, 0, verbose, relative_time);
}
if (fp != stdout) {
fclose(fp);
}
#if 0
unlink_lock();
#endif
return 0;
}