aboutsummaryrefslogtreecommitdiff
path: root/src/powercap-common.c
blob: e6b145fb1b3d7e5490fa6e6763346c0c84654cca (plain)
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
/*
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Common functions.
 *
 * @author Connor Imes
 * @date 2017-08-24
 */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
/* Main powercap header only used for enums */
#include "powercap.h"
#include "powercap-common.h"

#define MAX_U64_SIZE 24

#ifdef USE_VIRTUAL_DEVICES
  #define POWERCAP_PATH "/sys/devices/virtual/powercap"
#else
  #define POWERCAP_PATH "/sys/class/powercap"
#endif

/* These enums MUST align with powercap_control_type_file in powercap.h */
static const char* CONTROL_TYPE_FILE[] = {
  "enabled"
};

/* These enums MUST align with powercap_zone_file in powercap.h */
static const char* ZONE_FILE[] = {
  "max_energy_range_uj",
  "energy_uj",
  "max_power_range_uw",
  "power_uw",
  "enabled",
  "name"
};

/* These enums MUST align with powercap_constraint_file in powercap.h */
static const char* CONSTRAINT_FILE_SUFFIX[] = {
  "power_limit_uw",
  "time_window_us",
  "max_power_uw",
  "min_power_uw",
  "max_time_window_us",
  "min_time_window_us",
  "name"
};

ssize_t read_string_safe(int fd, char* buf, size_t size) {
  ssize_t ret;
  if ((ret = pread(fd, buf, size - 1, 0)) > 0) {
    /* force a terminating character in the buffer */
    if (buf[ret - 1] == '\n') {
      /* also remove newline character */
      buf[ret - 1] = '\0';
    } else {
      buf[ret] = '\0';
    }
  } else if (ret < 0) {
    ret = -errno;
  } else {
    errno = ENODATA;
    ret = -errno;
  }
  return ret;
}

ssize_t read_string(int fd, char* buf, size_t size) {
  if (!buf) {
    errno = EINVAL;
    return (ssize_t) -errno;
  } else if (!size) {
    errno = ENOBUFS;
    return (ssize_t) -errno;
  }
  return read_string_safe(fd, buf, size);
}

int read_u64(int fd, uint64_t* val) {
  char buf[MAX_U64_SIZE];
  char* end;
  if (!val) {
    errno = EINVAL;
  } else if (read_string_safe(fd, buf, sizeof(buf)) > 0) {
    errno = 0;
    *val = strtoull(buf, &end, 0);
    if (buf != end && errno != ERANGE) {
      return 0;
    }
  }
  return -errno;
}

int write_u64(int fd, uint64_t val) {
  char buf[MAX_U64_SIZE];
  ssize_t written;
  snprintf(buf, sizeof(buf), "%"PRIu64, val);
  if ((written = pwrite(fd, buf, sizeof(buf), 0)) < 0) {
    return -errno;
  }
  if (!written) {
    /* Is there a better error code? */
    errno = EIO;
    return -errno;
  }
  return 0;
}

int is_valid_control_type(const char* control_type) {
  return control_type && strlen(control_type) && strcspn(control_type, "./") == strlen(control_type);
}

int snprintf_base_path(char* buf, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth) {
  int w;
  int tot;
  uint32_t i;
  uint32_t j;
  if ((tot = snprintf(buf, size, POWERCAP_PATH"/%s/", control_type)) < 0) {
    return tot;
  }
  for (j = 1; j <= depth; j++) {
    if ((size_t) tot >= size) {
      return size + 1; // strictly > size since there was still more work to do
    }
    if ((w = snprintf(buf + (size_t) tot, size - (size_t) tot, "%s", control_type)) < 0) {
      return w;
    }
    tot += w;
    if ((size_t) tot >= size) {
      return size + 1; // strictly > size since there was still more work to do
    }
    for (i = 0; i < j && (size_t) tot < size; i++, tot += w) {
      if ((w = snprintf(buf + (size_t) tot, size - (size_t) tot, ":%x", zones[i])) < 0) {
        return w;
      }
    }
    if ((size_t) tot >= size) {
      return size + 1; // strictly > size since there was still more work to do
    }
    buf[tot++] = '/';
  }
  if ((size_t) tot < size) {
    buf[tot] = '\0';
  }
  return tot;
}

int snprintf_control_type_file(char* buf, size_t size, powercap_control_type_file type) {
  return snprintf(buf, size, "%s", CONTROL_TYPE_FILE[type]);
}

int snprintf_zone_file(char* buf, size_t size, powercap_zone_file type) {
  return snprintf(buf, size, "%s", ZONE_FILE[type]);
}

int snprintf_constraint_file(char* buf, size_t size, powercap_constraint_file type, uint32_t constraint) {
  return snprintf(buf, size, "constraint_%"PRIu32"_%s", constraint, CONSTRAINT_FILE_SUFFIX[type]);
}

