[RFC PATCH v3 10/15] perf workqueue: create global workqueue

From: Riccardo Mancini
Date: Fri Aug 20 2021 - 06:54:56 EST


This patch adds a global static workqueue, using the same API from the
kernel (schedule_work, flush_scheduled_work, and so on).

Before use, the global workqueue should be set up using
setup_global_workqueue and eventually destroyed using
teardown_global_workqueue.

Signed-off-by: Riccardo Mancini <rickyman7@xxxxxxxxx>
---
tools/perf/util/workqueue/workqueue.c | 2 ++
tools/perf/util/workqueue/workqueue.h | 49 +++++++++++++++++++++++++++
2 files changed, 51 insertions(+)

diff --git a/tools/perf/util/workqueue/workqueue.c b/tools/perf/util/workqueue/workqueue.c
index 305a9cda39810b84..a89370e68bd720c8 100644
--- a/tools/perf/util/workqueue/workqueue.c
+++ b/tools/perf/util/workqueue/workqueue.c
@@ -13,6 +13,8 @@
#include <internal/lib.h>
#include "workqueue.h"

+struct workqueue_struct *global_wq;
+
enum worker_msg {
WORKER_MSG__UNDEFINED,
WORKER_MSG__READY, /* from worker: ack */
diff --git a/tools/perf/util/workqueue/workqueue.h b/tools/perf/util/workqueue/workqueue.h
index 37ef84fc9c6ed4b6..fc6166757f0e1d0d 100644
--- a/tools/perf/util/workqueue/workqueue.h
+++ b/tools/perf/util/workqueue/workqueue.h
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <sys/types.h>
#include <linux/list.h>
+#include <linux/err.h>
#include "threadpool.h"

struct work_struct;
@@ -29,6 +30,54 @@ extern int flush_workqueue(struct workqueue_struct *wq);

extern void init_work(struct work_struct *work);

+/* Global workqueue */
+
+extern struct workqueue_struct *global_wq;
+
+/**
+ * setup_global_wq - create the global_wq
+ */
+static inline int setup_global_workqueue(int nr_threads)
+{
+ global_wq = create_workqueue(nr_threads);
+ return IS_ERR(global_wq) ? PTR_ERR(global_wq) : 0;
+}
+
+/**
+ * teardown_global_wq - destroy the global_wq
+ */
+static inline int teardown_global_workqueue(void)
+{
+ int ret = destroy_workqueue(global_wq);
+
+ global_wq = NULL;
+ return ret;
+}
+
+/**
+ * schedule_work - queue @work on the global_wq
+ */
+static inline int schedule_work(struct work_struct *work)
+{
+ return queue_work(global_wq, work);
+}
+
+/**
+ * schedule_work - queue @work on thread @tidx of global_wq
+ */
+static inline int schedule_work_on_worker(int tidx, struct work_struct *work)
+{
+ return queue_work_on_worker(tidx, global_wq, work);
+}
+
+/**
+ * flush_scheduled_work - ensure that any scheduled work in global_wq has run to completion
+ */
+static inline int flush_scheduled_work(void)
+{
+ return flush_workqueue(global_wq);
+}
+
#define WORKQUEUE_STRERR_BUFSIZE (128+THREADPOOL_STRERR_BUFSIZE)
#define WORKQUEUE_ERROR__OFFSET 512
enum {
--
2.31.1