perf record: Be lazier in allocating lost samples buffer
authorIan Rogers <irogers@google.com>
Mon, 27 Nov 2023 22:08:20 +0000 (14:08 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 6 Dec 2023 12:46:14 +0000 (09:46 -0300)
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 <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Guilherme Amadio <amadio@gentoo.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Li Dong <lidong@vivo.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Ming Wang <wangming01@loongson.cn>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Vincent Whitchurch <vincent.whitchurch@axis.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20231127220902.1315692-9-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-record.c

index db814eadc16b968c32638d9f63ea1eaaec2ff42f..eb5a398ddb1d8da504420e5193e89928b34279bb 100644 (file)
@@ -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);