From d08800799f487c2ab02cf567dca2e4ecfb589b63 Mon Sep 17 00:00:00 2001 From: Yang Guo Date: Tue, 20 Nov 2018 09:16:23 +0100 Subject: [PATCH 3/4] deps: cherry-pick 073073b from upstream V8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: [profiler] introduce API to enable detailed source positions This allows Node.js to enable detailed source positions for optimized code early on, without having to pass a flag string. R=petermarshall@chromium.org Change-Id: Ie74ea41f600cf6e31acbe802116df4976ccf1c75 Reviewed-on: https://chromium-review.googlesource.com/c/1319757 Commit-Queue: Yang Guo Reviewed-by: Peter Marshall Cr-Commit-Position: refs/heads/master@{#57380} Refs: https://github.com/v8/v8/commit/073073b4f12b683fc0406cd15b3cb284633fe18e PR-URL: https://github.com/nodejs/node/pull/24515 Refs: https://github.com/nodejs/node/pull/24274 Refs: https://github.com/nodejs/node/pull/24394 Refs: https://github.com/nodejs/node/issues/24393 Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung Reviewed-By: Peter Marshall Reviewed-By: James M Snell Reviewed-By: Refael Ackermann --- include/v8-profiler.h | 6 +++++ src/api.cc | 5 ++++ src/isolate.cc | 3 ++- src/isolate.h | 3 ++- test/cctest/test-cpu-profiler.cc | 56 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) diff --git a/include/v8-profiler.h b/include/v8-profiler.h index c034518def..f30688582d 100644 --- a/include/v8-profiler.h +++ b/include/v8-profiler.h @@ -341,6 +341,12 @@ class V8_EXPORT CpuProfiler { V8_DEPRECATED("Use Isolate::SetIdle(bool) instead.", void SetIdle(bool is_idle)); + /** + * Generate more detailed source positions to code objects. This results in + * better results when mapping profiling samples to script source. + */ + static void UseDetailedSourcePositionsForProfiling(Isolate* isolate); + private: CpuProfiler(); ~CpuProfiler(); diff --git a/src/api.cc b/src/api.cc index 4e233d96dc..161538638b 100644 --- a/src/api.cc +++ b/src/api.cc @@ -10112,6 +10112,11 @@ void CpuProfiler::SetIdle(bool is_idle) { isolate->SetIdle(is_idle); } +void CpuProfiler::UseDetailedSourcePositionsForProfiling(Isolate* isolate) { + reinterpret_cast(isolate) + ->set_detailed_source_positions_for_profiling(true); +} + uintptr_t CodeEvent::GetCodeStartAddress() { return reinterpret_cast(this)->code_start_address; } diff --git a/src/isolate.cc b/src/isolate.cc index 94033f446b..e6a9e95a2f 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -3461,7 +3461,8 @@ bool Isolate::use_optimizer() { } bool Isolate::NeedsDetailedOptimizedCodeLineInfo() const { - return NeedsSourcePositionsForProfiling() || FLAG_detailed_line_info; + return NeedsSourcePositionsForProfiling() || + detailed_source_positions_for_profiling(); } bool Isolate::NeedsSourcePositionsForProfiling() const { diff --git a/src/isolate.h b/src/isolate.h index ad124586cc..c25f143cf8 100644 --- a/src/isolate.h +++ b/src/isolate.h @@ -540,7 +540,8 @@ typedef std::vector DebugObjectCache; V(int, last_console_context_id, 0) \ V(v8_inspector::V8Inspector*, inspector, nullptr) \ V(bool, next_v8_call_is_safe_for_termination, false) \ - V(bool, only_terminate_in_safe_scope, false) + V(bool, only_terminate_in_safe_scope, false) \ + V(bool, detailed_source_positions_for_profiling, FLAG_detailed_line_info) #define THREAD_LOCAL_TOP_ACCESSOR(type, name) \ inline void set_##name(type v) { thread_local_top_.name##_ = v; } \ diff --git a/test/cctest/test-cpu-profiler.cc b/test/cctest/test-cpu-profiler.cc index 75af3f6d98..e08bec375e 100644 --- a/test/cctest/test-cpu-profiler.cc +++ b/test/cctest/test-cpu-profiler.cc @@ -40,6 +40,7 @@ #include "src/objects-inl.h" #include "src/profiler/cpu-profiler-inl.h" #include "src/profiler/profiler-listener.h" +#include "src/source-position-table.h" #include "src/utils.h" #include "test/cctest/cctest.h" #include "test/cctest/profiler-extension.h" @@ -2544,6 +2545,61 @@ TEST(MultipleProfilers) { profiler2->StopProfiling("2"); } +int GetSourcePositionEntryCount(i::Isolate* isolate, const char* source) { + i::Handle function = i::Handle::cast( + v8::Utils::OpenHandle(*CompileRun(source))); + if (function->IsInterpreted()) return -1; + i::Handle code(function->code(), isolate); + i::SourcePositionTableIterator iterator( + ByteArray::cast(code->source_position_table())); + int count = 0; + while (!iterator.done()) { + count++; + iterator.Advance(); + } + return count; +} + +UNINITIALIZED_TEST(DetailedSourcePositionAPI) { + i::FLAG_detailed_line_info = false; + i::FLAG_allow_natives_syntax = true; + v8::Isolate::CreateParams create_params; + create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); + v8::Isolate* isolate = v8::Isolate::New(create_params); + + const char* source = + "function fib(i) {" + " if (i <= 1) return 1; " + " return fib(i - 1) +" + " fib(i - 2);" + "}" + "fib(5);" + "%OptimizeFunctionOnNextCall(fib);" + "fib(5);" + "fib"; + { + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + v8::Local context = v8::Context::New(isolate); + v8::Context::Scope context_scope(context); + i::Isolate* i_isolate = reinterpret_cast(isolate); + + CHECK(!i_isolate->NeedsDetailedOptimizedCodeLineInfo()); + + int non_detailed_positions = GetSourcePositionEntryCount(i_isolate, source); + + v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate); + CHECK(i_isolate->NeedsDetailedOptimizedCodeLineInfo()); + + int detailed_positions = GetSourcePositionEntryCount(i_isolate, source); + + CHECK((non_detailed_positions == -1 && detailed_positions == -1) || + non_detailed_positions < detailed_positions); + } + + isolate->Dispose(); +} + } // namespace test_cpu_profiler } // namespace internal } // namespace v8 -- 2.14.3 (Apple Git-98)