Turbostat: re-order init tests
[collectd.git] / src / turbostat.c
index 5236eee..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,7 +221,13 @@ struct topology {
        struct cpu_topology *cpus;
 } topology;
 
-struct timeval tv_even, tv_odd, tv_delta;
+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 *
@@ -564,7 +570,7 @@ submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
        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);
 
@@ -1440,7 +1446,7 @@ turbostat_read(void)
        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;
@@ -1450,9 +1456,9 @@ turbostat_read(void)
        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)
@@ -1460,9 +1466,9 @@ turbostat_read(void)
        } 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)
@@ -1484,6 +1490,14 @@ 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("Turbostat plugin: Initialization failed: this plugin "
                      "requires collectd to run as root in order to read "
@@ -1493,14 +1507,6 @@ turbostat_init(void)
 
        DO_OR_GOTO_ERR(probe_cpu());
 
-       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;
-       }
-
        DO_OR_GOTO_ERR(setup_all_buffers());
 
        plugin_register_read(PLUGIN_NAME, turbostat_read);
@@ -1511,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)
 {