Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2019-05-02 15:41:54 +0300
committerRich Trott <rtrott@gmail.com>2019-05-05 20:19:14 +0300
commit0171bab46d6f57cf6b1ba89d4a79a022139937d1 (patch)
tree8ebd754a1aa9fe4dc043d9a4c66b3d1adeb8e9cb /src/inspector_profiler.cc
parent3d98051e207324353c462b1f53ee0e8fe262297d (diff)
src: refactor V8ProfilerConnection::DispatchMessage()
- Auto-generate the message id and return it for future use (we can always parse the response to find the message containing the profile instead of relying on the inspector connection being synchornous). - Generate the message from method and parameter strings and create a `StringView` directly to avoid the unnecessary copy in `ToProtocolString()`. PR-URL: https://github.com/nodejs/node/pull/27535 Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'src/inspector_profiler.cc')
-rw-r--r--src/inspector_profiler.cc74
1 files changed, 29 insertions, 45 deletions
diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc
index 530636f4d09..a3739d52b21 100644
--- a/src/inspector_profiler.cc
+++ b/src/inspector_profiler.cc
@@ -1,4 +1,5 @@
#include "inspector_profiler.h"
+#include <sstream>
#include "base_object-inl.h"
#include "debug_utils.h"
#include "node_file.h"
@@ -33,12 +34,6 @@ const char* const kPathSeparator = "/";
#define CWD_BUFSIZE (PATH_MAX)
#endif
-std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
- Local<Value> value) {
- TwoByteValue buffer(isolate, value);
- return StringBuffer::create(StringView(*buffer, buffer.length()));
-}
-
V8ProfilerConnection::V8ProfilerConnection(Environment* env)
: session_(env->inspector_agent()->Connect(
std::make_unique<V8ProfilerConnection::V8ProfilerSessionDelegate>(
@@ -46,8 +41,27 @@ V8ProfilerConnection::V8ProfilerConnection(Environment* env)
false)),
env_(env) {}
-void V8ProfilerConnection::DispatchMessage(Local<String> message) {
- session_->Dispatch(ToProtocolString(env()->isolate(), message)->string());
+size_t V8ProfilerConnection::DispatchMessage(const char* method,
+ const char* params) {
+ std::stringstream ss;
+ size_t id = next_id();
+ ss << R"({ "id": )" << id;
+ DCHECK(method != nullptr);
+ ss << R"(, "method": ")" << method << '"';
+ if (params != nullptr) {
+ ss << R"(, "params": )" << params;
+ }
+ ss << " }";
+ std::string message = ss.str();
+ const uint8_t* message_data =
+ reinterpret_cast<const uint8_t*>(message.c_str());
+ Debug(env(),
+ DebugCategory::INSPECTOR_PROFILER,
+ "Dispatching message %s\n",
+ message.c_str());
+ session_->Dispatch(StringView(message_data, message.length()));
+ // TODO(joyeecheung): use this to identify the ending message.
+ return id;
}
static void WriteResult(Environment* env,
@@ -202,34 +216,15 @@ std::string V8CoverageConnection::GetDirectory() const {
}
void V8CoverageConnection::Start() {
- Debug(env(),
- DebugCategory::INSPECTOR_PROFILER,
- "Sending Profiler.startPreciseCoverage\n");
- Isolate* isolate = env()->isolate();
- Local<String> enable = FIXED_ONE_BYTE_STRING(
- isolate, R"({"id": 1, "method": "Profiler.enable"})");
- Local<String> start = FIXED_ONE_BYTE_STRING(isolate, R"({
- "id": 2,
- "method": "Profiler.startPreciseCoverage",
- "params": { "callCount": true, "detailed": true }
- })");
- DispatchMessage(enable);
- DispatchMessage(start);
+ DispatchMessage("Profiler.enable");
+ DispatchMessage("Profiler.startPreciseCoverage",
+ R"({ "callCount": true, "detailed": true })");
}
void V8CoverageConnection::End() {
CHECK_EQ(ending_, false);
ending_ = true;
- Debug(env(),
- DebugCategory::INSPECTOR_PROFILER,
- "Sending Profiler.takePreciseCoverage\n");
- Isolate* isolate = env()->isolate();
- HandleScope scope(isolate);
- Local<String> end = FIXED_ONE_BYTE_STRING(isolate, R"({
- "id": 3,
- "method": "Profiler.takePreciseCoverage"
- })");
- DispatchMessage(end);
+ DispatchMessage("Profiler.takePreciseCoverage");
}
std::string V8CpuProfilerConnection::GetDirectory() const {
@@ -257,25 +252,14 @@ MaybeLocal<Object> V8CpuProfilerConnection::GetProfile(Local<Object> result) {
}
void V8CpuProfilerConnection::Start() {
- Debug(env(), DebugCategory::INSPECTOR_PROFILER, "Sending Profiler.start\n");
- Isolate* isolate = env()->isolate();
- Local<String> enable = FIXED_ONE_BYTE_STRING(
- isolate, R"({"id": 1, "method": "Profiler.enable"})");
- Local<String> start = FIXED_ONE_BYTE_STRING(
- isolate, R"({"id": 2, "method": "Profiler.start"})");
- DispatchMessage(enable);
- DispatchMessage(start);
+ DispatchMessage("Profiler.enable");
+ DispatchMessage("Profiler.start");
}
void V8CpuProfilerConnection::End() {
CHECK_EQ(ending_, false);
ending_ = true;
- Debug(env(), DebugCategory::INSPECTOR_PROFILER, "Sending Profiler.stop\n");
- Isolate* isolate = env()->isolate();
- HandleScope scope(isolate);
- Local<String> end =
- FIXED_ONE_BYTE_STRING(isolate, R"({"id": 3, "method": "Profiler.stop"})");
- DispatchMessage(end);
+ DispatchMessage("Profiler.stop");
}
// For now, we only support coverage profiling, but we may add more