ERR_CPU_ALLOC,
ERR_NOT_ROOT,
UNSUPPORTED_CPU,
+ ERR_PATH_TOO_LONG,
};
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);
+ ERROR("Turbostat plugin: Could not migrate to CPU %d", cpu);
return -ERR_CPU_MIGRATE;
}
}
retval = pread(fd, msr, sizeof *msr, offset);
if (retval != sizeof *msr) {
- ERROR("MSR offset 0x%llx read failed", (unsigned long long)offset);
+ ERROR("Turbostat plugin: MSR offset 0x%llx read failed",
+ (unsigned long long)offset);
return -1;
}
return 0;
goto out;
}
- if (do_pkg_cstate & (1 << 2))
- READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
- if (do_pkg_cstate & (1 << 3))
- READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
- if (do_pkg_cstate & (1 << 6))
- READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
- if (do_pkg_cstate & (1 << 7))
- READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
- if (do_pkg_cstate & (1 << 8))
- READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
- if (do_pkg_cstate & (1 << 9))
- READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
- if (do_pkg_cstate & (1 << 10))
- READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
+ if (do_pkg_cstate & (1 << 2))
+ READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
+ if (do_pkg_cstate & (1 << 3))
+ READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
+ if (do_pkg_cstate & (1 << 6))
+ READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
+ if (do_pkg_cstate & (1 << 7))
+ READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
+ if (do_pkg_cstate & (1 << 8))
+ READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
+ if (do_pkg_cstate & (1 << 9))
+ READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
+ if (do_pkg_cstate & (1 << 10))
+ READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
if (do_rapl & RAPL_PKG) {
READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
/* check for TSC < 1 Mcycles over interval */
if (delta->tsc < (1000 * 1000)) {
- WARNING("Insanely slow TSC rate, TSC stops in idle? ");
- WARNING("You can disable all c-states by booting with \"idle=poll\" ");
- WARNING("or just the deep ones with \"processor.max_cstate=1\"");
+ WARNING("Turbostat plugin: Insanely slow TSC rate, TSC stops "
+ "in idle? You can disable all c-states by booting with"
+ " 'idle=poll' or just the deep ones with"
+ " 'processor.max_cstate=1'");
return -1;
}
delta->mperf = new->mperf - old->mperf;
} else {
if (!aperf_mperf_unstable) {
- WARNING(" APERF or MPERF went backwards * ");
- WARNING("* Frequency results do not cover entire interval *");
- WARNING("* fix this by running Linux-2.6.30 or later *");
+ WARNING("Turbostat plugin: APERF or MPERF went "
+ "backwards. Frequency results do not cover "
+ "the entire interval. Fix this by running "
+ "Linux-2.6.30 or later.");
aperf_mperf_unstable = 1;
}
}
if (delta->mperf == 0) {
- WARNING("cpu%d MPERF 0!", old->cpu_id);
+ WARNING("Turbostat plugin: cpu%d MPERF 0!", old->cpu_id);
delta->mperf = 1; /* divide by 0 protection */
}
}
-static void
-free_all_buffers(void)
-{
- allocated = 0;
- initialized = 0;
-
- CPU_FREE(cpu_present_set);
- cpu_present_set = NULL;
- cpu_present_set = 0;
-
- CPU_FREE(cpu_affinity_set);
- 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);
-
- thread_even = NULL;
- core_even = NULL;
- package_even = NULL;
-
- free(thread_odd);
- free(core_odd);
- free(package_odd);
-
- thread_odd = NULL;
- core_odd = NULL;
- package_odd = NULL;
-
- free(thread_delta);
- free(core_delta);
- free(package_delta);
-
- thread_delta = NULL;
- core_delta = NULL;
- package_delta = NULL;
-}
-
-
-/****************
- * File helpers *
- ****************/
-
-/*
- * Read a single int from a file.
- */
-static int __attribute__ ((format(printf,1,2)))
-parse_int_file(const char *fmt, ...)
-{
- va_list args;
- char path[PATH_MAX];
- FILE *filep;
- int value;
-
- va_start(args, fmt);
- vsnprintf(path, sizeof(path), fmt, args);
- va_end(args);
- filep = fopen(path, "r");
- if (!filep) {
- ERROR("%s: open failed", path);
- return -ERR_CANT_OPEN_FILE;
- }
- if (fscanf(filep, "%d", &value) != 1) {
- ERROR("%s: failed to parse number from file", path);
- return -ERR_CANT_READ_NUMBER;
- }
- fclose(filep);
- return value;
-}
-
-static int
-get_threads_on_core(int cpu)
-{
- char path[80];
- FILE *filep;
- int sib1, sib2;
- int matches;
- char character;
-
- ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
- filep = fopen(path, "r");
- if (!filep) {
- ERROR("%s: open failed", path);
- return -ERR_CANT_OPEN_FILE;
- }
- /*
- * file format:
- * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
- * otherwinse 1 sibling (self).
- */
- matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
-
- fclose(filep);
-
- if (matches == 3)
- return 2;
- else
- return 1;
-}
-
-/*
- * run func(cpu) on every cpu in /proc/stat
- * return max_cpu number
- */
-static int __attribute__((warn_unused_result))
-for_all_proc_cpus(int (func)(int))
-{
- FILE *fp;
- int cpu_num;
- int retval;
-
- fp = fopen("/proc/stat", "r");
- if (!fp) {
- ERROR("Failed to open /proc/stat");
- return -ERR_CANT_OPEN_FILE;
- }
-
- retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
- if (retval != 0) {
- ERROR("Failed to parse /proc/stat");
- fclose(fp);
- return -ERR_CANT_READ_PROC_STAT;
- }
-
- while (1) {
- retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
- if (retval != 1)
- break;
-
- retval = func(cpu_num);
- if (retval) {
- fclose(fp);
- return(retval);
- }
- }
- fclose(fp);
- return 0;
-}
-
-/*
- * Update the stored topology.max_cpu_id
- */
-static int
-update_max_cpu_id(int cpu)
-{
- if (topology.max_cpu_id < cpu)
- topology.max_cpu_id = cpu;
- return 0;
-}
-
-static int
-mark_cpu_present(int cpu)
-{
- CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
- return 0;
-}
-
-
-static int setup_all_buffers(void);
-
-static int
-turbostat_read(user_data_t * not_used)
-{
- int ret;
-
- if (!allocated) {
- if ((ret = setup_all_buffers()) < 0)
- return ret;
- }
-
- if (for_all_proc_cpus(cpu_is_not_present)) {
- free_all_buffers();
- if ((ret = setup_all_buffers()) < 0)
- return ret;
- if (for_all_proc_cpus(cpu_is_not_present))
- 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)
- goto out;
- gettimeofday(&tv_even, (struct timezone *)NULL);
- is_even = 1;
- initialized = 1;
- ret = 0;
- goto out;
- }
-
- if (is_even) {
- if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
- goto out;
- gettimeofday(&tv_odd, (struct timezone *)NULL);
- is_even = 0;
- timersub(&tv_odd, &tv_even, &tv_delta);
- if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
- goto out;
- if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
- goto out;
- } else {
- if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
- goto out;
- gettimeofday(&tv_even, (struct timezone *)NULL);
- is_even = 1;
- timersub(&tv_even, &tv_odd, &tv_delta);
- if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
- goto out;
- if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
- goto out;
- }
- 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))
-check_dev_msr()
-{
- struct stat sb;
-
- if (stat("/dev/cpu/0/msr", &sb)) {
- ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
- return -ERR_NO_MSR;
- }
- return 0;
-}
-
-static int __attribute__((warn_unused_result))
-check_super_user()
-{
- if (getuid() != 0) {
- ERROR("must be root");
- return -ERR_NOT_ROOT;
- }
- return 0;
-}
+/***************
+ * CPU Probing *
+ ***************/
/*
* MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
guess:
p->tcc_activation_temp = TJMAX_DEFAULT;
- WARNING("cpu%d: Guessing tjMax %d C, Please use TCCActivationTemp to specify",
+ WARNING("Turbostat plugin: cpu%d: Guessing tjMax %d C,"
+ " Please use TCCActivationTemp to specify it.",
t->cpu_id, p->tcc_activation_temp);
return 0;
* - EAX: Maximum Input Value for Basic CPUID Information
* - EBX: "Genu" (0x756e6547)
* - EDX: "ineI" (0x49656e69)
- * - ECX: "ntel" (0x6c65746e)
- */
+ * - ECX: "ntel" (0x6c65746e)
+ */
max_level = ebx = ecx = edx = 0;
__get_cpuid(0, &max_level, &ebx, &ecx, &edx);
if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
- ERROR("Unsupported CPU");
+ ERROR("Turbostat plugin: Unsupported CPU (not Intel)");
return -UNSUPPORTED_CPU;
}
/* CPUID(1):
* - EAX: Version Information: Type, Family, Model, and Stepping ID
- * + 4-7: Model ID
- * + 8-11: Family ID
+ * + 4-7: Model ID
+ * + 8-11: Family ID
* + 12-13: Processor type
* + 16-19: Extended Model ID
* + 20-27: Extended Family ID
* - EDX: Feature Information:
* + 5: Support for MSR read/write operations
- */
+ */
fms = ebx = ecx = edx = 0;
__get_cpuid(1, &fms, &ebx, &ecx, &edx);
family = (fms >> 8) & 0xf;
if (family == 6 || family == 0xf)
model += ((fms >> 16) & 0xf) << 4;
if (!(edx & (1 << 5))) {
- ERROR("CPUID: no MSR");
+ ERROR("Turbostat plugin: Unsupported CPU (no MSR support)");
return -ERR_NO_MSR;
}
max_level = ebx = ecx = edx = 0;
__get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
if (max_level < 0x80000007) {
- ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
+ ERROR("Turbostat plugin: Unsupported CPU (no invariant TSC, "
+ " Maximum Extended Function: 0x%x)", max_level);
return -ERR_NO_INVARIANT_TSC;
}
eax = ebx = ecx = edx = 0;
__get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
if (!(edx & (1 << 8))) {
- ERROR("No invariant TSC");
+ ERROR("Turbostat plugin: Unsupported CPU (No invariant TSC)");
return -ERR_NO_INVARIANT_TSC;
}
do_dts = eax & (1 << 0);
do_ptm = eax & (1 << 6);
if (!(ecx & (1 << 0))) {
- ERROR("No APERF");
+ ERROR("Turbostat plugin: Unsupported CPU (No APERF)");
return -ERR_NO_APERF;
}
do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
break;
default:
- ERROR("Unsupported CPU");
+ ERROR("Turbostat plugin: Unsupported CPU (family: %#x,"
+ " model: %#x)", family, model);
}
switch (model) {
case 0x2A:
do_rapl = 0;
}
} else {
- ERROR("Unsupported CPU");
+ ERROR("Turbostat plugin: Unsupported CPU (family: %#x, "
+ "model: %#x)", family, model);
return -UNSUPPORTED_CPU;
}
return 0;
}
+
+/********************
+ * Topology Probing *
+ ********************/
+
+/*
+ * Read a single int from a file.
+ */
+static int __attribute__ ((format(printf,1,2)))
+parse_int_file(const char *fmt, ...)
+{
+ va_list args;
+ char path[PATH_MAX];
+ FILE *filep;
+ int len, value;
+
+ va_start(args, fmt);
+ len = vsnprintf(path, sizeof(path), fmt, args);
+ va_end(args);
+ if (len < 0 || len >= PATH_MAX) {
+ ERROR("Turbostat plugin: path truncated: '%s'", path);
+ return -ERR_PATH_TOO_LONG;
+ }
+
+ filep = fopen(path, "r");
+ if (!filep) {
+ ERROR("Turbostat plugin: Failed to open '%s'", path);
+ return -ERR_CANT_OPEN_FILE;
+ }
+ if (fscanf(filep, "%d", &value) != 1) {
+ ERROR("Turbostat plugin: Failed to parse number from '%s'", path);
+ return -ERR_CANT_READ_NUMBER;
+ }
+ fclose(filep);
+ return value;
+}
+
+static int
+get_threads_on_core(int cpu)
+{
+ char path[80];
+ FILE *filep;
+ int sib1, sib2;
+ int matches;
+ char character;
+
+ ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
+ filep = fopen(path, "r");
+ if (!filep) {
+ ERROR("Turbostat plugin: Failed to open '%s'", path);
+ return -ERR_CANT_OPEN_FILE;
+ }
+ /*
+ * file format:
+ * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
+ * otherwinse 1 sibling (self).
+ */
+ matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
+
+ fclose(filep);
+
+ if (matches == 3)
+ return 2;
+ else
+ return 1;
+}
+
+/*
+ * run func(cpu) on every cpu in /proc/stat
+ * return max_cpu number
+ */
+static int __attribute__((warn_unused_result))
+for_all_proc_cpus(int (func)(int))
+{
+ FILE *fp;
+ int cpu_num;
+ int retval;
+
+ fp = fopen("/proc/stat", "r");
+ if (!fp) {
+ ERROR("Turbostat plugin: Failed to open /proc/stat");
+ return -ERR_CANT_OPEN_FILE;
+ }
+
+ retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
+ if (retval != 0) {
+ ERROR("Turbostat plugin: Failed to parse /proc/stat");
+ fclose(fp);
+ return -ERR_CANT_READ_PROC_STAT;
+ }
+
+ while (1) {
+ retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
+ if (retval != 1)
+ break;
+
+ retval = func(cpu_num);
+ if (retval) {
+ fclose(fp);
+ return(retval);
+ }
+ }
+ fclose(fp);
+ return 0;
+}
+
+/*
+ * Update the stored topology.max_cpu_id
+ */
+static int
+update_max_cpu_id(int cpu)
+{
+ if (topology.max_cpu_id < cpu)
+ topology.max_cpu_id = cpu;
+ return 0;
+}
+
+static int
+mark_cpu_present(int cpu)
+{
+ CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
+ return 0;
+}
+
static int __attribute__((warn_unused_result))
-allocate_cpu_set(cpu_set_t * set, size_t * size) {
- set = CPU_ALLOC(topology.max_cpu_id + 1);
- if (set == NULL) {
- ERROR("Unable to allocate CPU state");
+allocate_cpu_set(cpu_set_t ** set, size_t * size) {
+ *set = CPU_ALLOC(topology.max_cpu_id + 1);
+ if (*set == NULL) {
+ ERROR("Turbostat plugin: Unable to allocate CPU state");
return -ERR_CPU_ALLOC;
}
*size = CPU_ALLOC_SIZE(topology.max_cpu_id + 1);
- CPU_ZERO_S(*size, set);
+ CPU_ZERO_S(*size, *set);
return 0;
}
free(topology.cpus);
memset(&topology, 0, sizeof(topology));
- /* Can't fail (update_max_cpu_id always returns 0) */
- assert(for_all_proc_cpus(update_max_cpu_id));
+ ret = for_all_proc_cpus(update_max_cpu_id);
+ if (ret != 0)
+ goto err;
topology.cpus = calloc(1, (topology.max_cpu_id + 1) * sizeof(struct cpu_topology));
if (topology.cpus == NULL) {
- ERROR("Unable to allocate memory for cpu topology");
+ ERROR("Turbostat plugin: Unable to allocate memory for CPU topology");
return -ERR_CALLOC;
}
- ret = allocate_cpu_set(cpu_present_set, &cpu_present_setsize);
+ ret = allocate_cpu_set(&cpu_present_set, &cpu_present_setsize);
if (ret != 0)
goto err;
- ret = allocate_cpu_set(cpu_affinity_set, &cpu_affinity_setsize);
+ ret = allocate_cpu_set(&cpu_affinity_set, &cpu_affinity_setsize);
if (ret != 0)
goto err;
- ret = allocate_cpu_set(cpu_saved_affinity_set, &cpu_saved_affinity_setsize);
+ ret = allocate_cpu_set(&cpu_saved_affinity_set, &cpu_saved_affinity_setsize);
if (ret != 0)
goto err;
- /* Can't fail (mark_cpu_present always returns 0) */
- assert(for_all_proc_cpus(mark_cpu_present));
+ ret = for_all_proc_cpus(mark_cpu_present);
+ if (ret != 0)
+ goto err;
/*
* For online cpus
struct cpu_topology *cpu = &topology.cpus[i];
if (cpu_is_not_present(i)) {
- WARNING("cpu%d NOT PRESENT", i);
+ WARNING("Turbostat plugin: cpu%d NOT PRESENT", i);
continue;
}
ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", i);
if (ret < 0)
goto err;
- else if (ret == cpu->core_id)
+ else if (ret == i)
cpu->first_core_in_package = 1;
ret = get_threads_on_core(i);
num_threads = ret;
if (num_threads > max_thread_id)
max_thread_id = num_threads;
- if (num_threads > 1) {
- ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
- if (ret < 0)
- goto err;
- else if (ret == num_threads)
- cpu->first_thread_in_core = 1;
- }
- DEBUG("cpu %d pkg %d core %d\n",
+ ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
+ if (ret < 0)
+ goto err;
+ else if (ret == i)
+ cpu->first_thread_in_core = 1;
+
+ DEBUG("Turbostat plugin: cpu %d pkg %d core %d\n",
i, cpu->package_id, cpu->core_id);
}
/* Num is max + 1 (need to count 0) */
return ret;
}
+
+/************************
+ * Main alloc/init/free *
+ ************************/
+
static int
allocate_counters(struct thread_data **threads, struct core_data **cores, struct pkg_data **packages)
{
err_clean_threads:
free(*threads);
err:
- ERROR("calloc counters");
+ ERROR("Turbostat plugin: Failled to allocate memory for counters");
return -ERR_CALLOC;
}
-/*
- * init_counter()
- *
- * set cpu_id, core_id, package_id
- * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
- *
- * increment topo.num_cores when 1st core in pkg seen
- */
static int
init_counter(struct thread_data *thread_base, struct core_data *core_base,
struct pkg_data *pkg_base, int cpu_id)
return 0;
}
-
static int
initialize_counters(void)
{
return 0;
}
+
+
+static void
+free_all_buffers(void)
+{
+ allocated = 0;
+ initialized = 0;
+
+ CPU_FREE(cpu_present_set);
+ cpu_present_set = NULL;
+ cpu_present_set = 0;
+
+ CPU_FREE(cpu_affinity_set);
+ 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);
+
+ thread_even = NULL;
+ core_even = NULL;
+ package_even = NULL;
+
+ free(thread_odd);
+ free(core_odd);
+ free(package_odd);
+
+ thread_odd = NULL;
+ core_odd = NULL;
+ package_odd = NULL;
+
+ free(thread_delta);
+ free(core_delta);
+ free(package_delta);
+
+ thread_delta = NULL;
+ core_delta = NULL;
+ package_delta = NULL;
+}
+
+
+/**********************
+ * Collectd functions *
+ **********************/
+
#define DO_OR_GOTO_ERR(something) \
-do { \
- ret = (something); \
- if (ret < 0) \
- goto err; \
+do { \
+ ret = (something); \
+ if (ret < 0) \
+ goto err; \
} while (0)
static int setup_all_buffers(void)
}
static int
+turbostat_read(void)
+{
+ int ret;
+
+ if (!allocated) {
+ if ((ret = setup_all_buffers()) < 0)
+ return ret;
+ }
+
+ if (for_all_proc_cpus(cpu_is_not_present)) {
+ free_all_buffers();
+ if ((ret = setup_all_buffers()) < 0)
+ return ret;
+ if (for_all_proc_cpus(cpu_is_not_present))
+ 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)
+ goto out;
+ gettimeofday(&tv_even, (struct timezone *)NULL);
+ is_even = 1;
+ initialized = 1;
+ ret = 0;
+ goto out;
+ }
+
+ if (is_even) {
+ if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
+ goto out;
+ gettimeofday(&tv_odd, (struct timezone *)NULL);
+ is_even = 0;
+ timersub(&tv_odd, &tv_even, &tv_delta);
+ if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
+ goto out;
+ if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
+ goto out;
+ } else {
+ if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
+ goto out;
+ gettimeofday(&tv_even, (struct timezone *)NULL);
+ is_even = 1;
+ timersub(&tv_even, &tv_odd, &tv_delta);
+ if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
+ goto out;
+ if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
+ goto out;
+ }
+ 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
turbostat_init(void)
{
+ struct stat sb;
int ret;
- DO_OR_GOTO_ERR(check_super_user());
+ if (getuid() != 0) {
+ ERROR("Turbostat plugin: Initialization failed: this plugin "
+ "requires collectd to run as root in order to read "
+ "special CPU registers");
+ return -ERR_NOT_ROOT;
+ }
+
DO_OR_GOTO_ERR(probe_cpu());
- DO_OR_GOTO_ERR(check_dev_msr());
+
+ if (stat("/dev/cpu/0/msr", &sb)) {
+ ERROR("Turbostat plugin: Initialization failed: /dev/cpu/0/msr"
+ " does not exist while the CPU supports MSR. You may be "
+ "missing the corresponding kernel module, please try '# "
+ "modprobe msr'");
+ return -ERR_NO_MSR;
+ }
+
DO_OR_GOTO_ERR(setup_all_buffers());
- plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
+ plugin_register_read(PLUGIN_NAME, turbostat_read);
return 0;
err:
return 0;
}
-void module_register(void);
void module_register(void)
{
plugin_register_init(PLUGIN_NAME, turbostat_init);