Skip to content

Commit ef12a14

Browse files
acmelIngo Molnar
authored andcommitted
perf buildid-cache: Add new command to manage build-id cache
For now it just has operations to examine a given file, find its build-id and add or remove it to/from the cache. Useful, for instance, when adding binaries sent together with a perf.data file, so that we can add them to the cache and have the tools find it when resolving symbols. It'll also manage the size of the cache like 'ccache' does. Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Frédéric Weisbecker <fweisbec@gmail.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> LKML-Reference: <1264008525-29025-1-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent dc8d6ab commit ef12a14

10 files changed

Lines changed: 241 additions & 12 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
perf-buildid-cache(1)
2+
=====================
3+
4+
NAME
5+
----
6+
perf-buildid-cache - Manage build-id cache.
7+
8+
SYNOPSIS
9+
--------
10+
[verse]
11+
'perf buildid-list <options>'
12+
13+
DESCRIPTION
14+
-----------
15+
This command manages the build-id cache. It can add and remove files to the
16+
cache. In the future it should as well purge older entries, set upper limits
17+
for the space used by the cache, etc.
18+
19+
OPTIONS
20+
-------
21+
-a::
22+
--add=::
23+
Add specified file to the cache.
24+
-r::
25+
--remove=::
26+
Remove specified file to the cache.
27+
-v::
28+
--verbose::
29+
Be more verbose.
30+
31+
SEE ALSO
32+
--------
33+
linkperf:perf-record[1], linkperf:perf-report[1]

‎tools/perf/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ BUILTIN_OBJS += builtin-diff.o
445445
BUILTIN_OBJS += builtin-help.o
446446
BUILTIN_OBJS += builtin-sched.o
447447
BUILTIN_OBJS += builtin-buildid-list.o
448+
BUILTIN_OBJS += builtin-buildid-cache.o
448449
BUILTIN_OBJS += builtin-list.o
449450
BUILTIN_OBJS += builtin-record.o
450451
BUILTIN_OBJS += builtin-report.o

‎tools/perf/builtin-buildid-cache.c‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* builtin-buildid-cache.c
3+
*
4+
* Builtin buildid-cache command: Manages build-id cache
5+
*
6+
* Copyright (C) 2010, Red Hat Inc.
7+
* Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
8+
*/
9+
#include "builtin.h"
10+
#include "perf.h"
11+
#include "util/cache.h"
12+
#include "util/debug.h"
13+
#include "util/header.h"
14+
#include "util/parse-options.h"
15+
#include "util/strlist.h"
16+
#include "util/symbol.h"
17+
18+
static char const *add_name_list_str, *remove_name_list_str;
19+
20+
static const char * const buildid_cache_usage[] = {
21+
"perf buildid-cache [<options>]",
22+
NULL
23+
};
24+
25+
static const struct option buildid_cache_options[] = {
26+
OPT_STRING('a', "add", &add_name_list_str,
27+
"file list", "file(s) to add"),
28+
OPT_STRING('r', "remove", &remove_name_list_str, "file list",
29+
"file(s) to remove"),
30+
OPT_BOOLEAN('v', "verbose", &verbose, "be more verbose"),
31+
OPT_END()
32+
};
33+
34+
static int build_id_cache__add_file(const char *filename, const char *debugdir)
35+
{
36+
char sbuild_id[BUILD_ID_SIZE * 2 + 1];
37+
u8 build_id[BUILD_ID_SIZE];
38+
int err;
39+
40+
if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
41+
pr_debug("Couldn't read a build-id in %s\n", filename);
42+
return -1;
43+
}
44+
45+
build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
46+
err = build_id_cache__add_s(sbuild_id, debugdir, filename, false);
47+
if (verbose)
48+
pr_info("Adding %s %s: %s\n", sbuild_id, filename,
49+
err ? "FAIL" : "Ok");
50+
return err;
51+
}
52+
53+
static int build_id_cache__remove_file(const char *filename __used,
54+
const char *debugdir __used)
55+
{
56+
u8 build_id[BUILD_ID_SIZE];
57+
char sbuild_id[BUILD_ID_SIZE * 2 + 1];
58+
59+
int err;
60+
61+
if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
62+
pr_debug("Couldn't read a build-id in %s\n", filename);
63+
return -1;
64+
}
65+
66+
build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
67+
err = build_id_cache__remove_s(sbuild_id, debugdir);
68+
if (verbose)
69+
pr_info("Removing %s %s: %s\n", sbuild_id, filename,
70+
err ? "FAIL" : "Ok");
71+
72+
return err;
73+
}
74+
75+
static int __cmd_buildid_cache(void)
76+
{
77+
struct strlist *list;
78+
struct str_node *pos;
79+
char debugdir[PATH_MAX];
80+
81+
snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"),
82+
DEBUG_CACHE_DIR);
83+
84+
if (add_name_list_str) {
85+
list = strlist__new(true, add_name_list_str);
86+
if (list) {
87+
strlist__for_each(pos, list)
88+
if (build_id_cache__add_file(pos->s, debugdir)) {
89+
if (errno == EEXIST) {
90+
pr_debug("%s already in the cache\n",
91+
pos->s);
92+
continue;
93+
}
94+
pr_warning("Couldn't add %s: %s\n",
95+
pos->s, strerror(errno));
96+
}
97+
98+
strlist__delete(list);
99+
}
100+
}
101+
102+
if (remove_name_list_str) {
103+
list = strlist__new(true, remove_name_list_str);
104+
if (list) {
105+
strlist__for_each(pos, list)
106+
if (build_id_cache__remove_file(pos->s, debugdir)) {
107+
if (errno == ENOENT) {
108+
pr_debug("%s wasn't in the cache\n",
109+
pos->s);
110+
continue;
111+
}
112+
pr_warning("Couldn't remove %s: %s\n",
113+
pos->s, strerror(errno));
114+
}
115+
116+
strlist__delete(list);
117+
}
118+
}
119+
120+
return 0;
121+
}
122+
123+
int cmd_buildid_cache(int argc, const char **argv, const char *prefix __used)
124+
{
125+
argc = parse_options(argc, argv, buildid_cache_options,
126+
buildid_cache_usage, 0);
127+
128+
if (symbol__init() < 0)
129+
return -1;
130+
131+
setup_pager();
132+
return __cmd_buildid_cache();
133+
}

