Skip to content

Commit

Permalink
feat: per-isolate node report (#130)
Browse files Browse the repository at this point in the history
PR-URL: #130
Reviewed-BY: hyj1991 <[email protected]>
  • Loading branch information
legendecas authored Feb 10, 2022
1 parent c4ced63 commit 391d395
Show file tree
Hide file tree
Showing 26 changed files with 159 additions and 169 deletions.
2 changes: 1 addition & 1 deletion src/commands/dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void HandleAction(void *data, string notify_type) {
break;
}
case NODE_REPORT: {
NodeReport::GetNodeReport(node_report_filepath);
NodeReport::GetNodeReport(node_isolate, node_report_filepath);
AfterDumpFile(node_report_filepath, notify_type, unique_key);
action_map.erase(NODE_REPORT);
break;
Expand Down
14 changes: 6 additions & 8 deletions src/commands/report/heap_statistics.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include "heap_statistics.h"

#include "node_report.h"
#include "v8.h"

namespace xprofiler {
using v8::HeapSpaceStatistics;
using v8::HeapStatistics;
using v8::Isolate;

void SetHeapStatistics(JSONWriter* writer) {
Isolate* isolate = Isolate::GetCurrent();
void NodeReport::SetHeapStatistics(JSONWriter* writer) {
HeapStatistics v8_heap_stats;
isolate->GetHeapStatistics(&v8_heap_stats);
isolate_->GetHeapStatistics(&v8_heap_stats);

writer->json_objectstart("heapStatistics");
writer->json_keyvalue("heapTotal", v8_heap_stats.total_heap_size());
Expand All @@ -24,8 +22,8 @@ void SetHeapStatistics(JSONWriter* writer) {

HeapSpaceStatistics v8_heap_space_stats;
writer->json_arraystart("heapSpaceStatistics");
for (size_t i = 0; i < isolate->NumberOfHeapSpaces(); i++) {
isolate->GetHeapSpaceStatistics(&v8_heap_space_stats, i);
for (size_t i = 0; i < isolate_->NumberOfHeapSpaces(); i++) {
isolate_->GetHeapSpaceStatistics(&v8_heap_space_stats, i);
writer->json_start();
writer->json_keyvalue("name", v8_heap_space_stats.space_name());
writer->json_keyvalue("size", v8_heap_space_stats.space_size());
Expand All @@ -41,4 +39,4 @@ void SetHeapStatistics(JSONWriter* writer) {
}
writer->json_arrayend();
}
} // namespace xprofiler
} // namespace xprofiler
10 changes: 0 additions & 10 deletions src/commands/report/heap_statistics.h

This file was deleted.

23 changes: 11 additions & 12 deletions src/commands/report/javascript_stack.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "javascript_stack.h"

#include "../../platform/platform.h"
#include "nan.h"
#include "node_report.h"
#include "platform/platform.h"
#include "util.h"
#include "xpf_v8.h"

static const unsigned kMaxFramesCount = 255;
static const size_t kMaxFramesCount = 255;
#if (NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION)
static const char* v8_states[] = {
"JS", "GC", "PARSER", "BYTECODE_COMPILER",
Expand All @@ -15,7 +16,6 @@ static const char* v8_states[] = {
#endif

namespace xprofiler {
using Nan::HandleScope;
using Nan::Utf8String;
using v8::Isolate;
using v8::Local;
Expand All @@ -24,11 +24,10 @@ using v8::SampleInfo;
using v8::StackFrame;
using v8::StackTrace;

void SetJavaScriptStack(JSONWriter* writer, bool fatal_error) {
HandleScope scope;
void NodeReport::SetJavaScriptStack(JSONWriter* writer, bool fatal_error) {
HandleScope scope(isolate_);
RegisterState state;
SampleInfo info;
Isolate* isolate = Isolate::GetCurrent();

// init state
state.pc = nullptr;
Expand All @@ -39,10 +38,10 @@ void SetJavaScriptStack(JSONWriter* writer, bool fatal_error) {
void* samples[kMaxFramesCount];

// get instruction pointer
isolate->GetStackSample(state, samples, kMaxFramesCount, &info);
isolate_->GetStackSample(state, samples, kMaxFramesCount, &info);

// set current vm state
if (static_cast<size_t>(info.vm_state) < 8) {
if (static_cast<size_t>(info.vm_state) < arraysize(v8_states)) {
writer->json_keyvalue("vmState", v8_states[info.vm_state]);
} else {
writer->json_keyvalue("vmState", "unknown");
Expand All @@ -56,7 +55,7 @@ void SetJavaScriptStack(JSONWriter* writer, bool fatal_error) {

// get js stacks
Local<StackTrace> stack = StackTrace::CurrentStackTrace(
isolate, kMaxFramesCount, StackTrace::kDetailed);
isolate_, kMaxFramesCount, StackTrace::kDetailed);
writer->json_arraystart("jsStacks");
for (int i = 0; i < stack->GetFrameCount(); i++) {
writer->json_start();
Expand All @@ -68,7 +67,7 @@ void SetJavaScriptStack(JSONWriter* writer, bool fatal_error) {

#if (NODE_VERSION_AT_LEAST(10, 12, 0))
// needs v8 version >= 6.8
Local<StackFrame> frame = stack->GetFrame(isolate, i);
Local<StackFrame> frame = stack->GetFrame(isolate_, i);
#else
Local<StackFrame> frame = stack->GetFrame(i);
#endif
Expand Down
10 changes: 0 additions & 10 deletions src/commands/report/javascript_stack.h

This file was deleted.

13 changes: 8 additions & 5 deletions src/commands/report/native_stack.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "native_stack.h"

#include "../../platform/platform.h"
#include "node_report.h"
#include "platform/platform.h"

namespace xprofiler {
void SetNativeStack(JSONWriter* writer) { PrintNativeStack(writer); }
} // namespace xprofiler

void NodeReport::SetNativeStack(JSONWriter* writer) {
PrintNativeStack(writer);
}

} // namespace xprofiler
10 changes: 0 additions & 10 deletions src/commands/report/native_stack.h

This file was deleted.

28 changes: 12 additions & 16 deletions src/commands/report/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@

#include <fstream>

#include "../../library/common.h"
#include "../../library/utils.h"
#include "../../library/writer.h"
#include "../../logger.h"
#include "../../platform/platform.h"
#include "heap_statistics.h"
#include "javascript_stack.h"
#include "native_stack.h"
#include "system_statistics.h"
#include "uv_statistics.h"
#include "library/common.h"
#include "library/utils.h"
#include "library/writer.h"
#include "logger.h"
#include "platform/platform.h"

namespace xprofiler {
using std::ios;
using std::ofstream;

NodeReport::NodeReport() {}
NodeReport::~NodeReport() {}
NodeReport::NodeReport(v8::Isolate* isolate) : isolate_(isolate) {}

static void WriteNodeReport(JSONWriter *writer, string location, string message,
bool fatal_error) {
void NodeReport::WriteNodeReport(JSONWriter* writer, std::string location,
std::string message, bool fatal_error) {
writer->json_start();

writer->json_keyvalue("pid", GetPid());
Expand All @@ -41,8 +35,10 @@ static void WriteNodeReport(JSONWriter *writer, string location, string message,
writer->json_end();
}

void NodeReport::GetNodeReport(string filepath, string location, string message,
void NodeReport::GetNodeReport(v8::Isolate* isolate, std::string filepath,
std::string location, std::string message,
bool fatal_error) {
NodeReport report(isolate);
ofstream outfile;
outfile.open(filepath, ios::out | ios::binary);
if (!outfile.is_open()) {
Expand All @@ -51,7 +47,7 @@ void NodeReport::GetNodeReport(string filepath, string location, string message,
return;
}
JSONWriter writer(outfile);
WriteNodeReport(&writer, location, message, fatal_error);
report.WriteNodeReport(&writer, location, message, fatal_error);
outfile.close();
}
} // namespace xprofiler
32 changes: 23 additions & 9 deletions src/commands/report/node_report.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
#ifndef _SRC_COMMANDS_REPORT_NODE_REPORT_H
#define _SRC_COMMANDS_REPORT_NODE_REPORT_H
#ifndef XPROFILER_SRC_COMMANDS_REPORT_NODE_REPORT_H
#define XPROFILER_SRC_COMMANDS_REPORT_NODE_REPORT_H

#include <string>

#include "library/writer.h"
#include "nan.h"

namespace xprofiler {
using std::string;
class NodeReport {
class NodeReport final {
public:
NodeReport();
virtual ~NodeReport();
static void GetNodeReport(string filepath, string location = "Active Dump",
string message = "Active Dump",
static void GetNodeReport(v8::Isolate* isolate, std::string filepath,
std::string location = "Active Dump",
std::string message = "Active Dump",
bool fatal_error = false);

private:
NodeReport(v8::Isolate* isolate);
void WriteNodeReport(JSONWriter* writer, std::string location,
std::string message, bool fatal_error);

void SetUvStatistics(JSONWriter* writer);
void SetSystemStatistics(JSONWriter* writer);
void SetNativeStack(JSONWriter* writer);
void SetJavaScriptStack(JSONWriter* writer, bool fatal_error = false);
void SetHeapStatistics(JSONWriter* writer);

v8::Isolate* isolate_;
};
} // namespace xprofiler

#endif
#endif /* XPROFILER_SRC_COMMANDS_REPORT_NODE_REPORT_H */
9 changes: 4 additions & 5 deletions src/commands/report/system_statistics.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "system_statistics.h"

#include "../../platform/platform.h"
#include "node_report.h"
#include "platform/platform.h"

namespace xprofiler {
void SetSystemStatistics(JSONWriter* writer) {
void NodeReport::SetSystemStatistics(JSONWriter* writer) {
writer->json_objectstart("system");

PrintSystemEnv(writer);
Expand All @@ -12,4 +11,4 @@ void SetSystemStatistics(JSONWriter* writer) {

writer->json_objectend();
}
} // namespace xprofiler
} // namespace xprofiler
10 changes: 0 additions & 10 deletions src/commands/report/system_statistics.h

This file was deleted.

16 changes: 10 additions & 6 deletions src/commands/report/uv_statistics.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "uv_statistics.h"

#include <sstream>

#include "../../platform/platform.h"
#include "environment_data.h"
#include "node_report.h"
#include "platform/platform.h"
#include "uv.h"

namespace xprofiler {
Expand Down Expand Up @@ -382,9 +382,13 @@ static void walkHandle(uv_handle_t *h, void *arg) {
writer->json_end();
}

void SetUvStatistics(JSONWriter *writer) {
void NodeReport::SetUvStatistics(JSONWriter* writer) {
writer->json_arraystart("libuvHandles");
uv_walk(uv_default_loop(), walkHandle, (void *)writer);
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate_);
if (env_data != nullptr) {
uv_loop_t* loop = env_data->loop();
uv_walk(loop, walkHandle, (void*)writer);
}
writer->json_arrayend();
}
} // namespace xprofiler
} // namespace xprofiler
10 changes: 0 additions & 10 deletions src/commands/report/uv_statistics.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/configure.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace xprofiler {

inline string GetLogDir();
inline std::string GetLogDir();
inline uint32_t GetLogInterval();
inline LOG_LEVEL GetLogLevel();
inline LOG_TYPE GetLogType();
Expand Down
13 changes: 7 additions & 6 deletions src/environment_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "util.h"
#include "xpf_node.h"
#include "xpf_v8.h"

namespace xprofiler {
using Nan::HandleScope;
using v8::Isolate;

namespace per_process {
Expand Down Expand Up @@ -36,18 +36,19 @@ EnvironmentData* EnvironmentData::Create(v8::Isolate* isolate) {
// TODO(legendecas): environment registry.
CHECK_EQ(per_process::environment_data, nullptr);

CHECK_EQ(isolate, Isolate::GetCurrent());
HandleScope scope;
HandleScope scope(isolate);
uv_loop_t* loop = node::GetCurrentEventLoop(isolate);
CHECK_NOT_NULL(loop);

per_process::environment_data =
std::unique_ptr<EnvironmentData>(new EnvironmentData(isolate));
std::unique_ptr<EnvironmentData>(new EnvironmentData(isolate, loop));
xprofiler::AtExit(isolate, AtExit, nullptr);

return per_process::environment_data.get();
}

EnvironmentData::EnvironmentData(v8::Isolate* isolate) : isolate_(isolate) {
uv_loop_t* loop = node::GetCurrentEventLoop(isolate);
EnvironmentData::EnvironmentData(v8::Isolate* isolate, uv_loop_t* loop)
: isolate_(isolate), loop_(loop) {
CHECK_EQ(0, uv_async_init(loop, &statistics_async_, CollectStatistics));
uv_unref(reinterpret_cast<uv_handle_t*>(&statistics_async_));
}
Expand Down
Loading

0 comments on commit 391d395

Please sign in to comment.