DynamoRIO / DynamoRIO/dynamorio
reduce shared_vm_areas lock contention during trace building
- Dominant language
- C
- Stars
- 3.2k
- Forks
- 629
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 30
Description
My measurements here are for STATIC_LIBRARY and an attach, so there is a
big burst of bb building, but it likely applies to init periods of regular
DR runs as well.
The shared_vm_areas lock is a big point of contention, with 40x more
contention than the bb_building_lock. It's called for each extension to a
trace and again at the end. There may also be races in building the same
trace?
These stats are from a client-server app with 33 threads on which we did
an attach, gathered a brief memory trace, and then detached:
```
Total times mutexes acquired : 1348141
Total times mutexes contended : 260426
5 lock 0x0000000000a224d0: name=memory_info_buf_lock(mutex)@core/unix/memquery_linux.c:71
rank=86 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000000a47368
lock count_times_acquired= 12300 count_times_contended= 100 0 count_times_spin_only= 6 max_contended_requests= 1+2 memory_info_buf_lock(mutex)@core/unix/memquery_linux.c:71
10 lock 0x0000000000a47460: name=global_alloc_lock(recursive)@core/heap.c:324
rank=72 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x000000005584aea8
lock count_times_acquired= 272265 count_times_contended= 824 0 count_times_spin_only= 684 max_contended_requests= 2+2 global_alloc_lock(recursive)@core/heap.c:324
12 lock 0x000000005584b790: name=all_memory_areas(readwrite)@core/unix/memcache.c:97
rank=67 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x000000005584b8e8
lock count_times_acquired= 15710 count_times_contended= 2256 0 count_times_spin_only= 343 max_contended_requests= 1+2 all_memory_areas(readwrite)@core/unix/memcache.c:97
15 lock 0x0000000055859068: name=shared_cache_lock(mutex)@core/fcache.c:1524
rank=16 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000000a46fa0
lock count_times_acquired= 34628 count_times_contended= 12 0 0 max_contended_requests= 1+2 shared_cache_lock(mutex)@core/fcache.c:1524
19 lock 0x0000000000a46e30: name=thread_initexit_lock(mutex)@core/dynamo.c:260
rank=4 owner=30731 owning_dc=0x00000000558d1080 contended_event=0xffffffff prev=0x0000000055851070
lock count_times_acquired= 43 count_times_contended= 63 0 count_times_spin_only= 31 max_contended_requests= 1+2 thread_initexit_lock(mutex)@core/dynamo.c:260
27 lock 0x0000000000a46d38: name=bb_building_lock(mutex)@core/arch/interp.c:114
rank=5 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x00000000558d3770
lock count_times_acquired= 11776 count_times_contended= 6003 0 count_times_spin_only= 2992 max_contended_requests= 2+2 bb_building_lock(mutex)@core/arch/interp.c:114
28 lock 0x00000000558d3770: name=shared_vm_areas(readwrite)@core/vmareas.c:1558
rank=18 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000055869498
lock count_times_acquired= 94279 count_times_contended= 251037 0 count_times_spin_only= 146 max_contended_requests= 1+2 shared_vm_areas(readwrite)@core/vmareas.c:1558
30 lock 0x0000000000a475f0: name=change_linking_lock(recursive)@core/link.c:108
rank=17 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000055859908
lock count_times_acquired= 19071 count_times_contended= 10 0 count_times_spin_only= 10 0+2 change_linking_lock(recursive)@core/link.c:108
32 lock 0x0000000000a47810: name=trace_building_lock(mutex)@core/monitor.c:102
rank=3 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000000a47ed8
lock count_times_acquired= 199 count_times_contended= 10 0 0 max_contended_requests= 1+2 trace_building_lock(mutex)@core/monitor.c:102
```
Now compare the same app running with -disable_traces:
```
Total times mutexes acquired : 761947
Total times mutexes contended : 5884
5 lock 0x0000000000a224d0: name=memory_info_buf_lock(mutex)@core/unix/memquery_linux.c:71
rank=86 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000000a47368
lock count_times_acquired= 11316 count_times_contended= 97 0 count_times_spin_only= 9 max_contended_requests= 1+2 memory_info_buf_lock(mutex)@core/unix/memquery_linux.c:71
10 lock 0x0000000000a47460: name=global_alloc_lock(recursive)@core/heap.c:324
rank=72 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x000000004cf86ea8
lock count_times_acquired= 253641 count_times_contended= 279 0 count_times_spin_only= 225 max_contended_requests= 1+2 global_alloc_lock(recursive)@core/heap.c:324
11 lock 0x000000004cf86ea8: name=dynamo_areas(readwrite)@core/vmareas.c:1565
rank=70 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x000000004cf87790
lock count_times_acquired= 4222 count_times_contended= 3 0 0 0+2 dynamo_areas(readwrite)@core/vmareas.c:1565
12 lock 0x000000004cf87790: name=all_memory_areas(readwrite)@core/unix/memcache.c:97
rank=67 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x000000004cf878e8
lock count_times_acquired= 14683 count_times_contended= 430 0 count_times_spin_only= 94 max_contended_requests= 1+2 all_memory_areas(readwrite)@core/unix/memcache.c:97
15 lock 0x000000004cf95068: name=shared_cache_lock(mutex)@core/fcache.c:1524
rank=16 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000000a46fa0
lock count_times_acquired= 33959 count_times_contended= 16 0 count_times_spin_only= 2 max_contended_requests= 1+2 shared_cache_lock(mutex)@core/fcache.c:1524
18 lock 0x0000000000a46e30: name=thread_initexit_lock(mutex)@core/dynamo.c:260
rank=4 owner=27218 owning_dc=0x000000004d06ef00 contended_event=0xffffffff prev=0x000000004cfef678
lock count_times_acquired= 39 count_times_contended= 62 0 count_times_spin_only= 29 max_contended_requests= 1+2 thread_initexit_lock(mutex)@core/dynamo.c:260
21 lock 0x000000004cfef1a0: name=executable_areas(readwrite)@core/vmareas.c:1582
rank=26 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x0000000000a47b00
lock count_times_acquired= 8606 count_times_contended= 2 0 0 0+2 executable_areas(readwrite)@core/vmareas.c:1582
25 lock 0x0000000000a46d38: name=bb_building_lock(mutex)@core/arch/interp.c:114
rank=5 owner=0 owning_dc=0x0000000000000000 contended_event=0xffffffff prev=0x000000004cfef770
lock count_times_acquired= 11330 count_times_contended= 4992 0 count_times_spin_only= 2498 max_contended_requests= 2+2 bb_building_lock(mutex)@core/arch/interp.c:114
```
Contributor guide
Research direction
Start by inspecting shared_vm_areas in core/vmareas.c:1558 and the bb_building_lock and trace_building_lock sites noted in core/arch/interp.c:114 and core/monitor.c:102. Reproduce the STATIC_LIBRARY attach measurements, compare them with -disable_traces, and determine whether contention or trace-building races remain after the change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- devtools, performance
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100