From afd75fe5d47118d51c58d338a2f54fbe3327fb95 Mon Sep 17 00:00:00 2001 From: Pavel Rochnyack Date: Sat, 27 Oct 2018 20:16:38 +0700 Subject: [PATCH] turbostat plugin: Polished RestoreAffinityPolicy option --- src/collectd.conf.pod | 27 +++++++++++++++++++++++++++ src/turbostat.c | 31 ++++++++++++------------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index d830573b..03b163ef 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -8865,6 +8865,33 @@ dynamic number assigned by the kernel. Otherwise, CnE> is used if there is only one package and CnE-coreEmE> if there is more than one, where I is the n-th core of package I. +=item B I|I + +Reading data from CPU has side-effect: collectd process's CPU affinity mask +changes. After reading data is completed, affinity mask needs to be restored. +This option allows to set restore policy. + +B (the default): Restore the affinity by setting affinity to any/all +CPUs. + +B: Save affinity using sched_getaffinity() before reading data and +restore it after. + +On some systems, sched_getaffinity() will fail due to inconsistency of the CPU +set size between userspace and kernel. In these cases plugin will detect the +unsuccessful call and fail with an error, preventing data collection. +Most of configurations does not need to save affinity as Collectd process is +allowed to run on any/all available CPUs. + +If you need to save and restore affinity and get errors like 'Unable to save +the CPU affinity', setting 'possible_cpus' kernel boot option may also help. + +See following links for details: + +L +L +L + =back =head2 Plugin C diff --git a/src/turbostat.c b/src/turbostat.c index ca872197..4a7af4c7 100644 --- a/src/turbostat.c +++ b/src/turbostat.c @@ -51,12 +51,11 @@ 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 */ - policy_invalid + policy_allcpus_affinity /* do not restore affinity, set to all cpus */ } affinity_policy_t; -/* the default is to preserve cpu affinity */ -static affinity_policy_t affinity_policy = policy_restore_affinity; +/* 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 @@ -1467,8 +1466,8 @@ err: int save_affinity(void) { if (affinity_policy == policy_restore_affinity) { /* Try to save the scheduling affinity, as it will be modified by - * get_counters. - */ + * get_counters(). + */ if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0) return -1; @@ -1508,7 +1507,8 @@ static int turbostat_read(void) { } if (save_affinity() != 0) { - ERROR("turbostat plugin: Unable to save the CPU affinity"); + ERROR("turbostat plugin: Unable to save the CPU affinity. Please read the " + "docs about RestoreAffinityPolicy option."); return -1; } @@ -1617,15 +1617,6 @@ err: return ret; } -affinity_policy_t parse_affinity_policy(const char *value) { - if (strcasecmp("AffinityRestore", value) == 0) - return policy_restore_affinity; - else if (strcasecmp("AffinityAllCPUs", value) == 0) - return policy_allcpus_affinity; - - return policy_invalid; -} - static int turbostat_config(const char *key, const char *value) { long unsigned int tmp_val; char *end; @@ -1673,11 +1664,13 @@ static int turbostat_config(const char *key, const char *value) { } tcc_activation_temp = (unsigned int)tmp_val; } else if (strcasecmp("RestoreAffinityPolicy", key) == 0) { - affinity_policy = parse_affinity_policy(value); - if (affinity_policy == policy_invalid) { - /* set to default policy if requested policy is invalid */ + 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); -- 2.11.0