Re: [PATCH v8 09/58] perf evlist: Add reference count

Ian Rogers
2026-04-29T16:17:23Z

On Wed, Apr 29, 2026 at 12:25 AM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Tue, Apr 28, 2026 at 12:18:14AM -0700, Ian Rogers wrote:
> > This a no-op for most of the perf tool. The reference count is set to
> > 1 at allocation, the put will see the 1, decrement it and perform the
> > delete. The purpose for adding the reference count is for the python
> > code. Prior to this change the python code would clone evlists, but
> > this has issues if events are opened, etc. This change adds a
> > reference count for the evlists and a later change will add it to
> > evsels. The combination is needed for the python code to operate
> > correctly (not hit asserts in the evsel clone), but the changes are
> > broken apart for the sake of smaller patches.
>
> Can we keep the existing evlist__delete() code? IIUC most parts of the
> code don't need to worry about python. Then do we really need to
> convert them all? Maybe we can convert only the relevant codes.

Are you thinking of evlist__delete being something like:
```
void evlist__delete(struct evlist *evlist)
{
assert(refcount_read(evlist__refcnt(evlist)) == 1);
evlist__put(evlist);
}
```
This would allow the existing delete cases to remain and minimize
churn, but it creates an opportunity for a runtime assertion failure.
I'm not sure what the codebase gains long-term from this as having
both `put` and `delete` feels like an unnecessary choice for the user.

> Also I think it's better to split evlist__put() conversion and python
> code changes.

So the existing Python code embeds an evlist inside a Python object. I
don't see a way to add the reference count and keep the embedding
without inventing a 3rd way of handling Python evlists, which would be
as complex as just using the reference counts. We could remove the
embedded evlist and use evlist__new/delete, but the code wouldn't be
functional.

Thanks,
Ian

> Thanks,
> Namhyung
>
> >
> > Assisted-by: Gemini:gemini-3.1-pro-preview
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> > v2: Added evlist__put to pyrf_evlist__init in case init is called more
> > than once.
> >
> > I double-checked trace__replay() and confirmed that trace->evlist
> > is not assigned to session->evlist in that function.
> > trace__replay creates a new session and uses its own evlist for
> > processing file events, leaving trace->evlist pointing to the
> > empty list created at startup. Therefore, the
> > evlist__put(trace->evlist) call in trace__exit() is safe and
> > correct to avoid leaking that empty list.
> >
> > v7:
> > - Added pyrf_evlist__new to zero-initialize pevlist->evlist to fix
> > crash on re-initialization.