int snprintf_control_type_file_path(char* path, size_t size, const char* control_type, powercap_control_type_file type) {
  int tot;
  int w;
  if ((tot = snprintf_base_path(path, size, control_type, NULL, 0)) < 0) {
    return tot;
  }
  if ((size_t) tot >= size) {
    return size + 1; // strictly > size since there was still more work to do
  }
  if ((w = snprintf_control_type_file(path + (size_t) tot, size - (size_t) tot, type)) < 0) {
    return w;
  }
  return tot + w;
}

int snprintf_zone_file_path(char* path, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth,
                            powercap_zone_file type) {
  int tot;
  int w;
  if ((tot = snprintf_base_path(path, size, control_type, zones, depth)) < 0) {
    return tot;
  }
  if ((size_t) tot >= size) {
    return size + 1; // strictly > size since there was still more work to do
  }
  if ((w = snprintf_zone_file(path + tot, size - tot, type)) < 0) {
    return w;
  }
  return tot + w;
}

int snprintf_constraint_file_path(char* path, size_t size, const char* control_type, const uint32_t* zones,
                                  uint32_t depth, uint32_t constraint, powercap_constraint_file type) {
  int tot;
  int w;
  if ((tot = snprintf_base_path(path, size, control_type, zones, depth)) < 0) {
    return tot;
  }
  if ((size_t) tot >= size) {
    return size + 1; // strictly > size since there was still more work to do
  }
  if ((w = snprintf_constraint_file(path + tot, size - tot, type, constraint)) < 0) {
    return w;
  }
  return tot + w;
}

int open_control_type_file(char* path, size_t size, const char* control_type, powercap_control_type_file type,
                           int flags) {
  int w = snprintf_control_type_file_path(path, size, control_type, type);
  if (w < 0) {
    // POSIX says snprintf should only fail if size > INT_MAX, which it's not, so this code branch should never run
    // If we're here, we don't even know what the error should be, so assert that errno is set
    assert(errno);
    return w;
  }
  if ((size_t) w >= size) {
    errno = ENOBUFS;
    return -1;
  }
  return open(path, flags);
}

int open_zone_file(char* path, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth,
                   powercap_zone_file type, int flags) {
  int w = snprintf_zone_file_path(path, size, control_type, zones, depth, type);
  if (w < 0) {
    // POSIX says snprintf should only fail if size > INT_MAX, which it's not, so this code branch should never run
    // If we're here, we don't even know what the error should be, so assert that errno is set
    assert(errno);
    return w;
  }
  if ((size_t) w >= size) {
    errno = ENOBUFS;
    return -1;
  }
  return open(path, flags);
}

int open_constraint_file(char* path, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth,
                         uint32_t constraint, powercap_constraint_file type, int flags) {
  int w = snprintf_constraint_file_path(path, size, control_type, zones, depth, constraint, type);
  if (w < 0) {
    // POSIX says snprintf should only fail if size > INT_MAX, which it's not, so this code branch should never run
    // If we're here, we don't even know what the error should be, so assert that errno is set
    assert(errno);
    return w;
  }
  if ((size_t) w >= size) {
    errno = ENOBUFS;
    return -1;
  }
  return open(path, flags);
}

// like open(2), but returns 0 on ENOENT (No such file or directory)
int powercap_control_type_file_open(char* buf, size_t bsize, const char* ct_name, powercap_control_type_file type,
                                    int flags) {
  int fd = open_control_type_file(buf, bsize, ct_name, type, flags);
  return (fd < 0 && errno == ENOENT) ? 0 : fd;
}

// like open(2), but returns 0 on ENOENT (No such file or directory)
int powercap_zone_file_open(char* buf, size_t bsize, const char* ct_name, const uint32_t* zones, uint32_t depth,
                            powercap_zone_file type, int flags) {
  int fd = open_zone_file(buf, bsize, ct_name, zones, depth, type, flags);
  return (fd < 0 && errno == ENOENT) ? 0 : fd;
}

// like open(2), but returns 0 on ENOENT (No such file or directory)
int powercap_constraint_file_open(char* buf, size_t bsize, const char* ct_name, const uint32_t* zones, uint32_t depth,
                                  uint32_t constraint, powercap_constraint_file type, int flags) {
  int fd = open_constraint_file(buf, bsize, ct_name, zones, depth, constraint, type, flags);
  return (fd < 0 && errno == ENOENT) ? 0 : fd;
}

int powercap_control_type_open(powercap_control_type* pct, char* buf, size_t bsize, const char* ct_name, int ro) {
  return ((pct->enabled = powercap_control_type_file_open(buf, bsize, ct_name, POWERCAP_CONTROL_TYPE_FILE_ENABLED,
                                                          ro ? O_RDONLY : O_RDWR)) < 0)
         ? -1 : 0;
}

