-
Notifications
You must be signed in to change notification settings - Fork 13
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
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4107568
feat: export pprof (#517)
rauno56 2c12b57
fix: add source.event.period to samples
seemk 12f3ba1
fix: handle unknown line numbers
seemk fdc0976
initial memory samples serialization
seemk 95fa794
fix: don't overwrite stacktrace lines array
seemk f03d78d
Merge branch 'pprof-export' into memory-profiling
seemk eb96e3f
feat: simplify sample collection
seemk dadab9e
feat: bfs traversal of allocations
seemk 80e2e09
fix: keep track of new samples
seemk bd432c8
feat: add stop for memory profiling, clean out printouts
seemk 67b4f1d
refactor: remove duplicate code
seemk 9b7c3ce
refactor: remove duplicate code, fix cpu serialization test
seemk 00028b2
fix: add timestamps to heap profile, add a test
seemk 417558a
test: add test for heap profile collection
seemk 2e5f1d7
test: add test for memory profiling export
seemk 0759bff
chore: remove unsupported versions from prebuilds
seemk b28140f
doc: update profiling docs
seemk ea1d844
test: reduce allocation test flakiness
seemk e95beae
Merge branch 'main' into memory-profiling
seemk 0bf4bf9
doc: add a doc about memory profiling
seemk ba710a9
doc: improve wording
seemk 6b6d482
refactor: remove unused function
seemk d9a517a
Merge branch 'main' into memory-profiling
seemk 284799e
refactor: use range based for
seemk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
#include "memory_profiling.h" | ||
#include "khash.h" | ||
#include "util/platform.h" | ||
#include <v8-profiler.h> | ||
#include <vector> | ||
|
||
namespace Splunk { | ||
namespace Profiling { | ||
|
||
namespace { | ||
enum MemoryProfilingStringIndex { | ||
V8String_Name, | ||
V8String_ScriptName, | ||
V8String_LineNumber, | ||
V8String_ParentId, | ||
V8String_MAX | ||
}; | ||
|
||
struct BFSNode { | ||
BFSNode(v8::AllocationProfile::Node* node, uint32_t parentId) : node(node), parentId(parentId) {} | ||
v8::AllocationProfile::Node* node; | ||
uint32_t parentId; | ||
}; | ||
|
||
using AllocationSample = v8::AllocationProfile::Sample; | ||
KHASH_MAP_INIT_INT64(SampleId, uint64_t); | ||
|
||
struct MemoryProfiling { | ||
MemoryProfiling() : tracking(kh_init(SampleId)) { stack.reserve(128); } | ||
~MemoryProfiling() { kh_destroy(SampleId, tracking); } | ||
uint64_t generation = 0; | ||
// Used to keep track which were the new samples added to the allocation profile. | ||
khash_t(SampleId) * tracking; | ||
std::vector<BFSNode> stack; | ||
bool isRunning = false; | ||
}; | ||
|
||
MemoryProfiling* profiling = nullptr; | ||
|
||
struct StringStash { | ||
v8::Local<v8::String> strings[V8String_MAX]; | ||
}; | ||
|
||
v8::Local<v8::Object> | ||
ToJsHeapNode(v8::AllocationProfile::Node* node, uint32_t parentId, StringStash* stash) { | ||
auto jsNode = Nan::New<v8::Object>(); | ||
Nan::Set(jsNode, stash->strings[V8String_Name], node->name); | ||
Nan::Set(jsNode, stash->strings[V8String_ScriptName], node->script_name); | ||
Nan::Set(jsNode, stash->strings[V8String_LineNumber], Nan::New<v8::Integer>(node->line_number)); | ||
Nan::Set(jsNode, stash->strings[V8String_ParentId], Nan::New<v8::Uint32>(parentId)); | ||
return jsNode; | ||
} | ||
|
||
} // namespace | ||
|
||
NAN_METHOD(StartMemoryProfiling) { | ||
if (!profiling) { | ||
profiling = new MemoryProfiling(); | ||
} | ||
|
||
if (profiling->isRunning) { | ||
return; | ||
} | ||
|
||
v8::HeapProfiler* profiler = info.GetIsolate()->GetHeapProfiler(); | ||
|
||
if (!profiler) { | ||
Nan::ThrowError("unable to get heap profiler - isolate not initialized"); | ||
return; | ||
} | ||
|
||
int64_t sampleIntervalBytes = 1024 * 128; | ||
int32_t maxStackDepth = 256; | ||
|
||
if (info.Length() >= 1 && info[0]->IsObject()) { | ||
auto options = Nan::To<v8::Object>(info[0]).ToLocalChecked(); | ||
auto maybeSampleIntervalBytes = | ||
Nan::Get(options, Nan::New("sampleIntervalBytes").ToLocalChecked()); | ||
if ( | ||
!maybeSampleIntervalBytes.IsEmpty() && | ||
maybeSampleIntervalBytes.ToLocalChecked()->IsNumber()) { | ||
sampleIntervalBytes = Nan::To<int64_t>(maybeSampleIntervalBytes.ToLocalChecked()).FromJust(); | ||
} | ||
|
||
auto maybeMaxStackDepth = Nan::Get(options, Nan::New("maxStackDepth").ToLocalChecked()); | ||
if (!maybeMaxStackDepth.IsEmpty() && maybeMaxStackDepth.ToLocalChecked()->IsNumber()) { | ||
maxStackDepth = Nan::To<int32_t>(maybeMaxStackDepth.ToLocalChecked()).FromJust(); | ||
} | ||
} | ||
|
||
profiling->isRunning = profiler->StartSamplingHeapProfiler(sampleIntervalBytes, maxStackDepth); | ||
} | ||
|
||
NAN_METHOD(CollectHeapProfile) { | ||
info.GetReturnValue().SetNull(); | ||
|
||
if (!profiling) { | ||
return; | ||
} | ||
|
||
if (!profiling->isRunning) { | ||
return; | ||
} | ||
|
||
v8::HeapProfiler* profiler = info.GetIsolate()->GetHeapProfiler(); | ||
|
||
if (!profiler) { | ||
return; | ||
} | ||
|
||
v8::AllocationProfile* profile = profiler->GetAllocationProfile(); | ||
|
||
if (!profile) { | ||
return; | ||
} | ||
|
||
auto jsResult = Nan::New<v8::Object>(); | ||
auto jsSamples = Nan::New<v8::Array>(); | ||
auto jsNodeTree = Nan::New<v8::Object>(); | ||
int32_t jsSamplesLength = 0; | ||
|
||
v8::AllocationProfile::Node* root = profile->GetRootNode(); | ||
|
||
const std::vector<v8::AllocationProfile::Sample>& samples = profile->GetSamples(); | ||
|
||
profiling->generation++; | ||
uint64_t generation = profiling->generation; | ||
|
||
khash_t(SampleId)* tracking = profiling->tracking; | ||
|
||
for (const auto& sample : samples) { | ||
if (kh_get(SampleId, tracking, sample.sample_id) == kh_end(tracking)) { | ||
auto jsSample = Nan::New<v8::Object>(); | ||
Nan::Set( | ||
jsSample, Nan::New<v8::String>("nodeId").ToLocalChecked(), | ||
Nan::New<v8::Uint32>(sample.node_id)); | ||
Nan::Set( | ||
jsSample, Nan::New<v8::String>("size").ToLocalChecked(), | ||
Nan::New<v8::Uint32>(uint32_t(sample.size * sample.count))); | ||
Nan::Set(jsSamples, jsSamplesLength++, jsSample); | ||
} | ||
|
||
int ret; | ||
khiter_t it = kh_put(SampleId, tracking, sample.sample_id, &ret); | ||
if (ret != -1) { | ||
kh_value(tracking, it) = generation; | ||
} | ||
} | ||
|
||
for (khiter_t it = kh_begin(tracking); it != kh_end(tracking); ++it) { | ||
if (!kh_exist(tracking, it)) { | ||
continue; | ||
} | ||
|
||
if (kh_val(tracking, it) != generation) { | ||
kh_del(SampleId, tracking, it); | ||
} | ||
} | ||
|
||
StringStash stash; | ||
rauno56 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stash.strings[V8String_Name] = Nan::New<v8::String>("name").ToLocalChecked(); | ||
stash.strings[V8String_ScriptName] = Nan::New<v8::String>("scriptName").ToLocalChecked(); | ||
stash.strings[V8String_LineNumber] = Nan::New<v8::String>("lineNumber").ToLocalChecked(); | ||
stash.strings[V8String_ParentId] = Nan::New<v8::String>("parentId").ToLocalChecked(); | ||
|
||
std::vector<BFSNode>& stack = profiling->stack; | ||
stack.clear(); | ||
|
||
// Cut off the root node | ||
for (v8::AllocationProfile::Node* child : root->children) { | ||
stack.emplace_back(child, root->node_id); | ||
} | ||
|
||
while (!stack.empty()) { | ||
rauno56 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BFSNode graphNode = stack.back(); | ||
stack.pop_back(); | ||
|
||
v8::AllocationProfile::Node* node = graphNode.node; | ||
|
||
auto jsNode = ToJsHeapNode(node, graphNode.parentId, &stash); | ||
Nan::Set(jsNodeTree, Nan::New<v8::Uint32>(node->node_id), jsNode); | ||
|
||
for (v8::AllocationProfile::Node* child : node->children) { | ||
stack.emplace_back(child, node->node_id); | ||
} | ||
} | ||
|
||
Nan::Set(jsResult, Nan::New<v8::String>("treeMap").ToLocalChecked(), jsNodeTree); | ||
Nan::Set(jsResult, Nan::New<v8::String>("samples").ToLocalChecked(), jsSamples); | ||
Nan::Set( | ||
jsResult, Nan::New<v8::String>("timestamp").ToLocalChecked(), | ||
Nan::New<v8::Number>(MilliSecondsSinceEpoch())); | ||
|
||
info.GetReturnValue().Set(jsResult); | ||
|
||
delete profile; | ||
} | ||
|
||
NAN_METHOD(StopMemoryProfiling) { | ||
if (!profiling) { | ||
return; | ||
} | ||
|
||
if (profiling->isRunning) { | ||
v8::HeapProfiler* profiler = info.GetIsolate()->GetHeapProfiler(); | ||
|
||
if (!profiler) { | ||
return; | ||
} | ||
|
||
profiler->StopSamplingHeapProfiler(); | ||
} | ||
|
||
delete profiling; | ||
profiling = nullptr; | ||
} | ||
|
||
} // namespace Profiling | ||
} // namespace Splunk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <nan.h> | ||
|
||
namespace Splunk { | ||
namespace Profiling { | ||
|
||
NAN_METHOD(StartMemoryProfiling); | ||
NAN_METHOD(CollectHeapProfile); | ||
NAN_METHOD(StopMemoryProfiling); | ||
|
||
} // namespace Profiling | ||
} // namespace Splunk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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']).
There was a problem hiding this comment.
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 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.