LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Riccardo Mancini <rickyman7@gmail.com>
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: Fri, 6 Aug 2021 19:43:09 -0700	[thread overview]
Message-ID: <CAM9d7cg-AF4u=MPJwBs5QSbT_RgEFXYyum97yu3gy0+2w16d6g@mail.gmail.com> (raw)
In-Reply-To: <6b18cd4441733f487b45bdf1e6f507b45be3bfbf.1627657061.git.rickyman7@gmail.com>

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.

> +{
> +       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.

> +{
> +       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.

> +       default:
> +               emsg = str_error_r(err, sbuf, sizeof(sbuf));

I'm confused whether the 'err' is negative or positive?

Thanks,
Namhyung


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

  reply	other threads:[~2021-08-07  2:43 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 [this message]
2021-08-09 10:35     ` Riccardo Mancini
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='CAM9d7cg-AF4u=MPJwBs5QSbT_RgEFXYyum97yu3gy0+2w16d6g@mail.gmail.com' \
    --to=namhyung@kernel.org \
    --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=peterz@infradead.org \
    --cc=rickyman7@gmail.com \
    --subject='Re: [RFC PATCH v2 03/10] perf workqueue: add threadpool start and stop functions' \
    /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

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).