Skip to content

Commit 0e39702

Browse files
Patryk Wlazlynlenb
authored andcommitted
tools/power turbostat: Enable non-privileged users to read sysfs counters
A group of counters called "sysfs" displays software C-state request counts and resulting perceived C-state residency. They are not built-in counters that turbostat knows about ahead of time, rather they are discovered in sysfs when turbostat starts. Thus, they are added dynamically, using the same interface as user-added MSR counters. When turbostat enters "no-msr" mode, such as when running as a non-privileged user, it clears all added counters. Updating that to clear only actual MSR added counters allows regular users to see the sysfs counters. [lenb: commit message] Signed-off-by: Patryk Wlazlyn <[email protected]> Signed-off-by: Len Brown <[email protected]>
1 parent d3e6f62 commit 0e39702

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,36 +1377,42 @@ struct sys_counters {
13771377
struct msr_counter *pp;
13781378
} sys;
13791379

1380-
void free_sys_counters(void)
1380+
static size_t free_msr_counters_(struct msr_counter **pp)
13811381
{
1382-
struct msr_counter *p = sys.tp, *pnext = NULL;
1382+
struct msr_counter *p = NULL;
1383+
size_t num_freed = 0;
13831384

1384-
while (p) {
1385-
pnext = p->next;
1386-
free(p);
1387-
p = pnext;
1388-
}
1385+
while (*pp) {
1386+
p = *pp;
13891387

1390-
p = sys.cp, pnext = NULL;
1391-
while (p) {
1392-
pnext = p->next;
1393-
free(p);
1394-
p = pnext;
1395-
}
1388+
if (p->msr_num != 0) {
1389+
*pp = p->next;
13961390

1397-
p = sys.pp, pnext = NULL;
1398-
while (p) {
1399-
pnext = p->next;
1400-
free(p);
1401-
p = pnext;
1391+
free(p);
1392+
++num_freed;
1393+
1394+
continue;
1395+
}
1396+
1397+
pp = &p->next;
14021398
}
14031399

1404-
sys.added_thread_counters = 0;
1405-
sys.added_core_counters = 0;
1406-
sys.added_package_counters = 0;
1407-
sys.tp = NULL;
1408-
sys.cp = NULL;
1409-
sys.pp = NULL;
1400+
return num_freed;
1401+
}
1402+
1403+
/*
1404+
* Free all added counters accessed via msr.
1405+
*/
1406+
static void free_sys_msr_counters(void)
1407+
{
1408+
/* Thread counters */
1409+
sys.added_thread_counters -= free_msr_counters_(&sys.tp);
1410+
1411+
/* Core counters */
1412+
sys.added_core_counters -= free_msr_counters_(&sys.cp);
1413+
1414+
/* Package counters */
1415+
sys.added_package_counters -= free_msr_counters_(&sys.pp);
14101416
}
14111417

14121418
struct system_summary {
@@ -1566,7 +1572,7 @@ static void bic_disable_msr_access(void)
15661572

15671573
bic_enabled &= ~bic_msrs;
15681574

1569-
free_sys_counters();
1575+
free_sys_msr_counters();
15701576
}
15711577

15721578
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)

0 commit comments

Comments
 (0)