Turbostat: Fix sched affinity mess
[collectd.git] / src / turbostat.c
index 8c7af14..ecc2375 100644 (file)
@@ -69,7 +69,6 @@ static unsigned int do_rapl;
 static unsigned int do_dts;
 static unsigned int do_ptm;
 static unsigned int tcc_activation_temp;
-static unsigned int tcc_activation_temp_override;
 static double rapl_energy_units;
 
 #define RAPL_PKG               (1 << 0)
@@ -103,8 +102,8 @@ static double rapl_energy_units;
 int aperf_mperf_unstable;
 int backwards_count;
 
-cpu_set_t *cpu_present_set, *cpu_affinity_set;
-size_t cpu_present_setsize, cpu_affinity_setsize;
+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;
 
 struct thread_data {
        unsigned long long tsc;
@@ -141,8 +140,8 @@ struct pkg_data {
        unsigned int energy_gfx;        /* MSR_PP1_ENERGY_STATUS */
        unsigned int rapl_pkg_perf_status;      /* MSR_PKG_PERF_STATUS */
        unsigned int rapl_dram_perf_status;     /* MSR_DRAM_PERF_STATUS */
+       unsigned int tcc_activation_temp;
        unsigned int pkg_temp_c;
-
 } *package_even, *package_odd;
 
 #define ODD_COUNTERS thread_odd, core_odd, package_odd
@@ -174,6 +173,7 @@ 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,
@@ -255,26 +255,25 @@ for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_dat
 }
 
 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);
-       if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
-               return -ERR_CPU_MIGRATE;
-       else
-               return 0;
-}
-
-static int __attribute__((warn_unused_result))
-open_msr(int cpu)
+open_msr(int cpu, _Bool multiple_read)
 {
        char pathname[32];
        int fd;
 
-       /* FIXME: Do we really need this, why? */
-       if (cpu_migrate(cpu)) {
-               ERROR("Could not migrate to CPU %d", cpu);
-               return -ERR_CPU_MIGRATE;
+       /*
+        * If we need to do multiple read, let's migrate to the CPU
+        * Otherwise, we would lose time calling functions on another CPU
+        *
+        * If we are not yet initialized (cpu_affinity_setsize = 0),
+        * we need to skip this optimisation.
+        */
+       if (multiple_read && cpu_affinity_setsize) {
+               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;
+               }
        }
 
        ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
@@ -304,7 +303,7 @@ get_msr(int cpu, off_t offset, unsigned long long *msr)
        ssize_t retval;
        int fd;
 
-       fd = open_msr(cpu);
+       fd = open_msr(cpu, 0);
        if (fd < 0)
                return fd;
        retval = read_msr(fd, offset, msr);
@@ -447,7 +446,7 @@ get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
        int msr_fd;
        int retval = 0;
 
-       msr_fd = open_msr(cpu);
+       msr_fd = open_msr(cpu, 1);
        if (msr_fd < 0)
                return msr_fd;
 
@@ -482,7 +481,7 @@ do {                                                \
 
        if (do_dts) {
                READ_MSR(MSR_IA32_THERM_STATUS, &msr);
-               c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
+               c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
        }
 
        /* collect package counters only for 1st core in package */
@@ -532,7 +531,7 @@ do {                                                \
        }
        if (do_ptm) {
                READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
-               p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
+               p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
        }
 
 out:
@@ -554,6 +553,10 @@ free_all_buffers(void)
        cpu_affinity_set = NULL;
        cpu_affinity_setsize = 0;
 
+       CPU_FREE(cpu_saved_affinity_set);
+       cpu_saved_affinity_set = NULL;
+       cpu_saved_affinity_setsize = 0;
+
        free(thread_even);
        free(core_even);
        free(package_even);
@@ -899,37 +902,49 @@ turbostat_read(user_data_t * not_used)
                        return -ERR_CPU_NOT_PRESENT;
        }
 
+       /* 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 (!initialized) {
                if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
-                       return ret;
+                       goto out;
                gettimeofday(&tv_even, (struct timezone *)NULL);
                is_even = 1;
                initialized = 1;
-               return 0;
+               ret = 0;
+               goto out;
        }
 
        if (is_even) {
                if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
-                       return ret;
+                       goto out;
                gettimeofday(&tv_odd, (struct timezone *)NULL);
                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;
+                       goto out;
                if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
-                       return ret;
+                       goto out;
        } else {
                if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
-                       return ret;
+                       goto out;
                gettimeofday(&tv_even, (struct timezone *)NULL);
                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;
+                       goto out;
                if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
-                       return ret;
+                       goto out;
        }
-       return 0;
+       ret = 0;
+out:
+       /*
+        * Let's restore the affinity
+        * This might fail if the number of CPU changed, but we can't do anything in that case..
+        */
+       (void)sched_setaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set);
+       return ret;
 }
 
 static int __attribute__((warn_unused_result))
@@ -980,10 +995,8 @@ set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_da
        if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
                return 0;
 
-       if (tcc_activation_temp_override != 0) {
-               tcc_activation_temp = tcc_activation_temp_override;
-               ERROR("cpu%d: Using cmdline TCC Target (%d C)\n",
-                       cpu, tcc_activation_temp);
+       if (tcc_activation_temp != 0) {
+               p->tcc_activation_temp = tcc_activation_temp;
                return 0;
        }
 
@@ -995,14 +1008,14 @@ set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_da
        if (target_c_local < 85 || target_c_local > 127)
                goto guess;
 
-       tcc_activation_temp = target_c_local;
+       p->tcc_activation_temp = target_c_local;
 
        return 0;
 
 guess:
-       tcc_activation_temp = TJMAX_DEFAULT;
-       WARNING("cpu%d: Guessing tjMax %d C, Please use -T to specify",
-               t->cpu_id, tcc_activation_temp);
+       p->tcc_activation_temp = TJMAX_DEFAULT;
+       WARNING("cpu%d: Guessing tjMax %d C, Please use TCCActivationTemp to specify",
+               t->cpu_id, p->tcc_activation_temp);
 
        return 0;
 }
@@ -1272,6 +1285,19 @@ topology_probe()
 
 
        /*
+        * Allocate and initialize cpu_saved_affinity_set
+        */
+       cpu_saved_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
+       if (cpu_saved_affinity_set == NULL) {
+               free(cpus);
+               ERROR("CPU_ALLOC");
+               return -ERR_CPU_ALLOC;
+       }
+       cpu_saved_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
+       CPU_ZERO_S(cpu_saved_affinity_setsize, cpu_saved_affinity_set);
+
+
+       /*
         * For online cpus
         * find max_core_id, max_package_id
         */
@@ -1454,6 +1480,7 @@ turbostat_init(void)
        DO_OR_GOTO_ERR(check_dev_msr());
        DO_OR_GOTO_ERR(setup_all_buffers());
        DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
+       DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
 
        plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
 
@@ -1463,8 +1490,32 @@ 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)
+{
+       long unsigned int tmp_val;
+       char *end;
+
+       if (strcasecmp("TCCActivationTemp", key) == 0) {
+               tmp_val = strtoul(value, &end, 0);
+               if (*end != '\0' || tmp_val > UINT_MAX)
+                       return -1;
+               tcc_activation_temp = (unsigned int) tmp_val;
+       } 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);
 }