Skip to contentSkip to conversation

Measuring lock conention

DISCUSSION Mohamad Gebai 4 participants 8 messages Apr 13, 2017

lore
Share & tools
lore
#0 lore raw

Hi all,

Yesterday during the performance call there was some talk around measuring lock contention in wallclock, and it was mentioned that an option would be to monitor the futex system call. I just uploaded to Github a script I've used in the past that does exactly that, in case there was interest in pursuing this approach (https://github.com/mogeb/bt-futex). The Python script expects a CTF trace (e.g. LTTng). The functionality is very minimal at this point, but if there is interest I'd be happy to enhance it further. The main drawback of this approach is that it only works for locks that use futex (spinlocks, for instance, wouldn't be monitored). No instrumentation to the application is required.

Here's an example output of the amount of time spent in the futex syscall, per thread (system-wide). The LOCK_TIME column shows the amount of time waiting on a lock, in nanoseconds:

$> python3 count_futex.py kernel/

PROCESS_NAME (TID): LOCK_TIME ns
git (17550):  2,122
git (17551):  8,124
example (17554):  80,690,708
example (17555):  80,020,536
example (17556):  367,010,253
example (17557):  459,009,153
example (17558):  199,676,209
example (17559):  108,401
example (17560):  475,562,084
example (17561):  80,549,102
example (17562):  468,002,252
example (17564):  92,108,616
example (17566):  450,655,745
example (17567):  49,904,388
libvirtd (3772):  2,389
libvirtd (3773):  2,839
libvirtd (3775):  2,417
libvirtd (3776):  3,577
libvirtd (3777):  2,492

Mohamad

#1 lore raw

Nice! I will give it a try and see how it goes. Specifically I want to compare it to what I ended up working on yesterday. After the meeting I ended up doing major surgery on an existing gdb based wallclock profiler and modified it to a) work b) be thread aware c) print inverse call-graphs. The code is still pretty rough but you can see it here:

https://github.com/markhpc/gdbprof

I've included an example inverse-callgraph from a run I did this morning.

Right now it can be invoked like:

gdb -ex 'set pagination off' -ex 'attach <pid>' -ex 'source ./gdbprof.py' -ex 'profile begin' -ex 'quit'

I'm going to try and clean it up and give it more command line options for choosing the call-graph direction, sample count, etc.

Mark

Quoted 45 lines · Mohamad Gebai

Hi all,

Yesterday during the performance call there was some talk around measuring lock contention in wallclock, and it was mentioned that an option would be to monitor the futex system call. I just uploaded to Github a script I've used in the past that does exactly that, in case there was interest in pursuing this approach
(https://github.com/mogeb/bt-futex). The Python script expects a CTF trace (e.g. LTTng). The functionality is very minimal at this point, but if there is interest I'd be happy to enhance it further. The main drawback of this approach is that it only works for locks that use futex (spinlocks, for instance, wouldn't be monitored). No instrumentation to the application is required.

Here's an example output of the amount of time spent in the futex syscall, per thread (system-wide). The LOCK_TIME column shows the amount of time waiting on a lock, in nanoseconds:

$> python3 count_futex.py kernel/

PROCESS_NAME (TID): LOCK_TIME ns
git (17550):  2,122
git (17551):  8,124
example (17554):  80,690,708
example (17555):  80,020,536
example (17556):  367,010,253
example (17557):  459,009,153
example (17558):  199,676,209
example (17559):  108,401
example (17560):  475,562,084
example (17561):  80,549,102
example (17562):  468,002,252
example (17564):  92,108,616
example (17566):  450,655,745
example (17567):  49,904,388
libvirtd (3772):  2,389
libvirtd (3773):  2,839
libvirtd (3775):  2,417
libvirtd (3776):  3,577
libvirtd (3777):  2,492

Mohamad
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 3 lines · Mark Nelson

Nice! I will give it a try and see how it goes. Specifically I want to compare it to what I ended up working on yesterday. After the meeting I ended up doing major surgery on an existing gdb based wallclock profiler and modified it to a) work b) be thread aware c) print inverse call-graphs. The code is still pretty rough but you can see it here:

