Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests: ublk: set CPU affinity before thread initialization

Move ublk_thread_set_sched_affinity() call before ublk_thread_init()
to ensure memory allocations during thread initialization occur on
the correct NUMA node. This leverages Linux's first-touch memory
policy for better NUMA locality.

Also convert ublk_thread_set_sched_affinity() to use
pthread_setaffinity_np() instead of sched_setaffinity(), as the
pthread API is the proper interface for setting thread affinity in
multithreaded programs.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Ming Lei and committed by
Jens Axboe
0123bb91 c28ba6b6

+10 -4
+10 -4
tools/testing/selftests/ublk/kublk.c
··· 839 839 static void ublk_thread_set_sched_affinity(const struct ublk_thread *t, 840 840 cpu_set_t *cpuset) 841 841 { 842 - if (sched_setaffinity(0, sizeof(*cpuset), cpuset) < 0) 842 + if (pthread_setaffinity_np(pthread_self(), sizeof(*cpuset), cpuset) < 0) 843 843 ublk_err("ublk dev %u thread %u set affinity failed", 844 844 t->dev->dev_info.dev_id, t->idx); 845 845 } ··· 862 862 t->dev = info->dev; 863 863 t->idx = info->idx; 864 864 865 + /* 866 + * IO perf is sensitive with queue pthread affinity on NUMA machine 867 + * 868 + * Set sched_affinity at beginning, so following allocated memory/pages 869 + * could be CPU/NUMA aware. 870 + */ 871 + if (info->affinity) 872 + ublk_thread_set_sched_affinity(t, info->affinity); 873 + 865 874 ret = ublk_thread_init(t, info->extra_flags); 866 875 if (ret) { 867 876 ublk_err("ublk dev %d thread %u init failed\n", 868 877 dev_id, t->idx); 869 878 return NULL; 870 879 } 871 - /* IO perf is sensitive with queue pthread affinity on NUMA machine*/ 872 - if (info->affinity) 873 - ublk_thread_set_sched_affinity(t, info->affinity); 874 880 sem_post(info->ready); 875 881 876 882 ublk_dbg(UBLK_DBG_THREAD, "tid %d: ublk dev %d thread %u started\n",