lighthouse/beacon_node
Paul Hauner 456b313665 Tune GNU malloc (#2299)
## Issue Addressed

NA

## Proposed Changes

Modify the configuration of [GNU malloc](https://www.gnu.org/software/libc/manual/html_node/The-GNU-Allocator.html) to reduce memory footprint.

- Set `M_ARENA_MAX` to 4.
    - This reduces memory fragmentation at the cost of contention between threads.
- Set `M_MMAP_THRESHOLD` to 2mb
    - This means that any allocation >= 2mb is allocated via an anonymous mmap, instead of on the heap/arena. This reduces memory fragmentation since we don't need to keep growing the heap to find big contiguous slabs of free memory.
- ~~Run `malloc_trim` every 60 seconds.~~
    - ~~This shaves unused memory from the top of the heap, preventing the heap from constantly growing.~~
    - Removed, see: https://github.com/sigp/lighthouse/pull/2299#issuecomment-825322646

*Note: this only provides memory savings on the Linux (glibc) platform.*
    
## Additional Info

I'm going to close #2288 in favor of this for the following reasons:

- I've managed to get the memory footprint *smaller* here than with jemalloc.
- This PR seems to be less of a dramatic change than bringing in the jemalloc dep.
- The changes in this PR are strictly runtime changes, so we can create CLI flags which disable them completely. Since this change is wide-reaching and complex, it's nice to have an easy "escape hatch" if there are undesired consequences.

## TODO

- [x] Allow configuration via CLI flags
- [x] Test on Mac
- [x] Test on RasPi.
- [x] Determine if GNU malloc is present?
    - I'm not quite sure how to detect for glibc.. This issue suggests we can't really: https://github.com/rust-lang/rust/issues/33244
- [x] Make a clear argument regarding the affect of this on CPU utilization.
- [x] Test with higher `M_ARENA_MAX` values.
- [x] Test with longer trim intervals
- [x] Add some stats about memory savings
- [x] Remove `malloc_trim` calls & code
2021-05-28 05:59:45 +00:00
..
beacon_chain Enable Compatibility with Windows (#2333) 2021-05-19 23:05:16 +00:00
client Monitoring service api (#2251) 2021-05-26 05:58:41 +00:00
eth1 Monitoring service api (#2251) 2021-05-26 05:58:41 +00:00
eth2_libp2p More stringent dialing (#2363) 2021-05-26 14:21:44 +10:00
genesis Add SensitiveUrl to redact user secrets from endpoints (#2326) 2021-05-04 01:59:51 +00:00
http_api Updated to comply with new clippy formatting rules (#2336) 2021-05-10 00:53:09 +00:00
http_metrics Tune GNU malloc (#2299) 2021-05-28 05:59:45 +00:00
network Updated to comply with new clippy formatting rules (#2336) 2021-05-10 00:53:09 +00:00
operation_pool Pack attestations into blocks in parallel (#2307) 2021-04-13 05:27:42 +00:00
src Tune GNU malloc (#2299) 2021-05-28 05:59:45 +00:00
store Monitoring service api (#2251) 2021-05-26 05:58:41 +00:00
tests Update to tokio 1.1 (#2172) 2021-02-10 23:29:49 +00:00
timer Update to tokio 1.1 (#2172) 2021-02-10 23:29:49 +00:00
websocket_server Server sent events (#1920) 2020-12-04 00:18:58 +00:00
Cargo.toml Monitoring service api (#2251) 2021-05-26 05:58:41 +00:00