Re: [PATCH v2 1/2] perf symbol: Remove symbol_name_rb_node
From: Ian Rogers
Date: Thu Jun 22 2023 - 01:54:59 EST
On Wed, Jun 21, 2023 at 10:21 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
>
> On Wed, Jun 21, 2023 at 9:28 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
> >
> > On Wed, Jun 21, 2023 at 8:51 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
> > >
> > > Hi Ian,
> > >
> > > On Tue, Jun 20, 2023 at 11:37 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
> > > >
> > > > Most perf commands want to sort symbols by name and this is done via
> > > > an invasive rbtree that on 64-bit systems costs 24 bytes. Sorting the
> > > > symbols in a DSO by name is optional and not done by default, however,
> > > > if sorting is requested the 24 bytes is allocated for every
> > > > symbol.
> > > >
> > > > This change removes the rbtree and uses a sorted array of symbol
> > > > pointers instead (costing 8 bytes per symbol). As the array is created
> > > > on demand then there are further memory savings. The complexity of
> > > > sorting the array and using the rbtree are the same.
> > > >
> > > > To support going to the next symbol, the index of the current symbol
> > > > needs to be passed around as a pair with the current symbol. This
> > > > requires some API changes.
> > > >
> > > > Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> > > >
> > > > v2. map__find_symbol_by_name_idx so that map__find_symbol_by_name
> > > > doesn't need an optional parameter. Separate out
> > > > symbol_conf.sort_by_name removal.
> > > > ---
> > >
> > > [SNIP]
> > > > void dso__sort_by_name(struct dso *dso)
> > > > {
> > > > - dso__set_sorted_by_name(dso);
> > > > - return symbols__sort_by_name(&dso->symbol_names, &dso->symbols);
> > > > + mutex_lock(&dso->lock);
> > > > + if (!dso__sorted_by_name(dso)) {
> > > > + size_t len;
> > > > +
> > > > + dso->symbol_names = symbols__sort_by_name(&dso->symbols, &len);
> > > > + if (dso->symbol_names) {
> > > > + dso->symbol_names_len = len;
> > > > + dso__set_sorted_by_name(dso);
> > > > + }
> > > > + }
> > > > + mutex_unlock(&dso->lock);
> > >
> > > I think this part deserves a separate commit.
> >
> > Using the mutex or the use of sorted_by_name?
>
> For the mutex originally, but might be better to split further. :)
I can add the locks first. There's an obvious leak with the array
approach if two threads are sorting. With the invasive rbtree a leak
isn't possible but corruption could be.
> And now it grabs the mutex unconditionally, I think
> we can check the condition without the mutex first
> and again with the mutex if not sorted.
Is there a kernel pattern for double checked locking that isn't broken
in the normal ways double checked locking is broken?
https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
Thanks,
Ian
> Thanks,
> Namhyung