Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
'src/node_os.cc',
'src/node_perf.cc',
'src/node_platform.cc',
'src/node_profiling.cc',
'src/node_postmortem_metadata.cc',
'src/node_process_events.cc',
'src/node_process_methods.cc',
Expand Down Expand Up @@ -283,6 +284,7 @@
'src/node_perf.h',
'src/node_perf_common.h',
'src/node_platform.h',
'src/node_profiling.h',
'src/node_process.h',
'src/node_process-inl.h',
'src/node_realm.h',
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "node_main_instance.h"
#include "node_options.h"
#include "node_perf_common.h"
#include "node_profiling.h"
#include "node_realm.h"
#include "node_snapshotable.h"
#include "permission/permission.h"
Expand Down
130 changes: 130 additions & 0 deletions src/node_profiling.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "node_profiling.h"

#include "json_utils.h"
#include "util.h"

#include <memory>

namespace node {

using v8::AllocationProfile;
using v8::HandleScope;
using v8::HeapProfiler;
using v8::Isolate;
using v8::Value;

static void BuildHeapProfileNode(Isolate* isolate,
const AllocationProfile::Node* node,
JSONWriter* writer) {
size_t selfSize = 0;
for (const auto& allocation : node->allocations)
selfSize += allocation.size * allocation.count;

writer->json_keyvalue("selfSize", selfSize);
writer->json_keyvalue("id", node->node_id);
writer->json_objectstart("callFrame");
writer->json_keyvalue("scriptId", node->script_id);
writer->json_keyvalue("lineNumber", node->line_number - 1);
writer->json_keyvalue("columnNumber", node->column_number - 1);
Utf8Value name(isolate, node->name);
Utf8Value script_name(isolate, node->script_name);
writer->json_keyvalue("functionName", *name);
writer->json_keyvalue("url", *script_name);
writer->json_objectend();

writer->json_arraystart("children");
for (const auto* child : node->children) {
writer->json_start();
BuildHeapProfileNode(isolate, child, writer);
writer->json_end();
}
writer->json_arrayend();
}

bool SerializeHeapProfile(Isolate* isolate, std::ostringstream& out_stream) {
HandleScope scope(isolate);
HeapProfiler* profiler = isolate->GetHeapProfiler();
std::unique_ptr<AllocationProfile> profile(profiler->GetAllocationProfile());
if (!profile) {
return false;
}
profiler->StopSamplingHeapProfiler();
JSONWriter writer(out_stream, true);
writer.json_start();

writer.json_arraystart("samples");
for (const auto& sample : profile->GetSamples()) {
writer.json_start();
writer.json_keyvalue("size", sample.size * sample.count);
writer.json_keyvalue("nodeId", sample.node_id);
writer.json_keyvalue("ordinal", static_cast<double>(sample.sample_id));
writer.json_end();
}
writer.json_arrayend();

writer.json_objectstart("head");
BuildHeapProfileNode(isolate, profile->GetRootNode(), &writer);
writer.json_objectend();

writer.json_end();
return true;
}

HeapProfileOptions ParseHeapProfileOptions(
const v8::FunctionCallbackInfo<Value>& args) {
HeapProfileOptions options;
CHECK_LE(args.Length(), 3);
if (args.Length() > 0) {
CHECK(args[0]->IsNumber());
options.sample_interval =
static_cast<uint64_t>(args[0].As<v8::Number>()->Value());
}
if (args.Length() > 1) {
CHECK(args[1]->IsInt32());
options.stack_depth = args[1].As<v8::Int32>()->Value();
}
if (args.Length() > 2) {
CHECK(args[2]->IsUint32());
options.flags = static_cast<v8::HeapProfiler::SamplingFlags>(
args[2].As<v8::Uint32>()->Value());
}
return options;
}

CpuProfileOptions ParseCpuProfileOptions(
const v8::FunctionCallbackInfo<Value>& args) {
CpuProfileOptions options;
CHECK_LE(args.Length(), 2);
if (args.Length() > 0) {
CHECK(args[0]->IsInt32());
options.sampling_interval_us = args[0].As<v8::Int32>()->Value();
}
if (args.Length() > 1) {
CHECK(args[1]->IsUint32());
options.max_samples = args[1].As<v8::Uint32>()->Value();
}
return options;
}

} // namespace node
59 changes: 59 additions & 0 deletions src/node_profiling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright Joyent, Inc. and other Node contributors.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this file header necessary for these new files?

//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef SRC_NODE_PROFILING_H_
#define SRC_NODE_PROFILING_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "v8-profiler.h"
#include "v8.h"

#include <cstdint>
#include <sstream>

