Skip to content

Commit adb4973

Browse files
gautshenlenb
authored andcommitted
tools/power turbostat: Fix AMD package-energy reporting
commit 05a2f07 ("tools/power turbostat: read RAPL counters via perf") that adds support to read RAPL counters via perf defines the notion of a RAPL domain_id which is set to physical_core_id on platforms which support per_core_rapl counters (Eg: AMD processors Family 17h onwards) and is set to the physical_package_id on all the other platforms. However, the physical_core_id is only unique within a package and on platforms with multiple packages more than one core can have the same physical_core_id and thus the same domain_id. (For eg, the first cores of each package have the physical_core_id = 0). This results in all these cores with the same physical_core_id using the same entry in the rapl_counter_info_perdomain[]. Since rapl_perf_init() skips the perf-initialization for cores whose domain_ids have already been visited, cores that have the same physical_core_id always read the perf file corresponding to the physical_core_id of the first package and thus the package-energy is incorrectly reported to be the same value for different packages. Note: This issue only arises when RAPL counters are read via perf and not when they are read via MSRs since in the latter case the MSRs are read separately on each core. Fix this issue by associating each CPU with rapl_core_id which is unique across all the packages in the system. Fixes: 05a2f07 ("tools/power turbostat: read RAPL counters via perf") Signed-off-by: Gautham R. Shenoy <gautham.shenoy@amd.com> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent b4a734d commit adb4973

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4773,6 +4773,38 @@ unsigned long pmt_read_counter(struct pmt_counter *ppmt, unsigned int domain_id)
47734773
return (value & value_mask) >> value_shift;
47744774
}
47754775

4776+
4777+
/* Rapl domain enumeration helpers */
4778+
static inline int get_rapl_num_domains(void)
4779+
{
4780+
int num_packages = topo.max_package_id + 1;
4781+
int num_cores_per_package;
4782+
int num_cores;
4783+
4784+
if (!platform->has_per_core_rapl)
4785+
return num_packages;
4786+
4787+
num_cores_per_package = topo.max_core_id + 1;
4788+
num_cores = num_cores_per_package * num_packages;
4789+
4790+
return num_cores;
4791+
}
4792+
4793+
static inline int get_rapl_domain_id(int cpu)
4794+
{
4795+
int nr_cores_per_package = topo.max_core_id + 1;
4796+
int rapl_core_id;
4797+
4798+
if (!platform->has_per_core_rapl)
4799+
return cpus[cpu].physical_package_id;
4800+
4801+
/* Compute the system-wide unique core-id for @cpu */
4802+
rapl_core_id = cpus[cpu].physical_core_id;
4803+
rapl_core_id += cpus[cpu].physical_package_id * nr_cores_per_package;
4804+
4805+
return rapl_core_id;
4806+
}
4807+
47764808
/*
47774809
* get_counters(...)
47784810
* migrate to cpu
@@ -4828,7 +4860,7 @@ int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
48284860
goto done;
48294861

48304862
if (platform->has_per_core_rapl) {
4831-
status = get_rapl_counters(cpu, c->core_id, c, p);
4863+
status = get_rapl_counters(cpu, get_rapl_domain_id(cpu), c, p);
48324864
if (status != 0)
48334865
return status;
48344866
}
@@ -4894,7 +4926,7 @@ int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
48944926
p->sys_lpi = cpuidle_cur_sys_lpi_us;
48954927

48964928
if (!platform->has_per_core_rapl) {
4897-
status = get_rapl_counters(cpu, p->package_id, c, p);
4929+
status = get_rapl_counters(cpu, get_rapl_domain_id(cpu), c, p);
48984930
if (status != 0)
48994931
return status;
49004932
}
@@ -7877,7 +7909,7 @@ void linux_perf_init(void)
78777909

78787910
void rapl_perf_init(void)
78797911
{
7880-
const unsigned int num_domains = (platform->has_per_core_rapl ? topo.max_core_id : topo.max_package_id) + 1;
7912+
const unsigned int num_domains = get_rapl_num_domains();
78817913
bool *domain_visited = calloc(num_domains, sizeof(bool));
78827914

78837915
rapl_counter_info_perdomain = calloc(num_domains, sizeof(*rapl_counter_info_perdomain));
@@ -7918,8 +7950,7 @@ void rapl_perf_init(void)
79187950
continue;
79197951

79207952
/* Skip already seen and handled RAPL domains */
7921-
next_domain =
7922-
platform->has_per_core_rapl ? cpus[cpu].physical_core_id : cpus[cpu].physical_package_id;
7953+
next_domain = get_rapl_domain_id(cpu);
79237954

79247955
assert(next_domain < num_domains);
79257956

0 commit comments

Comments
 (0)