Turbostat: re-order init tests
[collectd.git] / src / turbostat.c
index 06f15cd..34c11b8 100644 (file)
  */
 #define _GNU_SOURCE
 
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "utils_time.h"
+
 #include <asm/msr-index.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -46,7 +51,6 @@
 #include <sys/resource.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <sys/time.h>
 #include <stdlib.h>
 #include <dirent.h>
 #include <string.h>
 #include <sched.h>
 #include <cpuid.h>
 
-#include "collectd.h"
-#include "common.h"
-#include "plugin.h"
-
 #define PLUGIN_NAME "turbostat"
 
 /*
@@ -145,10 +145,10 @@ static double rapl_energy_units;
                                        /* 0x642 MSR_PP1_POLICY */
 #define        TJMAX_DEFAULT   100
 
-cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
-size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
+static cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
+static size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
 
-struct thread_data {
+static struct thread_data {
        unsigned long long tsc;
        unsigned long long aperf;
        unsigned long long mperf;
@@ -160,7 +160,7 @@ struct thread_data {
 #define CPU_IS_FIRST_CORE_IN_PACKAGE   0x4
 } *thread_delta, *thread_even, *thread_odd;
 
-struct core_data {
+static struct core_data {
        unsigned long long c3;
        unsigned long long c6;
        unsigned long long c7;
@@ -168,7 +168,7 @@ struct core_data {
        unsigned int core_id;
 } *core_delta, *core_even, *core_odd;
 
-struct pkg_data {
+static struct pkg_data {
        unsigned long long pc2;
        unsigned long long pc3;
        unsigned long long pc6;
@@ -213,7 +213,7 @@ struct cpu_topology {
        _Bool first_thread_in_core;
 };
 
-struct topology {
+static struct topology {
        int max_cpu_id;
        int num_packages;
        int num_cores;
@@ -221,49 +221,13 @@ struct topology {
        struct cpu_topology *cpus;
 } topology;
 
-struct timeval tv_even, tv_odd, tv_delta;
-
-enum return_values {
-       OK = 0,
-       ERR_CPU_MIGRATE,
-       ERR_CPU_SAVE_SCHED_AFFINITY,
-       ERR_MSR_IA32_APERF,
-       ERR_MSR_IA32_MPERF,
-       ERR_MSR_SMI_COUNT,
-       ERR_MSR_CORE_C3_RESIDENCY,
-       ERR_MSR_CORE_C6_RESIDENCY,
-       ERR_MSR_CORE_C7_RESIDENCY,
-       ERR_MSR_IA32_THERM_STATUS,
-       ERR_MSR_PKG_C3_RESIDENCY,
-       ERR_MSR_PKG_C6_RESIDENCY,
-       ERR_MSR_PKG_C2_RESIDENCY,
-       ERR_MSR_PKG_C7_RESIDENCY,
-       ERR_MSR_PKG_C8_RESIDENCY,
-       ERR_MSR_PKG_C9_RESIDENCY,
-       ERR_MSR_PKG_C10_RESIDENCY,
-       ERR_MSR_PKG_ENERGY_STATUS,
-       ERR_MSR_PKG_POWER_INFO,
-       ERR_MSR_PP0_ENERGY_STATUS,
-       ERR_MSR_DRAM_ENERGY_STATUS,
-       ERR_MSR_PP1_ENERGY_STATUS,
-       ERR_MSR_PKG_PERF_STATUS,
-       ERR_MSR_DRAM_PERF_STATUS,
-       ERR_MSR_IA32_PACKAGE_THERM_STATUS,
-       ERR_MSR_IA32_TSC,
-       ERR_CPU_NOT_PRESENT,
-       ERR_NO_MSR,
-       ERR_CANT_OPEN_MSR,
-       ERR_CANT_OPEN_FILE,
-       ERR_CANT_READ_NUMBER,
-       ERR_CANT_READ_PROC_STAT,
-       ERR_NO_INVARIANT_TSC,
-       ERR_NO_APERF,
-       ERR_CALLOC,
-       ERR_CPU_ALLOC,
-       ERR_NOT_ROOT,
-       UNSUPPORTED_CPU,
-};
+static cdtime_t time_even, time_odd, time_delta;
 
+static const char *config_keys[] =
+{
+       "TCCActivationTemp",
+};
+static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 /*****************************
  *  MSR Manipulation helpers *
@@ -290,15 +254,17 @@ open_msr(int cpu, _Bool multiple_read)
                CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
                CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
                if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) {
-                       ERROR("Could not migrate to CPU %d", cpu);
-                       return -ERR_CPU_MIGRATE;
+                       ERROR("Turbostat plugin: Could not migrate to CPU %d", cpu);
+                       return -1;
                }
        }
 
        ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
        fd = open(pathname, O_RDONLY);
-       if (fd < 0)
-               return -ERR_CANT_OPEN_MSR;
+       if (fd < 0) {
+               ERROR("Turbostat plugin: failed to open %s", pathname);
+               return -1;
+       }
        return fd;
 }
 
@@ -313,7 +279,8 @@ read_msr(int fd, off_t offset, unsigned long long *msr)
        retval = pread(fd, msr, sizeof *msr, offset);
 
        if (retval != sizeof *msr) {
-               ERROR("MSR offset 0x%llx read failed", (unsigned long long)offset);
+               ERROR("Turbostat plugin: MSR offset 0x%llx read failed",
+                     (unsigned long long)offset);
                return -1;
        }
        return 0;
@@ -362,12 +329,13 @@ get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
        if (msr_fd < 0)
                return msr_fd;
 
-#define READ_MSR(msr, dst)                     \
-do {                                           \
-       if (read_msr(msr_fd, msr, dst)) {       \
-               retval = -ERR_##msr;            \
-               goto out;                       \
-       }                                       \
+#define READ_MSR(msr, dst)                                             \
+do {                                                                   \
+       if (read_msr(msr_fd, msr, dst)) {                               \
+               ERROR("Turbostat plugin: Unable to read " #msr);        \
+               retval = -1;                                            \
+               goto out;                                               \
+       }                                                               \
 } while (0)
 
        READ_MSR(MSR_IA32_TSC, &t->tsc);
@@ -402,20 +370,20 @@ do {                                              \
                goto out;
        }
 
-        if (do_pkg_cstate & (1 << 2))
-                READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
-        if (do_pkg_cstate & (1 << 3))
-                READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
-        if (do_pkg_cstate & (1 << 6))
-                READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
-        if (do_pkg_cstate & (1 << 7))
-                READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
-        if (do_pkg_cstate & (1 << 8))
-                READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
-        if (do_pkg_cstate & (1 << 9))
-                READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
-        if (do_pkg_cstate & (1 << 10))
-                READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
+       if (do_pkg_cstate & (1 << 2))
+               READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
+       if (do_pkg_cstate & (1 << 3))
+               READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
+       if (do_pkg_cstate & (1 << 6))
+               READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
+       if (do_pkg_cstate & (1 << 7))
+               READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
+       if (do_pkg_cstate & (1 << 8))
+               READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
+       if (do_pkg_cstate & (1 << 9))
+               READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
+       if (do_pkg_cstate & (1 << 10))
+               READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
 
        if (do_rapl & RAPL_PKG) {
                READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
@@ -515,9 +483,10 @@ delta_thread(struct thread_data *delta, const struct thread_data *new, const str
 
        /* check for TSC < 1 Mcycles over interval */
        if (delta->tsc < (1000 * 1000)) {
-               WARNING("Insanely slow TSC rate, TSC stops in idle? ");
-               WARNING("You can disable all c-states by booting with \"idle=poll\" ");
-               WARNING("or just the deep ones with \"processor.max_cstate=1\"");
+               WARNING("Turbostat plugin: Insanely slow TSC rate, TSC stops "
+                       "in idle? You can disable all c-states by booting with"
+                       " 'idle=poll' or just the deep ones with"
+                       " 'processor.max_cstate=1'");
                return -1;
        }
 
@@ -528,9 +497,10 @@ delta_thread(struct thread_data *delta, const struct thread_data *new, const str
                delta->mperf = new->mperf - old->mperf;
        } else {
                if (!aperf_mperf_unstable) {
-                       WARNING(" APERF or MPERF went backwards * ");
-                       WARNING("* Frequency results do not cover entire interval *");
-                       WARNING("* fix this by running Linux-2.6.30 or later *");
+                       WARNING("Turbostat plugin: APERF or MPERF went "
+                               "backwards. Frequency results do not cover "
+                               "the entire interval. Fix this by running "
+                               "Linux-2.6.30 or later.");
 
                        aperf_mperf_unstable = 1;
                }
@@ -550,7 +520,7 @@ delta_thread(struct thread_data *delta, const struct thread_data *new, const str
        }
 
        if (delta->mperf == 0) {
-               WARNING("cpu%d MPERF 0!", old->cpu_id);
+               WARNING("Turbostat plugin: cpu%d MPERF 0!", old->cpu_id);
                delta->mperf = 1;       /* divide by 0 protection */
        }
 
@@ -597,10 +567,10 @@ turbostat_submit (const char *plugin_instance,
 static int
 submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
 {
-       char name[12];
+       char name[DATA_MAX_NAME_LEN];
        double interval_float;
 
-       interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
+       interval_float = CDTIME_T_TO_DOUBLE(time_delta);
 
        ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
 
@@ -836,7 +806,8 @@ set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_da
 
 guess:
        p->tcc_activation_temp = TJMAX_DEFAULT;
-       WARNING("cpu%d: Guessing tjMax %d C, Please use TCCActivationTemp to specify",
+       WARNING("Turbostat plugin: cpu%d: Guessing tjMax %d C,"
+               " Please use TCCActivationTemp to specify it.",
                t->cpu_id, p->tcc_activation_temp);
 
        return 0;
@@ -855,25 +826,25 @@ probe_cpu()
         * - EAX: Maximum Input Value for Basic CPUID Information
         * - EBX: "Genu" (0x756e6547)
         * - EDX: "ineI" (0x49656e69)
-         * - ECX: "ntel" (0x6c65746e)
-         */
+        * - ECX: "ntel" (0x6c65746e)
+        */
        max_level = ebx = ecx = edx = 0;
        __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
        if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
-               ERROR("Unsupported CPU");
-               return -UNSUPPORTED_CPU;
+               ERROR("Turbostat plugin: Unsupported CPU (not Intel)");
+               return -1;
        }
 
        /* CPUID(1):
         * - EAX: Version Information: Type, Family, Model, and Stepping ID
-         *  + 4-7:   Model ID
-         *  + 8-11:  Family ID
+        *  + 4-7:   Model ID
+        *  + 8-11:  Family ID
         *  + 12-13: Processor type
         *  + 16-19: Extended Model ID
         *  + 20-27: Extended Family ID
         * - EDX: Feature Information:
         *  + 5: Support for MSR read/write operations
-         */
+        */
        fms = ebx = ecx = edx = 0;
        __get_cpuid(1, &fms, &ebx, &ecx, &edx);
        family = (fms >> 8) & 0xf;
@@ -883,8 +854,8 @@ probe_cpu()
        if (family == 6 || family == 0xf)
                model += ((fms >> 16) & 0xf) << 4;
        if (!(edx & (1 << 5))) {
-               ERROR("CPUID: no MSR");
-               return -ERR_NO_MSR;
+               ERROR("Turbostat plugin: Unsupported CPU (no MSR support)");
+               return -1;
        }
 
        /*
@@ -898,8 +869,9 @@ probe_cpu()
        max_level = ebx = ecx = edx = 0;
        __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
        if (max_level < 0x80000007) {
-               ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
-               return -ERR_NO_INVARIANT_TSC;
+               ERROR("Turbostat plugin: Unsupported CPU (no invariant TSC, "
+                     " Maximum Extended Function: 0x%x)", max_level);
+               return -1;
        }
 
        /*
@@ -912,8 +884,8 @@ probe_cpu()
        eax = ebx = ecx = edx = 0;
        __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
        if (!(edx & (1 << 8))) {
-               ERROR("No invariant TSC");
-               return -ERR_NO_INVARIANT_TSC;
+               ERROR("Turbostat plugin: Unsupported CPU (No invariant TSC)");
+               return -1;
        }
 
        /*
@@ -933,8 +905,8 @@ probe_cpu()
        do_dts = eax & (1 << 0);
        do_ptm = eax & (1 << 6);
        if (!(ecx & (1 << 0))) {
-               ERROR("No APERF");
-               return -ERR_NO_APERF;
+               ERROR("Turbostat plugin: Unsupported CPU (No APERF)");
+               return -1;
        }
 
        /*
@@ -1005,7 +977,8 @@ probe_cpu()
                        do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
                        break;
                default:
-                       ERROR("Unsupported CPU");
+                       ERROR("Turbostat plugin: Unsupported CPU (family: %#x,"
+                             " model: %#x)", family, model);
                }
                switch (model) {
                case 0x2A:
@@ -1030,8 +1003,9 @@ probe_cpu()
                        do_rapl = 0;
                }
        } else {
-               ERROR("Unsupported CPU");
-               return -UNSUPPORTED_CPU;
+               ERROR("Turbostat plugin: Unsupported CPU (family: %#x, "
+                     "model: %#x)", family, model);
+               return -1;
        }
 
        if (do_rapl) {
@@ -1062,19 +1036,24 @@ parse_int_file(const char *fmt, ...)
        va_list args;
        char path[PATH_MAX];
        FILE *filep;
-       int value;
+       int len, value;
 
        va_start(args, fmt);
-       vsnprintf(path, sizeof(path), fmt, args);
+       len = vsnprintf(path, sizeof(path), fmt, args);
        va_end(args);
+       if (len < 0 || len >= PATH_MAX) {
+               ERROR("Turbostat plugin: path truncated: '%s'", path);
+               return -1;
+       }
+
        filep = fopen(path, "r");
        if (!filep) {
-               ERROR("%s: open failed", path);
-               return -ERR_CANT_OPEN_FILE;
+               ERROR("Turbostat plugin: Failed to open '%s'", path);
+               return -1;
        }
        if (fscanf(filep, "%d", &value) != 1) {
-               ERROR("%s: failed to parse number from file", path);
-               return -ERR_CANT_READ_NUMBER;
+               ERROR("Turbostat plugin: Failed to parse number from '%s'", path);
+               return -1;
        }
        fclose(filep);
        return value;
@@ -1091,10 +1070,10 @@ get_threads_on_core(int cpu)
 
        ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
        filep = fopen(path, "r");
-        if (!filep) {
-                ERROR("%s: open failed", path);
-                return -ERR_CANT_OPEN_FILE;
-        }
+       if (!filep) {
+               ERROR("Turbostat plugin: Failed to open '%s'", path);
+               return -1;
+       }
        /*
         * file format:
         * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
@@ -1122,16 +1101,16 @@ for_all_proc_cpus(int (func)(int))
        int retval;
 
        fp = fopen("/proc/stat", "r");
-        if (!fp) {
-                ERROR("Failed to open /proc/stat");
-                return -ERR_CANT_OPEN_FILE;
-        }
+       if (!fp) {
+               ERROR("Turbostat plugin: Failed to open /proc/stat");
+               return -1;
+       }
 
        retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
        if (retval != 0) {
-               ERROR("Failed to parse /proc/stat");
+               ERROR("Turbostat plugin: Failed to parse /proc/stat");
                fclose(fp);
-               return -ERR_CANT_READ_PROC_STAT;
+               return -1;
        }
 
        while (1) {
@@ -1171,8 +1150,8 @@ static int __attribute__((warn_unused_result))
 allocate_cpu_set(cpu_set_t ** set, size_t * size) {
        *set = CPU_ALLOC(topology.max_cpu_id  + 1);
        if (*set == NULL) {
-               ERROR("Unable to allocate CPU state");
-               return -ERR_CPU_ALLOC;
+               ERROR("Turbostat plugin: Unable to allocate CPU state");
+               return -1;
        }
        *size = CPU_ALLOC_SIZE(topology.max_cpu_id  + 1);
        CPU_ZERO_S(*size, *set);
@@ -1200,8 +1179,8 @@ topology_probe()
 
        topology.cpus = calloc(1, (topology.max_cpu_id  + 1) * sizeof(struct cpu_topology));
        if (topology.cpus == NULL) {
-               ERROR("Unable to allocate memory for cpu topology");
-               return -ERR_CALLOC;
+               ERROR("Turbostat plugin: Unable to allocate memory for CPU topology");
+               return -1;
        }
 
        ret = allocate_cpu_set(&cpu_present_set, &cpu_present_setsize);
@@ -1227,7 +1206,7 @@ topology_probe()
                struct cpu_topology *cpu = &topology.cpus[i];
 
                if (cpu_is_not_present(i)) {
-                       WARNING("cpu%d NOT PRESENT", i);
+                       WARNING("Turbostat plugin: cpu%d NOT PRESENT", i);
                        continue;
                }
 
@@ -1265,7 +1244,7 @@ topology_probe()
                else if (ret == i)
                        cpu->first_thread_in_core = 1;
 
-               DEBUG("cpu %d pkg %d core %d\n",
+               DEBUG("Turbostat plugin: cpu %d pkg %d core %d\n",
                        i, cpu->package_id, cpu->core_id);
        }
        /* Num is max + 1 (need to count 0) */
@@ -1320,11 +1299,11 @@ err_clean_cores:
 err_clean_threads:
        free(*threads);
 err:
-       ERROR("calloc counters");
-       return -ERR_CALLOC;
+       ERROR("Turbostat plugin: Failled to allocate memory for counters");
+       return -1;
 }
 
-static int
+static void
 init_counter(struct thread_data *thread_base, struct core_data *core_base,
        struct pkg_data *pkg_base, int cpu_id)
 {
@@ -1345,32 +1324,20 @@ init_counter(struct thread_data *thread_base, struct core_data *core_base,
 
        c->core_id = cpu->core_id;
        p->package_id = cpu->package_id;
-
-       return 0;
 }
 
-static int
+static void
 initialize_counters(void)
 {
-       int ret;
        int cpu_id;
 
        for (cpu_id = 0; cpu_id <= topology.max_cpu_id; ++cpu_id) {
-               if (cpu_is_not_present(cpu_id)) {
+               if (cpu_is_not_present(cpu_id))
                        continue;
-               }
-
-               ret = init_counter(EVEN_COUNTERS, cpu_id);
-               if (ret < 0)
-                       return ret;
-               ret = init_counter(ODD_COUNTERS, cpu_id);
-               if (ret < 0)
-                       return ret;
-               ret = init_counter(DELTA_COUNTERS, cpu_id);
-               if (ret < 0)
-                       return ret;
+               init_counter(EVEN_COUNTERS, cpu_id);
+               init_counter(ODD_COUNTERS, cpu_id);
+               init_counter(DELTA_COUNTERS, cpu_id);
        }
-       return 0;
 }
 
 
@@ -1424,10 +1391,10 @@ free_all_buffers(void)
  **********************/
 
 #define DO_OR_GOTO_ERR(something) \
-do {                         \
-       ret = (something);     \
-       if (ret < 0)         \
-               goto err;    \
+do {                             \
+       ret = (something);        \
+       if (ret < 0)              \
+               goto err;         \
 } while (0)
 
 static int setup_all_buffers(void)
@@ -1438,7 +1405,7 @@ static int setup_all_buffers(void)
        DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
        DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
        DO_OR_GOTO_ERR(allocate_counters(&thread_delta, &core_delta, &package_delta));
-       DO_OR_GOTO_ERR(initialize_counters());
+       initialize_counters();
        DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
        DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
 
@@ -1450,7 +1417,7 @@ err:
 }
 
 static int
-turbostat_read(user_data_t * not_used)
+turbostat_read(void)
 {
        int ret;
 
@@ -1463,18 +1430,23 @@ turbostat_read(user_data_t * not_used)
                free_all_buffers();
                if ((ret = setup_all_buffers()) < 0)
                        return ret;
-               if (for_all_proc_cpus(cpu_is_not_present))
-                       return -ERR_CPU_NOT_PRESENT;
+               if (for_all_proc_cpus(cpu_is_not_present)) {
+                       ERROR("Turbostat plugin: CPU appeared just after "
+                             "initialization");
+                       return -1;
+               }
        }
 
        /* Saving the scheduling affinity, as it will be modified by get_counters */
-       if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0)
-               return -ERR_CPU_SAVE_SCHED_AFFINITY;
+       if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0) {
+               ERROR("Turbostat plugin: Unable to save the CPU affinity");
+               return -1;
+       }
 
        if (!initialized) {
                if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
                        goto out;
-               gettimeofday(&tv_even, (struct timezone *)NULL);
+               time_even = cdtime();
                is_even = 1;
                initialized = 1;
                ret = 0;
@@ -1484,9 +1456,9 @@ turbostat_read(user_data_t * not_used)
        if (is_even) {
                if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
                        goto out;
-               gettimeofday(&tv_odd, (struct timezone *)NULL);
+               time_odd = cdtime();
                is_even = 0;
-               timersub(&tv_odd, &tv_even, &tv_delta);
+               time_delta = time_odd - time_even;
                if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
                        goto out;
                if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
@@ -1494,9 +1466,9 @@ turbostat_read(user_data_t * not_used)
        } else {
                if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
                        goto out;
-               gettimeofday(&tv_even, (struct timezone *)NULL);
+               time_even = cdtime();
                is_even = 1;
-               timersub(&tv_even, &tv_odd, &tv_delta);
+               time_delta = time_even - time_odd;
                if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
                        goto out;
                if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
@@ -1518,21 +1490,26 @@ turbostat_init(void)
        struct stat sb;
        int ret;
 
+       if (stat("/dev/cpu/0/msr", &sb)) {
+               ERROR("Turbostat plugin: Initialization failed: /dev/cpu/0/msr"
+                     " does not exist while the CPU supports MSR. You may be "
+                     "missing the corresponding kernel module, please try '# "
+                     "modprobe msr'");
+               return -1;
+       }
+
        if (getuid() != 0) {
-               ERROR("must be root");
-               return -ERR_NOT_ROOT;
+               ERROR("Turbostat plugin: Initialization failed: this plugin "
+                     "requires collectd to run as root in order to read "
+                     "special CPU registers");
+               return -1;
        }
 
        DO_OR_GOTO_ERR(probe_cpu());
 
-       if (stat("/dev/cpu/0/msr", &sb)) {
-               ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
-               return -ERR_NO_MSR;
-       }
-
        DO_OR_GOTO_ERR(setup_all_buffers());
 
-       plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
+       plugin_register_read(PLUGIN_NAME, turbostat_read);
 
        return 0;
 err:
@@ -1540,12 +1517,6 @@ err:
        return ret;
 }
 
-static const char *config_keys[] =
-{
-       "TCCActivationTemp",
-};
-static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-
 static int
 turbostat_config(const char *key, const char *value)
 {
@@ -1554,16 +1525,20 @@ turbostat_config(const char *key, const char *value)
 
        if (strcasecmp("TCCActivationTemp", key) == 0) {
                tmp_val = strtoul(value, &end, 0);
-               if (*end != '\0' || tmp_val > UINT_MAX)
+               if (*end != '\0' || tmp_val > UINT_MAX) {
+                       ERROR("Turbostat plugin: Invalid TCCActivationTemp '%s'",
+                             value);
                        return -1;
+               }
                tcc_activation_temp = (unsigned int) tmp_val;
        } else {
+               ERROR("Turbostat plugin: Invalid configuration option '%s'",
+                     key);
                return -1;
        }
        return 0;
 }
 
-void module_register(void);
 void module_register(void)
 {
        plugin_register_init(PLUGIN_NAME, turbostat_init);