#define PLUGIN_NAME "turbostat"
static const char *proc_stat = "/proc/stat";
-static unsigned int skip_c0;
-static unsigned int skip_c1;
+
+/*
+ * If set, aperf_mperf_unstable disables a/mperf based stats.
+ * This includes: C0 & C1 states, frequency
+ *
+ * This value is automatically set if mperf or aperf decreases
+ */
+static _Bool aperf_mperf_unstable;
+
static unsigned int do_core_cstate;
static unsigned int do_pkg_cstate;
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)
/* 0x642 MSR_PP1_POLICY */
#define TJMAX_DEFAULT 100
-int aperf_mperf_unstable;
int backwards_count;
-char *progname;
-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;
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
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,
ERR_MSR_IA32_TSC,
ERR_CPU_NOT_PRESENT,
ERR_NO_MSR,
+ ERR_CANT_OPEN_MSR,
ERR_CANT_OPEN_FILE,
ERR_CANT_READ_NUMBER,
ERR_CANT_READ_PROC_STAT,
}
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))
-get_msr(int cpu, off_t offset, unsigned long long *msr)
+open_msr(int cpu, _Bool multiple_read)
{
- ssize_t retval;
char pathname[32];
int fd;
- ssnprintf(pathname, 32, "/dev/cpu/%d/msr", cpu);
+ /*
+ * 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);
fd = open(pathname, O_RDONLY);
if (fd < 0)
- return -1;
+ return -ERR_CANT_OPEN_MSR;
+ return fd;
+}
+
+static int __attribute__((warn_unused_result))
+read_msr(int fd, off_t offset, unsigned long long *msr)
+{
+ ssize_t retval;
retval = pread(fd, msr, sizeof *msr, offset);
- close(fd);
if (retval != sizeof *msr) {
- ERROR ("%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
+ ERROR("MSR offset 0x%llx read failed", (unsigned long long)offset);
return -1;
}
-
return 0;
}
+static int __attribute__((warn_unused_result))
+get_msr(int cpu, off_t offset, unsigned long long *msr)
+{
+ ssize_t retval;
+ int fd;
+
+ fd = open_msr(cpu, 0);
+ if (fd < 0)
+ return fd;
+ retval = read_msr(fd, offset, msr);
+ close(fd);
+ return retval;
+}
+
#define DELTA_WRAP32(new, old) \
if (new > old) { \
old = new - old; \
/* check for TSC < 1 Mcycles over interval */
if (old->tsc < (1000 * 1000)) {
- WARNING("Insanely slow TSC rate, TSC stops in idle?\n"
- "You can disable all c-states by booting with \"idle=poll\"\n"
- "or just the deep ones with \"processor.max_cstate=1\"");
+ 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\"");
return -1;
}
old->aperf = new->aperf - old->aperf;
old->mperf = new->mperf - old->mperf;
} else {
-
if (!aperf_mperf_unstable) {
- WARNING("%s: APERF or MPERF went backwards *\n", progname);
- WARNING("* Frequency results do not cover entire interval *\n");
- WARNING("* fix this by running Linux-2.6.30 or later *\n");
+ 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 *");
aperf_mperf_unstable = 1;
}
- /*
- * mperf delta is likely a huge "positive" number
- * can not use it for calculating c0 time
- */
- skip_c0 = 1;
- skip_c1 = 1;
}
}
if (old->mperf == 0) {
- WARNING("cpu%d MPERF 0!\n", old->cpu_id);
+ WARNING("cpu%d MPERF 0!", old->cpu_id);
old->mperf = 1; /* divide by 0 protection */
}
{
int cpu = t->cpu_id;
unsigned long long msr;
+ int msr_fd;
+ int retval = 0;
+
+ msr_fd = open_msr(cpu, 1);
+ if (msr_fd < 0)
+ return msr_fd;
+
+#define READ_MSR(msr, dst) \
+do { \
+ if (read_msr(msr_fd, msr, dst)) { \
+ retval = -ERR_##msr; \
+ goto out; \
+ } \
+} while (0)
- if (cpu_migrate(cpu)) {
- WARNING("Could not migrate to CPU %d\n", cpu);
- return -ERR_CPU_MIGRATE;
- }
-
- if (get_msr(cpu, MSR_IA32_TSC, &t->tsc))
- return -MSR_IA32_TSC;
+ READ_MSR(MSR_IA32_TSC, &t->tsc);
- if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
- return -ERR_MSR_IA32_APERF;
- if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
- return -ERR_MSR_IA32_MPERF;
+ READ_MSR(MSR_IA32_APERF, &t->aperf);
+ READ_MSR(MSR_IA32_MPERF, &t->mperf);
- if (get_msr(cpu, MSR_SMI_COUNT, &msr))
- return -ERR_MSR_SMI_COUNT;
+ READ_MSR(MSR_SMI_COUNT, &msr);
t->smi_count = msr & 0xFFFFFFFF;
/* collect core counters only for 1st thread in core */
- if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
- return 0;
+ if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
+ retval = 0;
+ goto out;
+ }
if (do_core_cstate & (1 << 3))
- if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
- return -ERR_MSR_CORE_C3_RESIDENCY
+ READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
if (do_core_cstate & (1 << 6))
- if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
- return -ERR_MSR_CORE_C6_RESIDENCY
+ READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
if (do_core_cstate & (1 << 7))
- if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
- return -ERR_MSR_CORE_C7_RESIDENCY
+ READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
if (do_dts) {
- if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
- return -ERR_MSR_IA32_THERM_STATUS;
- c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
+ READ_MSR(MSR_IA32_THERM_STATUS, &msr);
+ c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
}
/* collect package counters only for 1st core in package */
- if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
- return 0;
+ if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
+ retval = 0;
+ goto out;
+ }
if (do_pkg_cstate & (1 << 2))
- if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
- return -ERR_MSR_PKG_C2_RESIDENCY
+ READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
if (do_pkg_cstate & (1 << 3))
- if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
- return -ERR_MSR_PKG_C3_RESIDENCY
+ READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
if (do_pkg_cstate & (1 << 6))
- if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
- return -ERR_MSR_PKG_C6_RESIDENCY
+ READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
if (do_pkg_cstate & (1 << 7))
- if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
- return -ERR_MSR_PKG_C7_RESIDENCY
+ READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
if (do_pkg_cstate & (1 << 8))
- if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
- return -ERR_MSR_PKG_C8_RESIDENCY
+ READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
if (do_pkg_cstate & (1 << 9))
- if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
- return -ERR_MSR_PKG_C9_RESIDENCY
+ READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
if (do_pkg_cstate & (1 << 10))
- if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
- return -ERR_MSR_PKG_C10_RESIDENCY
+ READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
if (do_rapl & RAPL_PKG) {
- if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
- return -ERR_MSR_PKG_ENERGY_STATUS;
+ READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
p->energy_pkg = msr & 0xFFFFFFFF;
}
if (do_rapl & RAPL_CORES) {
- if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
- return MSR_PP0_ENERGY_STATUS;
+ READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
p->energy_cores = msr & 0xFFFFFFFF;
}
if (do_rapl & RAPL_DRAM) {
- if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
- return -ERR_MSR_DRAM_ENERGY_STATUS;
+ READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
p->energy_dram = msr & 0xFFFFFFFF;
}
if (do_rapl & RAPL_GFX) {
- if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
- return -ERR_MSR_PP1_ENERGY_STATUS;
+ READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
p->energy_gfx = msr & 0xFFFFFFFF;
}
if (do_rapl & RAPL_PKG_PERF_STATUS) {
- if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
- return -ERR_MSR_PKG_PERF_STATUS;
+ READ_MSR(MSR_PKG_PERF_STATUS, &msr);
p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
}
if (do_rapl & RAPL_DRAM_PERF_STATUS) {
- if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
- return -ERR_MSR_DRAM_PERF_STATUS;
+ READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
}
if (do_ptm) {
- if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
- return -ERR_MSR_IA32_PACKAGE_THERM_STATUS;
- p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
+ READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
+ p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
}
- return 0;
+
+out:
+ close(msr_fd);
+ return retval;
}
static 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);
int matches;
char character;
- ssnprintf(path, 80, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
+ 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);
interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
- snprintf(name, NAME_LEN, "cpu%02d", t->cpu_id);
+ ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
- if (!skip_c0)
+ if (!aperf_mperf_unstable)
turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
- if (!skip_c1)
+ if (!aperf_mperf_unstable)
turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
/* GHz */
if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
goto done;
- snprintf(name, NAME_LEN, "core%02d", c->core_id);
+ ssnprintf(name, sizeof(name), "core%02d", c->core_id);
if (do_core_cstate & (1 << 3))
turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
goto done;
- snprintf(name, NAME_LEN, "pkg%02d", p->package_id);
+ ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
if (do_ptm)
turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
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))
struct stat sb;
if (stat("/dev/cpu/0/msr", &sb)) {
- ERROR("no /dev/cpu/0/msr\n"
- "Try \"# modprobe msr\"");
+ ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
return -ERR_NO_MSR;
}
return 0;
{
unsigned long long msr;
unsigned int target_c_local;
- int cpu;
/* tcc_activation_temp is used only for dts or ptm */
if (!(do_dts || do_ptm))
if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
return 0;
- cpu = t->cpu_id;
- if (cpu_migrate(cpu)) {
- ERROR("Could not migrate to CPU %d\n", cpu);
- return -ERR_CPU_MIGRATE;
- }
-
- 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;
}
- if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
+ if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
goto guess;
target_c_local = (msr >> 16) & 0x7F;
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\n",
- cpu, 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;
}
/*
+ * 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
*/
int siblings;
if (cpu_is_not_present(i)) {
- //if (verbose > 1)
- fprintf(stderr, "cpu%d NOT PRESENT\n", i);
+ WARNING("cpu%d NOT PRESENT", i);
continue;
}
cpus[i].core_id = get_core_id(i);
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);
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);
}