Turbostat: Don't manage 'Interval' manually, already done by collectd
[collectd.git] / src / turbostat.c
index e6b59d9..19f0786 100644 (file)
@@ -2,7 +2,9 @@
  * turbostat -- Log CPU frequency and C-state residency
  * on modern Intel turbo-capable processors for collectd.
  *
- * Based on the kernel tools by:
+ * Based on the 'turbostat' tool of the Linux kernel, found at
+ * linux/tools/power/x86/turbostat/turbostat.c:
+ * ----
  * Copyright (c) 2013 Intel Corporation.
  * Len Brown <len.brown@intel.com>
  *
  * You should have received a copy of the GNU General Public License along with
  * this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
- *
+ * ----
  * Ported to collectd by Vincent Brillault <git@lerya.net>
  */
 
 #define _GNU_SOURCE
-#include "msr-index.h"
+#include <asm/msr-index.h>
 #include <stdarg.h>
 #include <stdio.h>
-#include <stdbool.h>
 #include <err.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -50,7 +51,6 @@
 #define PLUGIN_NAME "turbostat"
 
 static const char *proc_stat = "/proc/stat";
-static unsigned int interval_sec = 5;  /* set with -i interval_sec */
 static unsigned int skip_c0;
 static unsigned int skip_c1;
 static unsigned int do_nhm_cstates;