int powercap_zone_open(powercap_zone* pz, char* buf, size_t bsize, const char* ct_name, const uint32_t* zones,
                       uint32_t depth, int ro) {
  return ((pz->max_energy_range_uj =
            powercap_zone_file_open(buf, bsize, ct_name, zones, depth,
                                    POWERCAP_ZONE_FILE_MAX_ENERGY_RANGE_UJ, O_RDONLY)) < 0) ||
         // special case for energy_uj - it's allowed to be either RW or RO
         (
          ((pz->energy_uj =
              powercap_zone_file_open(buf, bsize, ct_name, zones, depth,
                                      POWERCAP_ZONE_FILE_ENERGY_UJ, ro ? O_RDONLY : O_RDWR)) < 0) &&
          (ro ||
            ((pz->energy_uj =
                powercap_zone_file_open(buf, bsize, ct_name, zones, depth,
                                        POWERCAP_ZONE_FILE_ENERGY_UJ, O_RDONLY)) < 0))
         ) ||
         ((pz->max_power_range_uw =
            powercap_zone_file_open(buf, bsize, ct_name, zones, depth,
                                    POWERCAP_ZONE_FILE_MAX_POWER_RANGE_UW, O_RDONLY)) < 0) ||
         ((pz->power_uw =
            powercap_zone_file_open(buf, bsize, ct_name, zones, depth,
                                    POWERCAP_ZONE_FILE_POWER_UW, O_RDONLY)) < 0) ||
         ((pz->enabled =
            powercap_zone_file_open(buf, bsize, ct_name, zones, depth,
                                    POWERCAP_ZONE_FILE_ENABLED, ro ? O_RDONLY : O_RDWR)) < 0) ||
         ((pz->name =
            powercap_zone_file_open(buf, bsize, ct_name, zones, depth,
                                    POWERCAP_ZONE_FILE_NAME, O_RDONLY)) < 0)
         ? -1 : 0;
}

int powercap_constraint_open(powercap_constraint* pc, char* buf, size_t bsize, const char* ct_name,
                             const uint32_t* zones, uint32_t depth, uint32_t constraint, int ro) {
  return ((pc->power_limit_uw =
            powercap_constraint_file_open(buf, bsize, ct_name, zones, depth, constraint,
                                          POWERCAP_CONSTRAINT_FILE_POWER_LIMIT_UW, ro ? O_RDONLY : O_RDWR)) < 0) ||
         ((pc->time_window_us =
            powercap_constraint_file_open(buf, bsize, ct_name, zones, depth, constraint,
                                          POWERCAP_CONSTRAINT_FILE_TIME_WINDOW_US, ro ? O_RDONLY : O_RDWR)) < 0) ||
         ((pc->max_power_uw =
            powercap_constraint_file_open(buf, bsize, ct_name, zones, depth, constraint,
                                          POWERCAP_CONSTRAINT_FILE_MAX_POWER_UW, O_RDONLY)) < 0) ||
         ((pc->min_power_uw =
            powercap_constraint_file_open(buf, bsize, ct_name, zones, depth, constraint,
                                          POWERCAP_CONSTRAINT_FILE_MIN_POWER_UW, O_RDONLY)) < 0) ||
         ((pc->max_time_window_us =
            powercap_constraint_file_open(buf, bsize, ct_name, zones, depth, constraint,
                                          POWERCAP_CONSTRAINT_FILE_MAX_TIME_WINDOW_US, O_RDONLY)) < 0) ||
         ((pc->min_time_window_us =
            powercap_constraint_file_open(buf, bsize, ct_name, zones, depth, constraint,
                                          POWERCAP_CONSTRAINT_FILE_MIN_TIME_WINDOW_US, O_RDONLY)) < 0) ||
         ((pc->name =
            powercap_constraint_file_open(buf, bsize, ct_name, zones, depth, constraint,
                                          POWERCAP_CONSTRAINT_FILE_NAME, O_RDONLY)) < 0)
         ? -1 : 0;
}

static int powercap_close(int fd) {
  return (fd > 0 && close(fd)) ? -1 : 0;
}

int powercap_control_type_close(powercap_control_type* pct) {
  return powercap_close(pct->enabled);
}

int powercap_zone_close(powercap_zone* pz) {
  int rc = 0;
  rc |= powercap_close(pz->max_energy_range_uj);
  rc |= powercap_close(pz->energy_uj);
  rc |= powercap_close(pz->max_power_range_uw);
  rc |= powercap_close(pz->power_uw);
  rc |= powercap_close(pz->enabled);
  rc |= powercap_close(pz->name);
  return rc;
}

int powercap_constraint_close(powercap_constraint* pc) {
  int rc = 0;
  rc |= powercap_close(pc->power_limit_uw);
  rc |= powercap_close(pc->time_window_us);
  rc |= powercap_close(pc->max_power_uw);
  rc |= powercap_close(pc->min_power_uw);
  rc |= powercap_close(pc->max_time_window_us);
  rc |= powercap_close(pc->min_time_window_us);
  rc |= powercap_close(pc->name);
  return rc;
}