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

selftests/powerpc: context_switch use private futexes with threads

This reduces overhead of mutex locking and increases context switch
rate significantly (which helps to measure and profile the context
switch path).

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Nicholas Piggin and committed by
Michael Ellerman
b27ce776 b802ab46

+33 -20
+33 -20
tools/testing/selftests/powerpc/benchmarks/context_switch.c
··· 258 258 return __atomic_exchange_n(p, val, __ATOMIC_SEQ_CST); 259 259 } 260 260 261 + static int processes; 262 + 261 263 static int mutex_lock(unsigned long *m) 262 264 { 263 265 int c; 266 + int flags = FUTEX_WAIT; 267 + if (!processes) 268 + flags |= FUTEX_PRIVATE_FLAG; 264 269 265 270 c = cmpxchg(m, 0, 1); 266 271 if (!c) ··· 275 270 c = xchg(m, 2); 276 271 277 272 while (c) { 278 - sys_futex(m, FUTEX_WAIT, 2, NULL, NULL, 0); 273 + sys_futex(m, flags, 2, NULL, NULL, 0); 279 274 c = xchg(m, 2); 280 275 } 281 276 ··· 284 279 285 280 static int mutex_unlock(unsigned long *m) 286 281 { 282 + int flags = FUTEX_WAKE; 283 + if (!processes) 284 + flags |= FUTEX_PRIVATE_FLAG; 285 + 287 286 if (*m == 2) 288 287 *m = 0; 289 288 else if (xchg(m, 0) == 1) 290 289 return 0; 291 290 292 - sys_futex(m, FUTEX_WAKE, 1, NULL, NULL, 0); 291 + sys_futex(m, flags, 1, NULL, NULL, 0); 293 292 294 293 return 0; 295 294 } ··· 302 293 303 294 static void futex_setup(int cpu1, int cpu2) 304 295 { 305 - int shmid; 306 - void *shmaddr; 296 + if (!processes) { 297 + static unsigned long _m1, _m2; 298 + m1 = &_m1; 299 + m2 = &_m2; 300 + } else { 301 + int shmid; 302 + void *shmaddr; 307 303 308 - shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W); 309 - if (shmid < 0) { 310 - perror("shmget"); 311 - exit(1); 312 - } 304 + shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W); 305 + if (shmid < 0) { 306 + perror("shmget"); 307 + exit(1); 308 + } 313 309 314 - shmaddr = shmat(shmid, NULL, 0); 315 - if (shmaddr == (char *)-1) { 316 - perror("shmat"); 310 + shmaddr = shmat(shmid, NULL, 0); 311 + if (shmaddr == (char *)-1) { 312 + perror("shmat"); 313 + shmctl(shmid, IPC_RMID, NULL); 314 + exit(1); 315 + } 316 + 317 317 shmctl(shmid, IPC_RMID, NULL); 318 - exit(1); 318 + 319 + m1 = shmaddr; 320 + m2 = shmaddr + sizeof(*m1); 319 321 } 320 - 321 - shmctl(shmid, IPC_RMID, NULL); 322 - 323 - m1 = shmaddr; 324 - m2 = shmaddr + sizeof(*m1); 325 322 326 323 *m1 = 0; 327 324 *m2 = 0; ··· 366 351 .thread1 = futex_thread1, 367 352 .thread2 = futex_thread2, 368 353 }; 369 - 370 - static int processes; 371 354 372 355 static struct option options[] = { 373 356 { "test", required_argument, 0, 't' },