Skip to content

Commit 606f972

Browse files
liu-song-6acmel
authored andcommitted
perf bpf: Save bpf_prog_info information as headers to perf.data
This patch enables perf-record to save bpf_prog_info information as headers to perf.data. A new header type HEADER_BPF_PROG_INFO is introduced for this data. Committer testing: As root, being on the kernel sources top level directory, run: # perf trace -e tools/perf/examples/bpf/augmented_raw_syscalls.c -e *msg Just to compile and load a BPF program that attaches to the raw_syscalls:sys_{enter,exit} tracepoints to trace the syscalls ending in "msg" (recvmsg, sendmsg, recvmmsg, sendmmsg, etc). Then do a systemwide perf record session for a few seconds: # perf record -a sleep 2s Then look at: # perf report --header-only | grep -i bpf # bpf_prog_info of id 13 # bpf_prog_info of id 14 # bpf_prog_info of id 15 # bpf_prog_info of id 16 # bpf_prog_info of id 17 # bpf_prog_info of id 18 # bpf_prog_info of id 21 # bpf_prog_info of id 22 # bpf_prog_info of id 208 # bpf_prog_info of id 209 # We need to show more info about these programs, like bpftool does for the ones running on the system, i.e. 'perf record/perf report' become a way of saving the BPF state in a machine to then analyse on another, together with all the other information that is already saved in the perf.data header: # perf report --header-only # ======== # captured on : Tue Mar 12 11:42:13 2019 # header version : 1 # data offset : 296 # data size : 16294184 # feat offset : 16294480 # hostname : quaco # os release : 5.0.0+ # perf version : 5.0.gd783c8 # arch : x86_64 # nrcpus online : 8 # nrcpus avail : 8 # cpudesc : Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz # cpuid : GenuineIntel,6,142,10 # total memory : 24555720 kB # cmdline : /home/acme/bin/perf (deleted) record -a # event : name = cycles:ppp, , id = { 3190123, 3190124, 3190125, 3190126, 3190127, 3190128, 3190129, 3190130 }, size = 112, { sample_period, sample_freq } = 4000, sample_type = IP|TID|TIME|CPU|PERIOD, read_format = ID, disabled = 1, inherit = 1, mmap = 1, comm = 1, freq = 1, task = 1, precise_ip = 3, sample_id_all = 1, exclude_guest = 1, mmap2 = 1, comm_exec = 1 # CPU_TOPOLOGY info available, use -I to display # NUMA_TOPOLOGY info available, use -I to display # pmu mappings: intel_pt = 8, software = 1, power = 11, uprobe = 7, uncore_imc = 12, cpu = 4, cstate_core = 18, uncore_cbox_2 = 15, breakpoint = 5, uncore_cbox_0 = 13, tracepoint = 2, cstate_pkg = 19, uncore_arb = 17, kprobe = 6, i915 = 10, msr = 9, uncore_cbox_3 = 16, uncore_cbox_1 = 14 # CACHE info available, use -I to display # time of first sample : 116392.441701 # time of last sample : 116400.932584 # sample duration : 8490.883 ms # MEM_TOPOLOGY info available, use -I to display # bpf_prog_info of id 13 # bpf_prog_info of id 14 # bpf_prog_info of id 15 # bpf_prog_info of id 16 # bpf_prog_info of id 17 # bpf_prog_info of id 18 # bpf_prog_info of id 21 # bpf_prog_info of id 22 # bpf_prog_info of id 208 # bpf_prog_info of id 209 # missing features: TRACING_DATA BRANCH_STACK GROUP_DESC AUXTRACE STAT CLOCKID DIR_FORMAT # ======== # Committer notes: We can't use the libbpf unconditionally, as the build may have been with NO_LIBBPF, when we end up with linking errors, so provide dummy {process,write}_bpf_prog_info() wrapped by HAVE_LIBBPF_SUPPORT for that case. Printing are not affected by this, so can continue as is. Signed-off-by: Song Liu <songliubraving@fb.com> Reviewed-by: Jiri Olsa <jolsa@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stanislav Fomichev <sdf@google.com> Cc: kernel-team@fb.com Link: http://lkml.kernel.org/r/20190312053051.2690567-8-songliubraving@fb.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent e4378f0 commit 606f972

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