https://github.com/markhpc/gdbprof

Very neat, thank you for this. Please let me know what happens, I'm interested to see which tool ends up working best for this use case. Also, could you share some information about what you're trying to find out?

If I'm not mistaken, there's work being done to add the call stack of a process within the context of LTTng events. That way we could have this information when a process blocks in sys_futex.

PS: found it, it's still in RFC - https://lists.lttng.org/pipermail/lttng-dev/2017-March/026985.html

Mohamad

#3 lore raw
Quoted 15 lines · Mohamad Gebai
Quoted 8 lines · Mark Nelson

Nice! I will give it a try and see how it goes. Specifically I want to compare it to what I ended up working on yesterday. After the meeting I ended up doing major surgery on an existing gdb based wallclock profiler and modified it to a) work b) be thread aware c) print inverse call-graphs. The code is still pretty rough but you can see it here:

https://github.com/markhpc/gdbprof

Very neat, thank you for this. Please let me know what happens, I'm interested to see which tool ends up working best for this use case. Also, could you share some information about what you're trying to find out?

Quoted 8 lines

If I'm not mistaken, there's work being done to add the call stack of a process within the context of LTTng events. That way we could have this information when a process blocks in sys_futex.

PS: found it, it's still in RFC -
https://lists.lttng.org/pipermail/lttng-dev/2017-March/026985.html

You can also use perf's syscall tracepoints to capture the syscalls:sys_enter_futex event. This way you get contended mutex. The nice thing about it is all the normal perf tools apply, so you can see source annotation, frequency (hot points) and also examine the callgraph of the hot points.

Signature · 7 lines
Milosz Tanski
CTO
16 East 34th Street, 15th floor
New York, NY 10016

p: 646-253-9055
e: [email protected]
#4 lore raw
Quoted 25 lines · Milosz Tanski
Quoted 15 lines · Mohamad Gebai
Quoted 8 lines · Mark Nelson

Nice! I will give it a try and see how it goes. Specifically I want to compare it to what I ended up working on yesterday. After the meeting I ended up doing major surgery on an existing gdb based wallclock profiler and modified it to a) work b) be thread aware c) print inverse call-graphs. The code is still pretty rough but you can see it here:

https://github.com/markhpc/gdbprof

Very neat, thank you for this. Please let me know what happens, I'm interested to see which tool ends up working best for this use case. Also, could you share some information about what you're trying to find out?

Quoted 8 lines

If I'm not mistaken, there's work being done to add the call stack of a process within the context of LTTng events. That way we could have this information when a process blocks in sys_futex.

PS: found it, it's still in RFC -
https://lists.lttng.org/pipermail/lttng-dev/2017-March/026985.html

Quoted 6 lines

You can also use perf's syscall tracepoints to capture the syscalls:sys_enter_futex event. This way you get contended mutex. The nice thing about it is all the normal perf tools apply, so you can see source annotation, frequency (hot points) and also examine the callgraph of the hot points.

Hi Milosz,

Do you know of any examples showing this technique? I've suspected there was a way to do this (and similar things) with perf, but always ran into roadblocks that made it not work. Potentially one of those roadblocks might have been errors on my part. ;)

Mark

#5 lore raw

Ah, nice to see poor man's getting a mention, a favoured weapon of mine.

Another possibility is systemtap. The following examples can be modified to give additional information (stack traces, etc.).

https://sourceware.org/systemtap/examples/keyword-index.html#FUTEX

I can probably help with a systemtap approach, at least getting up and running and probably with the probes.

My favourite sites for perf are:

https://perf.wiki.kernel.org/index.php/Main_Page

http://www.brendangregg.com/perf.html

Quoted 53 lines · Mark Nelson
Quoted 32 lines · Milosz Tanski
Quoted 21 lines · Mohamad Gebai
Quoted 11 lines · Mark Nelson

Nice! I will give it a try and see how it goes. Specifically I want to compare it to what I ended up working on yesterday. After the meeting I ended up doing major surgery on an existing gdb based wallclock profiler and
modified it to a) work b) be thread aware c) print inverse call-graphs. The
code is still pretty rough but you can see it here:

