MystenLabs / sui

Sui, a next-generation smart contract platform with high throughput, low latency, and an asset-oriented programming model powered by the Move programming language
https://sui.io
Apache License 2.0
6.17k stars 11.18k forks source link

Add support for tracking memory consumption of selected structures #5213

Open aschran opened 2 years ago

aschran commented 2 years ago

Summarizing input from François:

Look into integrating a trait-based crate for tracking heap memory usage, perhaps:

  1. https://github.com/paritytech/parity-common/tree/master/parity-util-mem
  2. https://github.com/Aeledfyr/deepsize/

Validate the approach by carefully instrumenting a set of types on Narwhal with trait implementations (ideally derived as often as possible), up to what's needed to track the memory usage of the following table: https://github.com/MystenLabs/narwhal/blob/main/consensus/src/consensus.rs#L33

Export the memory usage gauge in a metric.

Related work in Sui: https://github.com/MystenLabs/sui/issues/3835

andll commented 2 years ago

FWIW I think even simply checking objects count in global data structures can be a pretty good signal on memory leaks, without estimate on the size itself if it turns out complicated or performance costly

aschran commented 2 years ago

Yes, this is anyway going to just be a rough estimate, since we are not using exact size measurements from the allocator

velvia commented 2 years ago

I think we can obtain a cheap estimate by multiplying object count (say of a Vec) * the static size computed by for example deep size (It should be static since it is a struct).

It might also be possible to track retained memory size of specific structures using instrumentation and some custom tracking code. It wouldn't be that hard. (basically tracking memory allocator re-allocs and size changes for structures like Vec's - we just need to correlate addresses with code locations and whether it is a Vec, perhaps with some extra debug information)

aschran commented 2 years ago

I think we can obtain a cheap estimate by multiplying object count (say of a Vec) * the static size computed by for example deep size (It should be static since it is a struct).

This is for the most part how the implementations in parity-mem-util work when running it in estimate-heapsize mode (which is what I'm starting with to avoid interfering with the allocator-based heap profiling stuff).

velvia commented 2 years ago

FWIW I'm working on a separate heap profiling tool that should give much better signals than what current tools are capable of. Ping me for details