]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/power turbostat: Clear added counters when in no-msr mode
authorPatryk Wlazlyn <patryk.wlazlyn@linux.intel.com>
Thu, 14 Mar 2024 10:36:55 +0000 (11:36 +0100)
committerLen Brown <len.brown@intel.com>
Tue, 2 Apr 2024 16:50:15 +0000 (12:50 -0400)
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 <len.brown@intel.com>
tools/power/x86/turbostat/turbostat.c

index e5e01b58992e365333ebf25fb48138ac2bda4aef..b4a892bf22bfd1ba0d3704f6d554a3d9097eeecf 100644 (file)
@@ -1160,6 +1160,37 @@ struct sys_counters {
        struct msr_counter *pp;
 } sys;
 
+void free_sys_counters(void)
+{
+       struct msr_counter *p = sys.tp, *pnext = NULL;
+       while (p) {
+               pnext = p->next;
+               free(p);
+               p = pnext;
+       }
+
+       p = sys.cp, pnext = NULL;
+       while (p) {
+               pnext = p->next;
+               free(p);
+               p = pnext;
+       }
+
+       p = sys.pp, pnext = NULL;
+       while (p) {
+               pnext = p->next;
+               free(p);
+               p = pnext;
+       }
+
+       sys.added_thread_counters = 0;
+       sys.added_core_counters = 0;
+       sys.added_package_counters = 0;
+       sys.tp = NULL;
+       sys.cp = NULL;
+       sys.pp = NULL;
+}
+
 struct system_summary {
        struct thread_data threads;
        struct core_data cores;
@@ -1315,6 +1346,8 @@ static void bic_disable_msr_access(void)
            BIC_Pkgpc2 | BIC_Pkgpc3 | BIC_Pkgpc6 | BIC_Pkgpc7 | BIC_Pkgpc8 | BIC_Pkgpc9 | BIC_Pkgpc10 | BIC_PkgTmp;
 
        bic_enabled &= ~bic_msrs;
+
+       free_sys_counters();
 }
 
 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)
        fprintf(outf, "aperf/mperf source preference: %s\n", amperf_source == AMPERF_SOURCE_MSR ? "msr" : "perf");
 }
 
+bool has_added_counters(void)
+{
+       /*
+        * It only makes sense to call this after the command line is parsed,
+        * otherwise sys structure is not populated.
+        */
+
+       return sys.added_core_counters | sys.added_thread_counters | sys.added_package_counters;
+}
+
 bool is_msr_access_required(void)
 {
-       /* TODO: add detection for dynamic counters from add_counter() */
        if (no_msr)
                return false;
 
+       if (has_added_counters())
+               return true;
+
        return BIC_IS_ENABLED(BIC_SMI)
            || BIC_IS_ENABLED(BIC_CPU_c1)
            || BIC_IS_ENABLED(BIC_CPU_c3)