https://github.com/markhpc/gdbprof

Very neat, thank you for this. Please let me know what happens, I'm interested to see which tool ends up working best for this use case. Also,
could you share some information about what you're trying to find out?

Quoted 8 lines

If I'm not mistaken, there's work being done to add the call stack of a process within the context of LTTng events. That way we could have this information when a process blocks in sys_futex.

PS: found it, it's still in RFC -
https://lists.lttng.org/pipermail/lttng-dev/2017-March/026985.html

Quoted 6 lines

You can also use perf's syscall tracepoints to capture the syscalls:sys_enter_futex event. This way you get contended mutex. The nice thing about it is all the normal perf tools apply, so you can see source annotation, frequency (hot points) and also examine the callgraph of the hot points.

Hi Milosz,

Do you know of any examples showing this technique? I've suspected there was a way to do this (and similar things) with perf, but always ran into roadblocks that made it not work. Potentially one of those roadblocks might have been errors on my part. ;)

Mark

--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Cheers,
Brad
#7 lore raw
In reply to Mark Nelson
Quoted 38 lines · Mark Nelson
Quoted 29 lines · Milosz Tanski
Quoted 18 lines · Mohamad Gebai
Quoted 9 lines · Mark Nelson

Nice! I will give it a try and see how it goes. Specifically I want to compare it to what I ended up working on yesterday. After the meeting I ended up doing major surgery on an existing gdb based wallclock profiler and modified it to a) work b) be thread aware c) print inverse call-graphs. The code is still pretty rough but you can see it here:

https://github.com/markhpc/gdbprof

Very neat, thank you for this. Please let me know what happens, I'm interested to see which tool ends up working best for this use case. Also, could you share some information about what you're trying to find out?

Quoted 8 lines

If I'm not mistaken, there's work being done to add the call stack of a process within the context of LTTng events. That way we could have this information when a process blocks in sys_futex.

PS: found it, it's still in RFC -
https://lists.lttng.org/pipermail/lttng-dev/2017-March/026985.html

Quoted 6 lines

You can also use perf's syscall tracepoints to capture the syscalls:sys_enter_futex event. This way you get contended mutex. The nice thing about it is all the normal perf tools apply, so you can see source annotation, frequency (hot points) and also examine the callgraph of the hot points.

Quoted 5 lines

Hi Milosz,

Do you know of any examples showing this technique? I've suspected there was a way to do this (and similar things) with perf, but always ran into roadblocks that made it not work. Potentially one of those roadblocks might have been errors on my part. ;)

Mark

Mark, here's crash tutorial example.

# Build a mutex benchmark I stole from github mtanski@crunchy:~/tmp/bench$ g++ --std=c++11 -Og -g -pthread
bench.cpp -o bench

    # I can only make the syscall probes work as root :(
    mtanski@crunchy:~/tmp/bench$ sudo -s

    root@crunchy:~/tmp/bench# /usr/bin/perf record -F 4096 -g

--call-graph dwarf -e syscalls:sys_exit_futex ./bench
lock with 1 threads throughput = 48333
lock with 2 threads throughput = 19551
lock with 4 threads throughput = 14307
lock_guard with 1 threads throughput = 57499
lock_guard with 2 threads throughput = 16919
lock_guard with 4 threads throughput = 14460
atomic with 1 threads throughput = 175000
atomic with 2 threads throughput = 63888
atomic with 4 threads throughput = 65833
[ perf record: Woken up 296 times to write data ]
[ perf record: Captured and wrote 76.438 MB perf.data (9448 samples) ]

root@crunchy:~/tmp/bench# perf report --stdio
...
59.77% 59.77% 0x0
|
--59.75%--__clone
start_thread
0xb8c80
|

|--22.46%--_ZNSt6thread5_ImplISt12_Bind_simpleIFZ16bench_lock_guardILi4EEvvEUlvE_vEEE6_M_runEv

                           |          |
                           |          |--19.28%--pthread_mutex_unlock
                           |          |          __lll_unlock_wake
                           |          |
                           |           --3.17%--pthread_mutex_lock
                           |                     __lll_lock_wait
                           |

