Skip to content

Commit d966458

Browse files
Andi Kleenacmel
authored andcommitted
perf pmu: Use file system cache to optimize sysfs access
pmu.c does a lot of redundant /sys accesses while parsing aliases and probing for PMUs. On large systems with a lot of PMUs this can get expensive (>2s): % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 27.25 1.227847 8 160888 16976 openat 26.42 1.190481 7 164224 164077 stat Add a cache to remember if specific file names exist or don't exist, which eliminates most of this overhead. Also optimize some stat() calls to be slightly cheaper access() Resulting in: 0.18 0.004166 2 1851 305 open 0.08 0.001970 2 829 622 access Signed-off-by: Andi Kleen <ak@linux.intel.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: http://lore.kernel.org/lkml/20191121001522.180827-2-andi@firstfloor.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 5b596e0 commit d966458

5 files changed

Lines changed: 83 additions & 31 deletions

File tree

‎tools/perf/util/Build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ perf-y += header.o
4949
perf-y += callchain.o
5050
perf-y += values.o
5151
perf-y += debug.o
52+
perf-y += fncache.o
5253
perf-y += machine.o
5354
perf-y += map.o
5455
perf-y += pstack.o

‎tools/perf/util/fncache.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Manage a cache of file names' existence */
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <string.h>
6+
#include <linux/list.h>
7+
#include "fncache.h"
8+
9+
struct fncache {
10+
struct hlist_node nd;
11+
bool res;
12+
char name[];
13+
};
14+
15+
#define FNHSIZE 61
16+
17+
static struct hlist_head fncache_hash[FNHSIZE];
18+
19+
unsigned shash(const unsigned char *s)
20+
{
21+
unsigned h = 0;
22+
while (*s)
23+
h = 65599 * h + *s++;
24+
return h ^ (h >> 16);
25+
}
26+
27+
static bool lookup_fncache(const char *name, bool *res)
28+
{
29+
int h = shash((const unsigned char *)name) % FNHSIZE;
30+
struct fncache *n;
31+
32+
hlist_for_each_entry(n, &fncache_hash[h], nd) {
33+
if (!strcmp(n->name, name)) {
34+
*res = n->res;
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
41+
static void update_fncache(const char *name, bool res)
42+
{
43+
struct fncache *n = malloc(sizeof(struct fncache) + strlen(name) + 1);
44+
int h = shash((const unsigned char *)name) % FNHSIZE;
45+
46+
if (!n)
47+
return;
48+
strcpy(n->name, name);
49+
n->res = res;
50+
hlist_add_head(&n->nd, &fncache_hash[h]);
51+
}
52+
53+
/* No LRU, only use when bounded in some other way. */
54+
bool file_available(const char *name)
55+
{
56+
bool res;
57+
58+
if (lookup_fncache(name, &res))
59+
return res;
60+
res = access(name, R_OK) == 0;
61+
update_fncache(name, res);
62+
return res;
63+
}

‎tools/perf/util/fncache.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _FCACHE_H
2+
#define _FCACHE_H 1
3+
4+
unsigned shash(const unsigned char *s);
5+
bool file_available(const char *name);
6+
7+
#endif

‎tools/perf/util/pmu.c‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "pmu-events/pmu-events.h"
2525
#include "string2.h"
2626
#include "strbuf.h"
27+
#include "fncache.h"
2728

2829
struct perf_pmu_format {
2930
char *name;
@@ -82,7 +83,6 @@ int perf_pmu__format_parse(char *dir, struct list_head *head)
8283
*/
8384
static int pmu_format(const char *name, struct list_head *format)
8485
{
85-
struct stat st;
8686
char path[PATH_MAX];
8787
const char *sysfs = sysfs__mountpoint();
8888

@@ -92,8 +92,8 @@ static int pmu_format(const char *name, struct list_head *format)
9292
snprintf(path, PATH_MAX,
9393
"%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
9494

95-
if (stat(path, &st) < 0)
96-
return 0; /* no error if format does not exist */
95+
if (!file_available(path))
96+
return 0;
9797

9898
if (perf_pmu__format_parse(path, format))
9999
return -1;
@@ -475,7 +475,6 @@ static int pmu_aliases_parse(char *dir, struct list_head *head)
475475
*/
476476
static int pmu_aliases(const char *name, struct list_head *head)
477477
{
478-
struct stat st;
479478
char path[PATH_MAX];
480479
const char *sysfs = sysfs__mountpoint();
481480

@@ -485,8 +484,8 @@ static int pmu_aliases(const char *name, struct list_head *head)
485484
snprintf(path, PATH_MAX,
486485
"%s/bus/event_source/devices/%s/events", sysfs, name);
487486

488-
if (stat(path, &st) < 0)
489-
return 0; /* no error if 'events' does not exist */
487+
if (!file_available(path))
488+
return 0;
490489

491490
if (pmu_aliases_parse(path, head))
492491
return -1;
@@ -525,7 +524,6 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias,
525524
*/
526525
static int pmu_type(const char *name, __u32 *type)
527526
{
528-
struct stat st;
529527
char path[PATH_MAX];
530528
FILE *file;
531529
int ret = 0;
@@ -537,7 +535,7 @@ static int pmu_type(const char *name, __u32 *type)
537535
snprintf(path, PATH_MAX,
538536
"%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
539537

540-
if (stat(path, &st) < 0)
538+
if (access(path, R_OK) < 0)
541539
return -1;
542540

543541
file = fopen(path, "r");
@@ -628,14 +626,11 @@ static struct perf_cpu_map *pmu_cpumask(const char *name)
628626
static bool pmu_is_uncore(const char *name)
629627
{
630628
char path[PATH_MAX];
631-
struct perf_cpu_map *cpus;
632-
const char *sysfs = sysfs__mountpoint();
629+
const char *sysfs;
633630

631+
sysfs = sysfs__mountpoint();
634632
snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
635-
cpus = __pmu_cpumask(path);
636-
perf_cpu_map__put(cpus);
637-
638-
return !!cpus;
633+
return file_available(path);
639634
}
640635

641636
/*
@@ -645,7 +640,6 @@ static bool pmu_is_uncore(const char *name)
645640
*/
646641
static int is_arm_pmu_core(const char *name)
647642
{
648-
struct stat st;
649643
char path[PATH_MAX];
650644
const char *sysfs = sysfs__mountpoint();
651645

@@ -655,10 +649,7 @@ static int is_arm_pmu_core(const char *name)
655649
/* Look for cpu sysfs (specific to arm) */
656650
scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
657651
sysfs, name);
658-
if (stat(path, &st) == 0)
659-
return 1;
660-
661-
return 0;
652+
return file_available(path);
662653
}
663654

664655
static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
@@ -1544,7 +1535,6 @@ bool pmu_have_event(const char *pname, const char *name)
15441535

15451536
static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
15461537
{
1547-
struct stat st;
15481538
char path[PATH_MAX];
15491539
const char *sysfs;
15501540

@@ -1554,10 +1544,8 @@ static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
15541544

15551545
snprintf(path, PATH_MAX,
15561546
"%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1557-
1558-
if (stat(path, &st) < 0)
1547+
if (!file_available(path))
15591548
return NULL;
1560-
15611549
return fopen(path, "r");
15621550
}
15631551

‎tools/perf/util/srccode.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "srccode.h"
1717
#include "debug.h"
1818
#include <internal/lib.h> // page_size
19+
#include "fncache.h"
1920

2021
#define MAXSRCCACHE (32*1024*1024)
2122
#define MAXSRCFILES 64
@@ -36,14 +37,6 @@ static LIST_HEAD(srcfile_list);
3637
static long map_total_sz;
3738
static int num_srcfiles;
3839

39-
static unsigned shash(unsigned char *s)
40-
{
41-
unsigned h = 0;
42-
while (*s)
43-
h = 65599 * h + *s++;
44-
return h ^ (h >> 16);
45-
}
46-
4740
static int countlines(char *map, int maplen)
4841
{
4942
int numl;

0 commit comments

Comments
 (0)