Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3427950rdh; Mon, 27 Nov 2023 14:11:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IHl4f54mvH5ov0StRm6NOmeBqZioOcEePle6MOO6UUp6+qK3AltF6FB6fxxDiFCmVjAYsTu X-Received: by 2002:a05:6a20:1388:b0:187:e1cc:8cfc with SMTP id hn8-20020a056a20138800b00187e1cc8cfcmr10743684pzc.35.1701123078726; Mon, 27 Nov 2023 14:11:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123078; cv=none; d=google.com; s=arc-20160816; b=TW8v0DO+DDHYHGYgn4jsX09vgz3hHH4dL2+Zy06E7ELN2i+6DRsyA0iV0pGMqA+S+h Ep5Uk4qXrfWMZonlPazWac9/n5Fr4Cc0JU5cIbvJwjCnNM38F8slUZoBn/N8VSXnJn0/ mddCzyMKshHRB+radIDcXZL+LbW3YEzBXdisofT6nwKGsfV+FwLD7GeQiWE0H4edwSJN qUEy5fsdglgVoWLBNdutOHB2Gqh5+08DbStoE7ccHTdui0jT/wvnEyQmzeahaYYFi7Mn HMPetb2f7xsY6eDsCfe3BXV1W4/O51K0qzYwqQJ9scWMup1xWf50w9O8jiohQv+GIm+r ZRig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=ojWy9F2KHooGkBXRESL6aoFicBiCSxBaXbQBRQz0etk=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=qkIrqX9e5MO3du4/xc2QTVapGIX94jKVvmMegh5E/0EDawaJYOsKmM2lV8pC5nLZ/z TkfdBwlhUAW9bH+xaO3ZjEqIw40iiIvlkfvrJ/roTv1+vxQRbzz9WdlodR1d4GyKlYoa 8i1atXtXLmzUP4P219qzM4FUsF3Dk6Nm+vOrM6pjLP8nGz4twz4ZK2Ww6H6aKXgcBLlY m9lCt0S9TNMOs3c2ekauACivoOUY4AbaeEyjNrVhq1rbCK0kvYqEoNEyWMzi3anwjllL HV741zm9hPU0TqgscmqhEWUi6QRUW6OaRNfaYJlRYeFDMM0yeWq6EfwYqnGr75Pl6ruJ /yNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="qmn/TEip"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id i65-20020a62c144000000b006cda14f6856si447176pfg.349.2023.11.27.14.11.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:11:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="qmn/TEip"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 2FC1D824E7B2; Mon, 27 Nov 2023 14:11:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233672AbjK0WJs (ORCPT + 99 others); Mon, 27 Nov 2023 17:09:48 -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-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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.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 (howler.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:11:05 -0800 (PST) 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); + } } out: free(lost); -- 2.43.0.rc1.413.gea7ed67945-goog