tools/perf/util/header.c

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <sys/utsname.h>
1919
#include <linux/time64.h>
2020
#include <dirent.h>
21+
#include <bpf/libbpf.h>
2122

2223
#include "evlist.h"
2324
#include "evsel.h"
@@ -40,6 +41,7 @@
4041
#include "time-utils.h"
4142
#include "units.h"
4243
#include "cputopo.h"
44+
#include "bpf-event.h"
4345

4446
#include "sane_ctype.h"
4547

@@ -876,6 +878,56 @@ static int write_dir_format(struct feat_fd *ff,
876878
return do_write(ff, &data->dir.version, sizeof(data->dir.version));
877879
}
878880

881+
#ifdef HAVE_LIBBPF_SUPPORT
882+
static int write_bpf_prog_info(struct feat_fd *ff,
883+
struct perf_evlist *evlist __maybe_unused)
884+
{
885+
struct perf_env *env = &ff->ph->env;
886+
struct rb_root *root;
887+
struct rb_node *next;
888+
int ret;
889+
890+
down_read(&env->bpf_progs.lock);
891+
892+
ret = do_write(ff, &env->bpf_progs.infos_cnt,
893+
sizeof(env->bpf_progs.infos_cnt));
894+
if (ret < 0)
895+
goto out;
896+
897+
root = &env->bpf_progs.infos;
898+
next = rb_first(root);
899+
while (next) {
900+
struct bpf_prog_info_node *node;
901+
size_t len;
902+
903+
node = rb_entry(next, struct bpf_prog_info_node, rb_node);
904+
next = rb_next(&node->rb_node);
905+
len = sizeof(struct bpf_prog_info_linear) +
906+
node->info_linear->data_len;
907+
908+
/* before writing to file, translate address to offset */
909+
bpf_program__bpil_addr_to_offs(node->info_linear);
910+
ret = do_write(ff, node->info_linear, len);
911+
/*
912+
* translate back to address even when do_write() fails,
913+
* so that this function never changes the data.
914+
*/
915+
bpf_program__bpil_offs_to_addr(node->info_linear);
916+
if (ret < 0)
917+
goto out;
918+
}
919+
out:
920+
up_read(&env->bpf_progs.lock);
921+
return ret;
922+
}
923+
#else // HAVE_LIBBPF_SUPPORT
924+
static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused,
925+
struct perf_evlist *evlist __maybe_unused)
926+
{
927+
return 0;
928+
}
929+
#endif // HAVE_LIBBPF_SUPPORT
930+
879931
static int cpu_cache_level__sort(const void *a, const void *b)
880932
{
881933
struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
@@ -1367,6 +1419,29 @@ static void print_dir_format(struct feat_fd *ff, FILE *fp)
13671419
fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
13681420
}
13691421

