Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: memory profiling #524

Merged
merged 24 commits into from
Sep 16, 2022
Merged

feat: memory profiling #524

merged 24 commits into from
Sep 16, 2022

Conversation

seemk
Copy link
Collaborator

@seemk seemk commented Aug 7, 2022

tldr

  • Use v8::HeapProfiler to periodically capture an allocation profile along with the samples.
  • The allocation samples returned from v8 are "live memory", so we have to keep track which samples were added.
  • Samples have a node_id which is its node ID in the call graph. To generate a stack trace this node needs to be found from the allocation profile.
  • For the finding the whole allocation profile graph is converted to a more convenient format, so when we have a node id, we can do a dictionary lookup and reconstruct the path to a root callgraph node which maps nicely to the pprof location array. The conversion requires generating the whole graph. There is room for improvement here (e.g. caching the node graph and only regenerate it when we find an unknown node_id from the samples), but at the moment it seems to be fast enough.

⚠️ Trace and span ID correlation can't be done because V8 does not provide any timing information for the samples.

Misc

  • Removed obsolete Node.js versions (<12) from prebuild:os script

@seemk seemk requested review from a team as code owners August 7, 2022 12:39
@seemk seemk requested a review from a team as a code owner August 7, 2022 12:42
@codecov-commenter
Copy link

codecov-commenter commented Aug 7, 2022

Codecov Report

Merging #524 (d9a517a) into main (96a308a) will decrease coverage by 1.70%.
The diff coverage is 75.00%.

@@            Coverage Diff             @@
##             main     #524      +/-   ##
==========================================
- Coverage   88.94%   87.24%   -1.71%     
==========================================
  Files          27       27              
  Lines         914      972      +58     
  Branches      204      210       +6     
==========================================
+ Hits          813      848      +35     
- Misses        101      124      +23     
Impacted Files Coverage Δ
src/profiling/DebugExporter.ts 18.75% <0.00%> (-11.25%) ⬇️
src/profiling/types.ts 100.00% <ø> (ø)
src/profiling/OTLPProfilingExporter.ts 36.06% <10.00%> (-12.78%) ⬇️
src/profiling/index.ts 90.00% <100.00%> (+2.34%) ⬆️
src/profiling/utils.ts 90.81% <100.00%> (+1.65%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Base automatically changed from pprof-export to main August 7, 2022 14:11

### Memory profiling

Memory profiling is disabled by default, it can be enabled via the `memoryProfilingEnabled` flag.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Memory profiling is disabled by default. You can enable it via the memoryProfilingEnabled flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Perhaps you already know, but GitHub lets you add suggestions directly to the comment via a suggest code block, so the author can press a button to bring the change in, for example:

Suggested change
Memory profiling is disabled by default, it can be enabled via the `memoryProfilingEnabled` flag.
Memory profiling is disabled by default. You can enable it via the `memoryProfilingEnabled` flag.


Internally the profiler uses V8's sampling heap profiler, where it periodically queries for new allocation samples from the allocation profile.

The [V8 heap profiler's parameters](https://v8.github.io/api/head/classv8_1_1HeapProfiler.html#a6b9450bbf1f4e1a4909df92d4df4a174) can additionally be tuned by an optional `memoryProfilingOptions` configuration field:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: You can tune V8 heap profiler's parameters using the memoryProfilingOptions configuration field:

Copy link
Contributor

@rauno56 rauno56 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly curious questions.

test/profiling/extension.test.ts Show resolved Hide resolved

auto jsResult = Nan::New<v8::Object>();
auto jsSamples = Nan::New<v8::Array>();
auto jsNodeTree = Nan::New<v8::Object>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the node id's unique across the lifetime of the program? I ask to suss out why use object instead of an array where the keys are integers already - saves a conversion and perhaps some (type) errors(even though arr[2] === arr['2']).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node ID is an incrementing counter. No idea how large it gets and how many gaps will exist. Just used a denser form 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const arr = [];
array[5] = "hello";
array[10] = "world";

Makes a "holey" array, which is equivalent in terms of the density. I assume calling the same API from the native side behaves the same.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested it and it actually turns to DICTIONARY_ELEMENTS type even with a small app as the node IDs get large, thus it's basically the same as using an object 🤔 And for some reason the average read speed was actually faster when using an object (no idea why) while the write speeds stayed the same.

src/native_ext/memory_profiling.cpp Show resolved Hide resolved
src/native_ext/profiling.cpp Show resolved Hide resolved
src/native_ext/memory_profiling.cpp Show resolved Hide resolved
src/native_ext/memory_profiling.cpp Outdated Show resolved Hide resolved
src/native_ext/profiling.cpp Show resolved Hide resolved
@seemk seemk merged commit ad845c4 into main Sep 16, 2022
@seemk seemk deleted the memory-profiling branch September 16, 2022 12:59
@seemk seemk mentioned this pull request Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants