Turbostat: expand __must_check macro and remove it
[collectd.git] / src / turbostat.c
index 755d00f..128950c 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>
@@ -151,10 +152,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,14 +212,10 @@ enum return_values {
        ERR_NOT_ROOT,
 };
 
-#define STATIC_MUST_CHECK(function)          \
-function                                     \
-       __attribute__((warn_unused_result)); \
-function
-
 static int setup_all_buffers(void);
 
-static int cpu_is_not_present(int cpu)
+static int
+cpu_is_not_present(int cpu)
 {
        return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
 }
@@ -227,8 +224,9 @@ static int cpu_is_not_present(int cpu)
  * skip non-present cpus
  */
 
-STATIC_MUST_CHECK(static int 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))
+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)
 {
        int retval, pkg_no, core_no, thread_no;
 
@@ -257,7 +255,8 @@ STATIC_MUST_CHECK(static int for_all_cpus(int (func)(struct thread_data *, struc
        return 0;
 }
 
-STATIC_MUST_CHECK(static int cpu_migrate(int cpu))
+static int __attribute__((warn_unused_result))
+cpu_migrate(int cpu)
 {
        CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
        CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
@@ -267,7 +266,8 @@ STATIC_MUST_CHECK(static int cpu_migrate(int cpu))
                return 0;
 }
 
-STATIC_MUST_CHECK(static int get_msr(int cpu, off_t offset, unsigned long long *msr))
+static int __attribute__((warn_unused_result))
+get_msr(int cpu, off_t offset, unsigned long long *msr)
 {
        ssize_t retval;
        char pathname[32];
@@ -328,9 +328,9 @@ delta_core(struct core_data *new, struct core_data *old)
 /*
  * old = new - old
  */
-STATIC_MUST_CHECK(static int
+static int __attribute__((warn_unused_result))
 delta_thread(struct thread_data *new, struct thread_data *old,
-       struct core_data *core_delta))
+       struct core_data *core_delta)
 {
        old->tsc = new->tsc - old->tsc;
 
@@ -389,9 +389,10 @@ delta_thread(struct thread_data *new, struct thread_data *old,
        return 0;
 }
 
-STATIC_MUST_CHECK(static int delta_cpu(struct thread_data *t, struct core_data *c,
+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))
+       struct core_data *c2, struct pkg_data *p2)
 {
        int ret;
 
@@ -411,7 +412,8 @@ STATIC_MUST_CHECK(static int delta_cpu(struct thread_data *t, struct core_data *
        return 0;
 }
 
-static unsigned long long rdtsc(void)
+static unsigned long long
+rdtsc(void)
 {
        unsigned int low, high;
 
@@ -426,7 +428,8 @@ static unsigned long long rdtsc(void)
  * migrate to cpu
  * acquire and record local counters for that cpu
  */
-STATIC_MUST_CHECK(static int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p))
+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;
        unsigned long long msr;
@@ -538,10 +541,11 @@ STATIC_MUST_CHECK(static int get_counters(struct thread_data *t, struct core_dat
        return 0;
 }
 
-static void free_all_buffers(void)
+static void
+free_all_buffers(void)
 {
-       allocated = false;
-       initialized = false;
+       allocated = 0;
+       initialized = 0;
 
        CPU_FREE(cpu_present_set);
        cpu_present_set = NULL;
@@ -571,7 +575,8 @@ static void free_all_buffers(void)
 /*
  * Parse a file containing a single int.
  */
-static int parse_int_file(const char *fmt, ...)
+static int
+parse_int_file(const char *fmt, ...)
 {
        va_list args;
        char path[PATH_MAX];
@@ -598,7 +603,8 @@ static int parse_int_file(const char *fmt, ...)
  * cpu_is_first_sibling_in_core(cpu)
  * return 1 if given CPU is 1st HT sibling in the core
  */
-static int cpu_is_first_sibling_in_core(int cpu)
+static int
+cpu_is_first_sibling_in_core(int cpu)
 {
        return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
 }
@@ -607,22 +613,26 @@ static int cpu_is_first_sibling_in_core(int cpu)
  * cpu_is_first_core_in_package(cpu)
  * return 1 if given CPU is 1st core in package
  */
-static int cpu_is_first_core_in_package(int cpu)
+static int
+cpu_is_first_core_in_package(int cpu)
 {
        return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
 }
 
-static int get_physical_package_id(int cpu)
+static int
+get_physical_package_id(int cpu)
 {
        return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
 }
 
-static int get_core_id(int cpu)
+static int
+get_core_id(int cpu)
 {
        return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
 }
 
-static int get_num_ht_siblings(int cpu)
+static int
+get_num_ht_siblings(int cpu)
 {
        char path[80];
        FILE *filep;
@@ -656,13 +666,14 @@ static int get_num_ht_siblings(int cpu)
  * skip non-present cpus
  */
 
-STATIC_MUST_CHECK(
-static int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
+
+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,
        struct core_data *core_base, struct pkg_data *pkg_base,
        struct thread_data *thread_base2, struct core_data *core_base2,
-       struct pkg_data *pkg_base2))
+       struct pkg_data *pkg_base2)
 {
        int retval, pkg_no, core_no, thread_no;
 
@@ -700,7 +711,8 @@ static int 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_MUST_CHECK(static int for_all_proc_cpus(int (func)(int)))
+static int __attribute__((warn_unused_result))
+for_all_proc_cpus(int (func)(int))
 {
        FILE *fp;
        int cpu_num;
@@ -737,7 +749,8 @@ STATIC_MUST_CHECK(static int for_all_proc_cpus(int (func)(int)))
  * count_cpus()
  * remember the last one seen, it will be the max
  */
-static int count_cpus(int cpu)
+static int
+count_cpus(int cpu)
 {
        if (topo.max_cpu_num < cpu)
                topo.max_cpu_num = cpu;
@@ -745,14 +758,16 @@ static int count_cpus(int cpu)
        topo.num_cpus += 1;
        return 0;
 }
-static int mark_cpu_present(int cpu)
+static int
+mark_cpu_present(int cpu)
 {
        CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
        return 0;
 }
 
 
-static void turbostat_submit (const char *plugin_instance,
+static void
+turbostat_submit (const char *plugin_instance,
        const char *type, const char *type_instance,
        gauge_t value)
 {
@@ -790,7 +805,8 @@ static void turbostat_submit (const char *plugin_instance,
  * "CTMP" 4 columns %4d
  */
 #define NAME_LEN 12
-static int submit_counters(struct thread_data *t, struct core_data *c,
+static int
+submit_counters(struct thread_data *t, struct core_data *c,
        struct pkg_data *p)
 {
        char name[NAME_LEN];
@@ -835,7 +851,7 @@ static int submit_counters(struct thread_data *t, struct core_data *c,
        if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
                goto done;
 
-       snprintf(name, NAME_LEN, "pc%02d", p->package_id);
+       snprintf(name, NAME_LEN, "pkg%02d", p->package_id);
 
        if (do_ptm)
                turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
@@ -868,7 +884,8 @@ done:
        return 0;
 }
 
-static int turbostat_read (user_data_t * not_used)
+static int
+turbostat_read(user_data_t * not_used)
 {
        int ret;
 
@@ -889,8 +906,8 @@ static int 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;
        }
 
@@ -898,7 +915,7 @@ static int 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;
@@ -908,7 +925,7 @@ static int 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;
@@ -918,7 +935,8 @@ static int turbostat_read (user_data_t * not_used)
        return 0;
 }
 
-STATIC_MUST_CHECK(static int check_dev_msr())
+static int __attribute__((warn_unused_result))
+check_dev_msr()
 {
        struct stat sb;
 
@@ -930,7 +948,8 @@ STATIC_MUST_CHECK(static int check_dev_msr())
        return 0;
 }
 
-STATIC_MUST_CHECK(static int check_super_user())
+static int __attribute__((warn_unused_result))
+check_super_user()
 {
        if (getuid() != 0) {
                ERROR("must be root");
@@ -943,7 +962,8 @@ STATIC_MUST_CHECK(static int check_super_user())
 #define        RAPL_POWER_GRANULARITY  0x7FFF  /* 15 bit power granularity */
 #define        RAPL_TIME_GRANULARITY   0x3F /* 6 bit time granularity */
 
-static double get_tdp(unsigned int model)
+static double
+get_tdp(unsigned int model)
 {
        unsigned long long msr;
 
@@ -966,7 +986,8 @@ static double get_tdp(unsigned int model)
  *
  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
  */
-static void rapl_probe(unsigned int family, unsigned int model)
+static void
+rapl_probe(unsigned int family, unsigned int model)
 {
        unsigned long long msr;
        unsigned int time_unit;
@@ -1026,7 +1047,8 @@ static void rapl_probe(unsigned int family, unsigned int model)
        return;
 }
 
-static int is_snb(unsigned int family, unsigned int model)
+static int
+is_snb(unsigned int family, unsigned int model)
 {
        if (!genuine_intel)
                return 0;
@@ -1045,7 +1067,8 @@ static int is_snb(unsigned int family, unsigned int model)
        return 0;
 }
 
-static int has_c8_c9_c10(unsigned int family, unsigned int model)
+static int
+has_c8_c9_c10(unsigned int family, unsigned int model)
 {
        if (!genuine_intel)
                return 0;
@@ -1058,7 +1081,8 @@ static int has_c8_c9_c10(unsigned int family, unsigned int model)
 }
 
 
-static int is_slm(unsigned int family, unsigned int model)
+static int
+is_slm(unsigned int family, unsigned int model)
 {
        if (!genuine_intel)
                return 0;
@@ -1082,7 +1106,8 @@ static int 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_MUST_CHECK(static int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p))
+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;
        unsigned int target_c_local;
@@ -1133,7 +1158,8 @@ guess:
        return 0;
 }
 
-STATIC_MUST_CHECK(static int check_cpuid())
+static int __attribute__((warn_unused_result))
+check_cpuid()
 {
        unsigned int eax, ebx, ecx, edx, max_level;
        unsigned int fms, family, model;
@@ -1212,7 +1238,8 @@ STATIC_MUST_CHECK(static int check_cpuid())
 
 
 
-STATIC_MUST_CHECK(static int topology_probe())
+static int __attribute__((warn_unused_result))
+topology_probe()
 {
        int i;
        int ret;
@@ -1364,7 +1391,8 @@ error:
  *
  * increment topo.num_cores when 1st core in pkg seen
  */
-static int init_counter(struct thread_data *thread_base, struct core_data *core_base,
+static int
+init_counter(struct thread_data *thread_base, struct core_data *core_base,
        struct pkg_data *pkg_base, int thread_num, int core_num,
        int pkg_num, int cpu_id)
 {
@@ -1394,7 +1422,8 @@ static int init_counter(struct thread_data *thread_base, struct core_data *core_
 }
 
 
-static int initialize_counters(int cpu_id)
+static int
+initialize_counters(int cpu_id)
 {
        int my_thread_id, my_core_id, my_package_id;
        int ret;
@@ -1426,37 +1455,38 @@ static int initialize_counters(int cpu_id)
 
 #define DO_OR_GOTO_ERR(something) \
 do {                         \
-       ret = something;     \
+       ret = (something);     \
        if (ret < 0)         \
                goto err;    \
-} while (0);
+} while (0)
 
 static int setup_all_buffers(void)
 {
        int ret;
 
-       DO_OR_GOTO_ERR(topology_probe())
-       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(for_all_proc_cpus(initialize_counters))
+       DO_OR_GOTO_ERR(topology_probe());
+       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(for_all_proc_cpus(initialize_counters));
 
-       allocated = true;
+       allocated = 1;
        return 0;
 err:
        free_all_buffers();
        return ret;
 }
 
-static int turbostat_init(void)
+static int
+turbostat_init(void)
 {
        int ret;
        struct timespec ts;
 
-       DO_OR_GOTO_ERR(check_cpuid())
-       DO_OR_GOTO_ERR(check_dev_msr())
-       DO_OR_GOTO_ERR(check_super_user())
-       DO_OR_GOTO_ERR(setup_all_buffers())
-       DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS))
+       DO_OR_GOTO_ERR(check_cpuid());
+       DO_OR_GOTO_ERR(check_dev_msr());
+       DO_OR_GOTO_ERR(check_super_user());
+       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;
@@ -1475,7 +1505,8 @@ static const char *config_keys[] =
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-static int turbostat_config (const char *key, const char *value)
+static int
+turbostat_config(const char *key, const char *value)
 {
        if (strcasecmp("Interval", key) == 0)
                interval_sec = atoi(value);
@@ -1484,8 +1515,8 @@ static int turbostat_config (const char *key, const char *value)
        return 0;
 }
 
-void module_register (void);
-void module_register (void)
+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);