LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Riccardo Mancini <rickyman7@gmail.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Ian Rogers <irogers@google.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>, Jiri Olsa <jolsa@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>,
	Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v3 07/15] perf workqueue: implement worker thread and management
Date: Fri, 20 Aug 2021 12:53:53 +0200	[thread overview]
Message-ID: <e88e4e61309845043d1f0939b5c33cd60e9d3ed5.1629454773.git.rickyman7@gmail.com> (raw)
In-Reply-To: <cover.1629454773.git.rickyman7@gmail.com>

This patch adds the implementation of the worker thread that is executed
in the threadpool, and all management-related functions.

At startup, a worker registers itself with the workqueue by adding itself
to the idle_list, then it sends an ack back to the main thread. When
creating wotkers, the main thread will wait for the related acks.
Once there is work to do, threads are woken up to perform the work.
Threads will try to dequeue a new pending work before going to sleep.

This registering mechanism has been implemented to enable for lazy spin
up of worker threads in following patches

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/workqueue/workqueue.c | 376 +++++++++++++++++++++++++-
 1 file changed, 374 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/workqueue/workqueue.c b/tools/perf/util/workqueue/workqueue.c
index 053aac43e038f0b7..a2747fcc004ab0d1 100644
--- a/tools/perf/util/workqueue/workqueue.c
+++ b/tools/perf/util/workqueue/workqueue.c
@@ -13,6 +13,21 @@
 #include <internal/lib.h>
 #include "workqueue.h"
 
