Skip to content

Commit 9ac94e3

Browse files
committed
perf tools: Read the cache line size lazily
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 <[email protected]> Cc: David Ahern <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: https://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 6e1690c commit 9ac94e3

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

tools/perf/perf.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,6 @@ void pthread__unblock_sigwinch(void)
421421
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
422422
}
423423

424-
#ifdef _SC_LEVEL1_DCACHE_LINESIZE
425-
#define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
426-
#else
427-
static void cache_line_size(int *cacheline_sizep)
428-
{
429-
if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
430-
pr_debug("cannot determine cache line size");
431-
}
432-
#endif
433-
434424
int main(int argc, const char **argv)
435425
{
436426
int err;
@@ -444,7 +434,6 @@ int main(int argc, const char **argv)
444434

445435
/* The page_size is placed in util object. */
446436
page_size = sysconf(_SC_PAGE_SIZE);
447-
cache_line_size(&cacheline_size);
448437

449438
if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
450439
sysctl_perf_event_max_stack = value;

tools/perf/util/sort.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,7 +2582,7 @@ int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
25822582
if (sort__mode != SORT_MODE__MEMORY)
25832583
return -EINVAL;
25842584

2585-
if (sd->entry == &sort_mem_dcacheline && cacheline_size == 0)
2585+
if (sd->entry == &sort_mem_dcacheline && cacheline_size() == 0)
25862586
return -EINVAL;
25872587

25882588
if (sd->entry == &sort_mem_daddr_sym)
@@ -2628,7 +2628,7 @@ static int setup_sort_list(struct perf_hpp_list *list, char *str,
26282628
if (*tok) {
26292629
ret = sort_dimension__add(list, tok, evlist, level);
26302630
if (ret == -EINVAL) {
2631-
if (!cacheline_size && !strncasecmp(tok, "dcacheline", strlen(tok)))
2631+
if (!cacheline_size() && !strncasecmp(tok, "dcacheline", strlen(tok)))
26322632
pr_err("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
26332633
else
26342634
pr_err("Invalid --sort key: `%s'", tok);

tools/perf/util/sort.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ static inline float hist_entry__get_percent_limit(struct hist_entry *he)
186186
static inline u64 cl_address(u64 address)
187187
{
188188
/* return the cacheline of the address */
189-
return (address & ~(cacheline_size - 1));
189+
return (address & ~(cacheline_size() - 1));
190190
}
191191

192192
static inline u64 cl_offset(u64 address)
193193
{
194194
/* return the cacheline of the address */
195-
return (address & (cacheline_size - 1));
195+
return (address & (cacheline_size() - 1));
196196
}
197197

198198
enum sort_mode {

tools/perf/util/util.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,26 @@ void perf_set_multithreaded(void)
3838
}
3939

4040
unsigned int page_size;
41-
int cacheline_size;
41+
42+
#ifdef _SC_LEVEL1_DCACHE_LINESIZE
43+
#define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
44+
#else
45+
static void cache_line_size(int *cacheline_sizep)
46+
{
47+
if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
48+
pr_debug("cannot determine cache line size");
49+
}
50+
#endif
51+
52+
int cacheline_size(void)
53+
{
54+
static int size;
55+
56+
if (!size)
57+
cache_line_size(&size);
58+
59+
return size;
60+
}
4261

4362
int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
4463
int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;

tools/perf/util/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ size_t hex_width(u64 v);
4343
int hex2u64(const char *ptr, u64 *val);
4444

4545
extern unsigned int page_size;
46-
extern int cacheline_size;
46+
int __pure cacheline_size(void);
4747

4848
int fetch_kernel_version(unsigned int *puint,
4949
char *str, size_t str_sz);

0 commit comments

Comments
 (0)