Skip to content

Commit 4a1bb4d

Browse files
Patryk Wlazlynlenb
authored andcommitted
tools/power turbostat: Clear added counters when in no-msr mode
If user request --no-msr or is not able to access the MSRs, turbostat should clear all the counters added with --add. Because MSR access permission checks are done after the cmdline is parsed, the decision has to be defered up until the transition into no-msr mode happen. Signed-off-by: Len Brown <[email protected]>
1 parent aed48c4 commit 4a1bb4d

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,37 @@ struct sys_counters {
11601160
struct msr_counter *pp;
11611161
} sys;
11621162

1163+
void free_sys_counters(void)
1164+
{
1165+
struct msr_counter *p = sys.tp, *pnext = NULL;
1166+
while (p) {
1167+
pnext = p->next;
1168+
free(p);
1169+
p = pnext;
1170+
}
1171+
1172+
p = sys.cp, pnext = NULL;
1173+
while (p) {
1174+
pnext = p->next;
1175+
free(p);
1176+
p = pnext;
1177+
}
1178+
1179+
p = sys.pp, pnext = NULL;
1180+
while (p) {
1181+
pnext = p->next;
1182+
free(p);
1183+
p = pnext;
1184+
}
1185+
1186+
sys.added_thread_counters = 0;
1187+
sys.added_core_counters = 0;
1188+
sys.added_package_counters = 0;
1189+
sys.tp = NULL;
1190+
sys.cp = NULL;
1191+
sys.pp = NULL;
1192+
}
1193+
11631194
struct system_summary {
11641195
struct thread_data threads;
11651196
struct core_data cores;
@@ -1315,6 +1346,8 @@ static void bic_disable_msr_access(void)
13151346
BIC_Pkgpc2 | BIC_Pkgpc3 | BIC_Pkgpc6 | BIC_Pkgpc7 | BIC_Pkgpc8 | BIC_Pkgpc9 | BIC_Pkgpc10 | BIC_PkgTmp;
13161347

13171348
bic_enabled &= ~bic_msrs;
1349+
1350+
free_sys_counters();
13181351
}
13191352

13201353
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
@@ -6601,12 +6634,24 @@ static void set_amperf_source(void)
66016634
fprintf(outf, "aperf/mperf source preference: %s\n", amperf_source == AMPERF_SOURCE_MSR ? "msr" : "perf");
66026635
}
66036636

6637+
bool has_added_counters(void)
6638+
{
6639+
/*
6640+
* It only makes sense to call this after the command line is parsed,
6641+
* otherwise sys structure is not populated.
6642+
*/
6643+
6644+
return sys.added_core_counters | sys.added_thread_counters | sys.added_package_counters;
6645+
}
6646+
66046647
bool is_msr_access_required(void)
66056648
{
6606-
/* TODO: add detection for dynamic counters from add_counter() */
66076649
if (no_msr)
66086650
return false;
66096651

6652+
if (has_added_counters())
6653+
return true;
6654+
66106655
return BIC_IS_ENABLED(BIC_SMI)
66116656
|| BIC_IS_ENABLED(BIC_CPU_c1)
66126657
|| BIC_IS_ENABLED(BIC_CPU_c3)

0 commit comments

Comments
 (0)