+enum worker_msg {
+	WORKER_MSG__UNDEFINED,
+	WORKER_MSG__READY,                          /* from worker: ack */
+	WORKER_MSG__WAKE,                           /* to worker: wake up */
+	WORKER_MSG__STOP,                           /* to worker: exit */
+	WORKER_MSG__ERROR,
+	WORKER_MSG__MAX
+};
+
+enum worker_status {
+	WORKER_STATUS__IDLE,		/* worker is sleeping, waiting for signal */
+	WORKER_STATUS__BUSY,		/* worker is executing */
+	WORKER_STATUS__MAX
+};
+
 struct workqueue_struct {
 	pthread_mutex_t		lock;		/* locking of the workqueue */
 	pthread_cond_t		idle_cond;	/* all workers are idle cond */
@@ -22,6 +37,7 @@ struct workqueue_struct {
 	struct list_head	busy_list;	/* busy workers */
 	struct list_head	idle_list;	/* idle workers */
 	int			msg_pipe[2];	/* main thread comm pipes */
+	struct worker		**workers;	/* array of all workers */
 };
 
 static const char * const workqueue_errno_str[] = {
@@ -34,6 +50,310 @@ static const char * const workqueue_errno_str[] = {
 	"Received unexpected message from worker",
 };
 
+struct worker {
+	pthread_mutex_t			lock;		/* locking of the thread_pool */
+	int				tidx;		/* idx of thread in pool */
+	struct list_head		entry;		/* in idle or busy list */
+	struct work_struct		*current_work;	/* work being processed */
+	int				msg_pipe[2];	/* main thread comm pipes*/
+	struct list_head		queue;		/* pending work items */
+	enum worker_status		status;		/* worker status */
+};
+
+#define for_each_busy_worker(wq, m_worker) \
+	list_for_each_entry(m_worker, &wq->busy_list, entry)
+
+#define for_each_idle_worker(wq, m_worker) \
+	list_for_each_entry(m_worker, &wq->idle_list, entry)
+
+static inline int lock_workqueue(struct workqueue_struct *wq)
+__acquires(&wq->lock)
+{
+	__acquire(&wq->lock);
+	return pthread_mutex_lock(&wq->lock);
+}
+
+static inline int unlock_workqueue(struct workqueue_struct *wq)
+__releases(&wq->lock)
+{
+	__release(&wq->lock);
+	return pthread_mutex_unlock(&wq->lock);
+}
+
+static inline int lock_worker(struct worker *worker)
+__acquires(&worker->lock)
+{
+	__acquire(&worker->lock);
+	return pthread_mutex_lock(&worker->lock);
+}
+
+static inline int unlock_worker(struct worker *worker)
+__releases(&worker->lock)
+{
+	__release(&worker->lock);
+	return pthread_mutex_unlock(&worker->lock);
+}
+
+/**
+ * available_work - check if worker @worker has work to do
+ */
+static int available_work(struct worker *worker)
+__must_hold(&worker->lock)
+{
+	return !list_empty(&worker->queue);
+}
+
+/**
+ * dequeue_work - retrieve the next work in worker @worker's queue
+ *
+ * Called inside worker.
+ */
+static struct work_struct *dequeue_work(struct worker *worker)
+__must_hold(&worker->lock)
+{
+	struct work_struct *work = list_first_entry(&worker->queue, struct work_struct, entry);
+
+	list_del_init(&work->entry);
+	return work;
+}
+
+/**
+ * spinup_worker - start worker underlying thread and wait for it
+ *
+ * This function MUST NOT hold any lock and can be called only from main thread.
+ */
+static int spinup_worker(struct workqueue_struct *wq, int tidx)
+{
+	int ret;
+	enum worker_msg msg = WORKER_MSG__UNDEFINED;
+	char sbuf[STRERR_BUFSIZE];
+
+	wq->pool_errno = threadpool__start_thread(wq->pool, tidx);
+	if (wq->pool_errno)
+		return -WORKQUEUE_ERROR__POOLSTARTTHREAD;
+
+	ret = readn(wq->msg_pipe[0], &msg, sizeof(msg));
+	if (ret < 0) {
+		pr_debug("workqueue: error receiving ack: %s\n",
+			str_error_r(errno, sbuf, sizeof(sbuf)));
+		return -WORKQUEUE_ERROR__READPIPE;
+	}
+	if (msg != WORKER_MSG__READY) {
+		pr_debug2("workqueue: received error\n");
+		return -WORKQUEUE_ERROR__INVALIDMSG;
+	}
+
+	pr_debug("workqueue: spinup worker %d\n", tidx);
+
+	return 0;
+}
+
+/**
+ * sleep_worker - worker @worker of workqueue @wq goes to sleep
+ *
+ * Called inside worker.
+ * If this was the last idle thread, signal it to the main thread, in case it
+ * was flushing the workqueue.
+ */
+static void sleep_worker(struct workqueue_struct *wq, struct worker *worker)
+__must_hold(&wq->lock)
+{
+	worker->status = WORKER_STATUS__IDLE;
+	list_move(&worker->entry, &wq->idle_list);
+	if (list_empty(&wq->busy_list))
+		pthread_cond_signal(&wq->idle_cond);
+}
+
+/**
+ * dequeue_or_sleep - check if work is available and dequeue or go to sleep
+ *
+ * Called inside worker.
+ */
+static void dequeue_or_sleep(struct worker *worker, struct workqueue_struct *wq)
+__must_hold(&worker->lock)
+{
+	if (available_work(worker)) {
+		worker->current_work = dequeue_work(worker);
+		pr_debug2("worker[%d]: dequeued work\n", worker->tidx);
+	} else {
+		unlock_worker(worker);
+
+		lock_workqueue(wq);
+		lock_worker(worker);
+
+		// Check if I've been assigned new work in the
+		// meantime
+		if (available_work(worker)) {
+			// yep, no need to sleep
+			worker->current_work = dequeue_work(worker);
+		} else {
+			// nope, I gotta sleep
+			worker->current_work = NULL;
+			sleep_worker(wq, worker);
+			pr_debug2("worker[%d]: going to sleep\n", worker->tidx);
+		}
+		unlock_workqueue(wq);
+	}
+}
+
+
+/**
+ * stop_worker - stop worker @worker
+ *
+ * Called from main thread.
+ * Send stop message to worker @worker.
+ */
+static int stop_worker(struct worker *worker)
+{
+	int ret;
+	enum worker_msg msg;
+	char sbuf[STRERR_BUFSIZE];
+
+	msg = WORKER_MSG__STOP;
+	ret = writen(worker->msg_pipe[1], &msg, sizeof(msg));
+	if (ret < 0) {
+		pr_debug2("workqueue: error sending stop msg: %s\n",
+			str_error_r(errno, sbuf, sizeof(sbuf)));
+		return -WORKQUEUE_ERROR__WRITEPIPE;
+	}
+
+	return 0;
+}
+
+/**
+ * init_worker - init @worker struct
+ * @worker: the struct to init
+ * @tidx: index of the executing thread inside the threadpool
+ */
+static int init_worker(struct worker *worker, int tidx)
+{
+	int ret;
+	char sbuf[STRERR_BUFSIZE];
+
+	if (pipe(worker->msg_pipe)) {
+		pr_debug2("worker[%d]: error opening pipe: %s\n",
+			tidx, str_error_r(errno, sbuf, sizeof(sbuf)));
+		return -ENOMEM;
+	}
+
+	worker->tidx = tidx;
+	worker->current_work = NULL;
+	worker->status = WORKER_STATUS__IDLE;
+	INIT_LIST_HEAD(&worker->entry);
+	INIT_LIST_HEAD(&worker->queue);
+
+	ret = pthread_mutex_init(&worker->lock, NULL);
+	if (ret)
+		return -ret;
+
+	return 0;
+}
+
+/**
+ * fini_worker - deallocate resources used by @worker struct
+ */
+static void fini_worker(struct worker *worker)
+{
+	close(worker->msg_pipe[0]);
+	worker->msg_pipe[0] = -1;
+	close(worker->msg_pipe[1]);
+	worker->msg_pipe[1] = -1;
+	pthread_mutex_destroy(&worker->lock);
+}
+
+/**
+ * register_worker - add worker to @wq->idle_list
+ */
+static void register_worker(struct workqueue_struct *wq, struct worker *worker)
+__must_hold(&wq->lock)
+{
+	list_move(&worker->entry, &wq->idle_list);
+	wq->workers[worker->tidx] = worker;
+}
+
+/**
+ * unregister_worker - remove worker from @wq->idle_list
+ */
+static void unregister_worker(struct workqueue_struct *wq __maybe_unused,
+			struct worker *worker)
+__must_hold(&wq->lock)
+{
+	list_del_init(&worker->entry);
+	wq->workers[worker->tidx] = NULL;
+}
+
+/**
+ * worker_thread - worker function executed on threadpool
+ */
+static void worker_thread(int tidx, struct task_struct *task)
+{
+	struct workqueue_struct *wq = container_of(task, struct workqueue_struct, task);
+	char sbuf[STRERR_BUFSIZE];
+	struct worker this_worker;
+	enum worker_msg msg;
+	int ret, init_err = init_worker(&this_worker, tidx);
+
+	if (init_err) {
+		// send error message to main thread
+		msg = WORKER_MSG__ERROR;
+	} else {
+		lock_workqueue(wq);
+		register_worker(wq, &this_worker);
+		unlock_workqueue(wq);
+
+		// ack worker creation
+		msg = WORKER_MSG__READY;
+	}
+
+	ret = writen(wq->msg_pipe[1], &msg, sizeof(msg));
+	if (ret < 0) {
+		pr_debug("worker[%d]: error sending msg: %s\n",
+			tidx, str_error_r(errno, sbuf, sizeof(sbuf)));
+
+		if (init_err)
+			return;
+		goto out;
+	}
+
+	// stop if there have been errors in init
+	if (init_err)
+		return;
+
+	for (;;) {
+		msg = WORKER_MSG__UNDEFINED;
+		ret = readn(this_worker.msg_pipe[0], &msg, sizeof(msg));
+		if (ret < 0 || (msg != WORKER_MSG__WAKE && msg != WORKER_MSG__STOP)) {
+			pr_debug("worker[%d]: error receiving msg: %s\n",
+				tidx, str_error_r(errno, sbuf, sizeof(sbuf)));
+			break;
+		}
+
+		if (msg == WORKER_MSG__STOP)
+			break;
+
+		// main thread takes care of moving to busy list and appending
+		// work to list
+
+		for (;;) {
+			lock_worker(&this_worker);
+			dequeue_or_sleep(&this_worker, wq);
+			unlock_worker(&this_worker);
+
+			if (!this_worker.current_work)
+				break;
+
+			this_worker.current_work->func(this_worker.current_work);
+		};
+	}
+
+out:
+	lock_workqueue(wq);
+	unregister_worker(wq, &this_worker);
+	unlock_workqueue(wq);
+
+	fini_worker(&this_worker);
+}
+
 /**
  * create_workqueue - create a workqueue associated to @pool
  *
@@ -41,7 +361,8 @@ static const char * const workqueue_errno_str[] = {
  */
 struct workqueue_struct *create_workqueue(int nr_threads)
 {
-	int ret, err = 0;
+	int ret, err = 0, t;
+	struct worker *worker;
 	struct workqueue_struct *wq = zalloc(sizeof(struct workqueue_struct));
 
 	if (!wq) {
@@ -56,10 +377,16 @@ struct workqueue_struct *create_workqueue(int nr_threads)
 		goto out_free_wq;
 	}
 
+	wq->workers = calloc(nr_threads, sizeof(*wq->workers));
+	if (!wq->workers) {
+		err = -ENOMEM;
+		goto out_delete_pool;
+	}
+
 	ret = pthread_mutex_init(&wq->lock, NULL);
 	if (ret) {
 		err = -ret;
-		goto out_delete_pool;
+		goto out_free_workers;
 	}
 
 	ret = pthread_cond_init(&wq->idle_cond, NULL);
@@ -77,12 +404,41 @@ struct workqueue_struct *create_workqueue(int nr_threads)
 		goto out_destroy_cond;
 	}
 
+	wq->task.fn = worker_thread;
+
+	wq->pool_errno = threadpool__execute(wq->pool, &wq->task);
+	if (wq->pool_errno) {
+		err = -WORKQUEUE_ERROR__POOLEXE;
+		goto out_close_pipe;
+	}
+
+	for (t = 0; t < nr_threads; t++) {
+		err = spinup_worker(wq, t);
+		if (err)
+			goto out_stop_pool;
+	}
+
 	return wq;
 
+out_stop_pool:
+	lock_workqueue(wq);
+	for_each_idle_worker(wq, worker) {
+		ret = stop_worker(worker);
+		if (ret)
+			err = ret;
+	}
+	unlock_workqueue(wq);
+out_close_pipe:
+	close(wq->msg_pipe[0]);
+	wq->msg_pipe[0] = -1;
+	close(wq->msg_pipe[1]);
+	wq->msg_pipe[1] = -1;
 out_destroy_cond:
 	pthread_cond_destroy(&wq->idle_cond);
 out_destroy_mutex:
 	pthread_mutex_destroy(&wq->lock);
+out_free_workers:
+	free(wq->workers);
 out_delete_pool:
 	threadpool__delete(wq->pool);
 out_free_wq:
@@ -96,12 +452,27 @@ struct workqueue_struct *create_workqueue(int nr_threads)
  */
 int destroy_workqueue(struct workqueue_struct *wq)
 {
+	struct worker *worker;
 	int err = 0, ret;
 	char sbuf[STRERR_BUFSIZE];
 
 	if (IS_ERR_OR_NULL(wq))
 		return 0;
 
+	lock_workqueue(wq);
+	for_each_idle_worker(wq, worker) {
+		ret = stop_worker(worker);
+		if (ret)
+			err = ret;
+	}
+	unlock_workqueue(wq);
+
+	wq->pool_errno = threadpool__stop(wq->pool);
+	if (wq->pool_errno) {
+		pr_debug2("workqueue: error stopping threadpool\n");
+		err = -WORKQUEUE_ERROR__POOLSTOP;
+	}
+
 	threadpool__delete(wq->pool);
 	wq->pool = NULL;
 
@@ -125,6 +496,7 @@ int destroy_workqueue(struct workqueue_struct *wq)
 	close(wq->msg_pipe[1]);
 	wq->msg_pipe[1] = -1;
 
+	zfree(&wq->workers);
 	free(wq);
 	return err;
 }
-- 
2.31.1


  parent reply	other threads:[~2021-08-20 10:54 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-20 10:53 [RFC PATCH v3 00/15] perf: add workqueue library and use it in synthetic-events Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 01/15] perf workqueue: threadpool creation and destruction Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 02/15] perf tests: add test for workqueue Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 03/15] perf workqueue: add threadpool start and stop functions Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 04/15] perf workqueue: add threadpool execute and wait functions Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 05/15] tools: add sparse context/locking annotations in compiler-types.h Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 06/15] perf workqueue: introduce workqueue struct Riccardo Mancini