|--21.77%--_ZNSt6thread5_ImplISt12_Bind_simpleIFZ10bench_lockILi4EEvvEUlvE_vEEE6_M_runEv

                           |          |
                           |          |--19.29%--pthread_mutex_unlock
                           |          |          __lll_unlock_wake
                           |          |
                           |           --2.48%--pthread_mutex_lock
                           |                     __lll_lock_wait
    ...

Notes:
* Ubuntu's built in perf is built without symbol demangling. I haven't rebuilt my own perf; please ignore that.
* If you're running on a newer system, you can use the SDT events. And then glibc/pthreads expose their own event called 'sdt_libc:lll_lock_wait' that you can use.
* You're going to see the pthread_mutex_(un)lock here because the futex also needs to be called to wakeup waiters. That's why 'sdt_libc:lll_lock_wait' is a better event.
* Unless your glibc is built with frame pointers you have to use dwarf call graphs. Otherwise you'll lose the call graph symbol at __lll
* If you use the 'syscalls:sys_enter_futex' you will also get the uaddr of the futex and you can look at which mutex that corresponds to as well.
* On an application with a lot mutex contention play with the event capture limits. I've generated 32GB perf files that perf report chokes on.

Similarly you can use other perf (like live perf top with various lock events)

Best,
- Milosz

Signature · 7 lines
Milosz Tanski
CTO
16 East 34th Street, 15th floor
New York, NY 10016

p: 646-253-9055
e: [email protected]
#6 lore raw
In reply to Brad Hubbard

Yeah, poorman's is pretty much my default fallback for this kind of thing. Hence why I ended up going back to it again this time with an eye toward better callgraphs. Using the python gdb bindings is also much better than the traditional shell wrapper approach. You can collect the samples far more quickly (though it's still slow vs something like perf).

I've considered systemtap but never really got into it hoping we'd get the lttng tooling in before I had to learn it. I think Dan was interested in systemtap as well though, so it still might be worth doing.

Brendan's examples are certainly always nice!

Mark

Quoted 74 lines · Brad Hubbard

Ah, nice to see poor man's getting a mention, a favoured weapon of mine.

Another possibility is systemtap. The following examples can be modified to give additional information (stack traces, etc.).

https://sourceware.org/systemtap/examples/keyword-index.html#FUTEX

I can probably help with a systemtap approach, at least getting up and running and probably with the probes.

My favourite sites for perf are:

https://perf.wiki.kernel.org/index.php/Main_Page

http://www.brendangregg.com/perf.html

Quoted 53 lines · Mark Nelson
Quoted 32 lines · Milosz Tanski
Quoted 21 lines · Mohamad Gebai
Quoted 11 lines · Mark Nelson

Nice! I will give it a try and see how it goes. Specifically I want to compare it to what I ended up working on yesterday. After the meeting I ended up doing major surgery on an existing gdb based wallclock profiler and
modified it to a) work b) be thread aware c) print inverse call-graphs. The
code is still pretty rough but you can see it here:

https://github.com/markhpc/gdbprof

Very neat, thank you for this. Please let me know what happens, I'm interested to see which tool ends up working best for this use case. Also,
could you share some information about what you're trying to find out?

Quoted 8 lines

If I'm not mistaken, there's work being done to add the call stack of a process within the context of LTTng events. That way we could have this information when a process blocks in sys_futex.

PS: found it, it's still in RFC -
https://lists.lttng.org/pipermail/lttng-dev/2017-March/026985.html

Quoted 6 lines

You can also use perf's syscall tracepoints to capture the syscalls:sys_enter_futex event. This way you get contended mutex. The nice thing about it is all the normal perf tools apply, so you can see source annotation, frequency (hot points) and also examine the callgraph of the hot points.

Hi Milosz,

Do you know of any examples showing this technique? I've suspected there was a way to do this (and similar things) with perf, but always ran into roadblocks that made it not work. Potentially one of those roadblocks might have been errors on my part. ;)

Mark

--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html