linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Riccardo Mancini <rickyman7@gmail.com>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>, Jiri Olsa <jolsa@redhat.com>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	linux-perf-users <linux-perf-users@vger.kernel.org>,
	Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
Subject: Re: [RFC PATCH v2 03/10] perf workqueue: add threadpool start and stop functions
Date: Mon, 09 Aug 2021 12:35:16 +0200	[thread overview]
Message-ID: <cf825a794602de80fb0d7fc006ccbdf7c29f2644.camel@gmail.com> (raw)
In-Reply-To: <CAM9d7cg-AF4u=MPJwBs5QSbT_RgEFXYyum97yu3gy0+2w16d6g@mail.gmail.com>

Hi,

On Fri, 2021-08-06 at 19:43 -0700, Namhyung Kim wrote:
> On Fri, Jul 30, 2021 at 8:34 AM Riccardo Mancini <rickyman7@gmail.com> wrote:
> > 
> > This patch adds the start and stop functions, alongside the thread
> > function.
> > Each thread will run until a stop signal is received.
> > Furthermore, start and stop are added to the test.
> > 
> > Thread management is based on the prototype from Alexey:
> > https://lore.kernel.org/lkml/cover.1625227739.git.alexey.v.bayduraev@linux.intel.com/
> > 
> > Suggested-by: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
> > Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> > ---
> 
> [SNIP]
> > @@ -93,6 +134,130 @@ static void threadpool_entry__close_pipes(struct
> > threadpool_entry *thread)
> >         }
> >  }
> > 
> > +/**
> > + * threadpool__wait_thread - receive ack from thread
> > + *
> > + * NB: call only from main thread!
> > + */
> > +static int threadpool__wait_thread(struct threadpool_entry *thread)
> 
> If you wanted to differentiate APIs for main thread, I think it's better
> to pass the pool struct (according to the name) and the index like:
> 
>   int threadpool__wait_thread(struct threadpool *pool, int idx)
> 
> Then it can get a pointer to the entry easily.

Agree. 
I also need to more clearly separate those APIs too.

> 
> > +{
> > +       int res;
> > +       enum threadpool_msg msg = THREADPOOL_MSG__UNDEFINED;
> > +
> > +       res = readn(thread->pipes.ack[0], &msg, sizeof(msg));
> > +       if (res < 0) {
> > +               pr_debug2("threadpool: failed to recv msg from tid=%d:
> > %s\n",
> > +                      thread->tid, strerror(errno));
> > +               return -THREADPOOL_ERROR__READPIPE;
> > +       }
> > +       if (msg != THREADPOOL_MSG__ACK) {
> > +               pr_debug2("threadpool: received unexpected msg from tid=%d:
> > %s\n",
> > +                      thread->tid, threadpool_msg_tags[msg]);
> > +               return -THREADPOOL_ERROR__INVALIDMSG;
> > +       }
> > +
> > +       pr_debug2("threadpool: received ack from tid=%d\n", thread->tid);
> > +
> > +       return 0;
> > +}
> > +
> > +/**
> > + * threadpool__terminate_thread - send stop signal to thread and wait for
> > ack
> > + *
> > + * NB: call only from main thread!
> > + */
> > +static int threadpool__terminate_thread(struct threadpool_entry *thread)
> 
> Ditto.

ack

> 
> > +{
> > +       int res;
> > +       enum threadpool_msg msg = THREADPOOL_MSG__STOP;
> > +
> > +       res = writen(thread->pipes.cmd[1], &msg, sizeof(msg));
> > +       if (res < 0) {
> > +               pr_debug2("threadpool: error sending stop msg to tid=%d:
> > %s\n",
> > +                       thread->tid, strerror(errno));
> > +               return -THREADPOOL_ERROR__WRITEPIPE;
> > +       }
> > +
> > +       return threadpool__wait_thread(thread);
> > +}
> > +
> 
> [SNIP]
> > @@ -161,12 +326,30 @@ struct threadpool *threadpool__new(int n_threads)
> >   *
> >   * Buffer size should be at least THREADPOOL_STRERR_BUFSIZE bytes.
> >   */
> > -int threadpool__strerror(struct threadpool *pool __maybe_unused, int err,
> > char *buf, size_t size)
> > +int threadpool__strerror(struct threadpool *pool, int err, char *buf,
> > size_t size)
> >  {
> >         char sbuf[STRERR_BUFSIZE], *emsg;
> > +       const char *status_str, *errno_str;
> > 
> > -       emsg = str_error_r(err, sbuf, sizeof(sbuf));
> > -       return scnprintf(buf, size, "Error: %s.\n", emsg);
> > +       status_str = IS_ERR_OR_NULL(pool) ? "error" :
> > threadpool_status_tags[pool->status];
> > +
> > +       switch (err) {
> > +       case -THREADPOOL_ERROR__SIGPROCMASK:
> > +       case -THREADPOOL_ERROR__READPIPE:
> > +       case -THREADPOOL_ERROR__WRITEPIPE:
> > +               emsg = str_error_r(errno, sbuf, sizeof(sbuf));
> > +               errno_str = threadpool_errno_str[-err-
> > THREADPOOL_ERROR__OFFSET];
> > +               return scnprintf(buf, size, "%s: %s.\n", errno_str, emsg);
> > +       case -THREADPOOL_ERROR__INVALIDMSG:
> > +               errno_str = threadpool_errno_str[-err-
> > THREADPOOL_ERROR__OFFSET];
> > +               return scnprintf(buf, size, "%s.\n", errno_str);
> > +       case -THREADPOOL_ERROR__NOTALLOWED:
> > +               return scnprintf(buf, size, "%s (%s).\n",
> > +                       threadpool_errno_str[-err], status_str);
> 
> s/-err/-err-THREADPOOL_ERROR__OFFSET/ ?
> 
> It'd be nice if you calculate the index once.

agreed

> 
> > +       default:
> > +               emsg = str_error_r(err, sbuf, sizeof(sbuf));
> 
> I'm confused whether the 'err' is negative or positive?

It's negative, so it needs to be inverted in this case.

Thanks,
Riccardo

> 
> Thanks,
> Namhyung
> 
> 
> > +               return scnprintf(buf, size, "Error: %s", emsg);
> > +       }
> >  }
> > 



  reply	other threads:[~2021-08-09 10:35 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1627657061.git.rickyman7@gmail.com>
2021-07-30 15:34 ` [RFC PATCH v2 01/10] perf workqueue: threadpool creation and destruction Riccardo Mancini
2021-08-07  2:24   ` Namhyung Kim
2021-08-09 10:30     ` Riccardo Mancini
2021-08-10 18:54       ` Namhyung Kim
2021-08-10 20:24         ` Arnaldo Carvalho de Melo
2021-08-11 17:55           ` Riccardo Mancini
2021-07-30 15:34 ` [RFC PATCH v2 02/10] perf tests: add test for workqueue Riccardo Mancini
2021-07-30 15:34 ` [RFC PATCH v2 03/10] perf workqueue: add threadpool start and stop functions Riccardo Mancini
2021-08-07  2:43   ` Namhyung Kim
2021-08-09 10:35     ` Riccardo Mancini [this message]
2021-07-30 15:34 ` [RFC PATCH v2 04/10] perf workqueue: add threadpool execute and wait functions Riccardo Mancini
2021-08-07  2:56   ` Namhyung Kim
2021-07-30 15:34 ` [RFC PATCH v2 05/10] tools: add sparse context/locking annotations in compiler-types.h Riccardo Mancini
2021-07-30 15:34 ` [RFC PATCH v2 06/10] perf workqueue: introduce workqueue struct Riccardo Mancini
2021-08-09 12:04   ` Jiri Olsa
2021-07-30 15:34 ` [RFC PATCH v2 07/10] perf workqueue: implement worker thread and management Riccardo Mancini
2021-07-30 15:34 ` [RFC PATCH v2 08/10] perf workqueue: add queue_work and flush_workqueue functions Riccardo Mancini
2021-07-30 15:34 ` [RFC PATCH v2 09/10] perf workqueue: add utility to execute a for loop in parallel Riccardo Mancini
2021-07-30 15:34 ` [RFC PATCH v2 10/10] perf synthetic-events: use workqueue parallel_for Riccardo Mancini
2021-08-09 12:04   ` Jiri Olsa
2021-08-09 13:24     ` Riccardo Mancini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cf825a794602de80fb0d7fc006ccbdf7c29f2644.camel@gmail.com \
    --to=rickyman7@gmail.com \
    --cc=acme@kernel.org \
    --cc=alexey.v.bayduraev@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).