Thread (22 messages) 22 messages, 3 authors, 2021-08-31
STALE1738d
Revisions (2)
  1. rfc [diff vs current]
  2. v3 current

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

From: Riccardo Mancini <hidden>
Date: 2021-08-20 10:54:56
Also in: lkml
Subsystem: performance events subsystem, the rest · Maintainers: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Linus Torvalds

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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help