Turbostat: fix thread max number detection
[collectd.git] / src / turbostat.c
index 648ec40..5470e6e 100644 (file)
@@ -42,7 +42,9 @@
 
 #include <asm/msr-index.h>
 #include <cpuid.h>
+#ifdef HAVE_SYS_CAPABILITY_H
 #include <sys/capability.h>
+#endif /* HAVE_SYS_CAPABILITY_H */
 
 #define PLUGIN_NAME "turbostat"
 
@@ -1148,8 +1150,8 @@ topology_probe()
 {
        unsigned int i;
        int ret;
-       unsigned int max_package_id, max_core_id, max_thread_id;
-       max_package_id = max_core_id = max_thread_id = 0;
+       unsigned int max_package_id, max_core_id, max_threads;
+       max_package_id = max_core_id = max_threads = 0;
 
        /* Clean topology */
        free(topology.cpus);
@@ -1218,8 +1220,8 @@ topology_probe()
                        goto err;
                else
                        num_threads = (unsigned int) ret;
-               if (num_threads > max_thread_id)
-                       max_thread_id = num_threads;
+               if (num_threads > max_threads)
+                       max_threads = num_threads;
                ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
                if (ret < 0)
                        goto err;
@@ -1232,7 +1234,7 @@ topology_probe()
        /* Num is max + 1 (need to count 0) */
        topology.num_packages = max_package_id + 1;
        topology.num_cores = max_core_id + 1;
-       topology.num_threads = max_thread_id + 1;
+       topology.num_threads = max_threads;
 
        return 0;
 err:
@@ -1256,6 +1258,9 @@ allocate_counters(struct thread_data **threads, struct core_data **cores, struct
        if (*threads == NULL)
                goto err;
 
+       for (i = 0; i < total_threads; ++i)
+               (*threads)[i].cpu_id = topology.max_cpu_id + 1;
+
        total_cores = topology.num_cores * topology.num_packages;
        *cores = calloc(total_cores, sizeof(struct core_data));
        if (*cores == NULL)
@@ -1265,9 +1270,6 @@ allocate_counters(struct thread_data **threads, struct core_data **cores, struct
        if (*packages == NULL)
                goto err_clean_cores;
 
-       for (i = 0; i < topology.num_packages; i++)
-               (*packages)[i].package_id = i;
-
        return 0;
 
 err_clean_cores:
@@ -1463,15 +1465,24 @@ out:
 static int
 check_permissions(void)
 {
+#ifdef HAVE_SYS_CAPABILITY_H
        struct __user_cap_header_struct cap_header_data;
        cap_user_header_t cap_header = &cap_header_data;
        struct __user_cap_data_struct cap_data_data;
        cap_user_data_t cap_data = &cap_data_data;
        int ret = 0;
+#endif /* HAVE_SYS_CAPABILITY_H */
 
        if (getuid() == 0) {
                /* We have everything we need */
                return 0;
+#ifndef HAVE_SYS_CAPABILITY_H
+       } else {
+               ERROR("Turbostat plugin: Initialization failed: this plugin "
+                     "requires collectd to run as root");
+               return -1;
+       }
+#else /* HAVE_SYS_CAPABILITY_H */
        }
 
        /* check for CAP_SYS_RAWIO */
@@ -1504,6 +1515,7 @@ check_permissions(void)
                      "collectd a special capability (CAP_SYS_RAWIO) and read "
                       "access to /dev/cpu/*/msr (see previous warnings)");
        return ret;
+#endif /* HAVE_SYS_CAPABILITY_H */
 }
 
 static int