#include "collectd.h"
-#include "common.h"
#include "plugin.h"
+#include "utils/common/common.h"
#include "utils_time.h"
#include "msr-index.h"
#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.
"TCCActivationTemp",
"RunningAveragePowerLimit",
"LogicalCoreNames",
+ "RestoreAffinityPolicy",
};
static const int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
/* Ivy Bridge */
case 0x3A: /* IVB */
case 0x3E: /* IVB Xeon */
+ case 0x55: /* SKX,CLX Xeon */
+ case 0x6A: /* ICX Xeon */
do_smi = true;
do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
break;
case 0x2D: /* SNB Xeon */
case 0x3E: /* IVB Xeon */
+ case 0x55: /* SKX,CLX Xeon */
+ case 0x6A: /* ICX Xeon */
do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM;
do_power_fields = TURBO_PLATFORM | PSTATES_PLATFORM;
break;
parse_int_file(const char *fmt, ...) {
va_list args;
char path[PATH_MAX];
+ char buf[256];
int len;
+ value_t v;
+ char *c;
+ FILE *fp;
va_start(args, fmt);
len = vsnprintf(path, sizeof(path), fmt, args);
return -1;
}
- value_t v;
- if (parse_value_file(path, &v, DS_TYPE_DERIVE) != 0) {
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ ERROR("turbostat plugin: unable to open: '%s': %s", path, strerror(errno));
+ return -1;
+ }
+
+ if (fgets(buf, sizeof(buf), fp) == NULL) {
+ ERROR("turbostat plugin: unable to read: '%s': %s", path, strerror(errno));
+ fclose(fp);
+ return -1;
+ }
+ fclose(fp);
+
+ /* We only care about the first integer in the range */
+ c = strchr(buf, '-');
+ if (c != NULL)
+ *c = '\0';
+ c = strchr(buf, ',');
+ if (c != NULL)
+ *c = '\0';
+ strstripnewline(buf);
+
+ if (parse_value(buf, &v, DS_TYPE_DERIVE) != 0) {
ERROR("turbostat plugin: Parsing \"%s\" failed.", path);
return -1;
}
package_delta = NULL;
}
-/**********************
- * Collectd functions *
- **********************/
+ /**********************
+ * Collectd functions *
+ **********************/
#define DO_OR_GOTO_ERR(something) \
do { \
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;
}
}
- /* 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) {
- ERROR("turbostat plugin: Unable to save the CPU affinity");
+ if (save_affinity() != 0) {
+ ERROR("turbostat plugin: Unable to save the CPU affinity. Please read the "
+ "docs about RestoreAffinityPolicy option.");
return -1;
}
}
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);
+ restore_affinity();
+
return ret;
}
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;