diff mbox series

[08/10] tools/power turbostat: Consolidate graphics sysfs access

Message ID 20241114075946.118577-9-rui.zhang@intel.com
State Accepted
Commit d071004e623b7433573019d67cba79e345d83006
Headers show
Series tools/power turbostat: Fixes, enabling and enhancements | expand

Commit Message

Zhang, Rui Nov. 14, 2024, 7:59 a.m. UTC
Currently, there is an inconsistency in how graphics sysfs knobs are
accessed: graphics residency sysfs knobs are opened and closed for each
read, while graphics frequency sysfs knobs are opened once and remain
open until turbostat exits. This inconsistency is confusing and adds
unnecessary code complexity.

Consolidate the access method by opening the sysfs files once and
reusing the file pointers for subsequent accesses. This approach
simplifies the code and ensures a consistent method for accessing
graphics sysfs knobs.

Signed-off-by: Zhang Rui <rui.zhang@intel.com>
---
 tools/power/x86/turbostat/turbostat.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 4956f4b837d1..aa0dab13d346 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -5731,27 +5731,24 @@  int snapshot_proc_interrupts(void)
  */
 int snapshot_graphics(int idx)
 {
-	FILE *fp;
 	int retval;
 
+	if (gfx_info[idx].fp == NULL)
+		gfx_info[idx].fp = fopen_or_die(gfx_info[idx].path, "r");
+	else
+		rewind(gfx_info[idx].fp);
+
 	switch (idx) {
 	case GFX_rc6:
 	case SAM_mc6:
-		fp = fopen_or_die(gfx_info[idx].path, "r");
-		retval = fscanf(fp, "%lld", &gfx_info[idx].val_ull);
+		retval = fscanf(gfx_info[idx].fp, "%lld", &gfx_info[idx].val_ull);
 		if (retval != 1)
 			err(1, "rc6");
-		fclose(fp);
 		return 0;
 	case GFX_MHz:
 	case GFX_ACTMHz:
 	case SAM_MHz:
 	case SAM_ACTMHz:
-		if (gfx_info[idx].fp == NULL)
-			gfx_info[idx].fp = fopen_or_die(gfx_info[idx].path, "r");
-		else
-			rewind(gfx_info[idx].fp);
-
 		retval = fscanf(gfx_info[idx].fp, "%d", &gfx_info[idx].val);
 		if (retval != 1)
 			err(1, "MHz");