Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp5204194imb; Thu, 7 Mar 2019 10:00:05 -0800 (PST) X-Google-Smtp-Source: APXvYqxsv+ToHNKOn/KXaEcIgn20J2FHPI+ozL9WdWpKwjdMDzEqrJauFxxGbDOp7nF3Y8EWKaWX X-Received: by 2002:a17:902:7889:: with SMTP id q9mr14488468pll.305.1551981605736; Thu, 07 Mar 2019 10:00:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551981605; cv=none; d=google.com; s=arc-20160816; b=topzJYB5O6Siq1G7zZzX/LdhmIhQ5eo5vlLqvT6Hz8VXua5W47JtqUa3vs9HHEUPpV 0mUch2KLlNcz9Mj73KcMjKzCq+In5y00KVzeB8HNOhbdYvCVb7D0NYnCOwN9gyVaaOlw cDpC/acIvQ0b8u3Bhjkrpe44tVQKh9cQf6q1DJY2LPGIf+jRfoIcuwxUN1vDFq5Qss2f 3zthLGLhD6hLa7DEadocyyqWy6oGJKL362mb2AHSVQZwUgLhGAIbrJH2mP1KVuSbptqk zRbd5Kx9S/NO0uQOMUYWPltI+oEpkO50ZUUEZBNY5aB8Tg+krCet5TQz+FyQ0w49I/Pq 3kRA== 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=UQ9PGKZwpB6Sczcwb31AWVWXtELQC6S5mZ8nh/du5d0=; b=tz7z8NlW33LSyOmD+vFwDSF10B5/PheotqJsLvB9xkBGC//KulzNj/oWVZ/ET4ib4T Dm1Wn2ThlEbdtJtmh3KK0UtgYoteztQf/HxaZOaBlbRdn+za5L4DQdRV/OJNn1HWyjVi MiH7321OxAE9U54yJF2sjVAg9fb+pMfuLmYOuXV3ZZGRlk87JZ0kt7i4/9ie602augH+ wo1SCydJm/G088aRujyZHcSPpVYNU8EeCKVuwB1iFv8HFQMIVToUiH8Jyuk2NWqHlXfU ZxfiIOS+ANtuJJQ+FFBxThsJWgRPx/Z60s1LlJDiN5EdRZBfZuERqPUFxtETOBUIwUHL MGfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=DKVfG3Er; 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 24si4689195pfr.253.2019.03.07.09.59.50; Thu, 07 Mar 2019 10:00:05 -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=DKVfG3Er; 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 S1726344AbfCGR7B (ORCPT + 99 others); Thu, 7 Mar 2019 12:59:01 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:53566 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726530AbfCGR64 (ORCPT ); Thu, 7 Mar 2019 12:58:56 -0500 Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x27Hvfm8014924 for ; Thu, 7 Mar 2019 09:58:55 -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=UQ9PGKZwpB6Sczcwb31AWVWXtELQC6S5mZ8nh/du5d0=; b=DKVfG3ErssjEuOHtJBR5kQi4tMA2hlrlMvORPhv21O1tV0NW7Xb9BKicv9YuT9K1gMgI QpciSYPz1g/Ke3ndddsBNixK1ZnHDcDl0dlQq6V5Wk147V0885U29mujrgQKi+6ZX83q S3nXJbmzoFZ+tViy+8Jo9O8y1ieRKzv1Epk= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2r3607gmam-10 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 07 Mar 2019 09:58:54 -0800 Received: from mx-out.facebook.com (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::129) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1713.5; Thu, 7 Mar 2019 09:58:41 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 6992462E1750; Thu, 7 Mar 2019 09:58:41 -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 v7 perf,bpf 14/15] perf: introduce side band thread Date: Thu, 7 Mar 2019 09:58:09 -0800 Message-ID: <20190307175810.249857-15-songliubraving@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190307175810.249857-1-songliubraving@fb.com> References: <20190307175810.249857-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=2019-03-07_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 introduces side band thread that captures extended information for events like PERF_RECORD_BPF_EVENT. This new thread uses its own evlist that uses ring buffer with very low watermark for lower latency. To use side band thread, we need to: 1. add side band event(s) by calling perf_evlist__add_sb_event(); 2. calls perf_evlist__start_sb_thread(); 3. at the end of perf run, perf_evlist__stop_sb_thread(). In the next patch, we use this thread to handle PERF_RECORD_BPF_EVENT. Signed-off-by: Song Liu --- tools/perf/builtin-record.c | 5 ++ tools/perf/builtin-top.c | 5 ++ tools/perf/util/evlist.c | 113 ++++++++++++++++++++++++++++++++++++ tools/perf/util/evlist.h | 12 ++++ tools/perf/util/evsel.h | 6 ++ 5 files changed, 141 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 2355e0a9eda0..435b94f1f3b3 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -1106,6 +1106,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) struct perf_data *data = &rec->data; struct perf_session *session; bool disabled = false, draining = false; + struct perf_evlist *sb_evlist = NULL; int fd; atexit(record__sig_exit); @@ -1206,6 +1207,8 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) goto out_child; } + perf_evlist__start_sb_thread(sb_evlist, &rec->opts.target); + err = record__synthesize(rec, false); if (err < 0) goto out_child; @@ -1456,6 +1459,8 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) out_delete_session: perf_session__delete(session); + + perf_evlist__stop_sb_thread(sb_evlist); return status; } diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index ccdf5689452f..ec69eb040933 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1524,6 +1524,7 @@ int cmd_top(int argc, const char **argv) "number of thread to run event synthesize"), OPT_END() }; + struct perf_evlist *sb_evlist = NULL; const char * const top_usage[] = { "perf top []", NULL @@ -1654,8 +1655,12 @@ int cmd_top(int argc, const char **argv) top.record_opts.bpf_event = !top.no_bpf_event; + perf_evlist__start_sb_thread(sb_evlist, target); + status = __cmd_top(&top); + perf_evlist__stop_sb_thread(sb_evlist); + out_delete_evlist: perf_evlist__delete(top.evlist); diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 8c902276d4b4..a06a4f54b083 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -19,6 +19,7 @@ #include "debug.h" #include "units.h" #include "asm/bug.h" +#include "bpf-event.h" #include #include @@ -1841,3 +1842,115 @@ struct perf_evsel *perf_evlist__reset_weak_group(struct perf_evlist *evsel_list, } return leader; } + +int perf_evlist__add_sb_event(struct perf_evlist **evlist, + struct perf_event_attr *attr, + perf_evsel__sb_cb_t cb, + void *data) +{ + struct perf_evsel *evsel; + bool new_evlist = (*evlist) == NULL; + + if (*evlist == NULL) + *evlist = perf_evlist__new(); + if (*evlist == NULL) + return -1; + + if (!attr->sample_id_all) { + pr_warning("enabling sample_id_all for all side band events\n"); + attr->sample_id_all = 1; + } + + evsel = perf_evsel__new_idx(attr, (*evlist)->nr_entries); + if (!evsel) + goto out_err; + + evsel->side_band.cb = cb; + evsel->side_band.data = data; + perf_evlist__add(*evlist, evsel); + return 0; + +out_err: + if (new_evlist) { + perf_evlist__delete(*evlist); + *evlist = NULL; + } + return -1; +} + +static void *perf_evlist__poll_thread(void *arg) +{ + struct perf_evlist *evlist = arg; + int i; + + while (!(evlist->thread.done)) { + perf_evlist__poll(evlist, 1000); + + for (i = 0; i < evlist->nr_mmaps; i++) { + struct perf_mmap *map = &evlist->mmap[i]; + union perf_event *event; + + if (perf_mmap__read_init(map)) + continue; + while ((event = perf_mmap__read_event(map)) != NULL) { + struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); + + if (evsel && evsel->side_band.cb) + evsel->side_band.cb(event, evsel->side_band.data); + else + pr_warning("cannot locate proper evsel for the side band event\n"); + + perf_mmap__consume(map); + } + perf_mmap__read_done(map); + } + } + return NULL; +} + +int perf_evlist__start_sb_thread(struct perf_evlist *evlist, + struct target *target) +{ + struct perf_evsel *counter; + + if (!evlist) + return 0; + + if (perf_evlist__create_maps(evlist, target)) + goto out_delete_evlist; + + evlist__for_each_entry(evlist, counter) { + if (perf_evsel__open(counter, evlist->cpus, + evlist->threads) < 0) + goto out_delete_evlist; + } + + if (perf_evlist__mmap(evlist, UINT_MAX)) + goto out_delete_evlist; + + evlist__for_each_entry(evlist, counter) { + if (perf_evsel__enable(counter)) + goto out_delete_evlist; + } + + evlist->thread.done = 0; + if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) + goto out_delete_evlist; + + return 0; + +out_delete_evlist: + perf_evlist__delete(evlist); + evlist = NULL; + return -1; +} + +void perf_evlist__stop_sb_thread(struct perf_evlist *evlist) +{ + if (!evlist) + return; + evlist->thread.done = 1; + pthread_join(evlist->thread.th, NULL); + perf_evlist__exit(evlist); + evlist = NULL; +} diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index 868294491194..07395fbb4b3b 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -51,6 +51,10 @@ struct perf_evlist { struct perf_env *env; u64 first_sample_time; u64 last_sample_time; + struct { + pthread_t th; + volatile int done; + } thread; }; struct perf_evsel_str_handler { @@ -84,6 +88,14 @@ int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, int perf_evlist__add_dummy(struct perf_evlist *evlist); +int perf_evlist__add_sb_event(struct perf_evlist **evlist, + struct perf_event_attr *attr, + perf_evsel__sb_cb_t cb, + void *data); +int perf_evlist__start_sb_thread(struct perf_evlist *evlist, + struct target *target); +void perf_evlist__stop_sb_thread(struct perf_evlist *evlist); + int perf_evlist__add_newtp(struct perf_evlist *evlist, const char *sys, const char *name, void *handler); diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 29c5eb68c44b..94232ba9acce 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -73,6 +73,8 @@ struct perf_evsel_config_term { struct perf_stat_evsel; +typedef int (perf_evsel__sb_cb_t)(union perf_event *event, void *data); + /** struct perf_evsel - event selector * * @evlist - evlist this evsel is in, if it is in one. @@ -151,6 +153,10 @@ struct perf_evsel { bool collect_stat; bool weak_group; const char *pmu_name; + struct { + perf_evsel__sb_cb_t *cb; + void *data; + } side_band; }; union u64_swap { -- 2.17.1