2021-08-24 19:27   ` Namhyung Kim
2021-08-31 16:13     ` Riccardo Mancini
2021-08-20 10:53 ` Riccardo Mancini [this message]
2021-08-30  7:22   ` [RFC PATCH v3 07/15] perf workqueue: implement worker thread and management Jiri Olsa
2021-08-20 10:53 ` [RFC PATCH v3 08/15] perf workqueue: add queue_work and flush_workqueue functions Riccardo Mancini
2021-08-24 19:40   ` Namhyung Kim
2021-08-31 16:23     ` Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 09/15] perf workqueue: spinup threads when needed Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 10/15] perf workqueue: create global workqueue Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 11/15] perf workqueue: add utility to execute a for loop in parallel Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 12/15] perf record: setup global workqueue Riccardo Mancini
2021-08-20 10:53 ` [RFC PATCH v3 13/15] perf top: " Riccardo Mancini
2021-08-20 10:54 ` [RFC PATCH v3 14/15] perf test/synthesis: " Riccardo Mancini
2021-08-20 10:54 ` [RFC PATCH v3 15/15] perf synthetic-events: use workqueue parallel_for Riccardo Mancini
2021-08-29 21:59 ` [RFC PATCH v3 00/15] perf: add workqueue library and use it in synthetic-events Jiri Olsa
2021-08-31 15:46   ` Jiri Olsa
2021-08-31 16:57     ` 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=e88e4e61309845043d1f0939b5c33cd60e9d3ed5.1629454773.git.rickyman7@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 \
    --subject='Re: [RFC PATCH v3 07/15] perf workqueue: implement worker thread and management' \
    /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).