Re: [RFC PATCH 01/10] perf workqueue: threadpool creation and destruction

From: Namhyung Kim
Date: Mon Jul 19 2021 - 16:18:21 EST


On Fri, Jul 16, 2021 at 6:36 AM Riccardo Mancini <rickyman7@xxxxxxxxx> wrote:
>
> Hi Namhyung,
> thanks for the review.
>
> On Thu, 2021-07-15 at 16:29 -0700, Namhyung Kim wrote:
> > Hi Riccardo and Arnaldo,
> >
> > On Wed, Jul 14, 2021 at 7:16 AM Arnaldo Carvalho de Melo
> > <acme@xxxxxxxxxx> wrote:
> > >
> > > Em Tue, Jul 13, 2021 at 02:11:12PM +0200, Riccardo Mancini escreveu:
> > > > The workqueue library is made up by two components:
> > > > - threadpool: handles the lifetime of the threads
> > > > - workqueue: handles work distribution among the threads
> > > >
> > > > This first patch introduces the threadpool, starting from its creation
> > > > and destruction functions.
> > > > Thread management is based on the prototype from Alexey:
> > > > https://lore.kernel.org/lkml/cover.1625227739.git.alexey.v.bayduraev@xxxxxxxxxxxxxxx/
> > > >
> > > > Each thread in the threadpool executes the same function (aka task)
> > > > with a different argument tidx.
> > > > Threads use a pair of pipes to communicate with the main process.
> > > > The threadpool is static (all threads will be spawned at the same time).
> > > > Future work could include making it resizable and adding affinity support
> > > > (as in Alexey prototype).
> > > >
> > > > Suggested-by: Alexey Bayduraev <alexey.v.bayduraev@xxxxxxxxxxxxxxx>
> > > > Signed-off-by: Riccardo Mancini <rickyman7@xxxxxxxxx>
> > > > ---
> > > > tools/perf/util/Build | 1 +
> > > > tools/perf/util/workqueue/Build | 1 +
> > > > tools/perf/util/workqueue/threadpool.c | 175 +++++++++++++++++++++++++
> > > > tools/perf/util/workqueue/threadpool.h | 19 +++
> > > > 4 files changed, 196 insertions(+)
> > > > create mode 100644 tools/perf/util/workqueue/Build
> > > > create mode 100644 tools/perf/util/workqueue/threadpool.c
> > > > create mode 100644 tools/perf/util/workqueue/threadpool.h
> <SNIP>
> > > > +
> > > > +struct threadpool_struct {
> > >
> > > Can this be just 'struct threadpool'? I think its descriptive enough:
> > >
> > > > + int nr_threads; /* number of threads in the
> > > > pool */
> > > > + struct thread_struct *threads; /* array of threads in the
> > > > pool */
> > > > + struct task_struct *current_task; /* current executing
> > > > function */
> >
> > Does this mean it can only have a single function to run?
>
> Yes.
>
> > Why do we need it?
>
> My idea is to separate the workqueue from the actual implementation of the
> threads. This way, when the function executing on the threadpool ends, the
> threads are kept alive to execute new work.
> By adding this additional layer of abstraction, we can achieve more flexibility.
> For example, the use-case I have in mind is to recycle the same threadpool for
> both Alexey's threaded trace and the workqueue.
> I don't think this could be easily achieved with just the workqueue since the
> perf-record threads are not just a task that needs to be executed by they have
> specific affinities to be respected.
>
> What are your thoughts?

I'm fine with the separation of work(queue) and thread-pool.

I thought the backing thread-pool is general and can handle
multiple works at the same time.

The work queue should keep track of works it submitted
and their status. We can have multiple workqueues
sharing a single thread pool.


>
> >
> >
> > > > + enum threadpool_status status; /* current status of the
> > > > pool */
> > > > +};
> > > > +
> > > > +struct thread_struct {
> > > > + int idx; /* idx of thread in pool-
> > > > >threads */
> > > > + pid_t tid; /* tid of thread */
> > > > + struct threadpool_struct *pool; /* parent threadpool */
> > > > + struct {
> > > > + int from[2]; /* messages from thread
> > > > (acks) */
> > > > + int to[2]; /* messages to thread
> > > > (commands) */
> >
> > It can be confusing if you think from the main thread.
> > Maybe 'ack' and 'cmd' would be better.
>
> Agreed.
>
> >
> >
> > > > + } pipes;
> > > > +};
> > >
> > > This one, since we have already a 'struct thread' in tools/perf, to
> > > represent a PERF_RECORD_FORK, perhaps we can call it 'struct
> > > threadpool_entry'?
> >
> > I think we can even use 'worker' instead of 'thread' but it requires
> > huge renaming and conflicts so I won't insist on it strongly. :)
>
> Also, worker internally conflicts with the workqueue's worker, which runs on a
> (threadpool-)thread.
> Another name I had in mind is pool_thread to prevent having too many 'thread' in
> the name, but it might be confusing.
> I think threadpool_entry is fine.
>
> I have another question.
> In general, when should I use zfree instead of free?

I think the zfree is generally preferable to free.
Especially if the pointer can be accessed after free.

Thanks,
Namhyung