public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	David Ahern <dsahern@gmail.com>, Jiri Olsa <jolsa@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Wang Nan <wangnan0@huawei.com>
Subject: [PATCH 02/25] perf tools: Do parameter validation earlier on fetch_kernel_version()
Date: Wed, 21 Jun 2017 15:02:22 -0300	[thread overview]
Message-ID: <20170621180245.23134-3-acme@kernel.org> (raw)
In-Reply-To: <20170621180245.23134-1-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

While trying to reduce util.[ch] I noticed that fetch_kernel_version()
and fetch_ubuntu_kernel_version() do lots of operations only to check if
they are needed, i.e. it checks if the pointer where to return the
kernel version is NULL only after obtaining the kernel version from
/proc/version_signature or by parsing the results from uname().

Do it earlier not to confuse people reading this code in the future :-)

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-i94qwyekk4tzbu0b9ce1r1mz@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/util.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 3cd42995ac6f..988111e0bab5 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -350,8 +350,12 @@ fetch_ubuntu_kernel_version(unsigned int *puint)
 	size_t line_len = 0;
 	char *ptr, *line = NULL;
 	int version, patchlevel, sublevel, err;
-	FILE *vsig = fopen("/proc/version_signature", "r");
+	FILE *vsig;
 
+	if (!puint)
+		return 0;
+
+	vsig = fopen("/proc/version_signature", "r");
 	if (!vsig) {
 		pr_debug("Open /proc/version_signature failed: %s\n",
 			 strerror(errno));
@@ -381,8 +385,7 @@ fetch_ubuntu_kernel_version(unsigned int *puint)
 		goto errout;
 	}
 
-	if (puint)
-		*puint = (version << 16) + (patchlevel << 8) + sublevel;
+	*puint = (version << 16) + (patchlevel << 8) + sublevel;
 	err = 0;
 errout:
 	free(line);
@@ -409,6 +412,9 @@ fetch_kernel_version(unsigned int *puint, char *str,
 		str[str_size - 1] = '\0';
 	}
 
+	if (!puint || int_ver_ready)
+		return 0;
+
 	err = sscanf(utsname.release, "%d.%d.%d",
 		     &version, &patchlevel, &sublevel);
 
@@ -418,8 +424,7 @@ fetch_kernel_version(unsigned int *puint, char *str,
 		return -1;
 	}
 
-	if (puint && !int_ver_ready)
-		*puint = (version << 16) + (patchlevel << 8) + sublevel;
+	*puint = (version << 16) + (patchlevel << 8) + sublevel;
 	return 0;
 }
 
-- 
2.9.4

  parent reply	other threads:[~2017-06-21 18:03 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-21 18:02 [GIT PULL 00/25] perf/core improvements and fixes Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 01/25] perf evsel: Adopt find_process() Arnaldo Carvalho de Melo
2017-06-21 18:02 ` Arnaldo Carvalho de Melo [this message]
2017-06-21 18:02 ` [PATCH 03/25] perf tools: Remove unused _ALL_SOURCE define Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 04/25] tools lib api fs: Add sysfs__write_int function Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 05/25] perf stat: Add support to measure SMI cost Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 06/25] perf unwind: Support for powerpc Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 07/25] perf intel-pt: Move decoder error setting into one condition Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 08/25] perf intel-pt: Improve sample timestamp Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 09/25] perf intel-pt: Fix missing stack clear Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 10/25] perf intel-pt: Ensure IP is zero when state is INTEL_PT_STATE_NO_IP Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 11/25] perf intel-pt: Fix last_ip usage Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 12/25] perf intel-pt: Ensure never to set 'last_ip' when packet 'count' is zero Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 13/25] perf intel-pt: Use FUP always when scanning for an IP Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 14/25] perf intel-pt: Clear FUP flag on error Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 15/25] perf intel-pt: Add missing __fallthrough Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 16/25] perf intel-pt: Allow decoding with branch tracing disabled Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 17/25] perf intel-pt: Add default config for pass-through branch enable Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 18/25] perf intel-pt: Add documentation for new config terms Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 19/25] perf intel-pt: Add decoder support for ptwrite and power event packets Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 20/25] perf intel-pt: Add reserved byte to CBR packet payload Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 21/25] perf intel-pt: Add decoder support for CBR events Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 22/25] perf intel-pt: Remove redundant initial_skip checks Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 23/25] perf intel-pt: Fix transactions_sample_type Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 24/25] perf tools: Fix message because cpu list option is -C not -c Arnaldo Carvalho de Melo
2017-06-21 18:02 ` [PATCH 25/25] perf script: Fix message because field list option is -F not -f Arnaldo Carvalho de Melo
2017-06-21 18:13 ` [GIT PULL 00/25] perf/core improvements and fixes Ingo Molnar

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=20170621180245.23134-3-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=dsahern@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=wangnan0@huawei.com \
    /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