Skip to contentSkip to conversation

tools/power turbostat: Fix RAPL summary collection on AMD processors

PATCH In review Terry Bowman 5 participants 23 messages Apr 19, 2021 – Apr 24, 2021 Turbostat utility

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/power turbostat: Fix RAPL summary collection on AMD processors

Turbostat fails to correctly collect and display RAPL summary information on Family 17h and 19h AMD processors. Running turbostat on these processors returns immediately. If turbostat is working correctly then RAPL summary data is displayed until the user provided command completes. If a command is not provided by the user then turbostat is designed to continuously display RAPL information until interrupted.

The issue is due to offset_to_idx() and idx_to_offset() missing support for AMD MSR addresses/offsets. offset_to_idx()'s switch statement is missing cases for AMD MSRs and idx_to_offset() does not include a path to return AMD MSR(s) for any idx.

The solution is add AMD MSR support to offset_to_idx() and idx_to_offset(). These functions are split-out and renamed along architecture vendor lines for supporting both AMD and Intel MSRs.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")

---
Changes in V2:
- Set patch title to v2. The first patch submission was mistakenly titled as v4 when it should have been v1.
- Change offset variables from 'int' to 'off_t' type. Change is needed to prevent sign extension in code casting int->off_t. This is currently a problem with AMD MSRs using base of 0xC000_0000
- Update idx_valid_amd() capability masking to use RAPL_AMD_F17H

 tools/power/x86/turbostat/turbostat.c | 63 ++++++++++++++++++++++++---
 1 file changed, 57 insertions(+), 6 deletions(-)
