Skip to content

Commit 7ce7d5d

Browse files
praritlenb
authored andcommitted
tools/power turbostat: allow running without cpu0
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 <prarit@redhat.com> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent e9be7dd commit 7ce7d5d

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ unsigned int do_gfx_perf_limit_reasons;
9292
unsigned int do_ring_perf_limit_reasons;
9393
unsigned int crystal_hz;
9494
unsigned long long tsc_hz;
95+
int base_cpu;
9596

9697
#define RAPL_PKG (1 << 0)
9798
/* 0x610 MSR_PKG_POWER_LIMIT */
@@ -1154,7 +1155,7 @@ dump_nhm_platform_info(void)
11541155
unsigned long long msr;
11551156
unsigned int ratio;
11561157

1157-
get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
1158+
get_msr(base_cpu, MSR_NHM_PLATFORM_INFO, &msr);
11581159

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

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

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

@@ -1179,7 +1180,7 @@ dump_hsw_turbo_ratio_limits(void)
11791180
unsigned long long msr;
11801181
unsigned int ratio;
11811182

1182-
get_msr(0, MSR_TURBO_RATIO_LIMIT2, &msr);
1183+
get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
11831184

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

@@ -1201,7 +1202,7 @@ dump_ivt_turbo_ratio_limits(void)
12011202
unsigned long long msr;
12021203
unsigned int ratio;
12031204

1204-
get_msr(0, MSR_TURBO_RATIO_LIMIT1, &msr);
1205+
get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
12051206

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

@@ -1253,7 +1254,7 @@ dump_nhm_turbo_ratio_limits(void)
12531254
unsigned long long msr;
12541255
unsigned int ratio;
12551256

1256-
get_msr(0, MSR_TURBO_RATIO_LIMIT, &msr);
1257+
get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
12571258

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

@@ -1309,7 +1310,7 @@ dump_knl_turbo_ratio_limits(void)
13091310
int delta_ratio;
13101311
int i;
13111312

1312-
get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1313+
get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
13131314

13141315
fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
13151316
msr);
@@ -1365,7 +1366,7 @@ dump_nhm_cst_cfg(void)
13651366
{
13661367
unsigned long long msr;
13671368

1368-
get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1369+
get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
13691370

13701371
#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
13711372
#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
@@ -1694,8 +1695,10 @@ void turbostat_loop()
16941695
void check_dev_msr()
16951696
{
16961697
struct stat sb;
1698+
char pathname[32];
16971699

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

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

17241728
/* test file permissions */
1725-
if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1729+
sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1730+
if (euidaccess(pathname, R_OK)) {
17261731
do_exit++;
17271732
warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
17281733
}
@@ -1804,7 +1809,7 @@ int probe_nhm_msrs(unsigned int family, unsigned int model)
18041809
default:
18051810
return 0;
18061811
}
1807-
get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1812+
get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
18081813

18091814
pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
18101815

@@ -2043,7 +2048,7 @@ double get_tdp(model)
20432048
unsigned long long msr;
20442049

20452050
if (do_rapl & RAPL_PKG_POWER_INFO)
2046-
if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
2051+
if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
20472052
return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
20482053

20492054
switch (model) {
@@ -2126,7 +2131,7 @@ void rapl_probe(unsigned int family, unsigned int model)
21262131
}
21272132

21282133
/* units on package 0, verify later other packages match */
2129-
if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
2134+
if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
21302135
return;
21312136

21322137
rapl_power_units = 1.0 / (1 << (msr & 0xF));
@@ -2471,7 +2476,7 @@ double slm_bclk(void)
24712476
unsigned int i;
24722477
double freq;
24732478

2474-
if (get_msr(0, MSR_FSB_FREQ, &msr))
2479+
if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
24752480
fprintf(stderr, "SLM BCLK: unknown\n");
24762481

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

2542-
if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2547+
if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
25432548
goto guess;
25442549

25452550
target_c_local = (msr >> 16) & 0xFF;
@@ -2913,13 +2918,24 @@ void setup_all_buffers(void)
29132918
for_all_proc_cpus(initialize_counters);
29142919
}
29152920

2921+
void set_base_cpu(void)
2922+
{
2923+
base_cpu = sched_getcpu();
2924+
if (base_cpu < 0)
2925+
err(-ENODEV, "No valid cpus found");
2926+
2927+
if (debug > 1)
2928+
fprintf(stderr, "base_cpu = %d\n", base_cpu);
2929+
}
2930+
29162931
void turbostat_init()
29172932
{
2933+
setup_all_buffers();
2934+
set_base_cpu();
29182935
check_dev_msr();
29192936
check_permissions();
29202937
process_cpuid();
29212938

2922-
setup_all_buffers();
29232939

29242940
if (debug)
29252941
for_all_cpus(print_epb, ODD_COUNTERS);

0 commit comments

Comments
 (0)