lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAM9d7cj4QbsMoqzrCy+9MU-eOo=TNoew_h7V-mkD+X7AkZR7Dw@mail.gmail.com>
Date:   Wed, 25 Oct 2023 12:04:16 -0700
From:   Namhyung Kim <namhyung@...nel.org>
To:     Ian Rogers <irogers@...gle.com>
Cc:     Yang Jihong <yangjihong1@...wei.com>,
        Peter Zijlstra <peterz@...radead.org>,
        Ingo Molnar <mingo@...hat.com>,
        Arnaldo Carvalho de Melo <acme@...nel.org>,
        Mark Rutland <mark.rutland@....com>,
        Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
        Jiri Olsa <jolsa@...nel.org>,
        Adrian Hunter <adrian.hunter@...el.com>,
        Nick Terrell <terrelln@...com>,
        Kan Liang <kan.liang@...ux.intel.com>,
        Andi Kleen <ak@...ux.intel.com>, Leo Yan <leo.yan@...aro.org>,
        Song Liu <song@...nel.org>,
        Sandipan Das <sandipan.das@....com>,
        James Clark <james.clark@....com>,
        Anshuman Khandual <anshuman.khandual@....com>,
        Miguel Ojeda <ojeda@...nel.org>,
        Liam Howlett <liam.howlett@...cle.com>,
        Athira Rajeev <atrajeev@...ux.vnet.ibm.com>,
        Kajol Jain <kjain@...ux.ibm.com>,
        K Prateek Nayak <kprateek.nayak@....com>,
        Sean Christopherson <seanjc@...gle.com>,
        Yanteng Si <siyanteng@...ngson.cn>,
        Ravi Bangoria <ravi.bangoria@....com>,
        German Gomez <german.gomez@....com>,
        Changbin Du <changbin.du@...wei.com>,
        Paolo Bonzini <pbonzini@...hat.com>,
        Masami Hiramatsu <mhiramat@...nel.org>,
        liuwenyu <liuwenyu7@...wei.com>, linux-kernel@...r.kernel.org,
        linux-perf-users@...r.kernel.org
Subject: Re: [PATCH v3 20/50] perf record: Be lazier in allocating lost
 samples buffer

On Wed, Oct 25, 2023 at 10:01 AM Ian Rogers <irogers@...gle.com> wrote:
>
> On Tue, Oct 24, 2023 at 8:44 PM Yang Jihong <yangjihong1@...wei.com> 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 <irogers@...gle.com>
> > > ---
> > >   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
> >

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