about summary refs log tree commit diff
path: root/cpu.c
diff options
context:
space:
mode:
authorHeikki Kallasjoki <fis@zem.fi>2018-12-07 00:17:09 +0000
committerHeikki Kallasjoki <fis+github@zem.fi>2018-12-12 19:10:51 +0000
commit8569c2c85bec19daa36e013448399c42cf75a257 (patch)
tree7444488e4e5ae5c98f575a2eafd522c6427ffbdf /cpu.c
parentd03dea4d7dbfb62ed0c3c21cdd9ad350f0526432 (diff)
downloadnano-exporter-8569c2c85bec19daa36e013448399c42cf75a257.tar.gz
nano-exporter-8569c2c85bec19daa36e013448399c42cf75a257.tar.xz
nano-exporter-8569c2c85bec19daa36e013448399c42cf75a257.zip
Add a simple test harness and tests for the cpu collector.
Diffstat (limited to 'cpu.c')
-rw-r--r--cpu.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/cpu.c b/cpu.c
index d8f7886..d8bed66 100644
--- a/cpu.c
+++ b/cpu.c
@@ -84,7 +84,7 @@ void cpu_collect(scrape_req *req, void *ctx_ptr) {
 
   // collect node_cpu_seconds_total metrics from /proc/stat
 
-  f = fopen("/proc/stat", "r");
+  f = fopen(PATH("/proc/stat"), "r");
   if (f) {
     while (fgets_line(buf, sizeof buf, f)) {
       if (strncmp(buf, "cpu", 3) != 0 || (buf[3] < '0' || buf[3] > '9'))
@@ -124,7 +124,7 @@ void cpu_collect(scrape_req *req, void *ctx_ptr) {
   // 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"
+#define PATH_FORMAT PATH("/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);
 
@@ -145,3 +145,9 @@ void cpu_collect(scrape_req *req, void *ctx_ptr) {
     fclose(f);
   }
 }
+
+// exposed only for testing
+
+void cpu_test_override_tick(void *ctx, long tick) {
+  ((struct cpu_context *) ctx)->clock_tick = tick;
+}