[RFC PATCH v1 07/37] perf workqueue: add support for setting affinities to workers

From: Riccardo Mancini
Date: Sat Aug 21 2021 - 05:20:16 EST


This patch adds an interface to workqueue to set affinities to the
underlying threadpool threads.

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

diff --git a/tools/perf/util/workqueue/workqueue.c b/tools/perf/util/workqueue/workqueue.c
index 7daac65abb5d57d1..61f1b6c41deba031 100644
--- a/tools/perf/util/workqueue/workqueue.c
+++ b/tools/perf/util/workqueue/workqueue.c
@@ -50,6 +50,7 @@ static const char * const workqueue_errno_str[] = {
"Error executing function in threadpool",
"Error stopping the threadpool",
"Error starting thread in the threadpool",
+ "Error setting affinity in threadpool",
"Error sending message to worker",
"Error receiving message from worker",
"Received unexpected message from worker",
@@ -758,6 +759,26 @@ int flush_workqueue(struct workqueue_struct *wq)
return err;
}

+/**
+ * workqueue_set_affinities - set affinities to all threads in @wq->pool
+ */
+int workqueue_set_affinities(struct workqueue_struct *wq,
+ struct mmap_cpu_mask *affinities)
+{
+ wq->pool_errno = threadpool__set_affinities(wq->pool, affinities);
+ return wq->pool_errno ? -WORKQUEUE_ERROR__POOLAFFINITY : 0;
+}
+
+/**
+ * workqueue_set_affinities - set affinity to thread @tidx in @wq->pool
+ */
+int workqueue_set_affinity(struct workqueue_struct *wq, int tidx,
+ struct mmap_cpu_mask *affinity)
+{
+ wq->pool_errno = threadpool__set_affinity(wq->pool, tidx, affinity);
+ return wq->pool_errno ? -WORKQUEUE_ERROR__POOLAFFINITY : 0;
+}
+
/**
* init_work - initialize the @work struct
*/
diff --git a/tools/perf/util/workqueue/workqueue.h b/tools/perf/util/workqueue/workqueue.h
index 7a0eda923df25d85..dc6baee138b22ab2 100644
--- a/tools/perf/util/workqueue/workqueue.h
+++ b/tools/perf/util/workqueue/workqueue.h
@@ -28,6 +28,11 @@ extern int queue_work_on_worker(int tidx, struct workqueue_struct *wq, struct wo

extern int flush_workqueue(struct workqueue_struct *wq);

+extern int workqueue_set_affinities(struct workqueue_struct *wq,
+ struct mmap_cpu_mask *affinities);
+extern int workqueue_set_affinity(struct workqueue_struct *wq, int tidx,
+ struct mmap_cpu_mask *affinity);
+
extern void init_work(struct work_struct *work);

/* parallel_for utility */
@@ -92,6 +97,7 @@ enum {
WORKQUEUE_ERROR__POOLEXE,
WORKQUEUE_ERROR__POOLSTOP,
WORKQUEUE_ERROR__POOLSTARTTHREAD,
+ WORKQUEUE_ERROR__POOLAFFINITY,
WORKQUEUE_ERROR__WRITEPIPE,
WORKQUEUE_ERROR__READPIPE,
WORKQUEUE_ERROR__INVALIDMSG,
--
2.31.1