@@ -151,10 +151,10 @@ struct pkg_data {
 
 #define ODD_COUNTERS thread_odd, core_odd, package_odd
 #define EVEN_COUNTERS thread_even, core_even, package_even
-static bool is_even = true;
+static _Bool is_even = 1;
 
-static bool allocated = false;
-static bool initialized = false;
+static _Bool allocated = 0;
+static _Bool initialized = 0;
 
 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
        (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
@@ -211,8 +211,6 @@ enum return_values {
        ERR_NOT_ROOT,
 };
 
-#define __must_check __attribute__((warn_unused_result))
-
 static int setup_all_buffers(void);
 
 static int
@@ -225,7 +223,7 @@ cpu_is_not_present(int cpu)
  * skip non-present cpus
  */
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
        struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
 {
@@ -256,7 +254,7 @@ for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_dat
        return 0;
 }
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 cpu_migrate(int cpu)
 {
        CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
@@ -267,14 +265,14 @@ cpu_migrate(int cpu)
                return 0;
 }
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 get_msr(int cpu, off_t offset, unsigned long long *msr)
 {
        ssize_t retval;
        char pathname[32];
        int fd;
 
-       sprintf(pathname, "/dev/cpu/%d/msr", cpu);
+       ssnprintf(pathname, 32, "/dev/cpu/%d/msr", cpu);
        fd = open(pathname, O_RDONLY);
        if (fd < 0)
                return -1;
@@ -329,7 +327,7 @@ delta_core(struct core_data *new, struct core_data *old)
 /*
  * old = new - old
  */
-static int __must_check
+static int __attribute__((warn_unused_result))
 delta_thread(struct thread_data *new, struct thread_data *old,
        struct core_data *core_delta)
 {
@@ -390,7 +388,7 @@ delta_thread(struct thread_data *new, struct thread_data *old,
        return 0;
 }
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 delta_cpu(struct thread_data *t, struct core_data *c,
        struct pkg_data *p, struct thread_data *t2,
        struct core_data *c2, struct pkg_data *p2)
@@ -429,7 +427,7 @@ rdtsc(void)
  * migrate to cpu
  * acquire and record local counters for that cpu
  */
-static int __must_check
+static int __attribute__((warn_unused_result))
 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
 {
        int cpu = t->cpu_id;
@@ -545,8 +543,8 @@ get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
 static void
 free_all_buffers(void)
 {
-       allocated = false;
-       initialized = false;
+       allocated = 0;
+       initialized = 0;
 
        CPU_FREE(cpu_present_set);
        cpu_present_set = NULL;
@@ -576,7 +574,7 @@ free_all_buffers(void)
 /*
  * Parse a file containing a single int.
  */
-static int
+static int __attribute__ ((format(printf,1,2)))
 parse_int_file(const char *fmt, ...)
 {
        va_list args;
@@ -641,7 +639,7 @@ get_num_ht_siblings(int cpu)
        int matches;
        char character;
 
-       sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
+       ssnprintf(path, 80, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
        filep = fopen(path, "r");
         if (!filep) {
                 ERROR("%s: open failed", path);
@@ -668,7 +666,7 @@ get_num_ht_siblings(int cpu)
  */
 
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
        struct pkg_data *, struct thread_data *, struct core_data *,
        struct pkg_data *), struct thread_data *thread_base,
@@ -712,7 +710,7 @@ for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
  * run func(cpu) on every cpu in /proc/stat
  * return max_cpu number
  */
-static int __must_check
+static int __attribute__((warn_unused_result))
 for_all_proc_cpus(int (func)(int))
 {
        FILE *fp;
@@ -907,8 +905,8 @@ turbostat_read(user_data_t * not_used)
                if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
                        return ret;
                gettimeofday(&tv_even, (struct timezone *)NULL);
-               is_even = true;
-               initialized = true;
+               is_even = 1;
+               initialized = 1;
                return 0;
        }
 
@@ -916,7 +914,7 @@ turbostat_read(user_data_t * not_used)
                if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
                        return ret;
                gettimeofday(&tv_odd, (struct timezone *)NULL);
-               is_even = false;
+               is_even = 0;
                timersub(&tv_odd, &tv_even, &tv_delta);
                if ((ret = for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
                        return ret;
@@ -926,7 +924,7 @@ turbostat_read(user_data_t * not_used)
                if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
                        return ret;
                gettimeofday(&tv_even, (struct timezone *)NULL);
-               is_even = true;
+               is_even = 1;
                timersub(&tv_even, &tv_odd, &tv_delta);
                if ((ret = for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
                        return ret;
@@ -936,7 +934,7 @@ turbostat_read(user_data_t * not_used)
        return 0;
 }
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 check_dev_msr()
 {
        struct stat sb;
@@ -949,7 +947,7 @@ check_dev_msr()
        return 0;
 }
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 check_super_user()
 {
        if (getuid() != 0) {
@@ -1107,7 +1105,7 @@ is_slm(unsigned int family, unsigned int model)
  * below this value, including the Digital Thermal Sensor (DTS),
  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
  */
-static int __must_check
+static int __attribute__((warn_unused_result))
 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
 {
        unsigned long long msr;
@@ -1159,7 +1157,7 @@ guess:
        return 0;
 }
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 check_cpuid()
 {
        unsigned int eax, ebx, ecx, edx, max_level;
@@ -1239,7 +1237,7 @@ check_cpuid()
 
 
 
-static int __must_check
+static int __attribute__((warn_unused_result))
 topology_probe()
 {
        int i;
@@ -1470,7 +1468,7 @@ static int setup_all_buffers(void)
        DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
        DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
 
-       allocated = true;
+       allocated = 1;
        return 0;
 err:
        free_all_buffers();
@@ -1481,7 +1479,6 @@ static int
 turbostat_init(void)
 {
        int ret;
-       struct timespec ts;
 
        DO_OR_GOTO_ERR(check_cpuid());
        DO_OR_GOTO_ERR(check_dev_msr());
@@ -1489,10 +1486,7 @@ turbostat_init(void)
        DO_OR_GOTO_ERR(setup_all_buffers());
        DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
 
-       ts.tv_sec = interval_sec;
-       ts.tv_nsec = 0;
-
-       plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, &ts, NULL);
+       plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
 
        return 0;
 err:
@@ -1500,25 +1494,8 @@ err:
        return ret;
 }
 
-static const char *config_keys[] =
-{
-       "Interval",
-};
-static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
-
-static int
-turbostat_config(const char *key, const char *value)
-{
-       if (strcasecmp("Interval", key) == 0)
-               interval_sec = atoi(value);
-       else
-               return -1;
-       return 0;
-}
-
 void module_register(void);
 void module_register(void)
 {
        plugin_register_init(PLUGIN_NAME, turbostat_init);
-       plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);
 }