-
Notifications
You must be signed in to change notification settings - Fork 67
/
power.c
219 lines (200 loc) · 4.64 KB
/
power.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
#include "power.h"
#include "util.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define DIR_POWERCAP "/sys/class/powercap"
#define BUFSZ 80
struct rapl_device_ext_t {
char * dir;
int64_t last;
struct timespec time;
};
struct rapl_full_t {
struct rapl_t parent;
struct array_t * exts;
};
static void rapl_device_free(void * pointer) {
struct rapl_device_t * device = pointer;
free(device->name);
}
static void rapl_device_ext_free(void * pointer) {
struct rapl_device_ext_t * ext = pointer;
free(ext->dir);
}
struct rapl_t * rapl_init() {
char buf[BUFSZ];
DIR * dir;
struct dirent * dirent;
struct array_t * devices = NULL;
struct array_t * exts = NULL;
bool nomem = false;
struct rapl_full_t * full = NULL;
dir = opendir(DIR_POWERCAP);
if (dir == NULL) {
fprintf(stderr, "Failed to open powercap directory\n");
return NULL;
}
while ((dirent = readdir(dir))) {
if (strstr(dirent->d_name, ":") && strlen(dirent->d_name) <= 30) {
int fd;
BEGIN_IGNORE_FORMAT_OVERFLOW
sprintf(buf, DIR_POWERCAP "/%s/name", dirent->d_name);
END_IGNORE_FORMAT_OVERFLOW
fd = open(buf, O_RDONLY);
if (fd >= 0) {
int size = read(fd, buf, BUFSZ - 1);
if (size >= 1) {
struct rapl_device_t * device;
struct rapl_device_ext_t * ext;
char * name;
char * dir;
int name_length;
int dir_length;
name_length = buf[size - 1] == '\n' ? size - 1 : size;
buf[name_length] = '\0';
if (name_length == 0) {
close(fd);
continue;
}
name = malloc(name_length + 1);
if (!name) {
close(fd);
nomem = true;
break;
}
memcpy(name, buf, name_length + 1);
dir_length = strlen(dirent->d_name);
dir = malloc(dir_length + 1);
if (!dir) {
free(name);
close(fd);
nomem = true;
break;
}
memcpy(dir, dirent->d_name, dir_length + 1);
if (!devices && !exts) {
devices = array_new(sizeof(struct rapl_device_t),
rapl_device_free);
exts = array_new(sizeof(struct rapl_device_ext_t),
rapl_device_ext_free);
if (!devices || !exts) {
free(name);
free(dir);
close(fd);
nomem = true;
break;
}
}
device = array_add(devices);
if (!device) {
free(name);
free(dir);
close(fd);
nomem = true;
break;
}
device->name = name;
device->power = 0;
ext = array_add(exts);
if (!ext) {
free(dir);
close(fd);
nomem = true;
break;
}
ext->dir = dir;
ext->last = 0;
ext->time.tv_sec = 0;
ext->time.tv_nsec = 0;
}
close(fd);
}
}
}
closedir(dir);
if (!nomem) {
full = malloc(sizeof(struct rapl_full_t));
if (!full) {
nomem = true;
}
}
if (nomem) {
if (devices) {
array_free(devices);
}
if (exts) {
array_free(exts);
}
if (full) {
free(full);
}
fprintf(stderr, "No enough memory\n");
return NULL;
} else {
array_shrink(devices);
array_shrink(exts);
full->parent.devices = devices;
full->exts = exts;
return &full->parent;
}
}
void rapl_measure(struct rapl_t * rapl) {
if (rapl) {
char buf[BUFSZ];
struct rapl_full_t * full = (struct rapl_full_t *) rapl;
int i;
for (i = 0; i < full->parent.devices->count; i++) {
int fd;
struct rapl_device_t * device = array_get(full->parent.devices, i);
struct rapl_device_ext_t * ext = array_get(full->exts, i);
sprintf(buf, DIR_POWERCAP "/%s/energy_uj", ext->dir);
fd = open(buf, O_RDONLY);
if (fd >= 0) {
int size = read(fd, buf, BUFSZ - 1);
if (size > 0) {
struct timespec tnow;
double power = 0;
buf[buf[size - 1] == '\n' ? size - 1 : size] = '\0';
int64_t value = (int64_t) atoll(buf);
clock_gettime(CLOCK_MONOTONIC, &tnow);
if (ext->last > 0 && value >= ext->last) {
struct timespec tdiff;
int64_t diff;
tdiff.tv_sec = tnow.tv_sec - ext->time.tv_sec;
tdiff.tv_nsec = tnow.tv_nsec - ext->time.tv_nsec;
while (tdiff.tv_nsec < 0) {
tdiff.tv_nsec += 1000000000;
tdiff.tv_sec--;
}
diff = (int64_t) (tdiff.tv_sec * 1000000000 +
tdiff.tv_nsec);
power = (double) (value - ext->last) * 1000 / diff;
} else {
power = device->power;
}
ext->last = value;
ext->time = tnow;
device->power = power;
}
close(fd);
}
}
}
}
void rapl_free(struct rapl_t * rapl) {
if (rapl) {
struct rapl_full_t * full = (struct rapl_full_t *) rapl;
if (full->parent.devices) {
array_free(full->parent.devices);
}
if (full->exts) {
array_free(full->exts);
}
free(full);
}
}