Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2299266rda; Tue, 24 Oct 2023 20:45:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFb7rmH2dyMAP6n8EiiMhr0toQPB2wEBdNOMk8Kvq9j6cg6uyPxsM66kuNBwTLM+J2YOT3F X-Received: by 2002:a05:6214:27c2:b0:66d:63f8:255f with SMTP id ge2-20020a05621427c200b0066d63f8255fmr17608701qvb.56.1698205502223; Tue, 24 Oct 2023 20:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698205502; cv=none; d=google.com; s=arc-20160816; b=gY3ic/EVa8PE6p5Q+PTyomy5ME5uYKTIO0NbzP4cWT1JcM0j2bV+IbIO3J1wuKyRdn ZLywkXzOqQJ5JpfFkvhQ0hjcNFMxJ22YvQuM69zhI4cDlZqpnL8ktom1FnRzzoEcDXik rIFofIE010jcNYz6L+PQzm3Iz9yr1LfRe0Vw6lceI9LQ/7Jj+zxsgzC8Cos00rDif1h6 y4g07Fwo+qJJMQermhaIRU6oAGVy6e0ThEkqMV/6VV4Gn8OL8pU3tn/wDd8izTsBS7ws BvONVqvAh83m2Ge+3xiw5clO6kS3DUiby6tdwdvJeBOwDIsJ/QBWyjSUTVCdVDoMzGmu WJfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:cc:from :references:to:subject; bh=0zeMv0q2XCGCoEK5fI2F1jFYqwCLOnoRYNrv7R3zBHk=; fh=1txcerp29p4cQH1Vu9KsCdT1oKzSPBqk93FmjKumoGA=; b=VoSxdoz7DyC6uIVJoi6AqZ3+H+7UlZZYcyZGOSmaZmURSasHBhapIvL2G2QqzcdBqQ l+CLvqds6tgLE/1w+vvoE26Y2Uqg6Vm26I4HHhZawmoEXXIK1UMplFg00eR7tRtCn0cX LI8dTRGHOjUBVead3QT7qltDefSwgv17HZbnZHG6/ZX/LGOiWKLhKnDbFS97ElsY99Cx k+ImfyhsD+bQ7POIyB/78Rq00NNbK+dvzuQkGeG5p3MqFhNL/yOF9xo+3MFFwlKCnCWv tzr0Ni6WD4evSo1YCXKHY1qf+nO/gYJ3kIdwlz9zLJX9LoLFipz6nC7JwmW4MhD0yUsU R0gA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id x123-20020a81a081000000b0059f4c79850csi9666813ywg.298.2023.10.24.20.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 20:45:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 043BC8023931; Tue, 24 Oct 2023 20:44:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232397AbjJYDoa (ORCPT + 99 others); Tue, 24 Oct 2023 23:44:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232323AbjJYDo1 (ORCPT ); Tue, 24 Oct 2023 23:44:27 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AE0E90; Tue, 24 Oct 2023 20:44:24 -0700 (PDT) Received: from kwepemm000003.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4SFZV10fbkzrSB5; Wed, 25 Oct 2023 11:41:29 +0800 (CST) Received: from [10.67.111.205] (10.67.111.205) by kwepemm000003.china.huawei.com (7.193.23.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Wed, 25 Oct 2023 11:44:20 +0800 Subject: Re: [PATCH v3 20/50] perf record: Be lazier in allocating lost samples buffer To: Ian Rogers References: <20231024222353.3024098-1-irogers@google.com> <20231024222353.3024098-21-irogers@google.com> From: Yang Jihong CC: Ian Rogers , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , , Message-ID: <7516348d-fe6d-9768-049e-328cfcda89ee@huawei.com> Date: Wed, 25 Oct 2023 11:44:19 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <20231024222353.3024098-21-irogers@google.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.111.205] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm000003.china.huawei.com (7.193.23.66) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 20:44:59 -0700 (PDT) Hello, On 2023/10/25 6:23, Ian Rogers wrote: > Wait until a lost sample occurs to allocate the lost samples buffer, > often the buffer isn't necessary. This saves a 64kb allocation and > 5.3kb of peak memory consumption. > > Signed-off-by: Ian Rogers > --- > tools/perf/builtin-record.c | 29 +++++++++++++++++++---------- > 1 file changed, 19 insertions(+), 10 deletions(-) > > diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c > index 9b4f3805ca92..b6c8c1371b39 100644 > --- a/tools/perf/builtin-record.c > +++ b/tools/perf/builtin-record.c > @@ -1924,21 +1924,13 @@ static void __record__save_lost_samples(struct record *rec, struct evsel *evsel, > static void record__read_lost_samples(struct record *rec) > { > struct perf_session *session = rec->session; > - struct perf_record_lost_samples *lost; > + struct perf_record_lost_samples *lost = NULL; > struct evsel *evsel; > > /* there was an error during record__open */ > if (session->evlist == NULL) > return; > > - lost = zalloc(PERF_SAMPLE_MAX_SIZE); > - if (lost == NULL) { > - pr_debug("Memory allocation failed\n"); > - return; > - } > - > - lost->header.type = PERF_RECORD_LOST_SAMPLES; > - > evlist__for_each_entry(session->evlist, evsel) { > struct xyarray *xy = evsel->core.sample_id; > u64 lost_count; > @@ -1961,6 +1953,14 @@ static void record__read_lost_samples(struct record *rec) > } > > if (count.lost) { > + if (!lost) { > + lost = zalloc(PERF_SAMPLE_MAX_SIZE); > + if (!lost) { > + pr_debug("Memory allocation failed\n"); > + return; > + } > + lost->header.type = PERF_RECORD_LOST_SAMPLES; > + } > __record__save_lost_samples(rec, evsel, lost, > x, y, count.lost, 0); > } > @@ -1968,9 +1968,18 @@ static void record__read_lost_samples(struct record *rec) > } > > lost_count = perf_bpf_filter__lost_count(evsel); > - if (lost_count) > + if (lost_count) { > + if (!lost) { > + lost = zalloc(PERF_SAMPLE_MAX_SIZE); > + if (!lost) { > + pr_debug("Memory allocation failed\n"); > + return; > + } > + lost->header.type = PERF_RECORD_LOST_SAMPLES; > + } > __record__save_lost_samples(rec, evsel, lost, 0, 0, lost_count, > PERF_RECORD_MISC_LOST_SAMPLES_BPF); > + } > } Can zalloc for `lost` be moved to __record__save_lost_samples? This simplifies the code. diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index dcf288a4fb9a..8d2eb746031a 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -1888,14 +1888,25 @@ record__switch_output(struct record *rec, bool at_exit) } static void __record__save_lost_samples(struct record *rec, struct evsel *evsel, - struct perf_record_lost_samples *lost, + struct perf_record_lost_samples **plost, int cpu_idx, int thread_idx, u64 lost_count, u16 misc_flag) { struct perf_sample_id *sid; struct perf_sample sample = {}; + struct perf_record_lost_samples *lost = *plost; int id_hdr_size; + if (!lost) { + lost = zalloc(PERF_SAMPLE_MAX_SIZE); + if (!lost) { + pr_debug("Memory allocation failed\n"); + return; + } + lost->header.type = PERF_RECORD_LOST_SAMPLES; + *plost = lost; + } + lost->lost = lost_count; if (evsel->core.ids) { sid = xyarray__entry(evsel->core.sample_id, cpu_idx, thread_idx); @@ -1912,21 +1923,13 @@ static void __record__save_lost_samples(struct record *rec, struct evsel *evsel, static void record__read_lost_samples(struct record *rec) { struct perf_session *session = rec->session; - struct perf_record_lost_samples *lost; + struct perf_record_lost_samples *lost = NULL; struct evsel *evsel; /* there was an error during record__open */ if (session->evlist == NULL) return; - lost = zalloc(PERF_SAMPLE_MAX_SIZE); - if (lost == NULL) { - pr_debug("Memory allocation failed\n"); - return; - } - - lost->header.type = PERF_RECORD_LOST_SAMPLES; - evlist__for_each_entry(session->evlist, evsel) { struct xyarray *xy = evsel->core.sample_id; u64 lost_count; @@ -1949,7 +1952,7 @@ static void record__read_lost_samples(struct record *rec) } if (count.lost) { - __record__save_lost_samples(rec, evsel, lost, + __record__save_lost_samples(rec, evsel, &lost, x, y, count.lost, 0); } } @@ -1957,11 +1960,12 @@ static void record__read_lost_samples(struct record *rec) lost_count = perf_bpf_filter__lost_count(evsel); if (lost_count) - __record__save_lost_samples(rec, evsel, lost, 0, 0, lost_count, + __record__save_lost_samples(rec, evsel, &lost, 0, 0, lost_count, PERF_RECORD_MISC_LOST_SAMPLES_BPF); } out: - free(lost); + if (lost) + free(lost); } static volatile sig_atomic_t workload_exec_errno; Thanks, Yang