Skip to content

Commit aed48c4

Browse files
Patryk Wlazlynlenb
authored andcommitted
tools/power turbostat: add early exits for permission checks
Checking early if the permissions are even needed gets rid of the warnings about some of them missing. Earlier we issued a warning in case of missing MSR and/or perf permissions, even when user never asked for counters that require those. Signed-off-by: Patryk Wlazlyn <[email protected]> Signed-off-by: Len Brown <[email protected]>
1 parent 5088741 commit aed48c4

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5818,6 +5818,14 @@ static int has_instr_count_access(void)
58185818
return has_access;
58195819
}
58205820

5821+
bool is_aperf_access_required(void)
5822+
{
5823+
return BIC_IS_ENABLED(BIC_Avg_MHz)
5824+
|| BIC_IS_ENABLED(BIC_Busy)
5825+
|| BIC_IS_ENABLED(BIC_Bzy_MHz)
5826+
|| BIC_IS_ENABLED(BIC_IPC);
5827+
}
5828+
58215829
/*
58225830
* Linux-perf manages the HW instructions-retired counter
58235831
* by enabling when requested, and hiding rollover
@@ -5833,8 +5841,7 @@ void linux_perf_init(void)
58335841
err(-1, "calloc fd_instr_count_percpu");
58345842
}
58355843

5836-
const bool aperf_required = BIC_IS_ENABLED(BIC_Avg_MHz) || BIC_IS_ENABLED(BIC_Busy) ||
5837-
BIC_IS_ENABLED(BIC_Bzy_MHz) || BIC_IS_ENABLED(BIC_IPC);
5844+
const bool aperf_required = is_aperf_access_required();
58385845
if (aperf_required && has_aperf && amperf_source == AMPERF_SOURCE_PERF) {
58395846
fd_amperf_percpu = calloc(topo.max_cpu_num + 1, sizeof(*fd_amperf_percpu));
58405847
if (fd_amperf_percpu == NULL)
@@ -5903,6 +5910,9 @@ static int has_amperf_access_via_perf(void)
59035910
/* Check if we can access APERF and MPERF */
59045911
static int has_amperf_access(void)
59055912
{
5913+
if (!is_aperf_access_required())
5914+
return 0;
5915+
59065916
if (!no_msr && has_amperf_access_via_msr())
59075917
return 1;
59085918

@@ -6581,7 +6591,8 @@ static void set_amperf_source(void)
65816591
{
65826592
amperf_source = AMPERF_SOURCE_PERF;
65836593

6584-
if (no_perf || !has_amperf_access_via_perf())
6594+
const bool aperf_required = is_aperf_access_required();
6595+
if (no_perf || !aperf_required || !has_amperf_access_via_perf())
65856596
amperf_source = AMPERF_SOURCE_MSR;
65866597

65876598
if (quiet || !debug)
@@ -6590,8 +6601,51 @@ static void set_amperf_source(void)
65906601
fprintf(outf, "aperf/mperf source preference: %s\n", amperf_source == AMPERF_SOURCE_MSR ? "msr" : "perf");
65916602
}
65926603

6604+
bool is_msr_access_required(void)
6605+
{
6606+
/* TODO: add detection for dynamic counters from add_counter() */
6607+
if (no_msr)
6608+
return false;
6609+
6610+
return BIC_IS_ENABLED(BIC_SMI)
6611+
|| BIC_IS_ENABLED(BIC_CPU_c1)
6612+
|| BIC_IS_ENABLED(BIC_CPU_c3)
6613+
|| BIC_IS_ENABLED(BIC_CPU_c6)
6614+
|| BIC_IS_ENABLED(BIC_CPU_c7)
6615+
|| BIC_IS_ENABLED(BIC_Mod_c6)
6616+
|| BIC_IS_ENABLED(BIC_CoreTmp)
6617+
|| BIC_IS_ENABLED(BIC_Totl_c0)
6618+
|| BIC_IS_ENABLED(BIC_Any_c0)
6619+
|| BIC_IS_ENABLED(BIC_GFX_c0)
6620+
|| BIC_IS_ENABLED(BIC_CPUGFX)
6621+
|| BIC_IS_ENABLED(BIC_Pkgpc3)
6622+
|| BIC_IS_ENABLED(BIC_Pkgpc6)
6623+
|| BIC_IS_ENABLED(BIC_Pkgpc2)
6624+
|| BIC_IS_ENABLED(BIC_Pkgpc7)
6625+
|| BIC_IS_ENABLED(BIC_Pkgpc8)
6626+
|| BIC_IS_ENABLED(BIC_Pkgpc9)
6627+
|| BIC_IS_ENABLED(BIC_Pkgpc10)
6628+
|| BIC_IS_ENABLED(BIC_CorWatt)
6629+
|| BIC_IS_ENABLED(BIC_Cor_J)
6630+
|| BIC_IS_ENABLED(BIC_PkgWatt)
6631+
|| BIC_IS_ENABLED(BIC_CorWatt)
6632+
|| BIC_IS_ENABLED(BIC_GFXWatt)
6633+
|| BIC_IS_ENABLED(BIC_RAMWatt)
6634+
|| BIC_IS_ENABLED(BIC_Pkg_J)
6635+
|| BIC_IS_ENABLED(BIC_Cor_J)
6636+
|| BIC_IS_ENABLED(BIC_GFX_J)
6637+
|| BIC_IS_ENABLED(BIC_RAM_J)
6638+
|| BIC_IS_ENABLED(BIC_PKG__)
6639+
|| BIC_IS_ENABLED(BIC_RAM__)
6640+
|| BIC_IS_ENABLED(BIC_PkgTmp)
6641+
|| (is_aperf_access_required() && !has_amperf_access_via_perf());
6642+
}
6643+
65936644
void check_msr_access(void)
65946645
{
6646+
if (!is_msr_access_required())
6647+
no_msr = 1;
6648+
65956649
check_dev_msr();
65966650
check_msr_permission();
65976651

@@ -6601,10 +6655,12 @@ void check_msr_access(void)
66016655

66026656
void check_perf_access(void)
66036657
{
6604-
if (no_perf || !has_instr_count_access())
6658+
const bool intrcount_required = BIC_IS_ENABLED(BIC_IPC);
6659+
if (no_perf || !intrcount_required || !has_instr_count_access())
66056660
bic_enabled &= ~BIC_IPC;
66066661

6607-
if (!has_amperf_access()) {
6662+
const bool aperf_required = is_aperf_access_required();
6663+
if (!aperf_required || !has_amperf_access()) {
66086664
bic_enabled &= ~BIC_Avg_MHz;
66096665
bic_enabled &= ~BIC_Busy;
66106666
bic_enabled &= ~BIC_Bzy_MHz;

0 commit comments

Comments
 (0)