‎tools/perf/builtin.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern int check_pager_config(const char *cmd);
1616

1717
extern int cmd_annotate(int argc, const char **argv, const char *prefix);
1818
extern int cmd_bench(int argc, const char **argv, const char *prefix);
19+
extern int cmd_buildid_cache(int argc, const char **argv, const char *prefix);
1920
extern int cmd_buildid_list(int argc, const char **argv, const char *prefix);
2021
extern int cmd_diff(int argc, const char **argv, const char *prefix);
2122
extern int cmd_help(int argc, const char **argv, const char *prefix);

‎tools/perf/command-list.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
perf-annotate mainporcelain common
66
perf-archive mainporcelain common
77
perf-bench mainporcelain common
8+
perf-buildid-cache mainporcelain common
89
perf-buildid-list mainporcelain common
910
perf-diff mainporcelain common
1011
perf-list mainporcelain common

‎tools/perf/perf.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ static void handle_internal_command(int argc, const char **argv)
285285
{
286286
const char *cmd = argv[0];
287287
static struct cmd_struct commands[] = {
288+
{ "buildid-cache", cmd_buildid_cache, 0 },
288289
{ "buildid-list", cmd_buildid_list, 0 },
289290
{ "diff", cmd_diff, 0 },
290291
{ "help", cmd_help, 0 },

‎tools/perf/util/header.c‎

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,32 +231,29 @@ static int dsos__write_buildid_table(int fd)
231231
return err;
232232
}
233233

234-
static int dso__cache_build_id(struct dso *self, const char *debugdir)
234+
int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
235+
const char *name, bool is_kallsyms)
235236
{
236237
const size_t size = PATH_MAX;
237238
char *filename = malloc(size),
238-
*linkname = malloc(size), *targetname, *sbuild_id;
239+
*linkname = malloc(size), *targetname;
239240
int len, err = -1;
240-
bool is_kallsyms = self->kernel && self->long_name[0] != '/';
241241

242242
if (filename == NULL || linkname == NULL)
243243
goto out_free;
244244

245245
len = snprintf(filename, size, "%s%s%s",
246-
debugdir, is_kallsyms ? "/" : "", self->long_name);
246+
debugdir, is_kallsyms ? "/" : "", name);
247247
if (mkdir_p(filename, 0755))
248248
goto out_free;
249249

