From a90aa4fd2637af482451d20b301e86081ff74297 Mon Sep 17 00:00:00 2001 From: Shiqing Gao Date: Fri, 27 Oct 2023 18:28:54 +0800 Subject: [PATCH] dm: iothread: rename the thread for better readability This patch renames the iothread for better readability. For instance, the new name of the iothread for virtio-blk device looks like `iothr-0-blk9:0`. It could be helpful when tuning the performance and the CPU utilization. v1 -> v2: * add `const` qualifier for the input parameter of `iothread_create` Tracked-On: #8612 Signed-off-by: Shiqing Gao Acked-by: Wang, Yu1 --- devicemodel/core/iothread.c | 15 +++++++++------ devicemodel/hw/pci/virtio/virtio_block.c | 7 ++++++- devicemodel/include/iothread.h | 11 ++++++++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/devicemodel/core/iothread.c b/devicemodel/core/iothread.c index 5bc72096a..fb43f1aa8 100644 --- a/devicemodel/core/iothread.c +++ b/devicemodel/core/iothread.c @@ -63,7 +63,6 @@ io_thread(void *arg) static int iothread_start(struct iothread_ctx *ioctx_x) { - char tname[MAXCOMLEN + 1]; pthread_mutex_lock(&ioctx_x->mtx); if (ioctx_x->started) { @@ -78,10 +77,9 @@ iothread_start(struct iothread_ctx *ioctx_x) } ioctx_x->started = true; - snprintf(tname, sizeof(tname), "iothread_%d", ioctx_x->idx); - pthread_setname_np(ioctx_x->tid, tname); + pthread_setname_np(ioctx_x->tid, ioctx_x->name); pthread_mutex_unlock(&ioctx_x->mtx); - pr_info("iothread_%d started\n", ioctx_x->idx); + pr_info("%s started\n", ioctx_x->name); return 0; } @@ -158,7 +156,7 @@ iothread_deinit(void) ioctx_x->epfd = -1; } pthread_mutex_destroy(&ioctx_x->mtx); - pr_info("iothread_%d stop \n", i); + pr_info("%s stop \n", ioctx_x->name); } ioctx_active_cnt = 0; pthread_mutex_unlock(&ioctxes_mutex); @@ -169,7 +167,7 @@ iothread_deinit(void) * Return NULL if fails. Otherwise, return the base of those iothread context instances. */ struct iothread_ctx * -iothread_create(int ioctx_num) +iothread_create(int ioctx_num, const char *ioctx_tag) { pthread_mutexattr_t attr; int i, ret, base, end; @@ -199,6 +197,11 @@ iothread_create(int ioctx_num) ioctx_x->started = false; ioctx_x->epfd = epoll_create1(0); + if (snprintf(ioctx_x->name, PTHREAD_NAME_MAX_LEN, + "iothr-%d-%s", ioctx_x->idx, ioctx_tag) >= PTHREAD_NAME_MAX_LEN) { + pr_err("%s: iothread name too long \n", __func__); + } + if (ioctx_x->epfd < 0) { ret = -1; pr_err("%s: failed to create epoll fd, error is %d\r\n", diff --git a/devicemodel/hw/pci/virtio/virtio_block.c b/devicemodel/hw/pci/virtio/virtio_block.c index 0deb79aba..8c5182a93 100644 --- a/devicemodel/hw/pci/virtio/virtio_block.c +++ b/devicemodel/hw/pci/virtio/virtio_block.c @@ -469,6 +469,7 @@ virtio_blk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) { bool dummy_bctxt; char bident[16]; + char ioctx_tag[16]; struct blockif_ctxt *bctxt; char *opts_tmp = NULL; char *opts_start = NULL; @@ -585,7 +586,11 @@ virtio_blk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) num_iothread = num_vqs; } - ioctx_base = iothread_create(num_iothread); + if (snprintf(ioctx_tag, sizeof(ioctx_tag), "blk%s", bident) >= sizeof(ioctx_tag)) { + pr_err("%s: virtio-blk ioctx_tag too long \n", __func__); + } + + ioctx_base = iothread_create(num_iothread, ioctx_tag); if (ioctx_base == NULL) { pr_err("%s: Fails to create iothread context instance \n", __func__); return -1; diff --git a/devicemodel/include/iothread.h b/devicemodel/include/iothread.h index 415030d30..e5b36bfbe 100644 --- a/devicemodel/include/iothread.h +++ b/devicemodel/include/iothread.h @@ -9,6 +9,14 @@ #define IOTHREAD_NUM 40 +/* + * The pthread_setname_np() function can be used to set a unique name for a thread, + * which can be useful for debugging multithreaded applications. + * The thread name is a meaningful C language string, + * whose length is restricted to 16 characters, including the terminating null byte ('\0'). + */ +#define PTHREAD_NAME_MAX_LEN 16 + struct iothread_mevent { void (*run)(void *); void *arg; @@ -21,6 +29,7 @@ struct iothread_ctx { bool started; pthread_mutex_t mtx; int idx; + char name[PTHREAD_NAME_MAX_LEN]; }; struct iothreads_info { @@ -31,6 +40,6 @@ struct iothreads_info { int iothread_add(struct iothread_ctx *ioctx_x, int fd, struct iothread_mevent *aevt); int iothread_del(struct iothread_ctx *ioctx_x, int fd); void iothread_deinit(void); -struct iothread_ctx *iothread_create(int ioctx_num); +struct iothread_ctx *iothread_create(int ioctx_num, const char *ioctx_tag); #endif