X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fturbostat.c;h=9465ddc0f168436d1c52265036b8aaab0b795bbf;hb=950876683f3b214072e9295cee21999ed726cd83;hp=31d458db9481652e532a04f287b6c5944b4ed29a;hpb=8b039e3d6d460b149630044973514677c9f17475;p=collectd.git diff --git a/src/turbostat.c b/src/turbostat.c index 31d458db..9465ddc0 100644 --- a/src/turbostat.c +++ b/src/turbostat.c @@ -37,8 +37,8 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" +#include "utils/common/common.h" #include "utils_time.h" #include "msr-index.h" @@ -49,6 +49,14 @@ #define PLUGIN_NAME "turbostat" +typedef enum affinity_policy_enum { + policy_restore_affinity, /* restore cpu affinity to whatever it was before */ + policy_allcpus_affinity /* do not restore affinity, set to all cpus */ +} affinity_policy_t; + +/* the default is to set cpu affinity to all cpus */ +static affinity_policy_t affinity_policy = policy_allcpus_affinity; + /* * This tool uses the Model-Specific Registers (MSRs) present on Intel * processors. @@ -244,6 +252,7 @@ static const char *config_keys[] = { "TCCActivationTemp", "RunningAveragePowerLimit", "LogicalCoreNames", + "RestoreAffinityPolicy", }; static const int config_keys_num = STATIC_ARRAY_SIZE(config_keys); @@ -1425,9 +1434,9 @@ static void free_all_buffers(void) { package_delta = NULL; } -/********************** - * Collectd functions * - **********************/ + /********************** + * Collectd functions * + **********************/ #define DO_OR_GOTO_ERR(something) \ do { \ @@ -1454,9 +1463,32 @@ err: return ret; } +static int save_affinity(void) { + if (affinity_policy == policy_restore_affinity) { + /* Try to save 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 -1; + } + + return 0; +} + +static void restore_affinity(void) { + /* Let's restore the affinity to the value saved in save_affinity */ + if (affinity_policy == policy_restore_affinity) + (void)sched_setaffinity(0, cpu_saved_affinity_setsize, + cpu_saved_affinity_set); + else { + /* reset the affinity to all present cpus */ + (void)sched_setaffinity(0, cpu_present_setsize, cpu_present_set); + } +} + static int turbostat_read(void) { int ret; - int sched_getaffinity_ret; if (!allocated) { if ((ret = setup_all_buffers()) < 0) @@ -1474,12 +1506,11 @@ static int turbostat_read(void) { } } - /* Try to save the scheduling affinity, as it will be modified by - * get_counters. If the call is unsuccessfull, this will be handled later - * on by restoring affinity to cpu_present_set instead. - */ - sched_getaffinity_ret = sched_getaffinity(0, cpu_saved_affinity_setsize, - cpu_saved_affinity_set); + if (save_affinity() != 0) { + ERROR("turbostat plugin: Unable to save the CPU affinity. Please read the " + "docs about RestoreAffinityPolicy option."); + return -1; + } if (!initialized) { if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0) @@ -1514,21 +1545,8 @@ static int turbostat_read(void) { } 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.. - */ - if (sched_getaffinity_ret == 0) - (void)sched_setaffinity(0, cpu_saved_affinity_setsize, - cpu_saved_affinity_set); - else { - /* - * The previous call to sched_getaffinity() failed, so - * reset the affinity to all present cpus. - */ - (void)sched_setaffinity(0, cpu_present_setsize, cpu_present_set); - } + restore_affinity(); + return ret; } @@ -1645,6 +1663,15 @@ static int turbostat_config(const char *key, const char *value) { return -1; } tcc_activation_temp = (unsigned int)tmp_val; + } else if (strcasecmp("RestoreAffinityPolicy", key) == 0) { + if (strcasecmp("Restore", value) == 0) + affinity_policy = policy_restore_affinity; + else if (strcasecmp("AllCPUs", value) == 0) + affinity_policy = policy_allcpus_affinity; + else { + ERROR("turbostat plugin: Invalid RestoreAffinityPolicy '%s'", value); + return -1; + } } else { ERROR("turbostat plugin: Invalid configuration option '%s'", key); return -1;