Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp18524rdb; Wed, 29 Nov 2023 18:13:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHQHype5ItkrcuSyjw0bZP0XZMKeyib4wYFRo+kTqyWoDVYuhbeuSA6pOBdP7kmwe+b85e6 X-Received: by 2002:a17:902:d384:b0:1cf:932e:8691 with SMTP id e4-20020a170902d38400b001cf932e8691mr26393270pld.28.1701310438995; Wed, 29 Nov 2023 18:13:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701310438; cv=none; d=google.com; s=arc-20160816; b=Rib+EXpx70bttVD1LH/9beuRwhKG4XuXvxfHGR/36BgqjCQrfr0HWhYbfbYV0iXnVv HVB8J0VTNZNNZQzWhSzRhT3g0pIlbbSP3aQc/BJXTwRo7sEyDTT8FO8PpnUwVG1SCtIj KyDwJ5QoektDo03ckmVyH3sLxC5Z/yHGZa3NjIMt+lg144dD5Jm3pBtHBTU2lwJ3PL2m tluWyw/22xBtNPjeq8xxV3DmL8isJsgaS0ViBPMJQXw1IufSDicp0HV5QGqN7MQ8JfTW s8vXT7a6mLt7ys81dCs7I+nZlZcPC8dEkEgMOshw+PUDTnx+h5i6eK8LOvOr6Udt8j84 B+eQ== 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=IUltzy0FTNbI7YX0uM16wl5M6Ska+lW8i2splv+Oh5M=; fh=aWa9tyepd0GWyBFjkZa5ys7uNwpZ1AjfSj0Mu1iuQbE=; b=e+m4vNrbcuqQb+QJ77uYPOLnirH4hf430cyHJbvLnqOUiaaud0PQ4pka45s1hUgefQ uarS+qlbtenc4wefhUoz8+HRRF+1QIeCaAeUQOeAD7mwzM5Tq50tv+F4hC3mgZlCD7/0 PkyuV/XjsurnjN8QrD2f4Skn2S1lKrUS3d2dpl3vNfxqlDDyBpCiN3+2tNIoTtfffXF0 VmhvYdK4eXSn1W2lrzFp++ko8IkGMG6ZrCmYemOYsjmhGjD14hhVbaAMxRnuD0zExwdo 0ZP/SY9YGz3+fWnqHDBZQGqJvFxMtOlxnyGDsUkPAXF/GOQEjEgLkprRonIXXIvlWKo4 1DAQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id b7-20020a170902650700b001cfb1dab1b0si80960plk.589.2023.11.29.18.13.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 18:13:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 4217A8030A4C; Wed, 29 Nov 2023 18:13:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344014AbjK3CJr convert rfc822-to-8bit (ORCPT + 99 others); Wed, 29 Nov 2023 21:09:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229658AbjK3CJq (ORCPT ); Wed, 29 Nov 2023 21:09:46 -0500 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D9F5BD; Wed, 29 Nov 2023 18:09:53 -0800 (PST) Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-5c628bf8d2eso9715a12.1; Wed, 29 Nov 2023 18:09:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701310192; x=1701914992; 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=ovI0zY83WFzXVjFngO+tObf/AjtMhdLqbooVZa7zEB0=; b=k/H8w4/HfR/JOLvoTw2CdtCuled8aDxUqVGTgZ/CtAKdKT/v59atGqf8PnwRqPzJmz syE0Erw7zQh/xoVerIpJ7y0JccGD3bHfuFKhWqFwPZFwtrgvzE/qRlzLRoPb2rK2ei5U 9L2AscsNC5C3/A7aBmLtsadsoMcjofUq/UfRy5gjgIhOsXKYTBSubg5/sQdgTHwM4TdV x2kYlGLIzwMMCfOFA3p5aeRPm4xa46yUTrZCiodJhZFkw1QzoBMYYAdkLdUvdd3MtmJv Xdb7EI2QapoDRVpLSxyaom8SW69bOepOlfCrrgCOrKkV9trcwLsVbtV8kLC/goMMm9Oh jyKA== X-Gm-Message-State: AOJu0YyNyGQWaHzxfyi0DYG8gqNwdpg5s7er6nbOJT+m63BNd3QXVwRm 8euVd12gTbyAsphhNH7YbPjRm00KSzXPdtkMipk= X-Received: by 2002:a17:90a:dc08:b0:285:6622:ed1 with SMTP id i8-20020a17090adc0800b0028566220ed1mr34013929pjv.10.1701310192576; Wed, 29 Nov 2023 18:09:52 -0800 (PST) MIME-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> <20231127220902.1315692-9-irogers@google.com> In-Reply-To: <20231127220902.1315692-9-irogers@google.com> From: Namhyung Kim Date: Wed, 29 Nov 2023 18:09:41 -0800 Message-ID: Subject: Re: [PATCH v5 08/50] perf record: Be lazier in allocating lost samples buffer To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio 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,T_SCC_BODY_TEXT_LINE 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, 29 Nov 2023 18:13:56 -0800 (PST) On Mon, Nov 27, 2023 at 2:09 PM 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); To minimize the allocation size, this can be sizeof(*lost) + session->machines.host.id_hdr_size instead of PERF_SAMPLE_MAX_SIZE. Thanks, Namhyung > - 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); > + } > } > out: > free(lost); > -- > 2.43.0.rc1.413.gea7ed67945-goog >