Skip to content

Commit

Permalink
tools/power turbostat: allow running without cpu0
Browse files Browse the repository at this point in the history
Linux-3.7 added CONFIG_BOOTPARAM_HOTPLUG_CPU0,
allowing systems to offline cpu0.

But when cpu0 is offline, turbostat will not run:

 # turbostat ls
turbostat: no /dev/cpu/0/msr

This patch replaces the hard-coded use of cpu0 in turbostat
with the current cpu, allowing it to run without a cpu0.

Fewer cross-calls may also be needed due to use of current cpu,
though this hard-coding was used only for the --debug preamble.

Signed-off-by: Prarit Bhargava <[email protected]>
Signed-off-by: Len Brown <[email protected]>
  • Loading branch information
prarit authored and lenb committed May 27, 2015
1 parent e9be7dd commit 7ce7d5d
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions tools/power/x86/turbostat/turbostat.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ unsigned int do_gfx_perf_limit_reasons;
unsigned int do_ring_perf_limit_reasons;
unsigned int crystal_hz;
unsigned long long tsc_hz;
int base_cpu;

#define RAPL_PKG (1 << 0)
/* 0x610 MSR_PKG_POWER_LIMIT */
Expand Down Expand Up @@ -1154,7 +1155,7 @@ dump_nhm_platform_info(void)
unsigned long long msr;
unsigned int ratio;

get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
get_msr(base_cpu, MSR_NHM_PLATFORM_INFO, &msr);

fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);

Expand All @@ -1166,7 +1167,7 @@ dump_nhm_platform_info(void)
fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
ratio, bclk, ratio * bclk);

get_msr(0, MSR_IA32_POWER_CTL, &msr);
get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
msr, msr & 0x2 ? "EN" : "DIS");

Expand All @@ -1179,7 +1180,7 @@ dump_hsw_turbo_ratio_limits(void)
unsigned long long msr;
unsigned int ratio;

get_msr(0, MSR_TURBO_RATIO_LIMIT2, &msr);
get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);

fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", msr);

Expand All @@ -1201,7 +1202,7 @@ dump_ivt_turbo_ratio_limits(void)
unsigned long long msr;
unsigned int ratio;

get_msr(0, MSR_TURBO_RATIO_LIMIT1, &msr);
get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);

fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", msr);

Expand Down Expand Up @@ -1253,7 +1254,7 @@ dump_nhm_turbo_ratio_limits(void)
unsigned long long msr;
unsigned int ratio;

get_msr(0, MSR_TURBO_RATIO_LIMIT, &msr);
get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);

fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);

Expand Down Expand Up @@ -1309,7 +1310,7 @@ dump_knl_turbo_ratio_limits(void)
int delta_ratio;
int i;

get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);

fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
msr);
Expand Down Expand Up @@ -1365,7 +1366,7 @@ dump_nhm_cst_cfg(void)
{
unsigned long long msr;

get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);

#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
Expand Down Expand Up @@ -1694,8 +1695,10 @@ void turbostat_loop()
void check_dev_msr()
{
struct stat sb;
char pathname[32];

if (stat("/dev/cpu/0/msr", &sb))
sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
if (stat(pathname, &sb))
if (system("/sbin/modprobe msr > /dev/null 2>&1"))
err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
}
Expand All @@ -1708,6 +1711,7 @@ void check_permissions()
cap_user_data_t cap_data = &cap_data_data;
extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
int do_exit = 0;
char pathname[32];

/* check for CAP_SYS_RAWIO */
cap_header->pid = getpid();
Expand All @@ -1722,7 +1726,8 @@ void check_permissions()
}

/* test file permissions */
if (euidaccess("/dev/cpu/0/msr", R_OK)) {
sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
if (euidaccess(pathname, R_OK)) {
do_exit++;
warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
}
Expand Down Expand Up @@ -1804,7 +1809,7 @@ int probe_nhm_msrs(unsigned int family, unsigned int model)
default:
return 0;
}
get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);

pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];

Expand Down Expand Up @@ -2043,7 +2048,7 @@ double get_tdp(model)
unsigned long long msr;

if (do_rapl & RAPL_PKG_POWER_INFO)
if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;

switch (model) {
Expand Down Expand Up @@ -2126,7 +2131,7 @@ void rapl_probe(unsigned int family, unsigned int model)
}

/* units on package 0, verify later other packages match */
if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
return;

rapl_power_units = 1.0 / (1 << (msr & 0xF));
Expand Down Expand Up @@ -2471,7 +2476,7 @@ double slm_bclk(void)
unsigned int i;
double freq;

if (get_msr(0, MSR_FSB_FREQ, &msr))
if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
fprintf(stderr, "SLM BCLK: unknown\n");

i = msr & 0xf;
Expand Down Expand Up @@ -2539,7 +2544,7 @@ int set_temperature_target(struct thread_data *t, struct core_data *c, struct pk
if (!do_nhm_platform_info)
goto guess;

if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
goto guess;

target_c_local = (msr >> 16) & 0xFF;
Expand Down Expand Up @@ -2913,13 +2918,24 @@ void setup_all_buffers(void)
for_all_proc_cpus(initialize_counters);
}

void set_base_cpu(void)
{
base_cpu = sched_getcpu();
if (base_cpu < 0)
err(-ENODEV, "No valid cpus found");

if (debug > 1)
fprintf(stderr, "base_cpu = %d\n", base_cpu);
}

void turbostat_init()
{
setup_all_buffers();
set_base_cpu();
check_dev_msr();
check_permissions();
process_cpuid();

setup_all_buffers();

if (debug)
for_all_cpus(print_epb, ODD_COUNTERS);
Expand Down

0 comments on commit 7ce7d5d

Please sign in to comment.