Skip to content

Commit 48169de

Browse files
ahrenstonyhutter
authored andcommitted
Improve performance of zio_taskq_member
__zio_execute() calls zio_taskq_member() to determine if we are running in a zio interrupt taskq, in which case we may need to switch to processing this zio in a zio issue taskq. The call to zio_taskq_member() can become a performance bottleneck when we are processing a high rate of zio's. zio_taskq_member() calls taskq_member() on each of the zio interrupt taskqs, of which there are 21. This is slow because each call to taskq_member() does tsd_get(taskq_tsd), which on Linux is relatively slow. This commit improves the performance of zio_taskq_member() by having it cache the value of tsd_get(taskq_tsd), reducing the number of those calls to 1/21th of the current behavior. In a test case running `zfs send -c >/dev/null` of a filesystem with small blocks (average 2.5KB/block), zio_taskq_member() was using 6.7% of one CPU, and with this change it is reduced to 1.3%. Overall time to perform the `zfs send` reduced by 10% (~150,000 block/sec to ~165,000 blocks/sec). Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Serapheim Dimitropoulos <[email protected]> Reviewed-by: Ryan Moeller <[email protected]> Reviewed-by: Tony Nguyen <[email protected]> Signed-off-by: Matthew Ahrens <[email protected]> Closes openzfs#10070
1 parent 53cae84 commit 48169de

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

include/spl/sys/taskq.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ extern void taskq_wait_outstanding(taskq_t *, taskqid_t);
151151
extern void taskq_wait(taskq_t *);
152152
extern int taskq_cancel_id(taskq_t *, taskqid_t);
153153
extern int taskq_member(taskq_t *, kthread_t *);
154+
extern taskq_t *taskq_of_curthread(void);
154155

155156
#define taskq_create_proc(name, nthreads, pri, min, max, proc, flags) \
156157
taskq_create(name, nthreads, pri, min, max, flags)

include/sys/zfs_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ extern void taskq_wait(taskq_t *);
499499
extern void taskq_wait_id(taskq_t *, taskqid_t);
500500
extern void taskq_wait_outstanding(taskq_t *, taskqid_t);
501501
extern int taskq_member(taskq_t *, kthread_t *);
502+
extern taskq_t *taskq_of_curthread(void);
502503
extern int taskq_cancel_id(taskq_t *, taskqid_t);
503504
extern void system_taskq_init(void);
504505
extern void system_taskq_fini(void);

lib/libzpool/taskq.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ int taskq_now;
3434
taskq_t *system_taskq;
3535
taskq_t *system_delay_taskq;
3636

37+
static pthread_key_t taskq_tsd;
38+
3739
#define TASKQ_ACTIVE 0x00010000
3840

3941
static taskq_ent_t *
@@ -213,6 +215,8 @@ taskq_thread(void *arg)
213215
taskq_ent_t *t;
214216
boolean_t prealloc;
215217

218+
VERIFY0(pthread_setspecific(taskq_tsd, tq));
219+
216220
mutex_enter(&tq->tq_lock);
217221
while (tq->tq_flags & TASKQ_ACTIVE) {
218222
if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
@@ -343,6 +347,12 @@ taskq_member(taskq_t *tq, kthread_t *t)
343347
return (0);
344348
}
345349

350+
taskq_t *
351+
taskq_of_curthread(void)
352+
{
353+
return (pthread_getspecific(taskq_tsd));
354+
}
355+
346356
int
347357
taskq_cancel_id(taskq_t *tq, taskqid_t id)
348358
{
@@ -352,6 +362,7 @@ taskq_cancel_id(taskq_t *tq, taskqid_t id)
352362
void
353363
system_taskq_init(void)
354364
{
365+
VERIFY0(pthread_key_create(&taskq_tsd, NULL));
355366
system_taskq = taskq_create("system_taskq", 64, maxclsyspri, 4, 512,
356367
TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
357368
system_delay_taskq = taskq_create("delay_taskq", 4, maxclsyspri, 4,
@@ -365,4 +376,5 @@ system_taskq_fini(void)
365376
system_taskq = NULL; /* defensive */
366377
taskq_destroy(system_delay_taskq);
367378
system_delay_taskq = NULL;
379+
VERIFY0(pthread_key_delete(taskq_tsd));
368380
}

module/spl/spl-taskq.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ taskq_member(taskq_t *tq, kthread_t *t)
488488
}
489489
EXPORT_SYMBOL(taskq_member);
490490

491+
taskq_t *
492+
taskq_of_curthread(void)
493+
{
494+
return (tsd_get(taskq_tsd));
495+
}
496+
EXPORT_SYMBOL(taskq_of_curthread);
497+
491498
/*
492499
* Cancel an already dispatched task given the task id. Still pending tasks
493500
* will be immediately canceled, and if the task is active the function will

module/zfs/zio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,14 +1803,15 @@ zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
18031803
static boolean_t
18041804
zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
18051805
{
1806-
kthread_t *executor = zio->io_executor;
18071806
spa_t *spa = zio->io_spa;
18081807

1808+
taskq_t *tq = taskq_of_curthread();
1809+
18091810
for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
18101811
spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
18111812
uint_t i;
18121813
for (i = 0; i < tqs->stqs_count; i++) {
1813-
if (taskq_member(tqs->stqs_taskq[i], executor))
1814+
if (tqs->stqs_taskq[i] == tq)
18141815
return (B_TRUE);
18151816
}
18161817
}

0 commit comments

Comments
 (0)