From 4429ad9276cea193bb29463a7d6c38367d0d78ce Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Sun, 28 Apr 2024 11:03:11 +1000 Subject: [PATCH] libzpool: set thread names Arrange for the thread/task name to be set when new threads are created. This makes them visible in the process table etc. pthread_setname_np() is generally available in glibc, musl and FreeBSD, so no test is required. Reviewed-by: Brian Behlendorf Signed-off-by: Rob Norris Sponsored-by: https://despairlabs.com/sponsor/ Closes #16140 --- include/sys/zfs_context.h | 8 ++++---- lib/libzpool/kernel.c | 5 ++++- lib/libzpool/taskq.c | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/sys/zfs_context.h b/include/sys/zfs_context.h index 9ec2f73b36..8f264b50e9 100644 --- a/include/sys/zfs_context.h +++ b/include/sys/zfs_context.h @@ -228,9 +228,9 @@ typedef pthread_t kthread_t; #define thread_create_named(name, stk, stksize, func, arg, len, \ pp, state, pri) \ - zk_thread_create(func, arg, stksize, state) + zk_thread_create(name, func, arg, stksize, state) #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \ - zk_thread_create(func, arg, stksize, state) + zk_thread_create(#func, func, arg, stksize, state) #define thread_exit() pthread_exit(NULL) #define thread_join(t) pthread_join((pthread_t)(t), NULL) @@ -246,8 +246,8 @@ extern struct proc p0; #define PS_NONE -1 -extern kthread_t *zk_thread_create(void (*func)(void *), void *arg, - size_t stksize, int state); +extern kthread_t *zk_thread_create(const char *name, void (*func)(void *), + void *arg, size_t stksize, int state); #define issig(why) (FALSE) #define ISSIG(thr, why) (FALSE) diff --git a/lib/libzpool/kernel.c b/lib/libzpool/kernel.c index ffad7fc02b..a3930ee07f 100644 --- a/lib/libzpool/kernel.c +++ b/lib/libzpool/kernel.c @@ -92,7 +92,8 @@ zk_thread_wrapper(void *arg) } kthread_t * -zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state) +zk_thread_create(const char *name, void (*func)(void *), void *arg, + size_t stksize, int state) { pthread_attr_t attr; pthread_t tid; @@ -140,6 +141,8 @@ zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state) VERIFY0(pthread_create(&tid, &attr, zk_thread_wrapper, ztw)); VERIFY0(pthread_attr_destroy(&attr)); + pthread_setname_np(tid, name); + return ((void *)(uintptr_t)tid); } diff --git a/lib/libzpool/taskq.c b/lib/libzpool/taskq.c index 99a181ec3c..5fb2283cf0 100644 --- a/lib/libzpool/taskq.c +++ b/lib/libzpool/taskq.c @@ -295,8 +295,8 @@ taskq_create(const char *name, int nthreads, pri_t pri, } for (t = 0; t < nthreads; t++) - VERIFY((tq->tq_threadlist[t] = thread_create(NULL, 0, - taskq_thread, tq, 0, &p0, TS_RUN, pri)) != NULL); + VERIFY((tq->tq_threadlist[t] = thread_create_named(tq->tq_name, + NULL, 0, taskq_thread, tq, 0, &p0, TS_RUN, pri)) != NULL); return (tq); }