-
Notifications
You must be signed in to change notification settings - Fork 18
/
interval_test.c
71 lines (62 loc) · 1.26 KB
/
interval_test.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
#define _GNU_SOURCE
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "energymon.h"
#include "energymon-get.h"
int main(int argc, char** argv) {
char source[100];
long usec;
uint64_t start, end, uj;
energymon em;
if (argc < 2) {
fprintf(stderr, "Usage: %s <idle_usec>\n", argv[0]);
return EINVAL;
}
usec = strtol(argv[1], NULL, 0);
if(usec <= 0) {
fprintf(stderr, "idle_usec must be > 0\n");
return EINVAL;
}
if (energymon_get(&em)) {
return 1;
}
em.fsource(source, sizeof(source));
printf("Initializing reading from %s\n", source);
if (em.finit(&em)) {
perror("finit");
return 1;
}
errno = 0;
start = em.fread(&em);
if (start == 0 && errno) {
perror("fread");
return 1;
}
#ifdef _WIN32
Sleep(usec / 1000);
#else
usleep(usec);
#endif
errno = 0;
end = em.fread(&em);
if (end == 0 && errno) {
perror("fread");
return 1;
}
uj = end - start;
printf("Total energy: %"PRIu64" uJ\n", uj);
printf("Average power: %f W\n", (double) uj / (double) usec);
if (em.ffinish(&em)) {
perror("ffinish");
return 1;
}
printf("Finished reading from %s\n", source);
return 0;
}