Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2780961rda; Wed, 25 Oct 2023 12:04:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEXOfgtvX2ftUvMk9EExke4Yz4lUJMnQztF+89Dne2DfB6Rei66x21j5diCEGMARnBHaqBe X-Received: by 2002:a0d:f582:0:b0:59b:eab8:7ac6 with SMTP id e124-20020a0df582000000b0059beab87ac6mr17143308ywf.42.1698260685493; Wed, 25 Oct 2023 12:04:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698260685; cv=none; d=google.com; s=arc-20160816; b=t2RLiSMY6MLrb4d6iNvVO/3GKIo+1yy/L2lYlEm0ilyP3qwpSZOwNVvB6coT8To4ig fwOgz1ihAu4cMSyounNSBjhCLATbmfTYpTKfcjKekbhJoqhLfst2RFmM7iMLvD9vgeLt ikbOXyM/K47sENdnphxm1c9In9Cj7q0VWyvOKeNYpjJo4Sz6K6Hl14cQ7WNISQTddv4H AT5VxPyzhKZHBm/kWTXJAEqdISqTH//rB0IZlMVCj+m9EaxmAcrwE5vcXftXJTRRCRE0 +H6Cymp2HHx8219/A3QkfAnsCuNC3CJ4IBKUQDGjGxInLnO2eRaKjfczxg7j5fAiIAZh 2RjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=Qc/oF53YdCSM4v2/qOs8BZox/BBLUdY5mEINS5d4zCY=; fh=Uq54LRPoGNc58WPjrRX9AxWPcpIocIDAws/SV4RseoI=; b=w4hfG2MpUouFEfRrfB+CuNAM+qJr60+LmZmlXuQiVw9WPIls/tXQz5NFBq9Jmc33ju YVhRL/QaCTIPb37GRVQexHWXLo1/2594TBm+coBPzZuItEmLn0RHkgkDRZs94saaQ5D3 vFdBFbXXkxBYJXCwAwT3U+It47ZDLvUv1AbDhYDjPYJ+e3y7xXyQuYCxalfpBztUIvgl GZcHGd/PD+sMEhdcA9ytOz+iocicu9hVkhU//p+oHZbWXH8DcVTkWTm7WV0HF895BAiA ybJEvufz8LR/XoDBHA6mt60TN84QI5eIdVIT2LNFegmFcHXqmbroHuDB77QBfjz1/TaS M/8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id y205-20020a0dd6d6000000b005a1cd8cecc9si12582414ywd.416.2023.10.25.12.04.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 12:04:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A6952816EBD6; Wed, 25 Oct 2023 12:04:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234875AbjJYTEd convert rfc822-to-8bit (ORCPT + 99 others); Wed, 25 Oct 2023 15:04:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234874AbjJYTEb (ORCPT ); Wed, 25 Oct 2023 15:04:31 -0400 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88A50186; Wed, 25 Oct 2023 12:04:28 -0700 (PDT) Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-27d5fe999caso31384a91.1; Wed, 25 Oct 2023 12:04:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698260668; x=1698865468; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0d7Dj62K3ZkBF/x+7OimkWn3bFsJf98qoG9lznGMZYY=; b=QcR0M2nkjcY7viuaoOfalEYrKXMHwnx3m69vSkTAwdGLGlzMaLfX0xIZUFvSra4y+g /391W4gGKGqOLnN5M4qaloDEie9h9pGkROdtSPVii9pEGSZgsHVbAwsnM7kubTCcOjPo bKjzH9X0UlMEenbr74IC9U2wftCG4hQE6z3UF6xGzrsuyH5b1MJrafYnwz83oYXm26QI NeZ3bDdluG2weJ15TSmOG+ez7H7+GVZaGaDanNEWOoeDgLbPlJH8mPOSgGxNZFScsw1D /+gqPPaAjur89SpdwU1VqViM1dOmu6YzDWSPx+UJGVUhvWFB2o7w8uf5cVo2CkLuSc/T vq/w== X-Gm-Message-State: AOJu0YziyV2wIUcXqauOybzUY+lsz9NMWc0HESGMAW4GNOtOAW2ZQz3o hZds/4jl7YG3OPIBTEomUp1vDOSeonfHL8PqDBc= X-Received: by 2002:a17:90b:2786:b0:274:111c:c14d with SMTP id pw6-20020a17090b278600b00274111cc14dmr12568239pjb.13.1698260667789; Wed, 25 Oct 2023 12:04:27 -0700 (PDT) MIME-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> <20231024222353.3024098-21-irogers@google.com> <7516348d-fe6d-9768-049e-328cfcda89ee@huawei.com> In-Reply-To: From: Namhyung Kim Date: Wed, 25 Oct 2023 12:04:16 -0700 Message-ID: Subject: Re: [PATCH v3 20/50] perf record: Be lazier in allocating lost samples buffer To: Ian Rogers Cc: Yang Jihong , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , 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 , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 25 Oct 2023 12:04:41 -0700 (PDT) On Wed, Oct 25, 2023 at 10:01 AM Ian Rogers wrote: > > On Tue, Oct 24, 2023 at 8:44 PM Yang Jihong wrote: > > > > 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. > > Hmm.. seems marginal. This change makes the code not return in > record__read_lost_samples when the memory allocation fails, so I'm > 50/50 on it. Maybe you can make it return the failure. > > > > > > 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 [SNIP] > > out: > > - free(lost); > > + if (lost) > > + free(lost); This is unnecessary as free() can handle NULL pointers. Thanks, Namhyung > > } > > > > static volatile sig_atomic_t workload_exec_errno; > > > > > > Thanks, > > Yang > >