about summary refs log tree commit diff
path: root/cpu.c
blob: 4a058d72c4d1d93207eabb65bd6cf7f8cf50c71d (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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "collector.h"
#include "util.h"

// limits for CPU numbers
#define MAX_CPU_ID 9999999
#define MAX_CPU_DIGITS 7
// size of input buffer for reading lines
#define BUF_SIZE 256

static void *cpu_init(int argc, char *argv[]);
static void cpu_collect(scrape_req *req, void *ctx_ptr);

const struct collector cpu_collector = {
  .name = "cpu",
  .collect = cpu_collect,
  .init = cpu_init,
};

struct cpu_context {
  long clock_tick;
};

void *cpu_init(int argc, char *argv[]) {
  (void) argc; (void) argv;

  long clock_tick = sysconf(_SC_CLK_TCK);
  if (clock_tick <= 0) {
    perror("sysconf(_SC_CLK_TCK)");
    return 0;
  }

  struct cpu_context *ctx = malloc(sizeof *ctx);
  if (!ctx)
    return 0;
  ctx->clock_tick = clock_tick;
  return ctx;
}

void cpu_collect(scrape_req *req, void *ctx_ptr) {
  struct cpu_context *ctx = ctx_ptr;

  // buffers

  char cpu_label[MAX_CPU_DIGITS + 1] = "";
  static const char *modes[] = {
    "user", "nice", "system", "idle", "iowait", "irq", "softirq", "steal",
    0,
  };

  const char *stat_labels[][2] = {
    { "cpu", cpu_label },
    { "mode", 0 },  // filled by code
    { 0, 0 },
  };
  const char *freq_labels[][2] = {
    { "cpu", cpu_label },
    { 0, 0 },
  };

  char buf[BUF_SIZE];

  FILE *f;

  // collect node_cpu_seconds_total metrics from /proc/stat

  f = fopen("/proc/stat", "r");
  if (f) {
    while (fgets_line(buf, sizeof buf, f)) {
      if (strncmp(buf, "cpu", 3) != 0 || (buf[3] < '0' || buf[3] > '9'))
        continue;

      char *at = buf + 3;
      char *sep = strchr(at, ' ');
      if (!sep || sep - at + 1 > (ptrdiff_t) sizeof cpu_label)
        continue;
      *sep = '\0';
      strcpy(cpu_label, at);

      at = sep + 1;
      for (const char **mode = modes; *mode; mode++) {
        while (*at == ' ')
          at++;
        sep = strpbrk(at, " \n");
        if (!sep)
          break;
        *sep = '\0';

        char *endptr;
        double value = strtod(at, &endptr);
        if (*endptr != '\0')
          break;
        value /= ctx->clock_tick;

        stat_labels[1][1] = *mode;
        scrape_write(req, "node_cpu_seconds_total", stat_labels, value);

        at = sep + 1;
      }
    }
    fclose(f);
  }

  // collect node_cpu_frequency_hertz metrics from /sys/devices/system/cpu/cpu*/cpufreq

  for (int cpu = 0; cpu <= MAX_CPU_ID; cpu++) {
#define PATH_FORMAT "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq"
    char path[sizeof PATH_FORMAT - 2 + MAX_CPU_DIGITS + 1];
    snprintf(path, sizeof path, PATH_FORMAT, cpu);

    f = fopen(path, "r");
    if (!f)
      break;

    if (fgets(buf, sizeof buf, f)) {
      char *endptr;
      double value = strtod(buf, &endptr);
      if (*endptr == '\0' || *endptr == '\n') {
        value *= 1000;
        snprintf(cpu_label, sizeof cpu_label, "%d", cpu);
        scrape_write(req, "node_cpu_frequency_hertz", freq_labels, value);
      }
    }

    fclose(f);
  }
}