namespace node {

struct HeapProfileOptions {
uint64_t sample_interval = 512 * 1024;
int stack_depth = 16;
v8::HeapProfiler::SamplingFlags flags =
v8::HeapProfiler::SamplingFlags::kSamplingNoFlags;
};

HeapProfileOptions ParseHeapProfileOptions(
const v8::FunctionCallbackInfo<v8::Value>& args);

bool SerializeHeapProfile(v8::Isolate* isolate, std::ostringstream& out_stream);

struct CpuProfileOptions {
int sampling_interval_us = 0;
uint32_t max_samples = v8::CpuProfilingOptions::kNoSampleLimit;
};

CpuProfileOptions ParseCpuProfileOptions(
const v8::FunctionCallbackInfo<v8::Value>& args);

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_NODE_PROFILING_H_
1 change: 1 addition & 0 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_external_reference.h"
#include "node_profiling.h"
#include "util-inl.h"
#include "v8-profiler.h"
#include "v8.h"
Expand Down
1 change: 1 addition & 0 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "node_external_reference.h"
#include "node_options-inl.h"
#include "node_perf.h"
#include "node_profiling.h"
#include "node_snapshot_builder.h"
#include "permission/permission.h"
#include "util-inl.h"
Expand Down
96 changes: 0 additions & 96 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include "debug_utils-inl.h"
#include "env-inl.h"
#include "json_utils.h"
#include "node_buffer.h"
#include "node_errors.h"
#include "node_internals.h"
Expand Down Expand Up @@ -86,13 +85,10 @@ constexpr int kMaximumCopyMode =

namespace node {

using v8::AllocationProfile;
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::Context;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::HeapProfiler;
using v8::Isolate;
using v8::Local;
using v8::Object;
Expand Down Expand Up @@ -816,96 +812,4 @@ v8::Maybe<int> GetValidFileMode(Environment* env,
return v8::Just(mode);
}

static void BuildHeapProfileNode(Isolate* isolate,
const AllocationProfile::Node* node,
JSONWriter* writer) {
size_t selfSize = 0;
for (const auto& allocation : node->allocations)
selfSize += allocation.size * allocation.count;

writer->json_keyvalue("selfSize", selfSize);
writer->json_keyvalue("id", node->node_id);
writer->json_objectstart("callFrame");
writer->json_keyvalue("scriptId", node->script_id);
writer->json_keyvalue("lineNumber", node->line_number - 1);
writer->json_keyvalue("columnNumber", node->column_number - 1);
Utf8Value name(isolate, node->name);
Utf8Value script_name(isolate, node->script_name);
writer->json_keyvalue("functionName", *name);
writer->json_keyvalue("url", *script_name);
writer->json_objectend();

writer->json_arraystart("children");
for (const auto* child : node->children) {
writer->json_start();
BuildHeapProfileNode(isolate, child, writer);
writer->json_end();
}
writer->json_arrayend();
}

bool SerializeHeapProfile(Isolate* isolate, std::ostringstream& out_stream) {
HandleScope scope(isolate);
HeapProfiler* profiler = isolate->GetHeapProfiler();
std::unique_ptr<AllocationProfile> profile(profiler->GetAllocationProfile());
if (!profile) {
return false;
}
profiler->StopSamplingHeapProfiler();
JSONWriter writer(out_stream, true);
writer.json_start();

writer.json_arraystart("samples");
for (const auto& sample : profile->GetSamples()) {
writer.json_start();
writer.json_keyvalue("size", sample.size * sample.count);
writer.json_keyvalue("nodeId", sample.node_id);
writer.json_keyvalue("ordinal", static_cast<double>(sample.sample_id));
writer.json_end();
}
writer.json_arrayend();

writer.json_objectstart("head");
BuildHeapProfileNode(isolate, profile->GetRootNode(), &writer);
writer.json_objectend();

writer.json_end();
return true;
}

HeapProfileOptions ParseHeapProfileOptions(
const v8::FunctionCallbackInfo<v8::Value>& args) {
HeapProfileOptions options;
CHECK_LE(args.Length(), 3);
if (args.Length() > 0) {
CHECK(args[0]->IsNumber());
options.sample_interval =
static_cast<uint64_t>(args[0].As<v8::Number>()->Value());
}
if (args.Length() > 1) {
CHECK(args[1]->IsInt32());
options.stack_depth = args[1].As<v8::Int32>()->Value();
}
if (args.Length() > 2) {
CHECK(args[2]->IsUint32());
options.flags = static_cast<v8::HeapProfiler::SamplingFlags>(
args[2].As<v8::Uint32>()->Value());
}
return options;
}

CpuProfileOptions ParseCpuProfileOptions(
const v8::FunctionCallbackInfo<v8::Value>& args) {
CpuProfileOptions options;
CHECK_LE(args.Length(), 2);
if (args.Length() > 0) {
CHECK(args[0]->IsInt32());
options.sampling_interval_us = args[0].As<v8::Int32>()->Value();
}
if (args.Length() > 1) {
CHECK(args[1]->IsUint32());
options.max_samples = args[1].As<v8::Uint32>()->Value();
}
return options;
}
} // namespace node
20 changes: 0 additions & 20 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1078,26 +1078,6 @@ inline v8::Local<v8::String> Uint32ToString(v8::Local<v8::Context> context,
->ToString(context)
.ToLocalChecked();
}
bool SerializeHeapProfile(v8::Isolate* isolate, std::ostringstream& out_stream);

struct HeapProfileOptions {
uint64_t sample_interval = 512 * 1024;
int stack_depth = 16;
v8::HeapProfiler::SamplingFlags flags =
v8::HeapProfiler::SamplingFlags::kSamplingNoFlags;
};

HeapProfileOptions ParseHeapProfileOptions(
const v8::FunctionCallbackInfo<v8::Value>& args);

struct CpuProfileOptions {
int sampling_interval_us = 0;
uint32_t max_samples = v8::CpuProfilingOptions::kNoSampleLimit;
};

CpuProfileOptions ParseCpuProfileOptions(
const v8::FunctionCallbackInfo<v8::Value>& args);

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
Loading