Skip to content

Commit 495c765

Browse files
committed
tools/power turbostat: extend --add option to accept /sys path
Previously, the --add option could specify only an MSR. Here is is extended so an arbitrary /sys attribute, as specified by an absolute file path name. sudo ./turbostat --add /sys/devices/system/cpu/cpu0/cpuidle/state5/usage Signed-off-by: Len Brown <len.brown@intel.com>
1 parent ade0eba commit 495c765

1 file changed

Lines changed: 69 additions & 23 deletions

File tree

tools/power/x86/turbostat/turbostat.c

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ unsigned int has_misc_feature_control;
138138
* Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
139139
*/
140140
#define NAME_BYTES 20
141+
#define PATH_BYTES 128
141142

142143
int backwards_count;
143144
char *progname;
@@ -213,6 +214,7 @@ enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
213214
struct msr_counter {
214215
unsigned int msr_num;
215216
char name[NAME_BYTES];
217+
char path[PATH_BYTES];
216218
unsigned int width;
217219
enum counter_type type;
218220
enum counter_format format;
@@ -344,7 +346,7 @@ struct msr_counter bic[] = {
344346
{ 0x0, "Bzy_MHz" },
345347
{ 0x0, "TSC_MHz" },
346348
{ 0x0, "IRQ" },
347-
{ 0x0, "SMI", 32, 0, FORMAT_DELTA, NULL},
349+
{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
348350
{ 0x0, "Busy%" },
349351
{ 0x0, "CPU%c1" },
350352
{ 0x0, "CPU%c3" },
@@ -1307,6 +1309,51 @@ static unsigned long long rdtsc(void)
13071309
return low | ((unsigned long long)high) << 32;
13081310
}
13091311

1312+
/*
1313+
* Open a file, and exit on failure
1314+
*/
1315+
FILE *fopen_or_die(const char *path, const char *mode)
1316+
{
1317+
FILE *filep = fopen(path, mode);
1318+
1319+
if (!filep)
1320+
err(1, "%s: open failed", path);
1321+
return filep;
1322+
}
1323+
/*
1324+
* snapshot_sysfs_counter()
1325+
*
1326+
* return snapshot of given counter
1327+
*/
1328+
unsigned long long snapshot_sysfs_counter(char *path)
1329+
{
1330+
FILE *fp;
1331+
int retval;
1332+
unsigned long long counter;
1333+
1334+
fp = fopen_or_die(path, "r");
1335+
1336+
retval = fscanf(fp, "%lld", &counter);
1337+
if (retval != 1)
1338+
err(1, "snapshot_sysfs_counter(%s)", path);
1339+
1340+
fclose(fp);
1341+
1342+
return counter;
1343+
}
1344+
1345+
int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1346+
{
1347+
if (mp->msr_num != 0) {
1348+
if (get_msr(cpu, mp->msr_num, counterp))
1349+
return -1;
1350+
} else {
1351+
*counterp = snapshot_sysfs_counter(mp->path);
1352+
}
1353+
1354+
return 0;
1355+
}
1356+
13101357
/*
13111358
* get_counters(...)
13121359
* migrate to cpu
@@ -1397,11 +1444,10 @@ int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
13971444
}
13981445

13991446
for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1400-
if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1447+
if (get_mp(cpu, mp, &t->counter[i]))
14011448
return -10;
14021449
}
14031450

1404-
14051451
/* collect core counters only for 1st thread in core */
14061452
if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
14071453
return 0;
@@ -1434,7 +1480,7 @@ int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
14341480
}
14351481

14361482
for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1437-
if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1483+
if (get_mp(cpu, mp, &c->counter[i]))
14381484
return -10;
14391485
}
14401486

@@ -1524,7 +1570,7 @@ int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
15241570
p->gfx_mhz = gfx_cur_mhz;
15251571

15261572
for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1527-
if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1573+
if (get_mp(cpu, mp, &p->counter[i]))
15281574
return -10;
15291575
}
15301576

@@ -2013,16 +2059,6 @@ void free_all_buffers(void)
20132059
free(irqs_per_cpu);
20142060
}
20152061

2016-
/*
2017-
* Open a file, and exit on failure
2018-
*/
2019-
FILE *fopen_or_die(const char *path, const char *mode)
2020-
{
2021-
FILE *filep = fopen(path, mode);
2022-
if (!filep)
2023-
err(1, "%s: open failed", path);
2024-
return filep;
2025-
}
20262062

20272063
/*
20282064
* Parse a file containing a single int.
@@ -2581,7 +2617,7 @@ int probe_nhm_msrs(unsigned int family, unsigned int model)
25812617
return 1;
25822618
}
25832619
/*
2584-
* SLV client has supporet for unique MSRs:
2620+
* SLV client has support for unique MSRs:
25852621
*
25862622
* MSR_CC6_DEMOTION_POLICY_CONFIG
25872623
* MSR_MC6_DEMOTION_POLICY_CONFIG
@@ -4342,9 +4378,9 @@ void print_version() {
43424378
" - Len Brown <lenb@kernel.org>\n");
43434379
}
43444380

4345-
int add_counter(unsigned int msr_num, char *name, unsigned int width,
4346-
enum counter_scope scope, enum counter_type type,
4347-
enum counter_format format)
4381+
int add_counter(unsigned int msr_num, char *path, char *name,
4382+
unsigned int width, enum counter_scope scope,
4383+
enum counter_type type, enum counter_format format)
43484384
{
43494385
struct msr_counter *msrp;
43504386

@@ -4356,6 +4392,8 @@ int add_counter(unsigned int msr_num, char *name, unsigned int width,
43564392

43574393
msrp->msr_num = msr_num;
43584394
strncpy(msrp->name, name, NAME_BYTES);
4395+
if (path)
4396+
strncpy(msrp->path, path, PATH_BYTES);
43594397
msrp->width = width;
43604398
msrp->type = type;
43614399
msrp->format = format;
@@ -4402,6 +4440,7 @@ int add_counter(unsigned int msr_num, char *name, unsigned int width,
44024440
void parse_add_command(char *add_command)
44034441
{
44044442
int msr_num = 0;
4443+
char *path = NULL;
44054444
char name_buffer[NAME_BYTES] = "";
44064445
int width = 64;
44074446
int fail = 0;
@@ -4417,6 +4456,11 @@ void parse_add_command(char *add_command)
44174456
if (sscanf(add_command, "msr%d", &msr_num) == 1)
44184457
goto next;
44194458

4459+
if (*add_command == '/') {
4460+
path = add_command;
4461+
goto next;
4462+
}
4463+
44204464
if (sscanf(add_command, "u%d", &width) == 1) {
44214465
if ((width == 32) || (width == 64))
44224466
goto next;
@@ -4466,12 +4510,14 @@ void parse_add_command(char *add_command)
44664510

44674511
next:
44684512
add_command = strchr(add_command, ',');
4469-
if (add_command)
4513+
if (add_command) {
4514+
*add_command = '\0';
44704515
add_command++;
4516+
}
44714517

44724518
}
4473-
if (msr_num == 0) {
4474-
fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4519+
if ((msr_num == 0) && (path == NULL)) {
4520+
fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
44754521
fail++;
44764522
}
44774523

@@ -4495,7 +4541,7 @@ void parse_add_command(char *add_command)
44954541
}
44964542
}
44974543

4498-
if (add_counter(msr_num, name_buffer, width, scope, type, format))
4544+
if (add_counter(msr_num, path, name_buffer, width, scope, type, format))
44994545
fail++;
45004546

45014547
if (fail) {

0 commit comments

Comments
 (0)