Skip to content

Commit

Permalink
perf tools: Read the cache line size lazily
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
acmel committed May 17, 2018
1 parent 6e1690c commit 9ac94e3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
11 changes: 0 additions & 11 deletions tools/perf/perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/util/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/util/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 20 additions & 1 deletion tools/perf/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9ac94e3

Please sign in to comment.