From patchwork Fri May 13 07:56:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 67736 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp136134qge; Fri, 13 May 2016 00:59:06 -0700 (PDT) X-Received: by 10.98.42.216 with SMTP id q207mr20779195pfq.6.1463126346192; Fri, 13 May 2016 00:59:06 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id wc1si23326762pab.135.2016.05.13.00.59.05; Fri, 13 May 2016 00:59:06 -0700 (PDT) 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 S1752256AbcEMH66 (ORCPT + 29 others); Fri, 13 May 2016 03:58:58 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:62033 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752496AbcEMH4w (ORCPT ); Fri, 13 May 2016 03:56:52 -0400 Received: from 172.24.1.137 (EHLO szxeml433-hub.china.huawei.com) ([172.24.1.137]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DKL57621; Fri, 13 May 2016 15:56:43 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml433-hub.china.huawei.com (10.82.67.210) with Microsoft SMTP Server id 14.3.235.1; Fri, 13 May 2016 15:56:32 +0800 From: Wang Nan To: CC: , , Wang Nan , He Kuang , "Arnaldo Carvalho de Melo" , Jiri Olsa , Masami Hiramatsu , Namhyung Kim , "Zefan Li" , Subject: [PATCH 16/17] perf record: Toggle overwrite ring buffer for reading Date: Fri, 13 May 2016 07:56:13 +0000 Message-ID: <1463126174-119290-17-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1463126174-119290-1-git-send-email-wangnan0@huawei.com> References: <1463126174-119290-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.0A020205.573588BC.00C6, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: e7e74503c4e9fed62882a4aa5f4fc51a 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 | 94 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) -- 1.8.3.4 diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index e637ea2..606fcd05 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -42,6 +42,11 @@ #include #include +enum overwrite_evt_state { + OVERWRITE_EVT_RUNNING, + OVERWRITE_EVT_DATA_PENDING, + OVERWRITE_EVT_EMPTY, +}; struct record { struct perf_tool tool; @@ -60,6 +65,7 @@ struct record { bool buildid_all; bool timestamp_filename; bool switch_output; + enum overwrite_evt_state overwrite_evt_state; unsigned long long samples; }; @@ -416,6 +422,7 @@ try_again: session->evlist = evlist; perf_session__set_id_hdr_size(session); + rec->overwrite_evt_state = OVERWRITE_EVT_RUNNING; out: return rc; } @@ -496,6 +503,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; @@ -504,8 +557,13 @@ static bool record__mmap_should_read(struct record *rec, int idx) return false; if (perf_evlist__channel_idx(rec->evlist, &channel, &idx)) return false; - if (perf_evlist__channel_check(rec->evlist, channel, RDONLY)) - return false; + if (perf_evlist__channel_check(rec->evlist, channel, RDONLY)) { + if (rec->overwrite_evt_state != OVERWRITE_EVT_DATA_PENDING) + return false; + if (!perf_evlist__channel_check(rec->evlist, channel, BACKWARD)) + return false; + return true; + } return true; } @@ -540,6 +598,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; } @@ -917,6 +977,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 (trigger_is_hit(&switch_output_trigger) || done || draining) + record__toggle_overwrite_evsels(rec, OVERWRITE_EVT_DATA_PENDING); + if (record__mmap_read_all(rec) < 0) { trigger_error(&auxtrace_snapshot_trigger); trigger_error(&switch_output_trigger); @@ -936,8 +1007,27 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) } if (trigger_is_hit(&switch_output_trigger)) { + /* + * If switch_output_trigger is hit, the data in + * overwritable ring buffer should have been collected, + * so overwrite_evt_state should be set to + * OVERWRITE_EVT_EMPTY. + * + * If SIGUSR2 raise after or during record__mmap_read_all(), + * record__mmap_read_all() didn't collect data from + * overwritable ring buffer. Read again. + */ + if (rec->overwrite_evt_state == OVERWRITE_EVT_RUNNING) + continue; trigger_ready(&switch_output_trigger); + /* + * 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", waking);