From patchwork Fri Feb 5 14:02:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 61296 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp1114878lbl; Fri, 5 Feb 2016 06:11:38 -0800 (PST) X-Received: by 10.98.75.200 with SMTP id d69mr20416096pfj.108.1454681498624; Fri, 05 Feb 2016 06:11:38 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id fk1si24213132pad.35.2016.02.05.06.11.38; Fri, 05 Feb 2016 06:11:38 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754711AbcBEOLS (ORCPT + 30 others); Fri, 5 Feb 2016 09:11:18 -0500 Received: from szxga03-in.huawei.com ([119.145.14.66]:11465 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754273AbcBEOFG (ORCPT ); Fri, 5 Feb 2016 09:05:06 -0500 Received: from 172.24.1.47 (EHLO SZXEML429-HUB.china.huawei.com) ([172.24.1.47]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BVT55336; Fri, 05 Feb 2016 22:03:51 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by SZXEML429-HUB.china.huawei.com (10.82.67.184) with Microsoft SMTP Server id 14.3.235.1; Fri, 5 Feb 2016 22:03:40 +0800 From: Wang Nan To: Alexei Starovoitov , Arnaldo Carvalho de Melo , Arnaldo Carvalho de Melo , Brendan Gregg CC: Adrian Hunter , Cody P Schafer , "David S. Miller" , He Kuang , =?UTF-8?q?J=C3=A9r=C3=A9mie=20Galarneau?= , Jiri Olsa , Kirill Smelkov , Li Zefan , Masami Hiramatsu , Namhyung Kim , Peter Zijlstra , , Wang Nan , Subject: [PATCH 50/54] perf record: Toggle overwrite ring buffer for reading Date: Fri, 5 Feb 2016 14:02:15 +0000 Message-ID: <1454680939-24963-51-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1454680939-24963-1-git-send-email-wangnan0@huawei.com> References: <1454680939-24963-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020202.56B4ABC7.011B, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 59d7411b444dd8c402294cf0379b4594 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reading from a overwrite ring buffer is unrelible. perf_evlist__channel_toggle_paused() should be called before reading from them. Toggel overwrite_evt_paused director after receiving done or switch output. Signed-off-by: Wang Nan Signed-off-by: He Kuang Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com --- tools/perf/builtin-record.c | 79 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) -- 1.8.3.4 diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 09aa4ee..4d89543 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -39,6 +39,11 @@ #include #include +enum overwrite_evt_state { + OVERWRITE_EVT_RUNNING, + OVERWRITE_EVT_DATA_PENDING, + OVERWRITE_EVT_EMPTY, +}; struct record { struct perf_tool tool; @@ -57,6 +62,7 @@ struct record { bool buildid_all; bool timestamp_filename; bool switch_output; + enum overwrite_evt_state overwrite_evt_state; unsigned long long samples; }; @@ -388,6 +394,7 @@ try_again: session->evlist = evlist; perf_session__set_id_hdr_size(session); + rec->overwrite_evt_state = OVERWRITE_EVT_RUNNING; out: return rc; } @@ -468,6 +475,52 @@ static struct perf_event_header finished_round_event = { .type = PERF_RECORD_FINISHED_ROUND, }; +static void +record__toggle_overwrite_evsels(struct record *rec, + enum overwrite_evt_state state) +{ + struct perf_evlist *evlist = rec->evlist; + enum overwrite_evt_state old_state = rec->overwrite_evt_state; + enum action { + NONE, + PAUSE, + RESUME, + } action = NONE; + int ch, nr_channels; + + switch (old_state) { + case OVERWRITE_EVT_RUNNING: + if (state != OVERWRITE_EVT_RUNNING) + action = PAUSE; + break; + case OVERWRITE_EVT_DATA_PENDING: + if (state == OVERWRITE_EVT_RUNNING) + action = RESUME; + break; + case OVERWRITE_EVT_EMPTY: + if (state == OVERWRITE_EVT_RUNNING) + action = RESUME; + if (state == OVERWRITE_EVT_DATA_PENDING) + state = OVERWRITE_EVT_EMPTY; + break; + default: + WARN_ONCE(1, "Shouldn't get there\n"); + } + + rec->overwrite_evt_state = state; + + if (action == NONE) + return; + + nr_channels = perf_evlist__channel_nr(evlist); + for (ch = 0; ch < nr_channels; ch++) { + if (!perf_evlist__channel_check(evlist, ch, RDONLY)) + continue; + perf_evlist__channel_toggle_paused(evlist, ch, + action == PAUSE); + } +} + static bool record__mmap_should_read(struct record *rec, int idx) { int channel = -1; @@ -512,6 +565,8 @@ static int record__mmap_read_all(struct record *rec) if (bytes_written != rec->bytes_written) rc = record__write(rec, &finished_round_event, sizeof(finished_round_event)); + if (rec->overwrite_evt_state == OVERWRITE_EVT_DATA_PENDING) + record__toggle_overwrite_evsels(rec, OVERWRITE_EVT_EMPTY); out: return rc; } @@ -870,6 +925,17 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) for (;;) { unsigned long long hits = rec->samples; + /* + * rec->overwrite_evt_state is possible to be + * OVERWRITE_EVT_EMPTY here: when done == true and + * hits != rec->samples after previous reading. + * + * record__toggle_overwrite_evsels ensure we never + * convert OVERWRITE_EVT_EMPTY to OVERWRITE_EVT_DATA_PENDING. + */ + if (switch_output_started || done || draining) + record__toggle_overwrite_evsels(rec, OVERWRITE_EVT_DATA_PENDING); + if (record__mmap_read_all(rec) < 0) { auxtrace_snapshot_disable(); err = -1; @@ -888,7 +954,20 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) } if (switch_output_started) { + /* + * SIGUSR2 raise after or during record__mmap_read_all(). + * continue to read again. + */ + if (rec->overwrite_evt_state == OVERWRITE_EVT_RUNNING) + continue; + switch_output_started = 0; + /* + * Reenable events in overwrite ring buffer after + * record__mmap_read_all(): we should have collected + * data from it. + */ + record__toggle_overwrite_evsels(rec, OVERWRITE_EVT_RUNNING); if (!quiet) fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",