about summary refs log tree commit diff
path: root/rapl.c
blob: 2a5e472a024e1c81b57428d80edb63d3bfb48de2 (plain) (blame)
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
#define _GNU_SOURCE
#include <fcntl.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "scrape.h"
#include "util.h"

// size of input buffer for paths and lines
#define BUF_SIZE 256

static void rapl_collect(scrape_req *req, void *ctx);

const struct collector rapl_collector = {
  .name = "rapl",
  .collect = rapl_collect,
};

static void rapl_collect_zone(scrape_req *req, char *path) {
  char name[BUF_SIZE];
  char buf[BUF_SIZE];

  if (read_file_at(AT_FDCWD, path, buf, sizeof buf) < 0)
    return;

  char *slash = strrchr(path, '/');
  if (!slash)
    return;
  *slash = 0;

  strcat(path, "/name");

  if (read_file_at(AT_FDCWD, path, name, sizeof name) < 0)
    return;

  *slash = 0;

  struct label rapl_label[] = {
    { .key = "rapl_zone", .value = path + strlen("/sys/class/powercap/") },
    { .key = "name", .value = name },
    LABEL_END,
  };

  scrape_write(req, "node_rapl_joules_total", rapl_label, atof(buf) / 1e6);
}

static void rapl_collect(scrape_req *req, void *ctx) {
  (void) ctx;

  // scan /sys/class/powercap

  glob_t globbuf = { 0 };
  if (glob("/sys/class/powercap/*/energy_uj", GLOB_NOSORT, 0, &globbuf) != 0)
    return;

  for (size_t i = 0; i < globbuf.gl_pathc; i++)
    rapl_collect_zone(req, globbuf.gl_pathv[i]);

  globfree(&globbuf);
}