1422+
static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1423+
{
1424+
struct perf_env *env = &ff->ph->env;
1425+
struct rb_root *root;
1426+
struct rb_node *next;
1427+
1428+
down_read(&env->bpf_progs.lock);
1429+
1430+
root = &env->bpf_progs.infos;
1431+
next = rb_first(root);
1432+
1433+
while (next) {
1434+
struct bpf_prog_info_node *node;
1435+
1436+
node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1437+
next = rb_next(&node->rb_node);
1438+
fprintf(fp, "# bpf_prog_info of id %u\n",
1439+
node->info_linear->info.id);
1440+
}
1441+
1442+
up_read(&env->bpf_progs.lock);
1443+
}
1444+
13701445
static void free_event_desc(struct perf_evsel *events)
13711446
{
13721447
struct perf_evsel *evsel;
@@ -2414,6 +2489,81 @@ static int process_dir_format(struct feat_fd *ff,
24142489
return do_read_u64(ff, &data->dir.version);
24152490
}
24162491

2492+
#ifdef HAVE_LIBBPF_SUPPORT
2493+
static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
2494+
{
2495+
struct bpf_prog_info_linear *info_linear;
2496+
struct bpf_prog_info_node *info_node;
2497+
struct perf_env *env = &ff->ph->env;
2498+
u32 count, i;
2499+
int err = -1;
2500+
2501+
if (ff->ph->needs_swap) {
2502+
pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2503+
return 0;
2504+
}
2505+
2506+
if (do_read_u32(ff, &count))
2507+
return -1;
2508+
2509+
down_write(&env->bpf_progs.lock);
2510+
2511+
for (i = 0; i < count; ++i) {
2512+
u32 info_len, data_len;
2513+
2514+
info_linear = NULL;
2515+
info_node = NULL;
2516+
if (do_read_u32(ff, &info_len))
2517+
goto out;
2518+
if (do_read_u32(ff, &data_len))
2519+
goto out;
2520+
2521+
if (info_len > sizeof(struct bpf_prog_info)) {
2522+
pr_warning("detected invalid bpf_prog_info\n");
2523+
goto out;
2524+
}
2525+
2526+
info_linear = malloc(sizeof(struct bpf_prog_info_linear) +
2527+
data_len);
2528+
if (!info_linear)
2529+
goto out;
2530+
info_linear->info_len = sizeof(struct bpf_prog_info);
2531+
info_linear->data_len = data_len;
2532+
if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
2533+
goto out;
2534+
if (__do_read(ff, &info_linear->info, info_len))
2535+
goto out;
2536+
if (info_len < sizeof(struct bpf_prog_info))
2537+
memset(((void *)(&info_linear->info)) + info_len, 0,
2538+
sizeof(struct bpf_prog_info) - info_len);
2539+
2540+
if (__do_read(ff, info_linear->data, data_len))
2541+
goto out;
2542+
2543+
info_node = malloc(sizeof(struct bpf_prog_info_node));
2544+
if (!info_node)
2545+
goto out;
2546+
2547+
/* after reading from file, translate offset to address */
2548+
bpf_program__bpil_offs_to_addr(info_linear);
2549+
info_node->info_linear = info_linear;
2550+
perf_env__insert_bpf_prog_info(env, info_node);
2551+
}
2552+
2553+
return 0;
2554+
out:
2555+
free(info_linear);
2556+
free(info_node);
2557+
up_write(&env->bpf_progs.lock);
2558+
return err;
2559+
}
2560+
#else // HAVE_LIBBPF_SUPPORT
2561+
static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused)
2562+
{
2563+
return 0;
2564+
}
2565+
#endif // HAVE_LIBBPF_SUPPORT
2566+
24172567
struct feature_ops {
24182568
int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
24192569
void (*print)(struct feat_fd *ff, FILE *fp);
@@ -2474,7 +2624,8 @@ static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
24742624
FEAT_OPR(SAMPLE_TIME, sample_time, false),
24752625
FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
24762626
FEAT_OPR(CLOCKID, clockid, false),
2477-
FEAT_OPN(DIR_FORMAT, dir_format, false)
2627+
FEAT_OPN(DIR_FORMAT, dir_format, false),
2628+
FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false)
24782629
};
24792630

24802631
struct header_print_data {

tools/perf/util/header.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum {
4040
HEADER_MEM_TOPOLOGY,
4141
HEADER_CLOCKID,
4242
HEADER_DIR_FORMAT,
43+
HEADER_BPF_PROG_INFO,
4344
HEADER_LAST_FEATURE,
4445
HEADER_FEAT_BITS = 256,
4546
};

0 commit comments

Comments
 (0)