All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: Clark Williams <williams@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@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 10/18] perf tools: Read the cache line size lazily
Date: Sat, 19 May 2018 07:54:59 -0300	[thread overview]
Message-ID: <20180519105507.16450-11-acme@kernel.org> (raw)
In-Reply-To: <20180519105507.16450-1-acme@kernel.org>

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

It is not read as commonly as 'page_size', so it makes sense to read it
lazily, caching its value when it is first read.

Less files open unconditionally at startup.

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: https://lkml.kernel.org/n/tip-35xhrq91u94uc1djtclek1ie@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/perf.c      | 11 -----------
 tools/perf/util/sort.c |  4 ++--
 tools/perf/util/sort.h |  4 ++--
 tools/perf/util/util.c | 21 ++++++++++++++++++++-
 tools/perf/util/util.h |  2 +-
 5 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index d5a0878de816..cefd8f74630c 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -421,16 +421,6 @@ void pthread__unblock_sigwinch(void)
 	pthread_sigmask(SIG_UNBLOCK, &set, NULL);
 }
 
-#ifdef _SC_LEVEL1_DCACHE_LINESIZE
-#define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
-#else
-static void cache_line_size(int *cacheline_sizep)
-{
-	if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
-		pr_debug("cannot determine cache line size");
-}
-#endif
-
 int main(int argc, const char **argv)
 {
 	int err;
@@ -444,7 +434,6 @@ int main(int argc, const char **argv)
 
 	/* The page_size is placed in util object. */
 	page_size = sysconf(_SC_PAGE_SIZE);
-	cache_line_size(&cacheline_size);
 
 	if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
 		sysctl_perf_event_max_stack = value;
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index e65903a695a6..4058ade352a5 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -2582,7 +2582,7 @@ int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
 		if (sort__mode != SORT_MODE__MEMORY)
 			return -EINVAL;
 
-		if (sd->entry == &sort_mem_dcacheline && cacheline_size == 0)
+		if (sd->entry == &sort_mem_dcacheline && cacheline_size() == 0)
 			return -EINVAL;
 
 		if (sd->entry == &sort_mem_daddr_sym)
@@ -2628,7 +2628,7 @@ static int setup_sort_list(struct perf_hpp_list *list, char *str,
 		if (*tok) {
 			ret = sort_dimension__add(list, tok, evlist, level);
 			if (ret == -EINVAL) {
-				if (!cacheline_size && !strncasecmp(tok, "dcacheline", strlen(tok)))
+				if (!cacheline_size() && !strncasecmp(tok, "dcacheline", strlen(tok)))
 					pr_err("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
 				else
 					pr_err("Invalid --sort key: `%s'", tok);
diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h
index 035b62e2c60b..9e6896293bbd 100644
--- a/tools/perf/util/sort.h
+++ b/tools/perf/util/sort.h
@@ -186,13 +186,13 @@ static inline float hist_entry__get_percent_limit(struct hist_entry *he)
 static inline u64 cl_address(u64 address)
 {
 	/* return the cacheline of the address */
-	return (address & ~(cacheline_size - 1));
+	return (address & ~(cacheline_size() - 1));
 }
 
 static inline u64 cl_offset(u64 address)
 {
 	/* return the cacheline of the address */
-	return (address & (cacheline_size - 1));
+	return (address & (cacheline_size() - 1));
 }
 
 enum sort_mode {
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 1019bbc5dbd8..99ab52165680 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -38,7 +38,26 @@ void perf_set_multithreaded(void)
 }
 
 unsigned int page_size;
-int cacheline_size;
+
+#ifdef _SC_LEVEL1_DCACHE_LINESIZE
+#define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
+#else
+static void cache_line_size(int *cacheline_sizep)
+{
+	if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
+		pr_debug("cannot determine cache line size");
+}
+#endif
+
+int cacheline_size(void)
+{
+	static int size;
+
+	if (!size)
+		cache_line_size(&size);
+
+	return size;
+}
 
 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index c9626c206208..74d21dfe0d29 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -43,7 +43,7 @@ size_t hex_width(u64 v);
 int hex2u64(const char *ptr, u64 *val);
 
 extern unsigned int page_size;
-extern int cacheline_size;
+int __pure cacheline_size(void);
 
 int fetch_kernel_version(unsigned int *puint,
 			 char *str, size_t str_sz);
-- 
2.14.3

  parent reply	other threads:[~2018-05-19 10:58 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-19 10:54 [GIT PULL 00/18] perf/core improvements and fixes Arnaldo Carvalho de Melo
2018-05-19 10:54 ` Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 01/18] perf config: Call perf_config__init() lazily Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 02/18] tools lib api: The tracing_mnt variable doesn't need to be global Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 03/18] tools lib api: Unexport 'tracing_path' variable Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 04/18] tools lib api fs tracing_path: Introduce get/put_events_file() helpers Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 05/18] perf tools: Reuse the path to the tracepoint /events/ directory Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 06/18] perf parse-events: Use get/put_events_file() Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 07/18] tools lib api fs tracing_path: Introduce opendir() method Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 08/18] tools lib api fs tracing_path: Make tracing_events_path private Arnaldo Carvalho de Melo
2018-05-19 10:54 ` [PATCH 09/18] tools include compiler-gcc: Add __pure attribute helper Arnaldo Carvalho de Melo
2018-05-19 10:54 ` Arnaldo Carvalho de Melo [this message]
2018-05-19 10:55 ` [PATCH 11/18] perf tools: No need to unconditionally read the max_stack sysctls Arnaldo Carvalho de Melo
2018-05-19 10:55 ` [PATCH 12/18] perf script: Show virtual addresses instead of offsets Arnaldo Carvalho de Melo
2018-05-19 10:55 ` [PATCH 13/18] perf script: Show symbol offsets by default Arnaldo Carvalho de Melo
2018-05-19 10:55 ` [PATCH 14/18] perf annotate: Record the min/max cycles Arnaldo Carvalho de Melo
2018-05-19 10:55 ` [PATCH 15/18] perf annotate: Create hotkey 'c' to show " Arnaldo Carvalho de Melo
2018-05-19 10:55 ` [PATCH 16/18] perf bpf: Fixup include and examples install messages Arnaldo Carvalho de Melo
2018-05-19 10:55 ` [PATCH 17/18] perf machine: Add machine__is() to identify machine arch Arnaldo Carvalho de Melo
2018-05-19 10:55 ` [PATCH 18/18] perf tools: Fix kernel_start for PTI on x86 Arnaldo Carvalho de Melo
2018-05-19 11:33 ` [GIT PULL 00/18] perf/core improvements and fixes Ingo Molnar
2018-05-19 11:33   ` 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=20180519105507.16450-11-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=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=wangnan0@huawei.com \
    --cc=williams@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.