public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: Shuah Khan <skhan@linuxfoundation.org>
Cc: X86 ML <x86@kernel.org>, Thomas Renninger <trenn@suse.com>,
	Shuah Khan <shuah@kernel.org>, Len Brown <lenb@kernel.org>,
	linux-pm@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/4] tools/power/cpupower: Read energy_perf_bias from sysfs
Date: Mon, 2 Nov 2020 12:52:35 +0100	[thread overview]
Message-ID: <20201102115235.GB15392@zn.tnic> (raw)
In-Reply-To: <e0d61fa5-03b9-1761-1578-f4fb5ce54d96@linuxfoundation.org>

On Thu, Oct 29, 2020 at 04:32:43PM -0600, Shuah Khan wrote:
> > Because a patch should do one thing and one thing only. So a separate
> > patch which converts them all in one go should come ontop. But if you
> > insist for the ones I'm adding to have error handling, I can do that, of
> > course.
> > 
> 
> A separate patch is fine.

Here's how it could look like, I've converted two users for now to get
your opinion first before I convert the rest.

Thx.

---
diff --git a/tools/power/cpupower/bench/Makefile b/tools/power/cpupower/bench/Makefile
index f68b4bc55273..d4ac380f7a56 100644
--- a/tools/power/cpupower/bench/Makefile
+++ b/tools/power/cpupower/bench/Makefile
@@ -9,13 +9,14 @@ endif
 ifeq ($(strip $(STATIC)),true)
 LIBS = -L../ -L$(OUTPUT) -lm
 OBJS = $(OUTPUT)main.o $(OUTPUT)parse.o $(OUTPUT)system.o $(OUTPUT)benchmark.o \
-       $(OUTPUT)../lib/cpufreq.o $(OUTPUT)../lib/cpupower.o
+       $(OUTPUT)../lib/cpufreq.o $(OUTPUT)../lib/cpupower.o $(OUTPUT)../utils/helpers/string.o
 else
 LIBS = -L../ -L$(OUTPUT) -lm -lcpupower
-OBJS = $(OUTPUT)main.o $(OUTPUT)parse.o $(OUTPUT)system.o $(OUTPUT)benchmark.o
+OBJS = $(OUTPUT)main.o $(OUTPUT)parse.o $(OUTPUT)system.o $(OUTPUT)benchmark.o \
+       $(OUTPUT)../utils/helpers/string.o
 endif
 
-CFLAGS += -D_GNU_SOURCE -I../lib -DDEFAULT_CONFIG_FILE=\"$(confdir)/cpufreq-bench.conf\"
+CFLAGS += -D_GNU_SOURCE -I../lib -I../utils -DDEFAULT_CONFIG_FILE=\"$(confdir)/cpufreq-bench.conf\"
 
 $(OUTPUT)%.o : %.c
 	$(ECHO) "  CC      " $@
diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c
index e63dc11fa3a5..54ba48ad0983 100644
--- a/tools/power/cpupower/bench/parse.c
+++ b/tools/power/cpupower/bench/parse.c
@@ -18,6 +18,8 @@
 #include "parse.h"
 #include "config.h"
 
+#include "helpers/helpers.h"
+
 /**
  * converts priority string to priority
  *
@@ -84,11 +86,12 @@ FILE *prepare_output(const char *dirname)
 		}
 
 		filename = filename_tmp;
-		snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
-			dirname, sysdata.nodename, sysdata.release, time(NULL));
+		if (!cpupower_snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
+		    dirname, sysdata.nodename, sysdata.release, time(NULL)))
+			return NULL;
 	} else {
-		snprintf(filename, len - 1, "%s/benchmark_%li.log",
-			dirname, time(NULL));
+		if (!cpupower_snprintf(filename, len - 1, "%s/benchmark_%li.log", dirname, time(NULL)))
+			return NULL;
 	}
 
 	dprintf("logfilename: %s\n", filename);
diff --git a/tools/power/cpupower/utils/helpers/helpers.h b/tools/power/cpupower/utils/helpers/helpers.h
index 37dac161f3fe..dadd919ee599 100644
--- a/tools/power/cpupower/utils/helpers/helpers.h
+++ b/tools/power/cpupower/utils/helpers/helpers.h
@@ -171,4 +171,6 @@ static inline unsigned int cpuid_ecx(unsigned int op) { return 0; };
 static inline unsigned int cpuid_edx(unsigned int op) { return 0; };
 #endif /* defined(__i386__) || defined(__x86_64__) */
 
+extern char *cpupower_snprintf(char *str, int size, const char *fmt, ...);
+
 #endif /* __CPUPOWERUTILS_HELPERS__ */
diff --git a/tools/power/cpupower/utils/helpers/string.c b/tools/power/cpupower/utils/helpers/string.c
new file mode 100644
index 000000000000..bbd5e90bdc6e
--- /dev/null
+++ b/tools/power/cpupower/utils/helpers/string.c
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "helpers/helpers.h"
+
+/*
+ * A helper with error handling for less code clutter.
+ */
+char *cpupower_snprintf(char *str, int size, const char *fmt, ...)
+{
+	int sz = 0;
+	va_list ap;
+
+	va_start(ap, fmt);
+	sz = vsnprintf(str, size, fmt, ap);
+	va_end(ap);
+
+	if (sz < 0) {
+		perror("vsnprintf");
+		return NULL;
+	}
+
+	if (sz >= size) {
+		fprintf(stderr, "Output truncated: [%s]\n", str);
+		return NULL;
+	}
+
+	return str;
+}

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

  parent reply	other threads:[~2020-11-02 11:52 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-15 14:45 [PATCH 0/4] x86: Remove direct use of MSR_IA32_ENERGY_PERF_BIAS Borislav Petkov
2020-10-15 14:46 ` [PATCH 1/4] tools/power/cpupower: Read energy_perf_bias from sysfs Borislav Petkov
2020-10-15 17:49   ` Shuah Khan
2020-10-16  8:37     ` Borislav Petkov
2020-10-29 21:38       ` Shuah Khan
2020-10-29 21:59         ` Borislav Petkov
2020-10-29 22:32           ` Shuah Khan
2020-10-29 23:11             ` Borislav Petkov
2020-11-02 11:52             ` Borislav Petkov [this message]
2020-10-15 14:46 ` [PATCH 2/4] tools/power/turbostat: " Borislav Petkov
2020-10-15 14:46 ` [PATCH 3/4] tools/power/x86_energy_perf_policy: " Borislav Petkov
2020-10-15 14:46 ` [PATCH 4/4] x86/msr: Do not allow writes to MSR_IA32_ENERGY_PERF_BIAS Borislav Petkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201102115235.GB15392@zn.tnic \
    --to=bp@alien8.de \
    --cc=lenb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=trenn@suse.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox