Skip to content

Commit 19d0769

Browse files
Patryk Wlazlynlenb
authored andcommitted
tools/power turbostat: Include umask=%x in perf counter's config
Some counters, like cpu/cache-misses/, expose and require umask=%x parameter alongside event=%x in the sysfs perf counter's event file. This change make sure we parse and use it when opening user added counters. Signed-off-by: Patryk Wlazlyn <patryk.wlazlyn@linux.intel.com> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent 944264a commit 19d0769

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4017,15 +4017,55 @@ static unsigned int read_perf_type(const char *subsys)
40174017
return read_perf_counter_info_n(path, format);
40184018
}
40194019

4020-
static unsigned int read_rapl_config(const char *subsys, const char *event_name)
4020+
static unsigned int read_perf_config(const char *subsys, const char *event_name)
40214021
{
40224022
const char *const path_format = "/sys/bus/event_source/devices/%s/events/%s";
4023-
const char *const format = "event=%x";
4023+
FILE *fconfig = NULL;
40244024
char path[128];
4025+
char config_str[64];
4026+
unsigned int config;
4027+
unsigned int umask;
4028+
bool has_config = false;
4029+
bool has_umask = false;
4030+
unsigned int ret = -1;
40254031

40264032
snprintf(path, sizeof(path), path_format, subsys, event_name);
40274033

4028-
return read_perf_counter_info_n(path, format);
4034+
fconfig = fopen(path, "r");
4035+
if (!fconfig)
4036+
return -1;
4037+
4038+
if (fgets(config_str, ARRAY_SIZE(config_str), fconfig) != config_str)
4039+
goto cleanup_and_exit;
4040+
4041+
for (char *pconfig_str = &config_str[0]; pconfig_str;) {
4042+
if (sscanf(pconfig_str, "event=%x", &config) == 1) {
4043+
has_config = true;
4044+
goto next;
4045+
}
4046+
4047+
if (sscanf(pconfig_str, "umask=%x", &umask) == 1) {
4048+
has_umask = true;
4049+
goto next;
4050+
}
4051+
4052+
next:
4053+
pconfig_str = strchr(pconfig_str, ',');
4054+
if (pconfig_str) {
4055+
*pconfig_str = '\0';
4056+
++pconfig_str;
4057+
}
4058+
}
4059+
4060+
if (!has_umask)
4061+
umask = 0;
4062+
4063+
if (has_config)
4064+
ret = (umask << 8) | config;
4065+
4066+
cleanup_and_exit:
4067+
fclose(fconfig);
4068+
return ret;
40294069
}
40304070

40314071
static unsigned int read_perf_rapl_unit(const char *subsys, const char *event_name)
@@ -4044,7 +4084,7 @@ static unsigned int read_perf_rapl_unit(const char *subsys, const char *event_na
40444084
return RAPL_UNIT_INVALID;
40454085
}
40464086

4047-
static double read_perf_rapl_scale(const char *subsys, const char *event_name)
4087+
static double read_perf_scale(const char *subsys, const char *event_name)
40484088
{
40494089
const char *const path_format = "/sys/bus/event_source/devices/%s/events/%s.scale";
40504090
const char *const format = "%lf";
@@ -7425,7 +7465,7 @@ int add_rapl_perf_counter_(int cpu, struct rapl_counter_info_t *rci, const struc
74257465
if (no_perf)
74267466
return -1;
74277467

7428-
const double scale = read_perf_rapl_scale(cai->perf_subsys, cai->perf_name);
7468+
const double scale = read_perf_scale(cai->perf_subsys, cai->perf_name);
74297469

74307470
if (scale == 0.0)
74317471
return -1;
@@ -7436,7 +7476,7 @@ int add_rapl_perf_counter_(int cpu, struct rapl_counter_info_t *rci, const struc
74367476
return -1;
74377477

74387478
const unsigned int rapl_type = read_perf_type(cai->perf_subsys);
7439-
const unsigned int rapl_energy_pkg_config = read_rapl_config(cai->perf_subsys, cai->perf_name);
7479+
const unsigned int rapl_energy_pkg_config = read_perf_config(cai->perf_subsys, cai->perf_name);
74407480

74417481
const int fd_counter =
74427482
open_perf_counter(cpu, rapl_type, rapl_energy_pkg_config, rci->fd_perf, PERF_FORMAT_GROUP);
@@ -7598,7 +7638,7 @@ int add_cstate_perf_counter_(int cpu, struct cstate_counter_info_t *cci, const s
75987638
return -1;
75997639

76007640
const unsigned int type = read_perf_type(cai->perf_subsys);
7601-
const unsigned int config = read_rapl_config(cai->perf_subsys, cai->perf_name);
7641+
const unsigned int config = read_perf_config(cai->perf_subsys, cai->perf_name);
76027642

76037643
const int fd_counter = open_perf_counter(cpu, type, config, *pfd_group, PERF_FORMAT_GROUP);
76047644

@@ -7628,7 +7668,7 @@ int add_msr_perf_counter_(int cpu, struct msr_counter_info_t *cci, const struct
76287668
return -1;
76297669

76307670
const unsigned int type = read_perf_type(cai->perf_subsys);
7631-
const unsigned int config = read_rapl_config(cai->perf_subsys, cai->perf_name);
7671+
const unsigned int config = read_perf_config(cai->perf_subsys, cai->perf_name);
76327672

76337673
const int fd_counter = open_perf_counter(cpu, type, config, cci->fd_perf, PERF_FORMAT_GROUP);
76347674

@@ -8571,15 +8611,15 @@ int added_perf_counters_init_(struct perf_counter_info *pinfo)
85718611
continue;
85728612
}
85738613

8574-
perf_config = read_rapl_config(pinfo->device, pinfo->event);
8614+
perf_config = read_perf_config(pinfo->device, pinfo->event);
85758615
if (perf_config == (unsigned int)-1) {
85768616
warnx("%s: perf/%s/%s: failed to read %s",
85778617
__func__, pinfo->device, pinfo->event, "config");
85788618
continue;
85798619
}
85808620

85818621
/* Scale is not required, some counters just don't have it. */
8582-
perf_scale = read_perf_rapl_scale(pinfo->device, pinfo->event);
8622+
perf_scale = read_perf_scale(pinfo->device, pinfo->event);
85838623
if (perf_scale == 0.0)
85848624
perf_scale = 1.0;
85858625

0 commit comments

Comments
 (0)