250-
len += snprintf(filename + len, sizeof(filename) - len, "/");
251-
sbuild_id = filename + len;
252-
build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
250+
snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
253251

254252
if (access(filename, F_OK)) {
255253
if (is_kallsyms) {
256254
if (copyfile("/proc/kallsyms", filename))
257255
goto out_free;
258-
} else if (link(self->long_name, filename) &&
259-
copyfile(self->long_name, filename))
256+
} else if (link(name, filename) && copyfile(name, filename))
260257
goto out_free;
261258
}
262259

@@ -278,6 +275,63 @@ static int dso__cache_build_id(struct dso *self, const char *debugdir)
278275
return err;
279276
}
280277

278+
static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
279+
const char *name, const char *debugdir,
280+
bool is_kallsyms)
281+
{
282+
char sbuild_id[BUILD_ID_SIZE * 2 + 1];
283+
284+
build_id__sprintf(build_id, build_id_size, sbuild_id);
285+
286+
return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
287+
}
288+
289+
int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
290+
{
291+
const size_t size = PATH_MAX;
292+
char *filename = malloc(size),
293+
*linkname = malloc(size);
294+
int err = -1;
295+
296+
if (filename == NULL || linkname == NULL)
297+
goto out_free;
298+
299+
snprintf(linkname, size, "%s/.build-id/%.2s/%s",
300+
debugdir, sbuild_id, sbuild_id + 2);
301+
302+
if (access(linkname, F_OK))
303+
goto out_free;
304+
305+
if (readlink(linkname, filename, size) < 0)
306+
goto out_free;
307+
308+
if (unlink(linkname))
309+
goto out_free;
310+
311+
/*
312+
* Since the link is relative, we must make it absolute:
313+
*/
314+
snprintf(linkname, size, "%s/.build-id/%.2s/%s",
315+
debugdir, sbuild_id, filename);
316+
317+
if (unlink(linkname))
318+
goto out_free;
319+
320+
err = 0;
321+
out_free:
322+
free(filename);
323+
free(linkname);
324+
return err;
325+
}
326+
327+
static int dso__cache_build_id(struct dso *self, const char *debugdir)
328+
{
329+
bool is_kallsyms = self->kernel && self->long_name[0] != '/';
330+
331+
return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
332+
self->long_name, debugdir, is_kallsyms);
333+
}
334+
281335
static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
282336
{
283337
struct dso *pos;

‎tools/perf/util/header.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <sys/types.h>
66
#include <stdbool.h>
77
#include "types.h"
8+
#include "event.h"
89

910
#include <linux/bitmap.h>
1011

@@ -84,4 +85,8 @@ int perf_header__process_sections(struct perf_header *self, int fd,
8485
struct perf_header *ph,
8586
int feat, int fd));
8687

88+
int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
89+
const char *name, bool is_kallsyms);
90+
int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir);
91+
8792
#endif /* __PERF_HEADER_H */

‎tools/perf/util/symbol.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,10 @@ void dso__sort_by_name(struct dso *self, enum map_type type)
345345
&self->symbols[type]);
346346
}
347347

348-
int build_id__sprintf(u8 *self, int len, char *bf)
348+
int build_id__sprintf(const u8 *self, int len, char *bf)
349349
{
350350
char *bid = bf;
351-
u8 *raw = self;
351+
const u8 *raw = self;
352352
int i;
353353

354354
for (i = 0; i < len; ++i) {

‎tools/perf/util/symbol.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
144144
int filename__read_build_id(const char *filename, void *bf, size_t size);
145145
int sysfs__read_build_id(const char *filename, void *bf, size_t size);
146146
bool dsos__read_build_ids(void);
147-
int build_id__sprintf(u8 *self, int len, char *bf);
147+
int build_id__sprintf(const u8 *self, int len, char *bf);
148148
int kallsyms__parse(const char *filename, void *arg,
149149
int (*process_symbol)(void *arg, const char *name,
150150
char type, u64 start));

0 commit comments

Comments
 (0)