From nobody Wed Dec 17 12:44:10 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 347D0C4167B for ; Mon, 27 Nov 2023 22:10:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233676AbjK0WJu (ORCPT ); Mon, 27 Nov 2023 17:09:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233662AbjK0WJh (ORCPT ); Mon, 27 Nov 2023 17:09:37 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D96410FB for ; Mon, 27 Nov 2023 14:09:40 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da07b5e6f75so4393776276.0 for ; Mon, 27 Nov 2023 14:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701122979; x=1701727779; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ojWy9F2KHooGkBXRESL6aoFicBiCSxBaXbQBRQz0etk=; b=qmn/TEip73mvx5EYOeHIMFGGxnoHN/gDTliprVJUWH7ti0Kq72uUoooDlbV7YtMTVj HugzSWldu4LeG0ow3G1pZHkxLESVDEkOas3F21q06TnAyGZkTOG+KlZPw+EKZd5UicR6 xHpeRLF/ZI/2nBO6BgnJR+82CiVJBzNwXDQ1MNKvMRfns20p9IRaBjBK60g3OnMGSjTu 39W+zPP6N309yET+DFlyPAguHGUqzufoQHBSvSUmhjgChdlsUePEqTZ2iAb9KYNZ7EpF CIyhdplbpxIb1R9DxaojCHx4gCKo5MaK3i01Ox8dmbye2rj7gWD1bhb35nmX/6WsfliG oj/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701122979; x=1701727779; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ojWy9F2KHooGkBXRESL6aoFicBiCSxBaXbQBRQz0etk=; b=Avpottr522oJDJhKbj5LkDMby7DAmxFFV3kI8iCBXJABWoSA6XlFZLED+stxIDYsuO 8VEmmCr6QXpgYiTTPaRV5b07IpMnJSbuMBFVbQ0/0S3auvXD5PD6dHvln3dc6kFUd7MA D0g0wTnMdZQrZV708RGAGfAcpHOpx19U2V3PilyeqNgbBbzG5XoxT3DHG5zmqofl3g3A Zbaj3ay/K8DgYJ48mrjQ7LTQQzo5tlWS8EEs05If+ubtp4cO2uzBShPJRiSEDj8jwNBP oluyF0DtlYtCzxR3rxSpcAybEGIvNlWtNh6JBAcMOMJp/mUoWJTeqsh6uYXqPStcR5OE Phjw== X-Gm-Message-State: AOJu0YwGxDHlyXqGOJG7VAdKlPIiB07/o/QwUhjlzsZ3g1nRL34mZMCN MXTSo6P9UVRwEWh1lwtAOiVQjlIF43SS X-Google-Smtp-Source: AGHT+IFZ6v6AkIBcPRNBxkmA7qOayZb3zUcQto6iDIhbSDG4P5BDPGDEKIore9XjAmmIUvfyadeho8WQYl07 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a25:d10e:0:b0:d9c:f811:eb9e with SMTP id i14-20020a25d10e000000b00d9cf811eb9emr476287ybg.4.1701122979134; Mon, 27 Nov 2023 14:09:39 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:20 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-9-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 08/50] perf record: Be lazier in allocating lost samples buffer From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Acked-by: Namhyung Kim --- 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 reco= rd *rec, struct evsel *evsel, static void record__read_lost_samples(struct record *rec) { struct perf_session *session =3D rec->session; - struct perf_record_lost_samples *lost; + struct perf_record_lost_samples *lost =3D NULL; struct evsel *evsel; =20 /* there was an error during record__open */ if (session->evlist =3D=3D NULL) return; =20 - lost =3D zalloc(PERF_SAMPLE_MAX_SIZE); - if (lost =3D=3D NULL) { - pr_debug("Memory allocation failed\n"); - return; - } - - lost->header.type =3D PERF_RECORD_LOST_SAMPLES; - evlist__for_each_entry(session->evlist, evsel) { struct xyarray *xy =3D evsel->core.sample_id; u64 lost_count; @@ -1961,6 +1953,14 @@ static void record__read_lost_samples(struct record = *rec) } =20 if (count.lost) { + if (!lost) { + lost =3D zalloc(PERF_SAMPLE_MAX_SIZE); + if (!lost) { + pr_debug("Memory allocation failed\n"); + return; + } + lost->header.type =3D 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) } =20 lost_count =3D perf_bpf_filter__lost_count(evsel); - if (lost_count) + if (lost_count) { + if (!lost) { + lost =3D zalloc(PERF_SAMPLE_MAX_SIZE); + if (!lost) { + pr_debug("Memory allocation failed\n"); + return; + } + lost->header.type =3D PERF_RECORD_LOST_SAMPLES; + } __record__save_lost_samples(rec, evsel, lost, 0, 0, lost_count, PERF_RECORD_MISC_LOST_SAMPLES_BPF); + } } out: free(lost); --=20 2.43.0.rc1.413.gea7ed67945-goog