Skip to content

Commit 294d2ea

Browse files
joyeecheungtargos
authored andcommittedMay 6, 2019
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: #27535 Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent bdabf69 commit 294d2ea

File tree

2 files changed

+38
-46
lines changed

2 files changed

+38
-46
lines changed
 

‎src/inspector_profiler.cc

+29-45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "inspector_profiler.h"
2+
#include <sstream>
23
#include "base_object-inl.h"
34
#include "debug_utils.h"
45
#include "node_file.h"
@@ -33,21 +34,34 @@ const char* const kPathSeparator = "/";
3334
#define CWD_BUFSIZE (PATH_MAX)
3435
#endif
3536

36-
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
37-
Local<Value> value) {
38-
TwoByteValue buffer(isolate, value);
39-
return StringBuffer::create(StringView(*buffer, buffer.length()));
40-
}
41-
4237
V8ProfilerConnection::V8ProfilerConnection(Environment* env)
4338
: session_(env->inspector_agent()->Connect(
4439
std::make_unique<V8ProfilerConnection::V8ProfilerSessionDelegate>(
4540
this),
4641
false)),
4742
env_(env) {}
4843

49-
void V8ProfilerConnection::DispatchMessage(Local<String> message) {
50-
session_->Dispatch(ToProtocolString(env()->isolate(), message)->string());
44+
size_t V8ProfilerConnection::DispatchMessage(const char* method,
45+
const char* params) {
46+
std::stringstream ss;
47+
size_t id = next_id();
48+
ss << R"({ "id": )" << id;
49+
DCHECK(method != nullptr);
50+
ss << R"(, "method": ")" << method << '"';
51+
if (params != nullptr) {
52+
ss << R"(, "params": )" << params;
53+
}
54+
ss << " }";
55+
std::string message = ss.str();
56+
const uint8_t* message_data =
57+
reinterpret_cast<const uint8_t*>(message.c_str());
58+
Debug(env(),
59+
DebugCategory::INSPECTOR_PROFILER,
60+
"Dispatching message %s\n",
61+
message.c_str());
62+
session_->Dispatch(StringView(message_data, message.length()));
63+
// TODO(joyeecheung): use this to identify the ending message.
64+
return id;
5165
}
5266

5367
static void WriteResult(Environment* env,
@@ -202,34 +216,15 @@ std::string V8CoverageConnection::GetDirectory() const {
202216
}
203217

204218
void V8CoverageConnection::Start() {
205-
Debug(env(),
206-
DebugCategory::INSPECTOR_PROFILER,
207-
"Sending Profiler.startPreciseCoverage\n");
208-
Isolate* isolate = env()->isolate();
209-
Local<String> enable = FIXED_ONE_BYTE_STRING(
210-
isolate, R"({"id": 1, "method": "Profiler.enable"})");
211-
Local<String> start = FIXED_ONE_BYTE_STRING(isolate, R"({
212-
"id": 2,
213-
"method": "Profiler.startPreciseCoverage",
214-
"params": { "callCount": true, "detailed": true }
215-
})");
216-
DispatchMessage(enable);
217-
DispatchMessage(start);
219+
DispatchMessage("Profiler.enable");
220+
DispatchMessage("Profiler.startPreciseCoverage",
221+
R"({ "callCount": true, "detailed": true })");
218222
}
219223

220224
void V8CoverageConnection::End() {
221225
CHECK_EQ(ending_, false);
222226
ending_ = true;
223-
Debug(env(),
224-
DebugCategory::INSPECTOR_PROFILER,
225-
"Sending Profiler.takePreciseCoverage\n");
226-
Isolate* isolate = env()->isolate();
227-
HandleScope scope(isolate);
228-
Local<String> end = FIXED_ONE_BYTE_STRING(isolate, R"({
229-
"id": 3,
230-
"method": "Profiler.takePreciseCoverage"
231-
})");
232-
DispatchMessage(end);
227+
DispatchMessage("Profiler.takePreciseCoverage");
233228
}
234229

235230
std::string V8CpuProfilerConnection::GetDirectory() const {
@@ -257,25 +252,14 @@ MaybeLocal<Object> V8CpuProfilerConnection::GetProfile(Local<Object> result) {
257252
}
258253

259254
void V8CpuProfilerConnection::Start() {
260-
Debug(env(), DebugCategory::INSPECTOR_PROFILER, "Sending Profiler.start\n");
261-
Isolate* isolate = env()->isolate();
262-
Local<String> enable = FIXED_ONE_BYTE_STRING(
263-
isolate, R"({"id": 1, "method": "Profiler.enable"})");
264-
Local<String> start = FIXED_ONE_BYTE_STRING(
265-
isolate, R"({"id": 2, "method": "Profiler.start"})");
266-
DispatchMessage(enable);
267-
DispatchMessage(start);
255+
DispatchMessage("Profiler.enable");
256+
DispatchMessage("Profiler.start");
268257
}
269258

270259
void V8CpuProfilerConnection::End() {
271260
CHECK_EQ(ending_, false);
272261
ending_ = true;
273-
Debug(env(), DebugCategory::INSPECTOR_PROFILER, "Sending Profiler.stop\n");
274-
Isolate* isolate = env()->isolate();
275-
HandleScope scope(isolate);
276-
Local<String> end =
277-
FIXED_ONE_BYTE_STRING(isolate, R"({"id": 3, "method": "Profiler.stop"})");
278-
DispatchMessage(end);
262+
DispatchMessage("Profiler.stop");
279263
}
280264

281265
// For now, we only support coverage profiling, but we may add more

‎src/inspector_profiler.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ class V8ProfilerConnection {
3434
virtual ~V8ProfilerConnection() = default;
3535

3636
Environment* env() const { return env_; }
37-
void DispatchMessage(v8::Local<v8::String> message);
37+
38+
// Dispatch a protocol message, and returns the id of the message.
39+
// `method` does not need to be surrounded by quotes.
40+
// The optional `params` should be formatted in JSON.
41+
// The strings should be in one byte characters - which is enough for
42+
// the commands we use here.
43+
size_t DispatchMessage(const char* method, const char* params = nullptr);
3844

3945
// Use DispatchMessage() to dispatch necessary inspector messages
4046
// to start and end the profiling.
@@ -55,9 +61,11 @@ class V8ProfilerConnection {
5561
v8::Local<v8::Object> result) = 0;
5662

5763
private:
64+
size_t next_id() { return id_++; }
5865
void WriteProfile(v8::Local<v8::String> message);
5966
std::unique_ptr<inspector::InspectorSession> session_;
6067
Environment* env_ = nullptr;
68+
size_t id_ = 1;
6169
};
6270

6371
class V8CoverageConnection : public V8ProfilerConnection {

0 commit comments

Comments
 (0)
Please sign in to comment.