Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp221282imu; Thu, 20 Dec 2018 20:45:53 -0800 (PST) X-Google-Smtp-Source: ALg8bN43cx27JDYkWBO4W1BjBaoDN5w5HBZfIOOJi9oHACq/OqAwSS0LRwGzJGDw8b9g8MamV4F6 X-Received: by 2002:a17:902:d68c:: with SMTP id v12mr1053560ply.4.1545367553818; Thu, 20 Dec 2018 20:45:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545367553; cv=none; d=google.com; s=arc-20160816; b=p4MHuGtMEfb3fIDKizxBURzUdW8AyLdxoqEb0pQeKtQGc4xMMmSG/OJNVV+y8HbwvZ Ohki3NFHzEJAZGyZNX3xEUOIqnK6pgb9tIQHQTlzvcAQCAaksxt8omjHH6E/QJHKlPLf QQieHweMpD8EIDs3SdeMeuMPk9KXzc/ogDgNd1Dz16Oa9XCatcYWGVyYbuCiZPyENwpJ 3BXp4XINC+MFzmCHTbtW8fa2kJXG2af+A+b/P+0gHEC8JG+hhibOh4A6HLCoEHeCMOO2 QnogDrxB1ATLZ8IwgsnSZ6IrCKiNKsmBO6bFiALnHNJCOZ8o94wO3Jwjif7A3US2QVoz EjIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:smtp-origin-cluster:cc:to :smtp-origin-hostname:from:smtp-origin-hostprefix:dkim-signature; bh=TOPwOVNfLVT+RHMwYzAnx3oQRuvZkf30d8y9gJuyX4c=; b=JYEUrmDUHucghW8gQR7hYAv16a6XH+xU99L6AOLMFpO4/URY8EGQ1mj2LNNhxzTCbG SR47s9yX4+9j8KWg+AaptoOny3QupVFRw33m3WD0YNyPBlEQcEFE2cUs8f7c2knL9UKM BVyw0yiPZO1WBdMtnlz7O8EyPA+2K4ip5G+jFBPzapTswmx97QLRxvl1M9dbrEtm/Q4J clKfzS19f6Hya716TlkwV/ccjnEEQlX2MPf1uZtBtFTx35mbjzFfNZkhS/0YtFGkomUZ E/LCyXXg/UHBA3Qm45jmSP9xq5/JWouZsTbX541mq+J7cRD68kj1hK6fc3V5/M5026df oSJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=UKgLXHzy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g6si20132985pgn.57.2018.12.20.20.45.23; Thu, 20 Dec 2018 20:45:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=UKgLXHzy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389106AbeLTS3W (ORCPT + 99 others); Thu, 20 Dec 2018 13:29:22 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:45482 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389065AbeLTS3U (ORCPT ); Thu, 20 Dec 2018 13:29:20 -0500 Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id wBKISC24023653 for ; Thu, 20 Dec 2018 10:29:19 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=TOPwOVNfLVT+RHMwYzAnx3oQRuvZkf30d8y9gJuyX4c=; b=UKgLXHzyxQ31D+BjL9mHpzAKPxBNeJ/9AFlq4ZwwBRDmWdXDXR6q/873db+0ONVNSpf+ CBX92VOLpgVuihpn393T6JHJ8K7xdKOKcai80zvcSjkzGVA/oPt4ZTvStrngrUJBH/yV /XEyZQ4Ao7pWHDRCKz8uJ7u9hIIDzizL0rM= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 2pgexwgd04-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 20 Dec 2018 10:29:19 -0800 Received: from mx-out.facebook.com (2620:10d:c0a1:3::13) by mail.thefacebook.com (2620:10d:c021:18::175) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1531.3; Thu, 20 Dec 2018 10:29:08 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id BB51A62E139A; Thu, 20 Dec 2018 10:29:07 -0800 (PST) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , CC: Song Liu , , , , , Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH v5 perf, bpf-next 7/7] perf tools: synthesize PERF_RECORD_* for loaded BPF programs Date: Thu, 20 Dec 2018 10:29:04 -0800 Message-ID: <20181220182904.4193196-8-songliubraving@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181220182904.4193196-1-songliubraving@fb.com> References: <20181220182904.4193196-1-songliubraving@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-12-20_09:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for BPF programs loaded before perf-record. This is achieved by gathering information about all BPF programs via sys_bpf. Signed-off-by: Song Liu --- tools/perf/builtin-record.c | 6 ++ tools/perf/util/bpf-event.c | 204 ++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-event.h | 5 + 3 files changed, 215 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 8983ab2e60dd..d239e8bf2f0f 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -41,6 +41,7 @@ #include "util/perf-hooks.h" #include "util/time-utils.h" #include "util/units.h" +#include "util/bpf-event.h" #include "asm/bug.h" #include @@ -855,6 +856,11 @@ static int record__synthesize(struct record *rec, bool tail) return err; } + err = perf_event__synthesize_bpf_events(tool, process_synthesized_event, + machine, opts); + if (err < 0) + pr_warning("Couldn't synthesize bpf events.\n"); + err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, process_synthesized_event, opts->sample_address, opts->proc_map_timeout, 1); diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index f24f75506f51..98208b577533 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -1,10 +1,24 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include +#include #include "bpf-event.h" #include "debug.h" #include "symbol.h" +#define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr)) + +static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len) +{ + int ret = 0; + size_t i; + + for (i = 0; i < len; i++) + ret += snprintf(buf + ret, size - ret, "%02x", data[i]); + return ret; +} + int machine__process_bpf_event(struct machine *machine __maybe_unused, union perf_event *event, struct perf_sample *sample __maybe_unused) @@ -13,3 +27,193 @@ int machine__process_bpf_event(struct machine *machine __maybe_unused, perf_event__fprintf_bpf_event(event, stderr); return 0; } + +/* + * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf + * program. One PERF_RECORD_BPF_EVENT is generated for the program. And + * one PERF_RECORD_KSYMBOL is generated for each sub program. + */ +static int perf_event__synthesize_one_bpf_prog(struct perf_tool *tool, + perf_event__handler_t process, + struct machine *machine, + int fd, + union perf_event *event, + struct record_opts *opts) +{ + struct ksymbol_event *ksymbol_event = &event->ksymbol_event; + struct bpf_event *bpf_event = &event->bpf_event; + u32 sub_prog_cnt, i, func_info_rec_size; + u8 (*prog_tags)[BPF_TAG_SIZE] = NULL; + struct bpf_prog_info info = {}; + u32 info_len = sizeof(info); + void *func_infos = NULL; + u64 *prog_addrs = NULL; + struct btf *btf = NULL; + u32 *prog_lens = NULL; + bool has_btf = false; + int err = 0; + + /* Call bpf_obj_get_info_by_fd() to get sizes of arrays */ + err = bpf_obj_get_info_by_fd(fd, &info, &info_len); + + if (err || info_len < 192 /* need field prog_tags */) + return -1; + + /* number of ksyms, func_lengths, and tags should match */ + sub_prog_cnt = info.nr_jited_ksyms; + if (sub_prog_cnt != info.nr_prog_tags || + sub_prog_cnt != info.nr_jited_func_lens) + return -1; + + /* check BTF func info support */ + if (info.btf_id && info.nr_func_info && info.func_info_rec_size) { + /* btf func info number should be same as sub_prog_cnt */ + if (sub_prog_cnt != info.nr_func_info) + return -1; + if (btf__get_from_id(info.btf_id, &btf)) + return -1; + func_info_rec_size = info.func_info_rec_size; + func_infos = malloc(sub_prog_cnt * func_info_rec_size); + if (!func_infos) + return -1; + has_btf = true; + } + + /* + * We need address, length, and tag for each sub program. + * Allocate memory and call bpf_obj_get_info_by_fd() again + */ + prog_addrs = (u64 *)malloc(sizeof(u64) * sub_prog_cnt); + prog_lens = (u32 *)malloc(sizeof(u64) * sub_prog_cnt); + prog_tags = malloc(sizeof(u8) * BPF_TAG_SIZE * sub_prog_cnt); + + err = !prog_addrs || !prog_lens || !prog_tags; + if (err) + goto out; + + memset(&info, 0, sizeof(info)); + info.nr_jited_ksyms = sub_prog_cnt; + info.nr_jited_func_lens = sub_prog_cnt; + info.nr_prog_tags = sub_prog_cnt; + info.jited_ksyms = ptr_to_u64(prog_addrs); + info.jited_func_lens = ptr_to_u64(prog_lens); + info.prog_tags = ptr_to_u64(prog_tags); + info_len = sizeof(info); + if (has_btf) { + info.nr_func_info = sub_prog_cnt; + info.func_info_rec_size = func_info_rec_size; + info.func_info = ptr_to_u64(func_infos); + } + + err = bpf_obj_get_info_by_fd(fd, &info, &info_len); + if (err) + goto out; + + /* Synthesize PERF_RECORD_KSYMBOL */ + for (i = 0; i < sub_prog_cnt; i++) { + const struct bpf_func_info *finfo; + const char *short_name = NULL; + const struct btf_type *t; + int name_len; + + *ksymbol_event = (struct ksymbol_event){ + .header = { + .type = PERF_RECORD_KSYMBOL, + .misc = PERF_RECORD_MISC_KSYMBOL_TYPE_BPF, + .size = sizeof(struct ksymbol_event), + }, + .addr = prog_addrs[i], + .len = prog_lens[i], + }; + name_len = snprintf(ksymbol_event->name, KSYM_NAME_LEN, + "bpf_prog_"); + name_len += snprintf_hex(ksymbol_event->name + name_len, + KSYM_NAME_LEN - name_len, + prog_tags[i], BPF_TAG_SIZE); + if (has_btf) { + finfo = func_infos + i * info.func_info_rec_size; + t = btf__type_by_id(btf, finfo->type_id); + short_name = btf__name_by_offset(btf, t->name_off); + } else if (i == 0 && sub_prog_cnt == 1) { + /* no subprog */ + if (info.name[0]) + short_name = info.name; + } else + short_name = "F"; + if (short_name) + name_len += snprintf(ksymbol_event->name + name_len, + KSYM_NAME_LEN - name_len, + "_%s", short_name); + + ksymbol_event->header.size += PERF_ALIGN(name_len + 1, + sizeof(u64)); + err = perf_tool__process_synth_event(tool, event, + machine, process); + } + + /* Synthesize PERF_RECORD_BPF_EVENT */ + if (opts->bpf_event) { + *bpf_event = (struct bpf_event){ + .header = { + .type = PERF_RECORD_BPF_EVENT, + .size = sizeof(struct bpf_event), + }, + .type = PERF_BPF_EVENT_PROG_LOAD, + .flags = 0, + .id = info.id, + }; + memcpy(bpf_event->tag, prog_tags[i], BPF_TAG_SIZE); + err = perf_tool__process_synth_event(tool, event, + machine, process); + } + +out: + free(prog_tags); + free(prog_lens); + free(prog_addrs); + free(func_infos); + free(btf); + return err ? -1 : 0; +} + +int perf_event__synthesize_bpf_events(struct perf_tool *tool, + perf_event__handler_t process, + struct machine *machine, + struct record_opts *opts) +{ + union perf_event *event; + __u32 id = 0; + int err; + int fd; + + event = malloc(sizeof(event->bpf_event) + KSYM_NAME_LEN); + if (!event) + return -1; + while (true) { + err = bpf_prog_get_next_id(id, &id); + if (err) { + if (errno == ENOENT) { + err = 0; + break; + } + pr_err("can't get next program: %s%s", + strerror(errno), + errno == EINVAL ? " -- kernel too old?" : ""); + err = -1; + break; + } + fd = bpf_prog_get_fd_by_id(id); + if (fd < 0) { + pr_debug("Failed to get fd for prog_id %u\n", id); + continue; + } + + err = perf_event__synthesize_one_bpf_prog( + tool, process, machine, fd, event, opts); + close(fd); + if (err) + break; + } + free(event); + return err; +} diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h index d5ca355dd298..38aee4040f12 100644 --- a/tools/perf/util/bpf-event.h +++ b/tools/perf/util/bpf-event.h @@ -8,4 +8,9 @@ int machine__process_bpf_event(struct machine *machine, union perf_event *event, struct perf_sample *sample); +int perf_event__synthesize_bpf_events(struct perf_tool *tool, + perf_event__handler_t process, + struct machine *machine, + struct record_opts *opts); + #endif -- 2.17.1