Skip to contentSkip to conversation

cpupower: fix TSC MHz calculation for Mperf monitor

PATCH Superseded He Rongguang 2 participants 3 messages Dec 11, 2024 – Dec 12, 2024 CPU power monitoring

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cpupower: fix TSC MHz calculation for Mperf monitor

From e17f252433c923578e9c386a998200e488d9567d Mon Sep 17 00:00:00 2001
From: He Rongguang <[email protected]>
Date: Thu, 28 Nov 2024 16:50:05 +0800
Subject: [PATCH] cpupower: fix TSC MHz calculation

Commit 'cpupower: Make TSC read per CPU for Mperf monitor' (c2adb1877b7) changes TSC counter reads per cpu, but left time diff global (from start of all cpus to end of all cpus), thus diff(time) is too large for a cpu's tsc counting, resulting in far less than acutal TSC_Mhz and thus cpupower monitor showing far less than actual cpu realtime frequency.

/proc/cpuinfo shows frequency:
cat /proc/cpuinfo | egrep -e 'processor' -e 'MHz'
...
processor : 171
cpu MHz : 4108.498
...

before fix (System 100% busy):

    | Mperf              || Idle_Stats
 CPU| C0   | Cx   | Freq  || POLL | C1   | C2
 171|  0.77| 99.23|  2279||  0.00|  0.00|  0.00

after fix (System 100% busy):

    | Mperf              || Idle_Stats
 CPU| C0   | Cx   | Freq  || POLL | C1   | C2
 171|  0.46| 99.54|  4095||  0.00|  0.00|  0.00
Fixes c2adb1877b76 ("cpupower: Make TSC read per CPU for Mperf monitor")

---
Changes in v2:
- Fix scripts/checkpatch.pl style warnings.
- Link to v1:
https://lore.kernel.org/linux-pm/[email protected]/T/#u

---
 .../cpupower/utils/idle_monitor/mperf_monitor.c   | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/tools/power/cpupower/utils/idle_monitor/mperf_monitor.c index ae6af354a81d..08a399b0be28 100644

tools/power/cpupower/utils/idle_monitor/mperf_monitor.c +0 -0
@@ -33,7 +33,7 @@ static int mperf_get_count_percent(unsigned int

self_id, double *percent,
unsigned int cpu);
static int mperf_get_count_freq(unsigned int id, unsigned long long *count,
unsigned int cpu);
-static struct timespec time_start, time_end;
+static struct timespec *time_start, *time_end;

