From 28878691f3ae97ecf552b4c0f4a0a6dbc21b7e1c Mon Sep 17 00:00:00 2001 From: Eugene Ostroukhov Date: Wed, 16 May 2018 09:43:17 -0700 Subject: [PATCH] deps: cherry-pick 23652c5f from upstream V8 Original commit message: Custom tag for the traceEvents array This API will be used by Node.js to provide output compatible with Chrome devtools. Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng Change-Id: I265495f8af39bfc78d7fdbe43ac308f0920e817d Reviewed-on: https://chromium-review.googlesource.com/1044491 Reviewed-by: Aleksey Kozyatinskiy Reviewed-by: Ulan Degenbaev Commit-Queue: Eugene Ostroukhov Cr-Commit-Position: refs/heads/master@{#53041} PR-URL: https://github.com/nodejs/node/pull/20608 Reviewed-By: James M Snell Reviewed-By: Ali Ijaz Sheikh Reviewed-By: Matteo Collina --- common.gypi | 2 +- deps/v8/include/libplatform/v8-tracing.h | 2 + .../src/libplatform/tracing/trace-writer.cc | 13 ++- .../v8/src/libplatform/tracing/trace-writer.h | 1 + .../test/cctest/libplatform/test-tracing.cc | 83 +++++++++++-------- 5 files changed, 63 insertions(+), 38 deletions(-) diff --git a/common.gypi b/common.gypi index 04a729e93b7021..dff0f092ebe451 100644 --- a/common.gypi +++ b/common.gypi @@ -27,7 +27,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.3', + 'v8_embedder_string': '-node.4', # Enable disassembler for `--print-code` v8 options 'v8_enable_disassembler': 1, diff --git a/deps/v8/include/libplatform/v8-tracing.h b/deps/v8/include/libplatform/v8-tracing.h index 9dcf3d7bca760a..e430e7c3502741 100644 --- a/deps/v8/include/libplatform/v8-tracing.h +++ b/deps/v8/include/libplatform/v8-tracing.h @@ -112,6 +112,8 @@ class V8_PLATFORM_EXPORT TraceWriter { virtual void Flush() = 0; static TraceWriter* CreateJSONTraceWriter(std::ostream& stream); + static TraceWriter* CreateJSONTraceWriter(std::ostream& stream, + const std::string& tag); private: // Disallow copy and assign diff --git a/deps/v8/src/libplatform/tracing/trace-writer.cc b/deps/v8/src/libplatform/tracing/trace-writer.cc index 36a8783499d140..7bfc7664694f8d 100644 --- a/deps/v8/src/libplatform/tracing/trace-writer.cc +++ b/deps/v8/src/libplatform/tracing/trace-writer.cc @@ -119,8 +119,12 @@ void JSONTraceWriter::AppendArgValue(ConvertableToTraceFormat* value) { stream_ << arg_stringified; } -JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { - stream_ << "{\"traceEvents\":["; +JSONTraceWriter::JSONTraceWriter(std::ostream& stream) + : JSONTraceWriter(stream, "traceEvents") {} + +JSONTraceWriter::JSONTraceWriter(std::ostream& stream, const std::string& tag) + : stream_(stream) { + stream_ << "{\"" << tag << "\":["; } JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } @@ -171,6 +175,11 @@ TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { return new JSONTraceWriter(stream); } +TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream, + const std::string& tag) { + return new JSONTraceWriter(stream, tag); +} + } // namespace tracing } // namespace platform } // namespace v8 diff --git a/deps/v8/src/libplatform/tracing/trace-writer.h b/deps/v8/src/libplatform/tracing/trace-writer.h index 7e1bdc24d687ae..d81135138992f7 100644 --- a/deps/v8/src/libplatform/tracing/trace-writer.h +++ b/deps/v8/src/libplatform/tracing/trace-writer.h @@ -14,6 +14,7 @@ namespace tracing { class JSONTraceWriter : public TraceWriter { public: explicit JSONTraceWriter(std::ostream& stream); + JSONTraceWriter(std::ostream& stream, const std::string& tag); ~JSONTraceWriter(); void AppendTraceEvent(TraceObject* trace_event) override; void Flush() override; diff --git a/deps/v8/test/cctest/libplatform/test-tracing.cc b/deps/v8/test/cctest/libplatform/test-tracing.cc index da202057de312c..b949785bcfacbe 100644 --- a/deps/v8/test/cctest/libplatform/test-tracing.cc +++ b/deps/v8/test/cctest/libplatform/test-tracing.cc @@ -128,44 +128,42 @@ TEST(TestTraceBufferRingBuffer) { delete ring_buffer; } -TEST(TestJSONTraceWriter) { - std::ostringstream stream; - // Create a scope for the tracing controller to terminate the trace writer. - { - v8::Platform* old_platform = i::V8::GetCurrentPlatform(); - std::unique_ptr default_platform( - v8::platform::NewDefaultPlatform()); - i::V8::SetPlatformForTesting(default_platform.get()); - auto tracing = - base::make_unique(); - v8::platform::tracing::TracingController* tracing_controller = - tracing.get(); - static_cast(default_platform.get()) - ->SetTracingController(std::move(tracing)); - TraceWriter* writer = TraceWriter::CreateJSONTraceWriter(stream); +void PopulateJSONWriter(TraceWriter* writer) { + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); + std::unique_ptr default_platform( + v8::platform::NewDefaultPlatform()); + i::V8::SetPlatformForTesting(default_platform.get()); + auto tracing = base::make_unique(); + v8::platform::tracing::TracingController* tracing_controller = tracing.get(); + static_cast(default_platform.get()) + ->SetTracingController(std::move(tracing)); - TraceBuffer* ring_buffer = - TraceBuffer::CreateTraceBufferRingBuffer(1, writer); - tracing_controller->Initialize(ring_buffer); - TraceConfig* trace_config = new TraceConfig(); - trace_config->AddIncludedCategory("v8-cat"); - tracing_controller->StartTracing(trace_config); + TraceBuffer* ring_buffer = + TraceBuffer::CreateTraceBufferRingBuffer(1, writer); + tracing_controller->Initialize(ring_buffer); + TraceConfig* trace_config = new TraceConfig(); + trace_config->AddIncludedCategory("v8-cat"); + tracing_controller->StartTracing(trace_config); - TraceObject trace_object; - trace_object.InitializeForTesting( - 'X', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test0", - v8::internal::tracing::kGlobalScope, 42, 123, 0, nullptr, nullptr, - nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID, 11, 22, 100, 50, 33, 44); - writer->AppendTraceEvent(&trace_object); - trace_object.InitializeForTesting( - 'Y', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test1", - v8::internal::tracing::kGlobalScope, 43, 456, 0, nullptr, nullptr, - nullptr, nullptr, 0, 55, 66, 110, 55, 77, 88); - writer->AppendTraceEvent(&trace_object); - tracing_controller->StopTracing(); - i::V8::SetPlatformForTesting(old_platform); - } + TraceObject trace_object; + trace_object.InitializeForTesting( + 'X', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test0", + v8::internal::tracing::kGlobalScope, 42, 123, 0, nullptr, nullptr, + nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID, 11, 22, 100, 50, 33, 44); + writer->AppendTraceEvent(&trace_object); + trace_object.InitializeForTesting( + 'Y', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test1", + v8::internal::tracing::kGlobalScope, 43, 456, 0, nullptr, nullptr, + nullptr, nullptr, 0, 55, 66, 110, 55, 77, 88); + writer->AppendTraceEvent(&trace_object); + tracing_controller->StopTracing(); + i::V8::SetPlatformForTesting(old_platform); +} +TEST(TestJSONTraceWriter) { + std::ostringstream stream; + TraceWriter* writer = TraceWriter::CreateJSONTraceWriter(stream); + PopulateJSONWriter(writer); std::string trace_str = stream.str(); std::string expected_trace_str = "{\"traceEvents\":[{\"pid\":11,\"tid\":22,\"ts\":100,\"tts\":50," @@ -177,6 +175,21 @@ TEST(TestJSONTraceWriter) { CHECK_EQ(expected_trace_str, trace_str); } +TEST(TestJSONTraceWriterWithCustomtag) { + std::ostringstream stream; + TraceWriter* writer = TraceWriter::CreateJSONTraceWriter(stream, "customTag"); + PopulateJSONWriter(writer); + std::string trace_str = stream.str(); + std::string expected_trace_str = + "{\"customTag\":[{\"pid\":11,\"tid\":22,\"ts\":100,\"tts\":50," + "\"ph\":\"X\",\"cat\":\"v8-cat\",\"name\":\"Test0\",\"dur\":33," + "\"tdur\":44,\"id\":\"0x2a\",\"args\":{}},{\"pid\":55,\"tid\":66," + "\"ts\":110,\"tts\":55,\"ph\":\"Y\",\"cat\":\"v8-cat\",\"name\":" + "\"Test1\",\"dur\":77,\"tdur\":88,\"args\":{}}]}"; + + CHECK_EQ(expected_trace_str, trace_str); +} + TEST(TestTracingController) { v8::Platform* old_platform = i::V8::GetCurrentPlatform(); std::unique_ptr default_platform(