-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds support for trace-event tracing to Node.js. It provides a mechanism to centralize tracing information generated by V8, Node core, and userspace code. It includes: - A trace writer responsible for serializing traces and cycling the output files so that no individual file becomes to large. - A buffer for aggregating traces to allow for batched flushes. - An agent which initializes the tracing controller and ensures that trace serialization is done on a separate thread. - A set of macros for generating trace events. - Tests and documentation. Author: Raymond Kang <raymondksi@gmail.com> Author: Kelvin Jin <kelvinjin@google.com> Author: Matthew Loring <mattloring@google.com> Author: Jason Ginchereau <jasongin@microsoft.com> PR-URL: #11106 Reviewed-By: Josh Gavant <josh.gavant@outlook.com>
- Loading branch information
1 parent
1c7f221
commit bd4ccc8
Showing
15 changed files
with
2,458 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Tracing | ||
|
||
Trace Event provides a mechanism to centralize tracing information generated by | ||
V8, Node core, and userspace code. | ||
|
||
Tracing can be enabled by passing the `--trace-events-enabled` flag when starting a | ||
Node.js application. | ||
|
||
The set of categories for which traces are recorded can be specified using the | ||
`--trace-event-categories` flag followed by a list of comma separated category names. | ||
By default the `node` and `v8` categories are enabled. | ||
|
||
```txt | ||
node --trace-events-enabled --trace-event-categories v8,node server.js | ||
``` | ||
|
||
Running Node.js with tracing enabled will produce log files that can be opened | ||
in the [`chrome://tracing`](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool) | ||
tab of Chrome. |
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,72 @@ | ||
#include "tracing/agent.h" | ||
|
||
#include <sstream> | ||
#include <string> | ||
|
||
#include "env-inl.h" | ||
#include "libplatform/libplatform.h" | ||
|
||
namespace node { | ||
namespace tracing { | ||
|
||
using v8::platform::tracing::TraceConfig; | ||
|
||
Agent::Agent() {} | ||
|
||
void Agent::Start(v8::Platform* platform, const char* enabled_categories) { | ||
platform_ = platform; | ||
|
||
int err = uv_loop_init(&tracing_loop_); | ||
CHECK_EQ(err, 0); | ||
|
||
NodeTraceWriter* trace_writer = new NodeTraceWriter(&tracing_loop_); | ||
TraceBuffer* trace_buffer = new NodeTraceBuffer( | ||
NodeTraceBuffer::kBufferChunks, trace_writer, &tracing_loop_); | ||
|
||
tracing_controller_ = new TracingController(); | ||
|
||
TraceConfig* trace_config = new TraceConfig(); | ||
if (enabled_categories) { | ||
std::stringstream category_list(enabled_categories); | ||
while (category_list.good()) { | ||
std::string category; | ||
getline(category_list, category, ','); | ||
trace_config->AddIncludedCategory(category.c_str()); | ||
} | ||
} else { | ||
trace_config->AddIncludedCategory("v8"); | ||
trace_config->AddIncludedCategory("node"); | ||
} | ||
|
||
// This thread should be created *after* async handles are created | ||
// (within NodeTraceWriter and NodeTraceBuffer constructors). | ||
// Otherwise the thread could shut down prematurely. | ||
err = uv_thread_create(&thread_, ThreadCb, this); | ||
CHECK_EQ(err, 0); | ||
|
||
tracing_controller_->Initialize(trace_buffer); | ||
tracing_controller_->StartTracing(trace_config); | ||
v8::platform::SetTracingController(platform, tracing_controller_); | ||
} | ||
|
||
void Agent::Stop() { | ||
if (!IsStarted()) { | ||
return; | ||
} | ||
// Perform final Flush on TraceBuffer. We don't want the tracing controller | ||
// to flush the buffer again on destruction of the V8::Platform. | ||
tracing_controller_->StopTracing(); | ||
delete tracing_controller_; | ||
// Thread should finish when the tracing loop is stopped. | ||
uv_thread_join(&thread_); | ||
v8::platform::SetTracingController(platform_, nullptr); | ||
} | ||
|
||
// static | ||
void Agent::ThreadCb(void* arg) { | ||
Agent* agent = static_cast<Agent*>(arg); | ||
uv_run(&agent->tracing_loop_, UV_RUN_DEFAULT); | ||
} | ||
|
||
} // namespace tracing | ||
} // namespace node |
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,31 @@ | ||
#ifndef SRC_TRACING_AGENT_H_ | ||
#define SRC_TRACING_AGENT_H_ | ||
|
||
#include "tracing/node_trace_buffer.h" | ||
#include "tracing/node_trace_writer.h" | ||
#include "uv.h" | ||
#include "v8.h" | ||
|
||
namespace node { | ||
namespace tracing { | ||
|
||
class Agent { | ||
public: | ||
explicit Agent(); | ||
void Start(v8::Platform* platform, const char* enabled_categories); | ||
void Stop(); | ||
|
||
private: | ||
bool IsStarted() { return platform_ != nullptr; } | ||
static void ThreadCb(void* arg); | ||
|
||
uv_thread_t thread_; | ||
uv_loop_t tracing_loop_; | ||
v8::Platform* platform_ = nullptr; | ||
TracingController* tracing_controller_; | ||
}; | ||
|
||
} // namespace tracing | ||
} // namespace node | ||
|
||
#endif // SRC_TRACING_AGENT_H_ |
Oops, something went wrong.