tools/power/x86/turbostat/turbostat.c +57 -6
index a7c4f0772e53..5aacdbd28fa8 100644
@@ -291,9 +291,9 @@ struct msr_sum_array {
 /* The percpu MSR sum array.*/
 struct msr_sum_array *per_cpu_msr_sum;
 
-int idx_to_offset(int idx)
+off_t idx_to_offset_intel(int idx)
 {
-	int offset;
+	off_t offset;
 
 	switch (idx) {
 	case IDX_PKG_ENERGY:
@@ -320,7 +320,7 @@ int idx_to_offset(int idx)
 	return offset;
 }
 
-int offset_to_idx(int offset)
+int offset_to_idx_intel(off_t offset)
 {
 	int idx;
 
@@ -349,7 +349,7 @@ int offset_to_idx(int offset)
 	return idx;
 }
 
-int idx_valid(int idx)
+int idx_valid_intel(int idx)
 {
 	switch (idx) {
 	case IDX_PKG_ENERGY:
@@ -368,6 +368,51 @@ int idx_valid(int idx)
 		return 0;
 	}
 }
+
+off_t (*idx_to_offset)(int idx) = idx_to_offset_intel;
+int (*offset_to_idx)(off_t offset) = offset_to_idx_intel;
+int (*idx_valid)(int idx) = idx_valid_intel;
+
+off_t idx_to_offset_amd(int idx)
+{
+	off_t offset;
+
+	switch (idx) {
+	case IDX_PKG_ENERGY:
+		offset = MSR_PKG_ENERGY_STAT;
+		break;
+	default:
+		offset = -1;
+	}
+
+	return offset;
+}
+
+int offset_to_idx_amd(off_t offset)
+{
+	int idx;
+
+	switch (offset) {
+	case MSR_PKG_ENERGY_STAT:
+		idx = IDX_PKG_ENERGY;
+		break;
+	default:
+		idx = -1;
+	}
+
+	return idx;
+}
+
+int idx_valid_amd(int idx)
+{
+	switch (idx) {
+	case IDX_PKG_ENERGY:
+		return do_rapl & RAPL_AMD_F17H;
+	default:
+		return 0;
+	}
+}
+
 struct sys_counters {
 	unsigned int added_thread_counters;
 	unsigned int added_core_counters;
@@ -3272,7 +3317,7 @@ static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg
 
 	for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
 		unsigned long long msr_cur, msr_last;
-		int offset;
+		off_t offset;
 
 		if (!idx_valid(i))
 			continue;
@@ -3281,7 +3326,7 @@ static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg
 			continue;
 		ret = get_msr(cpu, offset, &msr_cur);
 		if (ret) {
-			fprintf(outf, "Can not update msr(0x%x)\n", offset);
+			fprintf(outf, "Can not update msr(0x%lx)\n", offset);
 			continue;
 		}
 
@@ -5348,6 +5393,12 @@ void process_cpuid()
 	if (!quiet)
 		decode_misc_feature_control();
 
+	if (authentic_amd || hygon_genuine) {
+		idx_to_offset = idx_to_offset_amd;
+		offset_to_idx = offset_to_idx_amd;
+		idx_valid = idx_valid_amd;
+	}
+
 	return;
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw

On Mon, 2021-04-19 at 14:58 -0500, Terry Bowman wrote:

@@ -3281,7 +3326,7 @@ static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg continue;
                ret = get_msr(cpu, offset, &msr_cur);
                if (ret) {
-                       fprintf(outf, "Can not update msr(0x%x)\n", offset);
+                       fprintf(outf, "Can not update msr(0x%lx)\n", offset);

This gives a warning when compiled on 32-bit, since turbostat is compiled with -D_FILE_OFFSET_BITS=64:

turbostat.c: In function 'update_msr_sum':
turbostat.c:3329:42: warning: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'off_t' {aka 'long long int'} [-Wformat=]

 3329 |    fprintf(outf, "Can not update msr(0x%lx)\n", offset);
      |                                        ~~^      ~~~~~~
      |                                          |      |
      |                                          |      off_t {aka long long int}
      |                                          long unsigned int
      |                                        %llx

Easiest fix is probably to cast to (long long int) and use the %llx format specifier. That should be valid with i686, x32, and amd64 userspace.

Everything else looks fine as far as I can tell, so feel free to add a

once you've fixed that warning.

Signature · 1 line
 <[email protected]>
#2 lore raw
In reply to Terry Bowman
Quoted 12 lines · Terry Bowman

Turbostat fails to correctly collect and display RAPL summary information on Family 17h and 19h AMD processors. Running turbostat on these processors returns immediately. If turbostat is working correctly then RAPL summary data is displayed until the user provided command completes. If a command is not provided by the user then turbostat is designed to continuously display RAPL information until interrupted.

The issue is due to offset_to_idx() and idx_to_offset() missing support for AMD MSR addresses/offsets. offset_to_idx()'s switch statement is missing cases for AMD MSRs and idx_to_offset() does not include a path to return AMD MSR(s) for any idx.

Quoted 6 lines

The solution is add AMD MSR support to offset_to_idx() and idx_to_offset(). These functions are split-out and renamed along architecture vendor lines for supporting both AMD and Intel MSRs.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")

Thanks for fixing, Terry, and previously there was a patch for this from Bas Nieuwenhuizen:
https://lkml.org/lkml/2021/3/12/682
and it is expected to have been merged in Len's branch already.

thanks,
Chenyu

#3 lore raw
Quoted 17 lines · Chen Yu
Quoted 16 lines · Terry Bowman

Turbostat fails to correctly collect and display RAPL summary information on Family 17h and 19h AMD processors. Running turbostat on these processors returns immediately. If turbostat is working correctly then RAPL summary data is displayed until the user provided command completes. If a command is not provided by the user then turbostat is designed to continuously display RAPL information until interrupted.

The issue is due to offset_to_idx() and idx_to_offset() missing support for AMD MSR addresses/offsets. offset_to_idx()'s switch statement is missing cases for AMD MSRs and idx_to_offset() does not include a path to return AMD MSR(s) for any idx.

The solution is add AMD MSR support to offset_to_idx() and idx_to_offset(). These functions are split-out and renamed along architecture vendor lines for supporting both AMD and Intel MSRs.

Quoted 5 lines
Quoted 2 lines
Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")

Thanks for fixing, Terry, and previously there was a patch for this from Bas Nieuwenhuizen:
https://lkml.org/lkml/2021/3/12/682
and it is expected to have been merged in Len's branch already.

Expected?

So is it or is it not?

And can you folks agree on a patch already and give it to Artem for testing (CCed) because he's triggering it too:

https://bugzilla.kernel.org/show_bug.cgi?id=212357

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
Tested-by youling257 Tested-by sibingsong Tested-by Kurt Garloff
#4 lore raw
Quoted 20 lines · Borislav Petkov
Quoted 19 lines · Chen Yu
Quoted 18 lines · Terry Bowman

Turbostat fails to correctly collect and display RAPL summary information on Family 17h and 19h AMD processors. Running turbostat on these processors returns immediately. If turbostat is working correctly then RAPL summary data is displayed until the user provided command completes. If a command is not provided by the user then turbostat is designed to continuously display RAPL information until interrupted.

The issue is due to offset_to_idx() and idx_to_offset() missing support for AMD MSR addresses/offsets. offset_to_idx()'s switch statement is missing cases for AMD MSRs and idx_to_offset() does not include a path to return AMD MSR(s) for any idx.

The solution is add AMD MSR support to offset_to_idx() and idx_to_offset(). These functions are split-out and renamed along architecture vendor lines for supporting both AMD and Intel MSRs.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")
Quoted 8 lines
Quoted 3 lines

Thanks for fixing, Terry, and previously there was a patch for this from Bas Nieuwenhuizen:
https://lkml.org/lkml/2021/3/12/682
and it is expected to have been merged in Len's branch already.

Expected?

So is it or is it not?

This patch was sent to Len and it is not in public repo yet. He is preparing for a new release of turbostat as merge window is approaching.

Quoted 5 lines

And can you folks agree on a patch already and give it to Artem for testing (CCed) because he's triggering it too:

https://bugzilla.kernel.org/show_bug.cgi?id=212357

Okay. I would vote for the the patch from Bas as it was a combined work from two authors and tested by several AMD users. But let me paste it here too for Artem to see if this also works for him:

From 00e0622b1b693a5c7dc343aeb3aa51614a9e125e Mon Sep 17 00:00:00 2001
From: Bas Nieuwenhuizen <[email protected]>
Date: Fri, 12 Mar 2021 21:27:40 +0800
Subject: [PATCH] tools/power/turbostat: Fix turbostat for AMD Zen CPUs

It was reported that on Zen+ system turbostat started exiting, which was tracked down to the MSR_PKG_ENERGY_STAT read failing because offset_to_idx wasn't returning a non-negative index.

This patch combined the modification from Bingsong Si and
Bas Nieuwenhuizen and addd the MSR to the index system as alternative for MSR_PKG_ENERGY_STATUS.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")
---
 tools/power/x86/turbostat/turbostat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/power/x86/turbostat/turbostat.c +6 -2
index a7c4f0772e53..a7c965734fdf 100644
@@ -297,7 +297,10 @@ int idx_to_offset(int idx)
 
 	switch (idx) {
 	case IDX_PKG_ENERGY:
-		offset = MSR_PKG_ENERGY_STATUS;
+		if (do_rapl & RAPL_AMD_F17H)
+			offset = MSR_PKG_ENERGY_STAT;
+		else
+			offset = MSR_PKG_ENERGY_STATUS;
 		break;
 	case IDX_DRAM_ENERGY:
 		offset = MSR_DRAM_ENERGY_STATUS;
@@ -326,6 +329,7 @@ int offset_to_idx(int offset)
 
 	switch (offset) {
 	case MSR_PKG_ENERGY_STATUS:
+	case MSR_PKG_ENERGY_STAT:
 		idx = IDX_PKG_ENERGY;
 		break;
 	case MSR_DRAM_ENERGY_STATUS:
@@ -353,7 +357,7 @@ int idx_valid(int idx)
 {
 	switch (idx) {
 	case IDX_PKG_ENERGY:
-		return do_rapl & RAPL_PKG;
+		return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
 	case IDX_DRAM_ENERGY:
 		return do_rapl & RAPL_DRAM;
 	case IDX_PP0_ENERGY:
Signature · 5 lines
2.25.1


thanks,
Chenyu
Quoted 7 lines

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#5 lore raw
Quoted 20 lines · Chen Yu

Okay. I would vote for the the patch from Bas as it was a combined work from two
authors and tested by several AMD users. But let me paste it here too for Artem to
see if this also works for him:

From 00e0622b1b693a5c7dc343aeb3aa51614a9e125e Mon Sep 17 00:00:00 2001
From: Bas Nieuwenhuizen <[email protected]>
Date: Fri, 12 Mar 2021 21:27:40 +0800
Subject: [PATCH] tools/power/turbostat: Fix turbostat for AMD Zen CPUs

(quoted diff) +0 -0
@@ -297,7 +297,10 @@ int idx_to_offset(int idx)


        switch (idx) {
        case IDX_PKG_ENERGY:

Quoted 8 lines

-               offset = MSR_PKG_ENERGY_STATUS; +               if (do_rapl & RAPL_AMD_F17H)
+                       offset = MSR_PKG_ENERGY_STAT; +               else
+                       offset = MSR_PKG_ENERGY_STATUS; break;
        case IDX_DRAM_ENERGY:
                offset = MSR_DRAM_ENERGY_STATUS;

This patch has the same issue I noticed with the initial revision of Terry's patch - the idx_to_offset function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number)

The end result is, as far as I can tell, that it hits the if (offset <
0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not happen.

For short durations it still works fine since the background update isn't used.

Signature · 1 line
Calvin Walton <[email protected]>
#6 lore raw
Quoted 35 lines · Calvin Walton
Quoted 20 lines · Chen Yu

Okay. I would vote for the the patch from Bas as it was a combined work from two
authors and tested by several AMD users. But let me paste it here too for Artem to
see if this also works for him:

From 00e0622b1b693a5c7dc343aeb3aa51614a9e125e Mon Sep 17 00:00:00 2001
From: Bas Nieuwenhuizen <[email protected]>
Date: Fri, 12 Mar 2021 21:27:40 +0800
Subject: [PATCH] tools/power/turbostat: Fix turbostat for AMD Zen CPUs

(quoted diff) +0 -0
@@ -297,7 +297,10 @@ int idx_to_offset(int idx)


        switch (idx) {
        case IDX_PKG_ENERGY:

Quoted 8 lines

-               offset = MSR_PKG_ENERGY_STATUS; +               if (do_rapl & RAPL_AMD_F17H)
+                       offset = MSR_PKG_ENERGY_STAT; +               else
+                       offset = MSR_PKG_ENERGY_STATUS; break;
        case IDX_DRAM_ENERGY:
                offset = MSR_DRAM_ENERGY_STATUS;

This patch has the same issue I noticed with the initial revision of Terry's patch - the idx_to_offset function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number)

Quoted 8 lines

The end result is, as far as I can tell, that it hits the if (offset <
0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not happen.

For short durations it still works fine since the background update isn't used.

Ah, got it, nice catch. How about an incremental patch based on Bas' one to fix this 'overflow' issue? Would converting offset_to_idx(), idx_to_offset() and update_msr_sum() to use off_t instead of int be enough? Do you or Terry have interest to cook that patch? For Terry's version, I'm not sure if spliting
the code into different CPU vendor would benefit in the future, except that we would have plenty of new MSRs to be introduced in the future.

thanks,
Chenyu

Quoted 4 lines
Signature · 2 lines
-- 
Calvin Walton <[email protected]>
#7 lore raw
Quoted 18 lines · Chen Yu
Quoted 14 lines · Calvin Walton

This patch has the same issue I noticed with the initial revision of
Terry's patch - the idx_to_offset function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather,
would be interpreted as a negative number)

The end result is, as far as I can tell, that it hits the if (offset <
0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not
happen.

Quoted 3 lines

For short durations it still works fine since the background update isn't used.

Quoted 10 lines

Ah, got it, nice catch. How about an incremental patch based on Bas' one
to fix this 'overflow' issue? Would converting offset_to_idx(), idx_to_offset() and
update_msr_sum() to use off_t instead of int be enough? Do you or Terry have interest
to cook that patch? For Terry's version, I'm not sure if spliting the code into different CPU vendor would benefit in the future, except
that we would have plenty of new MSRs to be introduced in the future.

Yes, I believe updating the offset_to_idx(), idx_to_offset(), and update_msr_sum() functions is sufficient. I can do the incremental patch for that this evening if nobody beats me to it :)

Signature · 1 line
Calvin Walton <[email protected]>
#8 lore raw
Quoted 30 lines · Calvin Walton
Quoted 18 lines · Chen Yu
Quoted 14 lines · Calvin Walton

This patch has the same issue I noticed with the initial revision of
Terry's patch - the idx_to_offset function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather,
would be interpreted as a negative number)

The end result is, as far as I can tell, that it hits the if (offset <
0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not
happen.

Quoted 3 lines

For short durations it still works fine since the background update isn't used.

Quoted 10 lines

Ah, got it, nice catch. How about an incremental patch based on Bas' one
to fix this 'overflow' issue? Would converting offset_to_idx(), idx_to_offset() and
update_msr_sum() to use off_t instead of int be enough? Do you or Terry have interest
to cook that patch? For Terry's version, I'm not sure if spliting the code into different CPU vendor would benefit in the future, except
that we would have plenty of new MSRs to be introduced in the future.

Quoted 4 lines

Yes, I believe updating the offset_to_idx(), idx_to_offset(), and update_msr_sum() functions is sufficient. I can do the incremental patch for that this evening if nobody beats me to it :)

Calvin, could you please take a look at the following version if it is suitible?

From b2e63fe4f02e17289414b4f61237da822df115fb Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number). The end result is that it hits the if (offset < 0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not happen. The similar issue exists in offset_to_idx() and update_msr_sum().

This patch fixes this issue by converting the 'int' type to 'off_t' accordingly.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")
---
 tools/power/x86/turbostat/turbostat.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/power/x86/turbostat/turbostat.c +5 -5
index a211264b57fd..77557122b292 100644
@@ -296,9 +296,9 @@ struct msr_sum_array {
 /* The percpu MSR sum array.*/
 struct msr_sum_array *per_cpu_msr_sum;
 
-int idx_to_offset(int idx)
+off_t idx_to_offset(int idx)
 {
-	int offset;
+	off_t offset;
 
 	switch (idx) {
 	case IDX_PKG_ENERGY:
@@ -328,7 +328,7 @@ int idx_to_offset(int idx)
 	return offset;
 }
 
-int offset_to_idx(int offset)
+int offset_to_idx(off_t offset)
 {
 	int idx;
 
@@ -3338,7 +3338,7 @@ static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg
 
 	for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
 		unsigned long long msr_cur, msr_last;
-		int offset;
+		off_t offset;
 
 		if (!idx_valid(i))
 			continue;
@@ -3347,7 +3347,7 @@ static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg
 			continue;
 		ret = get_msr(cpu, offset, &msr_cur);
 		if (ret) {
-			fprintf(outf, "Can not update msr(0x%x)\n", offset);
+			fprintf(outf, "Can not update msr(0x%llx)\n", (long long int)offset);
 			continue;
 		}
 

thanks,
Chenyu

Signature · 1 line
2.25.1
Quoted 3 lines
Signature · 1 line
Calvin Walton <[email protected]>
#9 lore raw
Quoted 14 lines · Chen Yu

From b2e63fe4f02e17289414b4f61237da822df115fb Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number). The end result is that it hits the if (offset < 0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not happen. The similar issue exists in offset_to_idx() and update_msr_sum().

Quoted 5 lines

This patch fixes this issue by converting the 'int' type to 'off_t' accordingly.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")

This patch's authorship is weird: it says From: Calvin but doesn't have his SOB here - only yours.

Quoted 9 lines
---
 tools/power/x86/turbostat/turbostat.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/power/x86/turbostat/turbostat.c +0 -0
index a211264b57fd..77557122b292 100644

tools/power/x86/turbostat/turbostat.c struct msr_sum_array { · line 299

Quoted 5 lines
tools/power/x86/turbostat/turbostat.c +1 -1
@@ -296,9 +296,9 @@ struct msr_sum_array {
 /* The percpu MSR sum array.*/
 struct msr_sum_array *per_cpu_msr_sum;
 
-int idx_to_offset(int idx)
+off_t idx_to_offset(int idx)

And this is silly. MSRs are unsigned int. Fullstop.

So that function should either return u32 or unsigned int or so.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#10 lore raw

Hi Boris,

Quoted 15 lines · Borislav Petkov
Quoted 14 lines · Chen Yu

From b2e63fe4f02e17289414b4f61237da822df115fb Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number). The end result is that it hits the if (offset < 0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not happen. The similar issue exists in offset_to_idx() and update_msr_sum().

Quoted 9 lines
Quoted 5 lines

This patch fixes this issue by converting the 'int' type to 'off_t' accordingly.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")

This patch's authorship is weird: it says From: Calvin but doesn't have his SOB here - only yours.

I see, I'll add Calvin's SOB here. Previously I thought the 'From' field might be enough to indicate the Author, but it seems to not be the case.

Quoted 12 lines
Quoted 12 lines
---
 tools/power/x86/turbostat/turbostat.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/power/x86/turbostat/turbostat.c +0 -0
index a211264b57fd..77557122b292 100644
@@ -296,9 +296,9 @@ struct msr_sum_array {
 /* The percpu MSR sum array.*/
 struct msr_sum_array *per_cpu_msr_sum;
 
Quoted 7 lines

tools/power/x86/turbostat/turbostat.c struct msr_sum_array { · line 299

Quoted 2 lines
tools/power/x86/turbostat/turbostat.c +1 -1
-int idx_to_offset(int idx)
+off_t idx_to_offset(int idx)

And this is silly. MSRs are unsigned int. Fullstop.

So that function should either return u32 or unsigned int or so.

Got it. The off_t was derived from old code in this file from get_msr() and alike, let me convert this return value to unsigned int.

thanks,
Chenyu

Quoted 7 lines

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#11 lore raw
Quoted 2 lines · Chen Yu

I see, I'll add Calvin's SOB here. Previously I thought the 'From' field might be enough to indicate the Author, but it seems to not be the case.

The From: field is used by git to take the author but that's not the problem. You need the author her/himself to sign off on the work - no one else can do that.

You can refresh on that here:

Documentation/process/submitting-patches.rst

Quoted 2 lines

Got it. The off_t was derived from old code in this file from get_msr() and alike, let me convert this return value to unsigned int.

See my reply to Calvin - it needs to by off_t after all.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#12 lore raw
Quoted 8 lines · Borislav Petkov
Quoted 2 lines · Chen Yu

I see, I'll add Calvin's SOB here. Previously I thought the 'From' field might be enough to indicate the Author, but it seems to not be the case.

The From: field is used by git to take the author but that's not the problem. You need the author her/himself to sign off on the work - no one else can do that.

Thanks for the explanation.

Quoted 9 lines

You can refresh on that here:

Documentation/process/submitting-patches.rst

Quoted 2 lines

Got it. The off_t was derived from old code in this file from get_msr() and alike, let me convert this return value to unsigned int.

See my reply to Calvin - it needs to by off_t after all.

Okay.

Best,
Chenyu

Quoted 7 lines

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#13 lore raw
In reply to Borislav Petkov
Quoted 22 lines · Borislav Petkov
Quoted 21 lines · Chen Yu

From b2e63fe4f02e17289414b4f61237da822df115fb Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
 converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number). The end result is that it hits the if (offset < 0) check in update_msr_sum() resulting in the timer
callback for updating the stat in the background when long durations
are used to not happen. The similar issue exists in offset_to_idx() and update_msr_sum().

This patch fixes this issue by converting the 'int' type to 'off_t' accordingly.

Quoted 7 lines
Quoted 3 lines
Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL

display")

This patch's authorship is weird: it says From: Calvin but doesn't have
his SOB here - only yours.

I think this patch is adapted from one of my earlier submissions? I don't think I can really say that I wrote it, but I'll certainly review it.

Quoted 19 lines
Quoted 14 lines

---
 tools/power/x86/turbostat/turbostat.c | 10 +++++-----

 1 file changed, 5 insertions(+), 5 deletions(-)
tools/power/x86/turbostat/turbostat.c +0 -0
index a211264b57fd..77557122b292 100644
@@ -296,9 +296,9 @@ struct msr_sum_array {
  /* The percpu MSR sum array.*/
  struct msr_sum_array *per_cpu_msr_sum;

tools/power/x86/turbostat/turbostat.c struct msr_sum_array { · line 299

Quoted 2 lines
tools/power/x86/turbostat/turbostat.c +1 -1
-int idx_to_offset(int idx)
+off_t idx_to_offset(int idx)

And this is silly. MSRs are unsigned int. Fullstop.

Quoted 1 line

So that function should either return u32 or unsigned int or so.

So, there's two problems with that:
1. This function needs to be able to return an error value that cannot be confused with a valid MSR. This is currently done by returning a negative number. If an unsigned value is used, a different way of indicating errors needs to be written.
2. We are not using CPU instructions to access MSRs direction. Instead they are being read from /dev/msr. So the "offset" value is actually a seek into the /dev/msr file (using pread), and thus is of type off_t.

Signature · 1 line
Calvin Walton <[email protected]>
#14 lore raw
Quoted 8 lines · Calvin Walton

So, there's two problems with that:
1. This function needs to be able to return an error value that cannot be confused with a valid MSR. This is currently done by returning a negative number. If an unsigned value is used, a different way of indicating errors needs to be written.
2. We are not using CPU instructions to access MSRs direction. Instead they are being read from /dev/msr. So the "offset" value is actually a seek into the /dev/msr file (using pread), and thus is of type off_t.

Ah right, that's the /dev/msr thing. Then off_t is correct, forget what I said.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#15 lore raw
In reply to Calvin Walton
Quoted 56 lines · Calvin Walton
Quoted 22 lines · Borislav Petkov
Quoted 21 lines · Chen Yu

From b2e63fe4f02e17289414b4f61237da822df115fb Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
 converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number). The end result is that it hits the if (offset < 0) check in update_msr_sum() resulting in the timer
callback for updating the stat in the background when long durations
are used to not happen. The similar issue exists in offset_to_idx() and update_msr_sum().

This patch fixes this issue by converting the 'int' type to 'off_t' accordingly.

Quoted 7 lines
Quoted 3 lines
Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL

display")

This patch's authorship is weird: it says From: Calvin but doesn't have
his SOB here - only yours.

I think this patch is adapted from one of my earlier submissions? I don't think I can really say that I wrote it, but I'll certainly review it.

Quoted 19 lines
Quoted 14 lines

---
 tools/power/x86/turbostat/turbostat.c | 10 +++++-----

 1 file changed, 5 insertions(+), 5 deletions(-)
tools/power/x86/turbostat/turbostat.c +0 -0
index a211264b57fd..77557122b292 100644
@@ -296,9 +296,9 @@ struct msr_sum_array {
  /* The percpu MSR sum array.*/
  struct msr_sum_array *per_cpu_msr_sum;

tools/power/x86/turbostat/turbostat.c struct msr_sum_array { · line 299

Quoted 2 lines
tools/power/x86/turbostat/turbostat.c +1 -1
-int idx_to_offset(int idx)
+off_t idx_to_offset(int idx)

And this is silly. MSRs are unsigned int. Fullstop.

Quoted 1 line

So that function should either return u32 or unsigned int or so.

Quoted 9 lines

So, there's two problems with that:
1. This function needs to be able to return an error value that cannot be confused with a valid MSR. This is currently done by returning a negative number. If an unsigned value is used, a different way of indicating errors needs to be written.
2. We are not using CPU instructions to access MSRs direction. Instead they are being read from /dev/msr. So the "offset" value is actually a seek into the /dev/msr file (using pread), and thus is of type off_t.

I see, I misunderstood it with kernel's rdmsr() and ... I'll use the original version.

thanks,
Chenyu

Quoted 3 lines
Signature · 1 line
Calvin Walton <[email protected]>
#16 lore raw
In reply to Chen Yu

On Fri, 2021-04-23 at 20:16 +0800, Chen Yu wrote

Quoted 3 lines

Calvin, could you please take a look at the following version if it is suitible?

I assume this patch is adapted from my earlier submission of the complete fix to be an incremental fix on top of the already applied patch? If so, you are OK to add

Quoted 16 lines

From b2e63fe4f02e17289414b4f61237da822df115fb Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
 converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number). The end result is that it hits the if (offset < 0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not happen. The similar issue exists in offset_to_idx() and update_msr_sum().

Quoted 2 lines

This patch fixes this issue by converting the 'int' type to 'off_t' accordingly.

This patch covers all of the places I know about which had type issues
- it should be good. Thanks!

Signature · 1 line
Calvin Walton <[email protected]>
#17 lore raw
Quoted 11 lines · Calvin Walton

On Fri, 2021-04-23 at 20:16 +0800, Chen Yu wrote

Quoted 3 lines

Calvin, could you please take a look at the following version if it is suitible?

I assume this patch is adapted from my earlier submission of the complete fix to be an incremental fix on top of the already applied patch? If so, you are OK to add

Thanks Calvin. BTW, I did not receive your incremental patch on top of already applied one, so I post this patch in the name of yours.

Quoted 16 lines
Quoted 16 lines

From b2e63fe4f02e17289414b4f61237da822df115fb Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
 converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is greater than INT_MAX (or rather, would be interpreted as a negative number). The end result is that it hits the if (offset < 0) check in update_msr_sum() resulting in the timer callback for updating the stat in the background when long durations are used to not happen. The similar issue exists in offset_to_idx() and update_msr_sum().

Quoted 6 lines
Quoted 2 lines

This patch fixes this issue by converting the 'int' type to 'off_t' accordingly.

This patch covers all of the places I know about which had type issues
- it should be good. Thanks!

Okay, as per Boris' suggestion, this patch has changed the off_t to unsigned int, and the print format is kept the same as '%x' without casting, how about this:

From d5923502d0010d5e4d722c2a01ee8bda4a13bf75 Mon Sep 17 00:00:00 2001
From: Calvin Walton <[email protected]>
Date: Fri, 23 Apr 2021 17:32:13 +0800
Subject: [PATCH 3/5] tools/power turbostat: Fix offset overflow issue in index
converting

The idx_to_offset() function returns type int (32-bit signed), but MSR_PKG_ENERGY_STAT is u32 and would be interpreted as a negative number. The end result is that it hits the if (offset < 0) check in update_msr_sum() which prevents the timer callback from updating the stat in the background when long durations are used. The similar issue exists in offset_to_idx() and update_msr_sum().

This patch fixes this issue by converting the 'int' to 'unsigned int' accordingly.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")
---
 tools/power/x86/turbostat/turbostat.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/power/x86/turbostat/turbostat.c +4 -4
index a211264b57fd..dcde41784059 100644
@@ -296,9 +296,9 @@ struct msr_sum_array {
 /* The percpu MSR sum array.*/
 struct msr_sum_array *per_cpu_msr_sum;
 
-int idx_to_offset(int idx)
+unsigned int idx_to_offset(int idx)
 {
-	int offset;
+	unsigned int offset;
 
 	switch (idx) {
 	case IDX_PKG_ENERGY:
@@ -328,7 +328,7 @@ int idx_to_offset(int idx)
 	return offset;
 }
 
-int offset_to_idx(int offset)
+int offset_to_idx(unsigned int offset)
 {
 	int idx;
 
@@ -3338,7 +3338,7 @@ static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg
 
 	for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
 		unsigned long long msr_cur, msr_last;
-		int offset;
+		unsigned int offset;
 
 		if (!idx_valid(i))
 			continue;
Signature · 1 line
2.25.1
Quoted 3 lines
Signature · 1 line
Calvin Walton <[email protected]>
#18 lore raw
Quoted 3 lines · Chen Yu

This patch fixes this issue by converting the 'int' to 'unsigned int' accordingly.

NAK on this version of the patch. It needs to be off_t; see my conversation with Boris elsewhere in this thread.

Signature · 1 line
Calvin Walton <[email protected]>
#19 lore raw
In reply to Chen Yu
Quoted 91 lines · Chen Yu
Quoted 20 lines · Borislav Petkov
Quoted 19 lines · Chen Yu
Quoted 18 lines · Terry Bowman

Turbostat fails to correctly collect and display RAPL summary information on Family 17h and 19h AMD processors. Running turbostat on these processors returns immediately. If turbostat is working correctly then RAPL summary data is displayed until the user provided command completes. If a command is not provided by the user then turbostat is designed to continuously display RAPL information until interrupted.

The issue is due to offset_to_idx() and idx_to_offset() missing support for AMD MSR addresses/offsets. offset_to_idx()'s switch statement is missing cases for AMD MSRs and idx_to_offset() does not include a path to return AMD MSR(s) for any idx.

The solution is add AMD MSR support to offset_to_idx() and idx_to_offset(). These functions are split-out and renamed along architecture vendor lines for supporting both AMD and Intel MSRs.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")
Quoted 8 lines
Quoted 3 lines

Thanks for fixing, Terry, and previously there was a patch for this from Bas Nieuwenhuizen:
https://lkml.org/lkml/2021/3/12/682
and it is expected to have been merged in Len's branch already.

Expected?

So is it or is it not?

This patch was sent to Len and it is not in public repo yet. He is preparing for a new release of turbostat as merge window is approaching.

Quoted 5 lines

And can you folks agree on a patch already and give it to Artem for testing (CCed) because he's triggering it too:

https://bugzilla.kernel.org/show_bug.cgi?id=212357

Okay. I would vote for the the patch from Bas as it was a combined work from two authors and tested by several AMD users. But let me paste it here too for Artem to see if this also works for him:

From 00e0622b1b693a5c7dc343aeb3aa51614a9e125e Mon Sep 17 00:00:00 2001
From: Bas Nieuwenhuizen <[email protected]>
Date: Fri, 12 Mar 2021 21:27:40 +0800
Subject: [PATCH] tools/power/turbostat: Fix turbostat for AMD Zen CPUs

It was reported that on Zen+ system turbostat started exiting, which was tracked down to the MSR_PKG_ENERGY_STAT read failing because offset_to_idx wasn't returning a non-negative index.

This patch combined the modification from Bingsong Si and
Bas Nieuwenhuizen and addd the MSR to the index system as alternative for MSR_PKG_ENERGY_STATUS.

Fixes 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")
---
  tools/power/x86/turbostat/turbostat.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
tools/power/x86/turbostat/turbostat.c +5 -1
index a7c4f0772e53..a7c965734fdf 100644
@@ -297,7 +297,10 @@ int idx_to_offset(int idx)
 
  	switch (idx) {
  	case IDX_PKG_ENERGY:
-		offset = MSR_PKG_ENERGY_STATUS;
+		if (do_rapl & RAPL_AMD_F17H)
+			offset = MSR_PKG_ENERGY_STAT;
+		else
+			offset = MSR_PKG_ENERGY_STATUS;
  		break;
  	case IDX_DRAM_ENERGY:
  		offset = MSR_DRAM_ENERGY_STATUS;
@@ -326,6 +329,7 @@ int offset_to_idx(int offset)
 
  	switch (offset) {
  	case MSR_PKG_ENERGY_STATUS:
+	case MSR_PKG_ENERGY_STAT:
  		idx = IDX_PKG_ENERGY;
  		break;
  	case MSR_DRAM_ENERGY_STATUS:

tools/power/x86/turbostat/turbostat.c int idx_valid(int idx) · line 363

Quoted 8 lines
tools/power/x86/turbostat/turbostat.c +1 -1
@@ -353,7 +357,7 @@ int idx_valid(int idx)
  {
  	switch (idx) {
  	case IDX_PKG_ENERGY:
-		return do_rapl & RAPL_PKG;
+		return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
  	case IDX_DRAM_ENERGY:
  		return do_rapl & RAPL_DRAM;
  	case IDX_PP0_ENERGY:

The patch works for me.

#20 lore raw
In reply to Chen Yu
Quoted 1 line · Chen Yu

This patch was sent to Len and it is not in public repo yet.

If that thing were in a public repo, you'd have the advantage of pointing people to it and they could be testing patches.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#21 lore raw
Quoted 6 lines · Borislav Petkov
Quoted 1 line · Chen Yu

This patch was sent to Len and it is not in public repo yet.

If that thing were in a public repo, you'd have the advantage of pointing people to it and they could be testing patches.

Okay, I think we'll put it in public repo once verified.

thanks,
Chenyu

Quoted 5 lines
Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#22 lore raw
In reply to Terry Bowman
Quoted 12 lines · Terry Bowman

Turbostat fails to correctly collect and display RAPL summary information
on Family 17h and 19h AMD processors. Running turbostat on these processors
returns immediately. If turbostat is working correctly then RAPL summary
data is displayed until the user provided command completes. If a command
is not provided by the user then turbostat is designed to continuously
display RAPL information until interrupted.

Quoted 7 lines

The issue is due to offset_to_idx() and idx_to_offset() missing support for
AMD MSR addresses/offsets. offset_to_idx()'s switch statement is missing
cases for AMD MSRs and idx_to_offset() does not include a path to return
AMD MSR(s) for any idx.

Ah, when I was looking at Boris's patch, it lead me to one more spot that was missed with the off_t type - inside the function update_msr_sum(), the offset is also stored in a variable of type int, and as a result the background update for AMD's MSR when turbostat is used with long durations won't work. (It's skipped because of the offset < 0 check).

Signature · 1 line
Calvin Walton <[email protected]>