Skip to content

Commit d71cb40

Browse files
committed
tools/power turbostat: Add run-time MSR driver probe
Rather than starting down the conditional-compile road... Probe the location of the MSR files at run-time. Signed-off-by: Len Brown <len.brown@intel.com>
1 parent 2313b97 commit d71cb40

1 file changed

Lines changed: 39 additions & 29 deletions

File tree

tools/power/x86/turbostat/turbostat.c

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ struct msr_counter {
142142
#define FLAGS_SHOW (1 << 1)
143143
#define SYSFS_PERCPU (1 << 1)
144144
};
145+
static int use_android_msr_path;
145146

146147
struct msr_counter bic[] = {
147148
{ 0x0, "usec", NULL, 0, 0, 0, NULL, 0 },
@@ -2413,20 +2414,11 @@ int get_msr_fd(int cpu)
24132414

24142415
if (fd)
24152416
return fd;
2416-
#if defined(ANDROID)
2417-
sprintf(pathname, "/dev/msr%d", cpu);
2418-
#else
2419-
sprintf(pathname, "/dev/cpu/%d/msr", cpu);
2420-
#endif
2417+
sprintf(pathname, use_android_msr_path ? "/dev/msr%d" : "/dev/cpu/%d/msr", cpu);
24212418
fd = open(pathname, O_RDONLY);
24222419
if (fd < 0)
2423-
#if defined(ANDROID)
2424-
err(-1, "%s open failed, try chown or chmod +r /dev/msr*, "
2425-
"or run with --no-msr, or run as root", pathname);
2426-
#else
2427-
err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, "
2428-
"or run with --no-msr, or run as root", pathname);
2429-
#endif
2420+
err(-1, "%s open failed, try chown or chmod +r %s, "
2421+
"or run with --no-msr, or run as root", pathname, use_android_msr_path ? "/dev/msr*" : "/dev/cpu/*/msr");
24302422
fd_percpu[cpu] = fd;
24312423

24322424
return fd;
@@ -6777,21 +6769,43 @@ void turbostat_loop()
67776769
}
67786770
}
67796771

6780-
void check_dev_msr()
6772+
int probe_dev_msr(void)
6773+
{
6774+
struct stat sb;
6775+
char pathname[32];
6776+
6777+
sprintf(pathname, "/dev/msr%d", base_cpu);
6778+
return !stat(pathname, &sb);
6779+
}
6780+
6781+
int probe_dev_cpu_msr(void)
67816782
{
67826783
struct stat sb;
67836784
char pathname[32];
67846785

6785-
if (no_msr)
6786-
return;
6787-
#if defined(ANDROID)
6788-
sprintf(pathname, "/dev/msr%d", base_cpu);
6789-
#else
67906786
sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
6791-
#endif
6792-
if (stat(pathname, &sb))
6793-
if (system("/sbin/modprobe msr > /dev/null 2>&1"))
6794-
no_msr = 1;
6787+
return !stat(pathname, &sb);
6788+
}
6789+
6790+
int probe_msr_driver(void)
6791+
{
6792+
if (probe_dev_msr()) {
6793+
use_android_msr_path = 1;
6794+
return 1;
6795+
}
6796+
return probe_dev_cpu_msr();
6797+
}
6798+
6799+
void check_msr_driver(void)
6800+
{
6801+
if (probe_msr_driver())
6802+
return;
6803+
6804+
if (system("/sbin/modprobe msr > /dev/null 2>&1"))
6805+
no_msr = 1;
6806+
6807+
if (!probe_msr_driver())
6808+
no_msr = 1;
67956809
}
67966810

67976811
/*
@@ -6846,11 +6860,7 @@ void check_msr_permission(void)
68466860
failed += check_for_cap_sys_rawio();
68476861

68486862
/* test file permissions */
6849-
#if defined(ANDROID)
6850-
sprintf(pathname, "/dev/msr%d", base_cpu);
6851-
#else
6852-
sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
6853-
#endif
6863+
sprintf(pathname, use_android_msr_path ? "/dev/msr%d" : "/dev/cpu/%d/msr", base_cpu);
68546864
if (euidaccess(pathname, R_OK)) {
68556865
failed++;
68566866
}
@@ -9476,7 +9486,7 @@ bool has_added_counters(void)
94769486

94779487
void check_msr_access(void)
94789488
{
9479-
check_dev_msr();
9489+
check_msr_driver();
94809490
check_msr_permission();
94819491

94829492
if (no_msr)
@@ -10147,7 +10157,7 @@ int get_and_dump_counters(void)
1014710157

1014810158
void print_version()
1014910159
{
10150-
fprintf(outf, "turbostat version 2025.10.24 - Len Brown <lenb@kernel.org>\n");
10160+
fprintf(outf, "turbostat version 2025.11.29 - Len Brown <lenb@kernel.org>\n");
1015110161
}
1015210162

1015310163
#define COMMAND_LINE_SIZE 2048

0 commit comments

Comments
 (0)