static cstate_t mperf_cstates[MPERF_CSTATE_COUNT] = {
{
@@ -174,7 +174,7 @@ static int mperf_get_count_percent(unsigned int id, double *percent,
dprint("%s: TSC Ref - mperf_diff: %llu, tsc_diff: %llu\n",
mperf_cstates[id].name, mperf_diff, tsc_diff);
} else if (max_freq_mode == MAX_FREQ_SYSFS) {
- timediff = max_frequency * timespec_diff_us(time_start, time_end);
+ timediff = max_frequency * timespec_diff_us(time_start[cpu],
time_end[cpu]);
*percent = 100.0 * mperf_diff / timediff; dprint("%s: MAXFREQ - mperf_diff: %llu, time_diff: %llu\n",
mperf_cstates[id].name, mperf_diff, timediff);
@@ -207,7 +207,7 @@ static int mperf_get_count_freq(unsigned int id, unsigned long long *count,
if (max_freq_mode == MAX_FREQ_TSC_REF) {
/* Calculate max_freq from TSC count */
tsc_diff = tsc_at_measure_end[cpu] - tsc_at_measure_start[cpu];
- time_diff = timespec_diff_us(time_start, time_end);
+ time_diff = timespec_diff_us(time_start[cpu], time_end[cpu]);
max_frequency = tsc_diff / time_diff;
}

@@ -226,9 +226,8 @@ static int mperf_start(void)
{
int cpu;

- clock_gettime(CLOCK_REALTIME, &time_start);
-
for (cpu = 0; cpu < cpu_count; cpu++) {
+ clock_gettime(CLOCK_REALTIME, &time_start[cpu]);
mperf_get_tsc(&tsc_at_measure_start[cpu]); mperf_init_stats(cpu);
}
@@ -243,9 +242,9 @@ static int mperf_stop(void)
for (cpu = 0; cpu < cpu_count; cpu++) {
mperf_measure_stats(cpu); mperf_get_tsc(&tsc_at_measure_end[cpu]);
+ clock_gettime(CLOCK_REALTIME, &time_end[cpu]);
}

- clock_gettime(CLOCK_REALTIME, &time_end);
return 0;
}

@@ -349,6 +348,8 @@ struct cpuidle_monitor *mperf_register(void)
aperf_current_count = calloc(cpu_count, sizeof(unsigned long long)); tsc_at_measure_start = calloc(cpu_count, sizeof(unsigned long long)); tsc_at_measure_end = calloc(cpu_count, sizeof(unsigned long long));
+ time_start = calloc(cpu_count, sizeof(struct timespec));
+ time_end = calloc(cpu_count, sizeof(struct timespec));
mperf_monitor.name_len = strlen(mperf_monitor.name); return &mperf_monitor;
}
@@ -361,6 +362,8 @@ void mperf_unregister(void)
free(aperf_current_count);
free(tsc_at_measure_start);
free(tsc_at_measure_end);
+ free(time_start);
+ free(time_end);
free(is_valid);
}

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 28 lines · He Rongguang

From e17f252433c923578e9c386a998200e488d9567d Mon Sep 17 00:00:00 2001
From: He Rongguang <[email protected]>
Date: Thu, 28 Nov 2024 16:50:05 +0800
Subject: [PATCH] cpupower: fix TSC MHz calculation

Commit 'cpupower: Make TSC read per CPU for Mperf monitor' (c2adb1877b7) changes TSC counter reads per cpu, but left time diff global (from start of all cpus to end of all cpus), thus diff(time) is too large for a cpu's tsc counting, resulting in far less than acutal TSC_Mhz and thus cpupower monitor showing far less than actual cpu realtime frequency.

/proc/cpuinfo shows frequency:
cat /proc/cpuinfo | egrep -e 'processor' -e 'MHz'
...
processor : 171
cpu MHz : 4108.498
...

before fix (System 100% busy):

     | Mperf              || Idle_Stats
  CPU| C0   | Cx   | Freq  || POLL | C1   | C2
  171|  0.77| 99.23|  2279||  0.00|  0.00|  0.00

after fix (System 100% busy):

     | Mperf              || Idle_Stats
  CPU| C0   | Cx   | Freq  || POLL | C1   | C2
  171|  0.46| 99.54|  4095||  0.00|  0.00|  0.00
Quoted 8 lines
Fixes c2adb1877b76 ("cpupower: Make TSC read per CPU for Mperf monitor")

---
Changes in v2:
- Fix scripts/checkpatch.pl style warnings.
- Link to v1:
https://lore.kernel.org/linux-pm/[email protected]/T/#u
---

Are you sure you sent me the right patch. I am seeing exact same errors. How are you sending your patches.

Here are the problems I am seeing:

ERROR: patch seems to be corrupt (line wrapped?)
#103: FILE: :173:
double *percent,

WARNING: It's generally not useful to have the filename in the file #108: FILE: :177:
+ timediff = max_frequency * timespec_diff_us(time_start[cpu],

WARNING: It's generally not useful to have the filename in the file #119: FILE: :210:
+ time_diff = timespec_diff_us(time_start[cpu], time_end[cpu]);

WARNING: It's generally not useful to have the filename in the file #130: FILE: :230:
+ clock_gettime(CLOCK_REALTIME, &time_start[cpu]);

WARNING: It's generally not useful to have the filename in the file #138: FILE: :245:
+ clock_gettime(CLOCK_REALTIME, &time_end[cpu]);

WARNING: It's generally not useful to have the filename in the file #149: FILE: :351:
+ time_start = calloc(cpu_count, sizeof(struct timespec));

WARNING: It's generally not useful to have the filename in the file #150: FILE: :352:
+ time_end = calloc(cpu_count, sizeof(struct timespec));

WARNING: It's generally not useful to have the filename in the file #158: FILE: :365:
+ free(time_start);

WARNING: It's generally not useful to have the filename in the file #159: FILE: :366:
+ free(time_end);

thanks,
-- Shuah

#2 lore raw
Quoted 38 lines · Shuah Khan

Are you sure you sent me the right patch. I am seeing exact same errors. How are you sending your patches.

Here are the problems I am seeing:

ERROR: patch seems to be corrupt (line wrapped?)
#103: FILE: :173:
double *percent,

WARNING: It's generally not useful to have the filename in the file #108: FILE: :177:
+        timediff = max_frequency * timespec_diff_us(time_start[cpu],

WARNING: It's generally not useful to have the filename in the file #119: FILE: :210:
+        time_diff = timespec_diff_us(time_start[cpu], time_end[cpu]);

WARNING: It's generally not useful to have the filename in the file #130: FILE: :230:
+        clock_gettime(CLOCK_REALTIME, &time_start[cpu]);

WARNING: It's generally not useful to have the filename in the file #138: FILE: :245:
+        clock_gettime(CLOCK_REALTIME, &time_end[cpu]);

WARNING: It's generally not useful to have the filename in the file #149: FILE: :351:
+    time_start = calloc(cpu_count, sizeof(struct timespec));

WARNING: It's generally not useful to have the filename in the file #150: FILE: :352:
+    time_end = calloc(cpu_count, sizeof(struct timespec));

WARNING: It's generally not useful to have the filename in the file #158: FILE: :365:
+    free(time_start);

Quoted 6 lines

WARNING: It's generally not useful to have the filename in the file #159: FILE: :366:
+    free(time_end);

thanks,
-- Shuah

I am very sorry, it's thunderbird auto wrap my patch, sent a v3, currently seems ok:

$ ./scripts/checkpatch.pl test.patch
total: 0 errors, 0 warnings, 60 lines checked

test.patch has no obvious style problems and is ready for submission.