Sometimes your program uses a lot of system time. Let’s say 90%. You fire up your favorite profiling tool and it tells you which system call it is. If you are experiences and maybe lucky, you can say straight away which part of your program is to blame. But it’s not always so obvious.
GDB comes to the rescue. You can use catch syscall <syscall>
and it will break when particular syscall is called. And then you can use ‘bt’ to find where in your code this syscall is being called from.
(gdb)> catch syscall futex
(gdb)> c
Continuing.
[Switching to Thread 0x7ffff3ffc700 (LWP 14893)]
Catchpoint 1 (call to syscall futex), 0x00007ffff76ce779 in __lll_unlock_wake_private () from /lib64/libc.so.6
(gdb)> bt
#0 0x00007ffff76ce779 in __lll_unlock_wake_private () from /lib64/libc.so.6
#1 0x00007ffff76060fc in _L_unlock_147 () from /lib64/libc.so.6
#2 0x00007ffff7606006 in random () from /lib64/libc.so.6
#3 0x000000000040154c in thread_worker (ctx=0x0) at memorytest.c:90
#4 0x00007ffff7992f33 in start_thread () from /lib64/libpthread.so.0
#5 0x00007ffff76c0ded in clone () from /lib64/libc.so.6
P.S. Don’t use random() in multi-threaded program. Use random_r().