2010-03-19 13:51:01 +00:00
|
|
|
// Copyright 2010 the V8 project authors. All rights reserved.
|
2013-03-07 11:12:26 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2010-03-19 13:51:01 +00:00
|
|
|
//
|
|
|
|
// Tests of profiles generator and utilities.
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
|
|
|
|
|
|
|
#include "include/v8-profiler.h"
|
2017-02-23 11:46:29 +00:00
|
|
|
#include "src/api.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2015-02-12 19:51:26 +00:00
|
|
|
#include "src/deoptimizer.h"
|
2017-02-23 11:46:29 +00:00
|
|
|
#include "src/objects-inl.h"
|
2015-09-28 19:34:08 +00:00
|
|
|
#include "src/profiler/cpu-profiler-inl.h"
|
2016-06-22 16:43:46 +00:00
|
|
|
#include "src/profiler/profiler-listener.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/utils.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/profiler-extension.h"
|
2016-06-15 09:59:00 +00:00
|
|
|
|
2016-10-06 18:14:03 +00:00
|
|
|
#include "include/libplatform/v8-tracing.h"
|
|
|
|
#include "src/tracing/trace-event.h"
|
|
|
|
|
2010-03-19 13:51:01 +00:00
|
|
|
using i::CodeEntry;
|
2010-03-30 11:38:39 +00:00
|
|
|
using i::CpuProfile;
|
2010-08-10 12:06:42 +00:00
|
|
|
using i::CpuProfiler;
|
2010-03-19 13:51:01 +00:00
|
|
|
using i::CpuProfilesCollection;
|
2013-07-08 11:26:15 +00:00
|
|
|
using i::Heap;
|
2010-03-19 13:51:01 +00:00
|
|
|
using i::ProfileGenerator;
|
|
|
|
using i::ProfileNode;
|
|
|
|
using i::ProfilerEventsProcessor;
|
2016-06-22 16:43:46 +00:00
|
|
|
using i::ProfilerListener;
|
2013-04-10 14:31:13 +00:00
|
|
|
using i::ScopedVector;
|
2013-04-10 09:47:44 +00:00
|
|
|
using i::Vector;
|
2010-03-19 13:51:01 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
// Helper methods
|
2015-12-08 18:44:23 +00:00
|
|
|
static v8::Local<v8::Function> GetFunction(v8::Local<v8::Context> env,
|
|
|
|
const char* name) {
|
|
|
|
return v8::Local<v8::Function>::Cast(
|
|
|
|
env->Global()->Get(env, v8_str(name)).ToLocalChecked());
|
2015-02-06 16:50:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 16:13:24 +00:00
|
|
|
static size_t offset(const char* src, const char* substring) {
|
|
|
|
const char* it = strstr(src, substring);
|
|
|
|
CHECK(it);
|
|
|
|
return static_cast<size_t>(it - src);
|
2015-03-24 12:46:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 10:14:36 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
static int dist(A a, B b) {
|
|
|
|
return abs(static_cast<int>(a) - static_cast<int>(b));
|
|
|
|
}
|
|
|
|
|
2016-07-18 09:23:28 +00:00
|
|
|
static const char* reason(const i::DeoptimizeReason reason) {
|
|
|
|
return i::DeoptimizeReasonToString(reason);
|
2015-03-05 10:37:56 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 13:51:01 +00:00
|
|
|
TEST(StartStop) {
|
2016-10-28 20:17:44 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
|
|
|
CpuProfilesCollection profiles(isolate);
|
2017-01-27 01:12:53 +00:00
|
|
|
ProfileGenerator generator(&profiles);
|
2016-06-15 09:59:00 +00:00
|
|
|
std::unique_ptr<ProfilerEventsProcessor> processor(
|
2016-10-28 20:17:44 +00:00
|
|
|
new ProfilerEventsProcessor(isolate, &generator,
|
2016-06-15 09:59:00 +00:00
|
|
|
v8::base::TimeDelta::FromMicroseconds(100)));
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->Start();
|
|
|
|
processor->StopSynchronously();
|
2010-03-19 13:51:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 10:26:21 +00:00
|
|
|
static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc,
|
|
|
|
i::Address frame1,
|
|
|
|
i::Address frame2 = NULL,
|
|
|
|
i::Address frame3 = NULL) {
|
2016-07-06 18:37:10 +00:00
|
|
|
v8::TickSample* sample = proc->StartTickSample();
|
2010-03-19 13:51:01 +00:00
|
|
|
sample->pc = frame1;
|
2013-04-19 11:55:01 +00:00
|
|
|
sample->tos = frame1;
|
2010-03-19 13:51:01 +00:00
|
|
|
sample->frames_count = 0;
|
|
|
|
if (frame2 != NULL) {
|
|
|
|
sample->stack[0] = frame2;
|
|
|
|
sample->frames_count = 1;
|
|
|
|
}
|
|
|
|
if (frame3 != NULL) {
|
|
|
|
sample->stack[1] = frame3;
|
|
|
|
sample->frames_count = 2;
|
|
|
|
}
|
2013-08-23 08:22:07 +00:00
|
|
|
proc->FinishTickSample();
|
2010-03-19 13:51:01 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 07:23:43 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestSetup {
|
|
|
|
public:
|
|
|
|
TestSetup()
|
|
|
|
: old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
|
|
|
|
i::FLAG_prof_browser_mode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~TestSetup() {
|
|
|
|
i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool old_flag_prof_browser_mode_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-02-26 11:04:04 +00:00
|
|
|
i::AbstractCode* CreateCode(LocalContext* env) {
|
2013-07-01 10:12:03 +00:00
|
|
|
static int counter = 0;
|
|
|
|
i::EmbeddedVector<char, 256> script;
|
|
|
|
i::EmbeddedVector<char, 32> name;
|
|
|
|
|
2014-06-13 16:43:27 +00:00
|
|
|
i::SNPrintF(name, "function_%d", ++counter);
|
2013-07-01 10:12:03 +00:00
|
|
|
const char* name_start = name.start();
|
2014-06-13 16:43:27 +00:00
|
|
|
i::SNPrintF(script,
|
2013-07-01 10:12:03 +00:00
|
|
|
"function %s() {\n"
|
|
|
|
"var counter = 0;\n"
|
|
|
|
"for (var i = 0; i < %d; ++i) counter += i;\n"
|
|
|
|
"return '%s_' + counter;\n"
|
|
|
|
"}\n"
|
|
|
|
"%s();\n", name_start, counter, name_start, name_start);
|
|
|
|
CompileRun(script.start());
|
2015-02-06 16:50:56 +00:00
|
|
|
|
2015-10-23 12:26:49 +00:00
|
|
|
i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Utils::OpenHandle(*GetFunction(env->local(), name_start)));
|
2016-02-26 11:04:04 +00:00
|
|
|
return fun->abstract_code();
|
2013-07-01 10:12:03 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 13:51:01 +00:00
|
|
|
TEST(CodeEvents) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2013-07-01 10:12:03 +00:00
|
|
|
LocalContext env;
|
2013-09-19 09:17:13 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
2013-02-15 09:27:10 +00:00
|
|
|
i::Factory* factory = isolate->factory();
|
2010-04-12 07:23:43 +00:00
|
|
|
TestSetup test_setup;
|
2013-07-01 10:12:03 +00:00
|
|
|
|
|
|
|
i::HandleScope scope(isolate);
|
|
|
|
|
2016-02-26 11:04:04 +00:00
|
|
|
i::AbstractCode* aaa_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* comment_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* args5_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* comment2_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* moved_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* args3_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* args4_code = CreateCode(&env);
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
|
2017-01-27 01:12:53 +00:00
|
|
|
ProfileGenerator* generator = new ProfileGenerator(profiles);
|
2016-10-28 20:17:44 +00:00
|
|
|
ProfilerEventsProcessor* processor = new ProfilerEventsProcessor(
|
|
|
|
isolate, generator, v8::base::TimeDelta::FromMicroseconds(100));
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfiler profiler(isolate, profiles, generator, processor);
|
2013-12-18 08:59:09 +00:00
|
|
|
profiles->StartProfiling("", false);
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->Start();
|
2016-06-22 16:43:46 +00:00
|
|
|
ProfilerListener profiler_listener(isolate);
|
|
|
|
isolate->code_event_dispatcher()->AddListener(&profiler_listener);
|
|
|
|
profiler_listener.AddObserver(&profiler);
|
2010-03-19 13:51:01 +00:00
|
|
|
|
|
|
|
// Enqueue code creation events.
|
|
|
|
const char* aaa_str = "aaa";
|
2014-04-17 13:27:02 +00:00
|
|
|
i::Handle<i::String> aaa_name = factory->NewStringFromAsciiChecked(aaa_str);
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::FUNCTION_TAG, aaa_code,
|
|
|
|
*aaa_name);
|
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment_code,
|
|
|
|
"comment");
|
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, args5_code, 5);
|
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment2_code,
|
|
|
|
"comment2");
|
|
|
|
profiler_listener.CodeMoveEvent(comment2_code, moved_code->address());
|
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, args3_code, 3);
|
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, args4_code, 4);
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2012-11-30 10:26:21 +00:00
|
|
|
// Enqueue a tick event to enable code events processing.
|
2016-06-15 09:59:00 +00:00
|
|
|
EnqueueTickSampleEvent(processor, aaa_code->address());
|
2010-03-19 13:51:01 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.RemoveObserver(&profiler);
|
|
|
|
isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->StopSynchronously();
|
2010-03-19 13:51:01 +00:00
|
|
|
|
|
|
|
// Check the state of profile generator.
|
2016-06-15 09:59:00 +00:00
|
|
|
CodeEntry* aaa = generator->code_map()->FindEntry(aaa_code->address());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(aaa);
|
2015-02-06 16:50:56 +00:00
|
|
|
CHECK_EQ(0, strcmp(aaa_str, aaa->name()));
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CodeEntry* comment =
|
|
|
|
generator->code_map()->FindEntry(comment_code->address());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(comment);
|
2015-01-30 09:48:50 +00:00
|
|
|
CHECK_EQ(0, strcmp("comment", comment->name()));
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CodeEntry* args5 = generator->code_map()->FindEntry(args5_code->address());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(args5);
|
2015-01-30 09:48:50 +00:00
|
|
|
CHECK_EQ(0, strcmp("5", args5->name()));
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CHECK(!generator->code_map()->FindEntry(comment2_code->address()));
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CodeEntry* comment2 = generator->code_map()->FindEntry(moved_code->address());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(comment2);
|
2015-01-30 09:48:50 +00:00
|
|
|
CHECK_EQ(0, strcmp("comment2", comment2->name()));
|
2010-03-19 13:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static int CompareProfileNodes(const T* p1, const T* p2) {
|
|
|
|
return strcmp((*p1)->entry()->name(), (*p2)->entry()->name());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TickEvents) {
|
2010-04-12 07:23:43 +00:00
|
|
|
TestSetup test_setup;
|
2013-07-01 10:12:03 +00:00
|
|
|
LocalContext env;
|
2013-09-19 09:17:13 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
2013-07-01 10:12:03 +00:00
|
|
|
i::HandleScope scope(isolate);
|
|
|
|
|
2016-02-26 11:04:04 +00:00
|
|
|
i::AbstractCode* frame1_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* frame2_code = CreateCode(&env);
|
|
|
|
i::AbstractCode* frame3_code = CreateCode(&env);
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
|
2017-01-27 01:12:53 +00:00
|
|
|
ProfileGenerator* generator = new ProfileGenerator(profiles);
|
2016-07-04 19:21:36 +00:00
|
|
|
ProfilerEventsProcessor* processor =
|
|
|
|
new ProfilerEventsProcessor(CcTest::i_isolate(), generator,
|
|
|
|
v8::base::TimeDelta::FromMicroseconds(100));
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfiler profiler(isolate, profiles, generator, processor);
|
2013-12-18 08:59:09 +00:00
|
|
|
profiles->StartProfiling("", false);
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->Start();
|
2016-06-22 16:43:46 +00:00
|
|
|
ProfilerListener profiler_listener(isolate);
|
|
|
|
isolate->code_event_dispatcher()->AddListener(&profiler_listener);
|
|
|
|
profiler_listener.AddObserver(&profiler);
|
2010-03-19 13:51:01 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame1_code, "bbb");
|
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, frame2_code, 5);
|
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame3_code, "ddd");
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
EnqueueTickSampleEvent(processor, frame1_code->instruction_start());
|
2013-07-01 10:12:03 +00:00
|
|
|
EnqueueTickSampleEvent(
|
2016-06-15 09:59:00 +00:00
|
|
|
processor,
|
2013-07-01 10:12:03 +00:00
|
|
|
frame2_code->instruction_start() + frame2_code->ExecutableSize() / 2,
|
2016-08-09 12:08:31 +00:00
|
|
|
frame1_code->instruction_start() + frame1_code->ExecutableSize() / 2);
|
2016-06-15 09:59:00 +00:00
|
|
|
EnqueueTickSampleEvent(processor, frame3_code->instruction_end() - 1,
|
|
|
|
frame2_code->instruction_end() - 1,
|
|
|
|
frame1_code->instruction_end() - 1);
|
2010-03-19 13:51:01 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.RemoveObserver(&profiler);
|
|
|
|
isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->StopSynchronously();
|
2013-07-30 07:01:16 +00:00
|
|
|
CpuProfile* profile = profiles->StopProfiling("");
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(profile);
|
2010-03-19 13:51:01 +00:00
|
|
|
|
|
|
|
// Check call trees.
|
2010-03-30 11:38:39 +00:00
|
|
|
const i::List<ProfileNode*>* top_down_root_children =
|
|
|
|
profile->top_down()->root()->children();
|
|
|
|
CHECK_EQ(1, top_down_root_children->length());
|
2015-01-30 09:48:50 +00:00
|
|
|
CHECK_EQ(0, strcmp("bbb", top_down_root_children->last()->entry()->name()));
|
2010-03-30 11:38:39 +00:00
|
|
|
const i::List<ProfileNode*>* top_down_bbb_children =
|
|
|
|
top_down_root_children->last()->children();
|
|
|
|
CHECK_EQ(1, top_down_bbb_children->length());
|
2015-01-30 09:48:50 +00:00
|
|
|
CHECK_EQ(0, strcmp("5", top_down_bbb_children->last()->entry()->name()));
|
2010-03-30 11:38:39 +00:00
|
|
|
const i::List<ProfileNode*>* top_down_stub_children =
|
|
|
|
top_down_bbb_children->last()->children();
|
|
|
|
CHECK_EQ(1, top_down_stub_children->length());
|
2015-01-30 09:48:50 +00:00
|
|
|
CHECK_EQ(0, strcmp("ddd", top_down_stub_children->last()->entry()->name()));
|
2010-03-30 11:38:39 +00:00
|
|
|
const i::List<ProfileNode*>* top_down_ddd_children =
|
|
|
|
top_down_stub_children->last()->children();
|
|
|
|
CHECK_EQ(0, top_down_ddd_children->length());
|
2016-06-22 16:43:46 +00:00
|
|
|
|
|
|
|
isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
|
2010-03-19 13:51:01 +00:00
|
|
|
}
|
2010-03-30 11:38:39 +00:00
|
|
|
|
2010-08-10 12:06:42 +00:00
|
|
|
// http://crbug/51594
|
|
|
|
// This test must not crash.
|
|
|
|
TEST(CrashIfStoppingLastNonExistentProfile) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2010-08-10 12:06:42 +00:00
|
|
|
TestSetup test_setup;
|
2016-07-08 15:00:23 +00:00
|
|
|
std::unique_ptr<CpuProfiler> profiler(new CpuProfiler(CcTest::i_isolate()));
|
2013-04-02 07:53:50 +00:00
|
|
|
profiler->StartProfiling("1");
|
|
|
|
profiler->StopProfiling("2");
|
|
|
|
profiler->StartProfiling("1");
|
|
|
|
profiler->StopProfiling("");
|
2010-08-10 12:06:42 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 08:25:38 +00:00
|
|
|
// http://code.google.com/p/v8/issues/detail?id=1398
|
|
|
|
// Long stacks (exceeding max frames limit) must not be erased.
|
|
|
|
TEST(Issue1398) {
|
|
|
|
TestSetup test_setup;
|
2013-07-01 10:12:03 +00:00
|
|
|
LocalContext env;
|
2013-09-19 09:17:13 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
2013-07-01 10:12:03 +00:00
|
|
|
i::HandleScope scope(isolate);
|
|
|
|
|
2016-02-26 11:04:04 +00:00
|
|
|
i::AbstractCode* code = CreateCode(&env);
|
2013-07-01 10:12:03 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
|
2017-01-27 01:12:53 +00:00
|
|
|
ProfileGenerator* generator = new ProfileGenerator(profiles);
|
2016-07-04 19:21:36 +00:00
|
|
|
ProfilerEventsProcessor* processor =
|
|
|
|
new ProfilerEventsProcessor(CcTest::i_isolate(), generator,
|
|
|
|
v8::base::TimeDelta::FromMicroseconds(100));
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfiler profiler(isolate, profiles, generator, processor);
|
2013-12-18 08:59:09 +00:00
|
|
|
profiles->StartProfiling("", false);
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->Start();
|
2016-06-22 16:43:46 +00:00
|
|
|
ProfilerListener profiler_listener(isolate);
|
|
|
|
isolate->code_event_dispatcher()->AddListener(&profiler_listener);
|
|
|
|
profiler_listener.AddObserver(&profiler);
|
2011-05-19 08:25:38 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, code, "bbb");
|
2011-05-19 08:25:38 +00:00
|
|
|
|
2016-07-06 18:37:10 +00:00
|
|
|
v8::TickSample* sample = processor->StartTickSample();
|
2013-07-01 10:12:03 +00:00
|
|
|
sample->pc = code->address();
|
2013-04-19 11:55:01 +00:00
|
|
|
sample->tos = 0;
|
2016-07-06 18:37:10 +00:00
|
|
|
sample->frames_count = v8::TickSample::kMaxFramesCount;
|
2014-06-24 16:00:51 +00:00
|
|
|
for (unsigned i = 0; i < sample->frames_count; ++i) {
|
2013-07-01 10:12:03 +00:00
|
|
|
sample->stack[i] = code->address();
|
2011-05-19 08:25:38 +00:00
|
|
|
}
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->FinishTickSample();
|
2011-05-19 08:25:38 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.RemoveObserver(&profiler);
|
|
|
|
isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
|
2013-08-23 10:59:29 +00:00
|
|
|
processor->StopSynchronously();
|
2013-07-30 07:01:16 +00:00
|
|
|
CpuProfile* profile = profiles->StopProfiling("");
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(profile);
|
2011-05-19 08:25:38 +00:00
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
unsigned actual_depth = 0;
|
2011-05-19 08:25:38 +00:00
|
|
|
const ProfileNode* node = profile->top_down()->root();
|
|
|
|
while (node->children()->length() > 0) {
|
|
|
|
node = node->children()->last();
|
|
|
|
++actual_depth;
|
|
|
|
}
|
|
|
|
|
2016-07-06 18:37:10 +00:00
|
|
|
CHECK_EQ(1 + v8::TickSample::kMaxFramesCount, actual_depth); // +1 for PC.
|
2011-05-19 08:25:38 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 16:10:01 +00:00
|
|
|
TEST(DeleteAllCpuProfiles) {
|
2013-04-10 08:29:39 +00:00
|
|
|
CcTest::InitializeVM();
|
2011-03-22 16:10:01 +00:00
|
|
|
TestSetup test_setup;
|
2016-07-08 15:00:23 +00:00
|
|
|
std::unique_ptr<CpuProfiler> profiler(new CpuProfiler(CcTest::i_isolate()));
|
2013-04-02 07:53:50 +00:00
|
|
|
CHECK_EQ(0, profiler->GetProfilesCount());
|
|
|
|
profiler->DeleteAllProfiles();
|
|
|
|
CHECK_EQ(0, profiler->GetProfilesCount());
|
|
|
|
|
|
|
|
profiler->StartProfiling("1");
|
|
|
|
profiler->StopProfiling("1");
|
|
|
|
CHECK_EQ(1, profiler->GetProfilesCount());
|
|
|
|
profiler->DeleteAllProfiles();
|
|
|
|
CHECK_EQ(0, profiler->GetProfilesCount());
|
|
|
|
profiler->StartProfiling("1");
|
|
|
|
profiler->StartProfiling("2");
|
|
|
|
profiler->StopProfiling("2");
|
|
|
|
profiler->StopProfiling("1");
|
|
|
|
CHECK_EQ(2, profiler->GetProfilesCount());
|
|
|
|
profiler->DeleteAllProfiles();
|
|
|
|
CHECK_EQ(0, profiler->GetProfilesCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
|
|
|
|
// Test profiling cancellation by the 'delete' command.
|
2013-04-02 07:53:50 +00:00
|
|
|
profiler->StartProfiling("1");
|
|
|
|
profiler->StartProfiling("2");
|
|
|
|
CHECK_EQ(0, profiler->GetProfilesCount());
|
|
|
|
profiler->DeleteAllProfiles();
|
|
|
|
CHECK_EQ(0, profiler->GetProfilesCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-18 08:59:09 +00:00
|
|
|
static bool FindCpuProfile(v8::CpuProfiler* v8profiler,
|
|
|
|
const v8::CpuProfile* v8profile) {
|
|
|
|
i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(v8profiler);
|
|
|
|
const i::CpuProfile* profile =
|
|
|
|
reinterpret_cast<const i::CpuProfile*>(v8profile);
|
|
|
|
int length = profiler->GetProfilesCount();
|
2013-05-28 08:00:16 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2013-12-18 08:59:09 +00:00
|
|
|
if (profile == profiler->GetProfile(i))
|
|
|
|
return true;
|
2013-05-28 08:00:16 +00:00
|
|
|
}
|
2013-12-18 08:59:09 +00:00
|
|
|
return false;
|
2013-05-28 08:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-22 16:10:01 +00:00
|
|
|
TEST(DeleteCpuProfile) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfiler* cpu_profiler = v8::CpuProfiler::New(env->GetIsolate());
|
2013-12-18 08:59:09 +00:00
|
|
|
i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(cpu_profiler);
|
2011-03-22 16:10:01 +00:00
|
|
|
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2015-02-06 16:50:56 +00:00
|
|
|
v8::Local<v8::String> name1 = v8_str("1");
|
2014-03-28 09:24:49 +00:00
|
|
|
cpu_profiler->StartProfiling(name1);
|
|
|
|
v8::CpuProfile* p1 = cpu_profiler->StopProfiling(name1);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(p1);
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(1, iprofiler->GetProfilesCount());
|
|
|
|
CHECK(FindCpuProfile(cpu_profiler, p1));
|
2014-03-28 09:24:49 +00:00
|
|
|
p1->Delete();
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
v8::Local<v8::String> name2 = v8_str("2");
|
2014-03-28 09:24:49 +00:00
|
|
|
cpu_profiler->StartProfiling(name2);
|
|
|
|
v8::CpuProfile* p2 = cpu_profiler->StopProfiling(name2);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(p2);
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(1, iprofiler->GetProfilesCount());
|
|
|
|
CHECK(FindCpuProfile(cpu_profiler, p2));
|
2015-02-06 16:50:56 +00:00
|
|
|
v8::Local<v8::String> name3 = v8_str("3");
|
2014-03-28 09:24:49 +00:00
|
|
|
cpu_profiler->StartProfiling(name3);
|
|
|
|
v8::CpuProfile* p3 = cpu_profiler->StopProfiling(name3);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(p3);
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(2, iprofiler->GetProfilesCount());
|
|
|
|
CHECK_NE(p2, p3);
|
|
|
|
CHECK(FindCpuProfile(cpu_profiler, p3));
|
|
|
|
CHECK(FindCpuProfile(cpu_profiler, p2));
|
2014-03-28 09:24:49 +00:00
|
|
|
p2->Delete();
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(1, iprofiler->GetProfilesCount());
|
|
|
|
CHECK(!FindCpuProfile(cpu_profiler, p2));
|
|
|
|
CHECK(FindCpuProfile(cpu_profiler, p3));
|
2014-03-28 09:24:49 +00:00
|
|
|
p3->Delete();
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2016-07-08 15:00:23 +00:00
|
|
|
cpu_profiler->Dispose();
|
2011-03-22 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 07:17:08 +00:00
|
|
|
TEST(ProfileStartEndTime) {
|
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfiler* cpu_profiler = v8::CpuProfiler::New(env->GetIsolate());
|
2013-08-05 07:17:08 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
v8::Local<v8::String> profile_name = v8_str("test");
|
2014-03-28 09:24:49 +00:00
|
|
|
cpu_profiler->StartProfiling(profile_name);
|
|
|
|
const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
|
2013-08-05 07:17:08 +00:00
|
|
|
CHECK(profile->GetStartTime() <= profile->GetEndTime());
|
2016-07-08 15:00:23 +00:00
|
|
|
cpu_profiler->Dispose();
|
2013-08-05 07:17:08 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
class ProfilerHelper {
|
|
|
|
public:
|
|
|
|
explicit ProfilerHelper(const v8::Local<v8::Context>& context)
|
|
|
|
: context_(context),
|
|
|
|
profiler_(v8::CpuProfiler::New(context->GetIsolate())) {
|
|
|
|
i::ProfilerExtension::set_profiler(profiler_);
|
|
|
|
}
|
|
|
|
~ProfilerHelper() {
|
|
|
|
i::ProfilerExtension::set_profiler(static_cast<CpuProfiler*>(nullptr));
|
|
|
|
profiler_->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::CpuProfile* Run(v8::Local<v8::Function> function,
|
|
|
|
v8::Local<v8::Value> argv[], int argc,
|
|
|
|
unsigned min_js_samples = 0,
|
|
|
|
unsigned min_external_samples = 0,
|
|
|
|
bool collect_samples = false);
|
|
|
|
|
|
|
|
v8::CpuProfiler* profiler() { return profiler_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Local<v8::Context> context_;
|
|
|
|
v8::CpuProfiler* profiler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
v8::CpuProfile* ProfilerHelper::Run(v8::Local<v8::Function> function,
|
|
|
|
v8::Local<v8::Value> argv[], int argc,
|
|
|
|
unsigned min_js_samples,
|
|
|
|
unsigned min_external_samples,
|
|
|
|
bool collect_samples) {
|
2015-02-06 16:50:56 +00:00
|
|
|
v8::Local<v8::String> profile_name = v8_str("my_profile");
|
2013-07-10 12:56:58 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
profiler_->SetSamplingInterval(100);
|
|
|
|
profiler_->StartProfiling(profile_name, collect_samples);
|
2013-07-10 12:56:58 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::internal::CpuProfiler* iprofiler =
|
|
|
|
reinterpret_cast<v8::internal::CpuProfiler*>(profiler_);
|
|
|
|
v8::sampler::Sampler* sampler = iprofiler->processor()->sampler();
|
2013-07-10 12:56:58 +00:00
|
|
|
sampler->StartCountingSamples();
|
|
|
|
do {
|
2016-07-08 15:00:23 +00:00
|
|
|
function->Call(context_, context_->Global(), argc, argv).ToLocalChecked();
|
2016-02-12 16:02:29 +00:00
|
|
|
} while (sampler->js_sample_count() < min_js_samples ||
|
|
|
|
sampler->external_sample_count() < min_external_samples);
|
2013-07-10 12:56:58 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = profiler_->StopProfiling(profile_name);
|
2013-07-10 12:56:58 +00:00
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(profile);
|
2013-07-10 12:56:58 +00:00
|
|
|
// Dump collected profile to have a better diagnostic in case of failure.
|
2014-03-28 09:24:49 +00:00
|
|
|
reinterpret_cast<i::CpuProfile*>(profile)->Print();
|
2013-07-10 12:56:58 +00:00
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
static const v8::CpuProfileNode* FindChild(v8::Local<v8::Context> context,
|
|
|
|
const v8::CpuProfileNode* node,
|
2013-04-10 09:47:44 +00:00
|
|
|
const char* name) {
|
|
|
|
int count = node->GetChildrenCount();
|
2016-02-08 18:12:04 +00:00
|
|
|
v8::Local<v8::String> name_handle = v8_str(name);
|
2013-04-10 09:47:44 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
const v8::CpuProfileNode* child = node->GetChild(i);
|
2016-02-08 18:12:04 +00:00
|
|
|
if (name_handle->Equals(context, child->GetFunctionName()).FromJust()) {
|
2015-12-08 18:44:23 +00:00
|
|
|
return child;
|
|
|
|
}
|
2013-04-10 09:47:44 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
static const v8::CpuProfileNode* GetChild(v8::Local<v8::Context> context,
|
|
|
|
const v8::CpuProfileNode* node,
|
2013-05-24 16:19:06 +00:00
|
|
|
const char* name) {
|
2015-12-08 18:44:23 +00:00
|
|
|
const v8::CpuProfileNode* result = FindChild(context, node, name);
|
2013-10-10 13:03:41 +00:00
|
|
|
if (!result) {
|
|
|
|
char buffer[100];
|
2016-04-28 07:59:52 +00:00
|
|
|
i::SNPrintF(i::ArrayVector(buffer), "Failed to GetChild: %s", name);
|
2013-10-10 13:03:41 +00:00
|
|
|
FATAL(buffer);
|
|
|
|
}
|
2013-05-24 16:19:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
static void CheckSimpleBranch(v8::Local<v8::Context> context,
|
|
|
|
const v8::CpuProfileNode* node,
|
2013-04-10 09:47:44 +00:00
|
|
|
const char* names[], int length) {
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
const char* name = names[i];
|
2015-12-08 18:44:23 +00:00
|
|
|
node = GetChild(context, node, name);
|
2013-04-10 09:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
static const ProfileNode* GetSimpleBranch(v8::Local<v8::Context> context,
|
|
|
|
v8::CpuProfile* profile,
|
2015-03-05 10:37:56 +00:00
|
|
|
const char* names[], int length) {
|
|
|
|
const v8::CpuProfileNode* node = profile->GetTopDownRoot();
|
2015-02-10 14:32:42 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2015-12-08 18:44:23 +00:00
|
|
|
node = GetChild(context, node, names[i]);
|
2015-02-10 14:32:42 +00:00
|
|
|
}
|
2015-03-05 10:37:56 +00:00
|
|
|
return reinterpret_cast<const ProfileNode*>(node);
|
2015-02-10 14:32:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
static const char* cpu_profiler_test_source =
|
|
|
|
"%NeverOptimizeFunction(loop);\n"
|
|
|
|
"%NeverOptimizeFunction(delay);\n"
|
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
"%NeverOptimizeFunction(baz);\n"
|
|
|
|
"%NeverOptimizeFunction(foo);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
|
|
|
"function loop(timeout) {\n"
|
|
|
|
" this.mmm = 0;\n"
|
|
|
|
" var start = Date.now();\n"
|
|
|
|
" do {\n"
|
|
|
|
" var n = 1000;\n"
|
|
|
|
" while(n > 1) {\n"
|
|
|
|
" n--;\n"
|
|
|
|
" this.mmm += n * n * n;\n"
|
|
|
|
" }\n"
|
|
|
|
" } while (Date.now() - start < timeout);\n"
|
|
|
|
"}\n"
|
|
|
|
"function delay() { loop(10); }\n"
|
|
|
|
"function bar() { delay(); }\n"
|
|
|
|
"function baz() { delay(); }\n"
|
|
|
|
"function foo() {\n"
|
|
|
|
" delay();\n"
|
|
|
|
" bar();\n"
|
|
|
|
" delay();\n"
|
|
|
|
" baz();\n"
|
|
|
|
"}\n"
|
|
|
|
"function start(duration) {\n"
|
|
|
|
" var start = Date.now();\n"
|
|
|
|
" do {\n"
|
|
|
|
" foo();\n"
|
|
|
|
" } while (Date.now() - start < duration);\n"
|
|
|
|
"}\n";
|
2013-04-10 09:47:44 +00:00
|
|
|
|
|
|
|
// Check that the profile tree for the script above will look like the
|
|
|
|
// following:
|
|
|
|
//
|
|
|
|
// [Top down]:
|
|
|
|
// 1062 0 (root) [-1]
|
|
|
|
// 1054 0 start [-1]
|
|
|
|
// 1054 1 foo [-1]
|
|
|
|
// 265 0 baz [-1]
|
|
|
|
// 265 1 delay [-1]
|
|
|
|
// 264 264 loop [-1]
|
|
|
|
// 525 3 delay [-1]
|
|
|
|
// 522 522 loop [-1]
|
|
|
|
// 263 0 bar [-1]
|
|
|
|
// 263 1 delay [-1]
|
|
|
|
// 262 262 loop [-1]
|
|
|
|
// 2 2 (program) [-1]
|
|
|
|
// 6 6 (garbage collector) [-1]
|
|
|
|
TEST(CollectCpuProfile) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2013-04-10 09:47:44 +00:00
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(cpu_profiler_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-04-10 09:47:44 +00:00
|
|
|
|
2013-04-15 14:45:38 +00:00
|
|
|
int32_t profiling_interval_ms = 200;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
|
|
|
v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 1000);
|
2013-04-10 09:47:44 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
const v8::CpuProfileNode* foo_node = GetChild(env.local(), start_node, "foo");
|
2013-04-10 09:47:44 +00:00
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
const char* bar_branch[] = {"bar", "delay", "loop"};
|
|
|
|
CheckSimpleBranch(env.local(), foo_node, bar_branch, arraysize(bar_branch));
|
|
|
|
const char* baz_branch[] = {"baz", "delay", "loop"};
|
|
|
|
CheckSimpleBranch(env.local(), foo_node, baz_branch, arraysize(baz_branch));
|
|
|
|
const char* delay_branch[] = {"delay", "loop"};
|
|
|
|
CheckSimpleBranch(env.local(), foo_node, delay_branch,
|
|
|
|
arraysize(delay_branch));
|
2013-04-10 14:31:13 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-04-10 09:47:44 +00:00
|
|
|
}
|
2013-05-14 22:51:33 +00:00
|
|
|
|
2014-05-22 05:36:27 +00:00
|
|
|
static const char* hot_deopt_no_frame_entry_test_source =
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(foo);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
|
|
|
"function foo(a, b) {\n"
|
|
|
|
" return a + b;\n"
|
|
|
|
"}\n"
|
|
|
|
"function start(timeout) {\n"
|
|
|
|
" var start = Date.now();\n"
|
|
|
|
" do {\n"
|
|
|
|
" for (var i = 1; i < 1000; ++i) foo(1, i);\n"
|
|
|
|
" var duration = Date.now() - start;\n"
|
|
|
|
" } while (duration < timeout);\n"
|
|
|
|
" return duration;\n"
|
|
|
|
"}\n";
|
2014-05-22 05:36:27 +00:00
|
|
|
|
|
|
|
// Check that the profile tree for the script above will look like the
|
|
|
|
// following:
|
|
|
|
//
|
|
|
|
// [Top down]:
|
|
|
|
// 1062 0 (root) [-1]
|
|
|
|
// 1054 0 start [-1]
|
|
|
|
// 1054 1 foo [-1]
|
|
|
|
// 2 2 (program) [-1]
|
|
|
|
// 6 6 (garbage collector) [-1]
|
|
|
|
//
|
2016-02-12 17:30:01 +00:00
|
|
|
// The test checks no FP ranges are present in a deoptimized function.
|
2014-05-22 05:36:27 +00:00
|
|
|
// If 'foo' has no ranges the samples falling into the prologue will miss the
|
|
|
|
// 'start' function on the stack, so 'foo' will be attached to the (root).
|
|
|
|
TEST(HotDeoptNoFrameEntry) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2014-05-22 05:36:27 +00:00
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(hot_deopt_no_frame_entry_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2014-05-22 05:36:27 +00:00
|
|
|
|
|
|
|
int32_t profiling_interval_ms = 200;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
|
|
|
v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 1000);
|
2015-12-08 18:44:23 +00:00
|
|
|
function->Call(env.local(), env->Global(), arraysize(args), args)
|
|
|
|
.ToLocalChecked();
|
2014-05-22 05:36:27 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
GetChild(env.local(), start_node, "foo");
|
2014-05-22 05:36:27 +00:00
|
|
|
|
|
|
|
profile->Delete();
|
|
|
|
}
|
|
|
|
|
2014-04-25 18:53:06 +00:00
|
|
|
TEST(CollectCpuProfileSamples) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2014-04-25 18:53:06 +00:00
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(cpu_profiler_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2014-04-25 18:53:06 +00:00
|
|
|
|
|
|
|
int32_t profiling_interval_ms = 200;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
|
|
|
v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
2014-04-25 18:53:06 +00:00
|
|
|
v8::CpuProfile* profile =
|
2016-07-08 15:00:23 +00:00
|
|
|
helper.Run(function, args, arraysize(args), 1000, 0, true);
|
2014-04-25 18:53:06 +00:00
|
|
|
|
|
|
|
CHECK_LE(200, profile->GetSamplesCount());
|
|
|
|
uint64_t end_time = profile->GetEndTime();
|
|
|
|
uint64_t current_time = profile->GetStartTime();
|
|
|
|
CHECK_LE(current_time, end_time);
|
|
|
|
for (int i = 0; i < profile->GetSamplesCount(); i++) {
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(profile->GetSample(i));
|
2014-04-25 18:53:06 +00:00
|
|
|
uint64_t timestamp = profile->GetSampleTimestamp(i);
|
|
|
|
CHECK_LE(current_time, timestamp);
|
|
|
|
CHECK_LE(timestamp, end_time);
|
|
|
|
current_time = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
profile->Delete();
|
|
|
|
}
|
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
static const char* cpu_profiler_test_source2 =
|
|
|
|
"%NeverOptimizeFunction(loop);\n"
|
|
|
|
"%NeverOptimizeFunction(delay);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
|
|
|
"function loop() {}\n"
|
|
|
|
"function delay() { loop(); }\n"
|
|
|
|
"function start(duration) {\n"
|
|
|
|
" var start = Date.now();\n"
|
|
|
|
" do {\n"
|
|
|
|
" for (var i = 0; i < 10000; ++i) delay();\n"
|
|
|
|
" } while (Date.now() - start < duration);\n"
|
|
|
|
"}";
|
2013-05-14 22:51:33 +00:00
|
|
|
|
2013-10-10 13:03:41 +00:00
|
|
|
// Check that the profile tree doesn't contain unexpected traces:
|
2013-05-14 22:51:33 +00:00
|
|
|
// - 'loop' can be called only by 'delay'
|
|
|
|
// - 'delay' may be called only by 'start'
|
|
|
|
// The profile will look like the following:
|
|
|
|
//
|
|
|
|
// [Top down]:
|
|
|
|
// 135 0 (root) [-1] #1
|
|
|
|
// 121 72 start [-1] #3
|
|
|
|
// 49 33 delay [-1] #4
|
|
|
|
// 16 16 loop [-1] #5
|
|
|
|
// 14 14 (program) [-1] #2
|
|
|
|
TEST(SampleWhenFrameIsNotSetup) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2013-05-14 22:51:33 +00:00
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(cpu_profiler_test_source2);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-05-14 22:51:33 +00:00
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
int32_t duration_ms = 100;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
2016-02-08 18:12:04 +00:00
|
|
|
v8::Integer::New(env->GetIsolate(), duration_ms)};
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 1000);
|
2013-05-14 22:51:33 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
const v8::CpuProfileNode* delay_node =
|
|
|
|
GetChild(env.local(), start_node, "delay");
|
|
|
|
GetChild(env.local(), delay_node, "loop");
|
2013-05-14 22:51:33 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-05-14 22:51:33 +00:00
|
|
|
}
|
2013-06-04 10:57:32 +00:00
|
|
|
|
|
|
|
static const char* native_accessor_test_source = "function start(count) {\n"
|
|
|
|
" for (var i = 0; i < count; i++) {\n"
|
|
|
|
" var o = instance.foo;\n"
|
|
|
|
" instance.foo = o + 1;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
|
2013-06-11 15:00:41 +00:00
|
|
|
class TestApiCallbacks {
|
2013-06-04 10:57:32 +00:00
|
|
|
public:
|
2013-06-11 15:00:41 +00:00
|
|
|
explicit TestApiCallbacks(int min_duration_ms)
|
2013-06-05 06:15:41 +00:00
|
|
|
: min_duration_ms_(min_duration_ms),
|
2013-06-11 08:32:48 +00:00
|
|
|
is_warming_up_(false) {}
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2013-07-17 12:16:16 +00:00
|
|
|
static void Getter(v8::Local<v8::String> name,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
2016-02-08 18:12:04 +00:00
|
|
|
TestApiCallbacks* data = FromInfo(info);
|
2013-06-11 08:32:48 +00:00
|
|
|
data->Wait();
|
2013-06-04 10:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Setter(v8::Local<v8::String> name,
|
|
|
|
v8::Local<v8::Value> value,
|
2013-07-17 12:16:16 +00:00
|
|
|
const v8::PropertyCallbackInfo<void>& info) {
|
2016-02-08 18:12:04 +00:00
|
|
|
TestApiCallbacks* data = FromInfo(info);
|
2013-06-11 15:00:41 +00:00
|
|
|
data->Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
2016-02-08 18:12:04 +00:00
|
|
|
TestApiCallbacks* data = FromInfo(info);
|
2013-06-11 08:32:48 +00:00
|
|
|
data->Wait();
|
2013-06-05 06:15:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 08:32:48 +00:00
|
|
|
void set_warming_up(bool value) { is_warming_up_ = value; }
|
2013-06-04 10:57:32 +00:00
|
|
|
|
|
|
|
private:
|
2013-06-11 08:32:48 +00:00
|
|
|
void Wait() {
|
|
|
|
if (is_warming_up_) return;
|
2014-06-30 13:25:46 +00:00
|
|
|
double start = v8::base::OS::TimeCurrentMillis();
|
2013-06-05 06:15:41 +00:00
|
|
|
double duration = 0;
|
|
|
|
while (duration < min_duration_ms_) {
|
2015-05-04 22:53:22 +00:00
|
|
|
v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
|
2014-06-30 13:25:46 +00:00
|
|
|
duration = v8::base::OS::TimeCurrentMillis() - start;
|
2013-06-04 10:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
template <typename T>
|
|
|
|
static TestApiCallbacks* FromInfo(const T& info) {
|
2013-06-04 10:57:32 +00:00
|
|
|
void* data = v8::External::Cast(*info.Data())->Value();
|
2013-06-11 15:00:41 +00:00
|
|
|
return reinterpret_cast<TestApiCallbacks*>(data);
|
2013-06-04 10:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int min_duration_ms_;
|
2013-06-11 08:32:48 +00:00
|
|
|
bool is_warming_up_;
|
2013-06-04 10:57:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Test that native accessors are properly reported in the CPU profile.
|
|
|
|
// This test checks the case when the long-running accessors are called
|
|
|
|
// only once and the optimizer doesn't have chance to change the invocation
|
|
|
|
// code.
|
2013-06-11 08:32:48 +00:00
|
|
|
TEST(NativeAccessorUninitializedIC) {
|
2013-06-04 10:57:32 +00:00
|
|
|
LocalContext env;
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> func_template =
|
|
|
|
v8::FunctionTemplate::New(isolate);
|
2013-06-04 10:57:32 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> instance_template =
|
|
|
|
func_template->InstanceTemplate();
|
|
|
|
|
2013-06-11 15:00:41 +00:00
|
|
|
TestApiCallbacks accessors(100);
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::Local<v8::External> data = v8::External::New(isolate, &accessors);
|
2015-02-06 16:50:56 +00:00
|
|
|
instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
|
|
|
|
&TestApiCallbacks::Setter, data);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::Object> instance =
|
|
|
|
func->NewInstance(env.local()).ToLocalChecked();
|
|
|
|
env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(native_accessor_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
2013-06-04 10:57:32 +00:00
|
|
|
int32_t repeat_count = 1;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 0, 100);
|
2013-06-04 10:57:32 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
GetChild(env.local(), start_node, "get foo");
|
|
|
|
GetChild(env.local(), start_node, "set foo");
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-06-04 10:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that native accessors are properly reported in the CPU profile.
|
|
|
|
// This test makes sure that the accessors are called enough times to become
|
|
|
|
// hot and to trigger optimizations.
|
2013-06-11 08:32:48 +00:00
|
|
|
TEST(NativeAccessorMonomorphicIC) {
|
2013-06-04 10:57:32 +00:00
|
|
|
LocalContext env;
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> func_template =
|
|
|
|
v8::FunctionTemplate::New(isolate);
|
2013-06-04 10:57:32 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> instance_template =
|
|
|
|
func_template->InstanceTemplate();
|
|
|
|
|
2013-06-11 15:00:41 +00:00
|
|
|
TestApiCallbacks accessors(1);
|
2013-11-12 11:44:58 +00:00
|
|
|
v8::Local<v8::External> data =
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::External::New(isolate, &accessors);
|
2015-02-06 16:50:56 +00:00
|
|
|
instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
|
|
|
|
&TestApiCallbacks::Setter, data);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::Object> instance =
|
|
|
|
func->NewInstance(env.local()).ToLocalChecked();
|
|
|
|
env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(native_accessor_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2013-06-11 08:32:48 +00:00
|
|
|
{
|
|
|
|
// Make sure accessors ICs are in monomorphic state before starting
|
|
|
|
// profiling.
|
|
|
|
accessors.set_warming_up(true);
|
|
|
|
int32_t warm_up_iterations = 3;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
|
|
|
v8::Integer::New(isolate, warm_up_iterations)};
|
|
|
|
function->Call(env.local(), env->Global(), arraysize(args), args)
|
|
|
|
.ToLocalChecked();
|
2013-06-11 08:32:48 +00:00
|
|
|
accessors.set_warming_up(false);
|
|
|
|
}
|
|
|
|
|
2013-06-04 10:57:32 +00:00
|
|
|
int32_t repeat_count = 100;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 0, 100);
|
2013-06-04 10:57:32 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
GetChild(env.local(), start_node, "get foo");
|
|
|
|
GetChild(env.local(), start_node, "set foo");
|
2013-06-04 10:57:32 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-06-04 10:57:32 +00:00
|
|
|
}
|
2013-06-11 15:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
static const char* native_method_test_source = "function start(count) {\n"
|
|
|
|
" for (var i = 0; i < count; i++) {\n"
|
|
|
|
" instance.fooMethod();\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NativeMethodUninitializedIC) {
|
|
|
|
LocalContext env;
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2013-06-11 15:00:41 +00:00
|
|
|
|
|
|
|
TestApiCallbacks callbacks(100);
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::Local<v8::External> data = v8::External::New(isolate, &callbacks);
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> func_template =
|
|
|
|
v8::FunctionTemplate::New(isolate);
|
2016-07-08 15:00:23 +00:00
|
|
|
func_template->SetClassName(v8_str("Test_InstanceConstructor"));
|
2013-06-11 15:00:41 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> proto_template =
|
|
|
|
func_template->PrototypeTemplate();
|
2013-11-28 08:21:26 +00:00
|
|
|
v8::Local<v8::Signature> signature =
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Signature::New(isolate, func_template);
|
2015-02-06 16:50:56 +00:00
|
|
|
proto_template->Set(
|
|
|
|
v8_str("fooMethod"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
|
|
|
|
signature, 0));
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::Object> instance =
|
|
|
|
func->NewInstance(env.local()).ToLocalChecked();
|
|
|
|
env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(native_method_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
2013-06-11 15:00:41 +00:00
|
|
|
int32_t repeat_count = 1;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 0, 100);
|
2013-06-11 15:00:41 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
GetChild(env.local(), start_node, "fooMethod");
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-06-11 15:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NativeMethodMonomorphicIC) {
|
|
|
|
LocalContext env;
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
2013-06-11 15:00:41 +00:00
|
|
|
|
|
|
|
TestApiCallbacks callbacks(1);
|
2013-11-12 11:44:58 +00:00
|
|
|
v8::Local<v8::External> data =
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::External::New(isolate, &callbacks);
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> func_template =
|
|
|
|
v8::FunctionTemplate::New(isolate);
|
2015-02-06 16:50:56 +00:00
|
|
|
func_template->SetClassName(v8_str("Test_InstanceCostructor"));
|
2013-06-11 15:00:41 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> proto_template =
|
|
|
|
func_template->PrototypeTemplate();
|
2013-11-28 08:21:26 +00:00
|
|
|
v8::Local<v8::Signature> signature =
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::Signature::New(isolate, func_template);
|
2015-02-06 16:50:56 +00:00
|
|
|
proto_template->Set(
|
|
|
|
v8_str("fooMethod"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
|
|
|
|
signature, 0));
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::Object> instance =
|
|
|
|
func->NewInstance(env.local()).ToLocalChecked();
|
|
|
|
env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(native_method_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-06-11 15:00:41 +00:00
|
|
|
{
|
|
|
|
// Make sure method ICs are in monomorphic state before starting
|
|
|
|
// profiling.
|
|
|
|
callbacks.set_warming_up(true);
|
|
|
|
int32_t warm_up_iterations = 3;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
|
|
|
v8::Integer::New(isolate, warm_up_iterations)};
|
|
|
|
function->Call(env.local(), env->Global(), arraysize(args), args)
|
|
|
|
.ToLocalChecked();
|
2013-06-11 15:00:41 +00:00
|
|
|
callbacks.set_warming_up(false);
|
|
|
|
}
|
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
2013-06-11 15:00:41 +00:00
|
|
|
int32_t repeat_count = 100;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 0, 200);
|
2013-06-11 15:00:41 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2015-12-08 18:44:23 +00:00
|
|
|
GetChild(env.local(), root, "start");
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
GetChild(env.local(), start_node, "fooMethod");
|
2013-06-11 15:00:41 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-06-11 15:00:41 +00:00
|
|
|
}
|
2013-06-27 09:28:11 +00:00
|
|
|
|
|
|
|
|
2014-07-11 09:06:12 +00:00
|
|
|
static const char* bound_function_test_source =
|
|
|
|
"function foo() {\n"
|
|
|
|
" startProfiling('my_profile');\n"
|
|
|
|
"}\n"
|
|
|
|
"function start() {\n"
|
|
|
|
" var callback = foo.bind(this);\n"
|
|
|
|
" callback();\n"
|
|
|
|
"}";
|
2013-06-27 09:28:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(BoundFunctionCall) {
|
2014-07-11 09:06:12 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2013-06-27 09:28:11 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(bound_function_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
2013-06-27 09:28:11 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, nullptr, 0);
|
2013-06-27 09:28:11 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
|
|
|
|
GetChild(env, start_node, "foo");
|
2013-06-27 09:28:11 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-06-27 09:28:11 +00:00
|
|
|
}
|
2013-07-02 07:51:09 +00:00
|
|
|
|
2014-11-06 09:16:34 +00:00
|
|
|
// This tests checks distribution of the samples through the source lines.
|
2016-03-02 02:10:03 +00:00
|
|
|
static void TickLines(bool optimize) {
|
2017-04-28 13:33:43 +00:00
|
|
|
if (!optimize) i::FLAG_opt = false;
|
2014-11-06 09:16:34 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext env;
|
2016-03-02 02:10:03 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2014-11-06 09:16:34 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
|
|
|
i::Factory* factory = isolate->factory();
|
|
|
|
i::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
i::EmbeddedVector<char, 512> script;
|
2016-09-27 16:27:31 +00:00
|
|
|
i::EmbeddedVector<char, 64> optimize_call;
|
2014-11-06 09:16:34 +00:00
|
|
|
|
|
|
|
const char* func_name = "func";
|
2016-09-27 16:27:31 +00:00
|
|
|
if (optimize) {
|
|
|
|
i::SNPrintF(optimize_call, "%%OptimizeFunctionOnNextCall(%s);\n",
|
|
|
|
func_name);
|
|
|
|
} else {
|
2016-09-27 19:28:38 +00:00
|
|
|
optimize_call[0] = '\0';
|
2016-09-27 16:27:31 +00:00
|
|
|
}
|
2014-11-06 09:16:34 +00:00
|
|
|
i::SNPrintF(script,
|
|
|
|
"function %s() {\n"
|
|
|
|
" var n = 0;\n"
|
|
|
|
" var m = 100*100;\n"
|
|
|
|
" while (m > 1) {\n"
|
|
|
|
" m--;\n"
|
|
|
|
" n += m * m * m;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n"
|
2016-09-27 16:27:31 +00:00
|
|
|
"%s();\n"
|
|
|
|
"%s"
|
2014-11-06 09:16:34 +00:00
|
|
|
"%s();\n",
|
2016-09-27 16:27:31 +00:00
|
|
|
func_name, func_name, optimize_call.start(), func_name);
|
2014-11-06 09:16:34 +00:00
|
|
|
|
|
|
|
CompileRun(script.start());
|
|
|
|
|
2015-10-23 12:26:49 +00:00
|
|
|
i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Utils::OpenHandle(*GetFunction(env.local(), func_name)));
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(func->shared());
|
2016-02-26 11:04:04 +00:00
|
|
|
CHECK(func->shared()->abstract_code());
|
2016-07-29 10:21:17 +00:00
|
|
|
CHECK(!optimize || func->IsOptimized() ||
|
2017-04-28 15:36:46 +00:00
|
|
|
!CcTest::i_isolate()->use_optimizer());
|
2016-03-02 02:10:03 +00:00
|
|
|
i::AbstractCode* code = func->abstract_code();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(code);
|
2014-11-06 09:16:34 +00:00
|
|
|
i::Address code_address = code->instruction_start();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(code_address);
|
2014-11-06 09:16:34 +00:00
|
|
|
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
|
2017-01-27 01:12:53 +00:00
|
|
|
ProfileGenerator* generator = new ProfileGenerator(profiles);
|
2016-07-04 19:21:36 +00:00
|
|
|
ProfilerEventsProcessor* processor =
|
|
|
|
new ProfilerEventsProcessor(CcTest::i_isolate(), generator,
|
|
|
|
v8::base::TimeDelta::FromMicroseconds(100));
|
2016-06-15 09:59:00 +00:00
|
|
|
CpuProfiler profiler(isolate, profiles, generator, processor);
|
2014-11-06 09:16:34 +00:00
|
|
|
profiles->StartProfiling("", false);
|
|
|
|
processor->Start();
|
2016-06-22 16:43:46 +00:00
|
|
|
ProfilerListener profiler_listener(isolate);
|
|
|
|
isolate->code_event_dispatcher()->AddListener(&profiler_listener);
|
|
|
|
profiler_listener.AddObserver(&profiler);
|
2014-11-06 09:16:34 +00:00
|
|
|
|
|
|
|
// Enqueue code creation events.
|
|
|
|
i::Handle<i::String> str = factory->NewStringFromAsciiChecked(func_name);
|
|
|
|
int line = 1;
|
|
|
|
int column = 1;
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.CodeCreateEvent(i::Logger::FUNCTION_TAG, code,
|
|
|
|
func->shared(), *str, line, column);
|
2014-11-06 09:16:34 +00:00
|
|
|
|
|
|
|
// Enqueue a tick event to enable code events processing.
|
2016-06-15 09:59:00 +00:00
|
|
|
EnqueueTickSampleEvent(processor, code_address);
|
2014-11-06 09:16:34 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
profiler_listener.RemoveObserver(&profiler);
|
|
|
|
isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
|
2014-11-06 09:16:34 +00:00
|
|
|
processor->StopSynchronously();
|
|
|
|
|
|
|
|
CpuProfile* profile = profiles->StopProfiling("");
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(profile);
|
2014-11-06 09:16:34 +00:00
|
|
|
|
|
|
|
// Check the state of profile generator.
|
2016-06-15 09:59:00 +00:00
|
|
|
CodeEntry* func_entry = generator->code_map()->FindEntry(code_address);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(func_entry);
|
2015-02-06 16:50:56 +00:00
|
|
|
CHECK_EQ(0, strcmp(func_name, func_entry->name()));
|
2014-11-06 09:16:34 +00:00
|
|
|
const i::JITLineInfoTable* line_info = func_entry->line_info();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(line_info);
|
2014-11-06 09:16:34 +00:00
|
|
|
CHECK(!line_info->empty());
|
|
|
|
|
|
|
|
// Check the hit source lines using V8 Public APIs.
|
|
|
|
const i::ProfileTree* tree = profile->top_down();
|
|
|
|
ProfileNode* root = tree->root();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(root);
|
2014-11-06 09:16:34 +00:00
|
|
|
ProfileNode* func_node = root->FindChild(func_entry);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(func_node);
|
2014-11-06 09:16:34 +00:00
|
|
|
|
|
|
|
// Add 10 faked ticks to source line #5.
|
|
|
|
int hit_line = 5;
|
|
|
|
int hit_count = 10;
|
|
|
|
for (int i = 0; i < hit_count; i++) func_node->IncrementLineTicks(hit_line);
|
|
|
|
|
|
|
|
unsigned int line_count = func_node->GetHitLineCount();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(2u, line_count); // Expect two hit source lines - #1 and #5.
|
2014-11-06 09:16:34 +00:00
|
|
|
ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count);
|
|
|
|
CHECK(func_node->GetLineTicks(&entries[0], line_count));
|
|
|
|
int value = 0;
|
|
|
|
for (int i = 0; i < entries.length(); i++)
|
|
|
|
if (entries[i].line == hit_line) {
|
|
|
|
value = entries[i].hit_count;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CHECK_EQ(hit_count, value);
|
|
|
|
}
|
|
|
|
|
2016-03-02 02:10:03 +00:00
|
|
|
TEST(TickLinesBaseline) { TickLines(false); }
|
|
|
|
|
|
|
|
TEST(TickLinesOptimized) { TickLines(true); }
|
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
static const char* call_function_test_source =
|
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
|
|
|
"function bar(n) {\n"
|
|
|
|
" var s = 0;\n"
|
|
|
|
" for (var i = 0; i < n; i++) s += i * i * i;\n"
|
|
|
|
" return s;\n"
|
|
|
|
"}\n"
|
|
|
|
"function start(duration) {\n"
|
|
|
|
" var start = Date.now();\n"
|
|
|
|
" do {\n"
|
|
|
|
" for (var i = 0; i < 100; ++i)\n"
|
|
|
|
" bar.call(this, 1000);\n"
|
|
|
|
" } while (Date.now() - start < duration);\n"
|
|
|
|
"}";
|
2013-07-02 07:51:09 +00:00
|
|
|
|
|
|
|
// Test that if we sampled thread when it was inside FunctionCall buitin then
|
|
|
|
// its caller frame will be '(unresolved function)' as we have no reliable way
|
|
|
|
// to resolve it.
|
|
|
|
//
|
|
|
|
// [Top down]:
|
|
|
|
// 96 0 (root) [-1] #1
|
|
|
|
// 1 1 (garbage collector) [-1] #4
|
|
|
|
// 5 0 (unresolved function) [-1] #5
|
|
|
|
// 5 5 call [-1] #6
|
|
|
|
// 71 70 start [-1] #3
|
|
|
|
// 1 1 bar [-1] #7
|
|
|
|
// 19 19 (program) [-1] #2
|
|
|
|
TEST(FunctionCallSample) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2013-07-02 07:51:09 +00:00
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
// Collect garbage that might have be generated while installing
|
|
|
|
// extensions.
|
2017-04-26 22:16:41 +00:00
|
|
|
CcTest::CollectAllGarbage();
|
2013-07-08 11:26:15 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(call_function_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-07-02 07:51:09 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
2013-07-02 07:51:09 +00:00
|
|
|
int32_t duration_ms = 100;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
|
|
|
v8::Integer::New(env->GetIsolate(), duration_ms)};
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 1000);
|
2013-07-02 07:51:09 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
GetChild(env.local(), start_node, "bar");
|
2013-07-02 07:51:09 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
const v8::CpuProfileNode* unresolved_node =
|
|
|
|
FindChild(env.local(), root, i::CodeEntry::kUnresolvedFunctionName);
|
2016-02-08 18:12:04 +00:00
|
|
|
CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "call"));
|
2013-07-02 07:51:09 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-07-02 07:51:09 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
static const char* function_apply_test_source =
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
"%NeverOptimizeFunction(test);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
|
|
|
"function bar(n) {\n"
|
|
|
|
" var s = 0;\n"
|
|
|
|
" for (var i = 0; i < n; i++) s += i * i * i;\n"
|
|
|
|
" return s;\n"
|
2015-02-06 16:50:56 +00:00
|
|
|
"}\n"
|
|
|
|
"function test() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" bar.apply(this, [1000]);\n"
|
2015-02-06 16:50:56 +00:00
|
|
|
"}\n"
|
|
|
|
"function start(duration) {\n"
|
|
|
|
" var start = Date.now();\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" do {\n"
|
|
|
|
" for (var i = 0; i < 100; ++i) test();\n"
|
|
|
|
" } while (Date.now() - start < duration);\n"
|
2015-02-06 16:50:56 +00:00
|
|
|
"}";
|
2013-07-03 14:04:37 +00:00
|
|
|
|
|
|
|
// [Top down]:
|
|
|
|
// 94 0 (root) [-1] #0 1
|
|
|
|
// 2 2 (garbage collector) [-1] #0 7
|
|
|
|
// 82 49 start [-1] #16 3
|
|
|
|
// 1 0 (unresolved function) [-1] #0 8
|
|
|
|
// 1 1 apply [-1] #0 9
|
|
|
|
// 32 21 test [-1] #16 4
|
|
|
|
// 2 2 bar [-1] #16 6
|
|
|
|
// 10 10 (program) [-1] #0 2
|
|
|
|
TEST(FunctionApplySample) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2013-07-03 14:04:37 +00:00
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(function_apply_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
2013-07-03 14:04:37 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env.local());
|
2013-07-03 14:04:37 +00:00
|
|
|
int32_t duration_ms = 100;
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Value> args[] = {
|
|
|
|
v8::Integer::New(env->GetIsolate(), duration_ms)};
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.Run(function, args, arraysize(args), 1000);
|
2013-07-03 14:04:37 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
|
|
|
|
const v8::CpuProfileNode* test_node =
|
|
|
|
GetChild(env.local(), start_node, "test");
|
|
|
|
GetChild(env.local(), test_node, "bar");
|
2013-07-03 14:04:37 +00:00
|
|
|
|
2016-06-22 16:43:46 +00:00
|
|
|
const v8::CpuProfileNode* unresolved_node =
|
|
|
|
FindChild(env.local(), start_node, CodeEntry::kUnresolvedFunctionName);
|
2016-02-08 18:12:04 +00:00
|
|
|
CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "apply"));
|
2013-07-03 14:04:37 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-07-03 14:04:37 +00:00
|
|
|
}
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2014-06-24 16:00:51 +00:00
|
|
|
static const char* cpu_profiler_deep_stack_test_source =
|
2016-02-08 18:12:04 +00:00
|
|
|
"function foo(n) {\n"
|
|
|
|
" if (n)\n"
|
|
|
|
" foo(n - 1);\n"
|
|
|
|
" else\n"
|
|
|
|
" collectSample();\n"
|
|
|
|
"}\n"
|
|
|
|
"function start() {\n"
|
|
|
|
" startProfiling('my_profile');\n"
|
|
|
|
" foo(250);\n"
|
|
|
|
"}\n";
|
2014-06-24 16:00:51 +00:00
|
|
|
|
|
|
|
// Check a deep stack
|
|
|
|
//
|
|
|
|
// [Top down]:
|
|
|
|
// 0 (root) 0 #1
|
|
|
|
// 2 (program) 0 #2
|
|
|
|
// 0 start 21 #3 no reason
|
|
|
|
// 0 foo 21 #4 no reason
|
|
|
|
// 0 foo 21 #5 no reason
|
|
|
|
// ....
|
2016-02-08 18:12:04 +00:00
|
|
|
// 0 foo 21 #254 no reason
|
2014-06-24 16:00:51 +00:00
|
|
|
TEST(CpuProfileDeepStack) {
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
2014-06-24 16:00:51 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(cpu_profiler_deep_stack_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
2014-06-24 16:00:51 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
v8::Local<v8::String> profile_name = v8_str("my_profile");
|
2015-12-08 18:44:23 +00:00
|
|
|
function->Call(env, env->Global(), 0, NULL).ToLocalChecked();
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.profiler()->StopProfiling(profile_name);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(profile);
|
2014-06-24 16:00:51 +00:00
|
|
|
// Dump collected profile to have a better diagnostic in case of failure.
|
|
|
|
reinterpret_cast<i::CpuProfile*>(profile)->Print();
|
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2015-12-08 18:44:23 +00:00
|
|
|
const v8::CpuProfileNode* node = GetChild(env, root, "start");
|
2016-02-08 18:12:04 +00:00
|
|
|
for (int i = 0; i <= 250; ++i) {
|
2015-12-08 18:44:23 +00:00
|
|
|
node = GetChild(env, node, "foo");
|
2014-06-24 16:00:51 +00:00
|
|
|
}
|
2016-02-08 18:12:04 +00:00
|
|
|
CHECK(!FindChild(env, node, "foo"));
|
2014-06-24 16:00:51 +00:00
|
|
|
|
|
|
|
profile->Delete();
|
|
|
|
}
|
|
|
|
|
2013-10-10 13:03:41 +00:00
|
|
|
static const char* js_native_js_test_source =
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(foo);\n"
|
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
|
|
|
"function foo(n) {\n"
|
|
|
|
" var s = 0;\n"
|
|
|
|
" for (var i = 0; i < n; i++) s += i * i * i;\n"
|
|
|
|
" return s;\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}\n"
|
|
|
|
"function bar() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" foo(1000);\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}\n"
|
|
|
|
"function start() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" CallJsFunction(bar);\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}";
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
static void CallJsFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = info[0].As<v8::Function>();
|
|
|
|
v8::Local<v8::Value> argv[] = {info[1]};
|
|
|
|
function->Call(info.GetIsolate()->GetCurrentContext(), info.This(),
|
|
|
|
arraysize(argv), argv)
|
|
|
|
.ToLocalChecked();
|
2013-07-23 15:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// [Top down]:
|
|
|
|
// 58 0 (root) #0 1
|
|
|
|
// 2 2 (program) #0 2
|
|
|
|
// 56 1 start #16 3
|
|
|
|
// 55 0 CallJsFunction #0 4
|
|
|
|
// 55 1 bar #16 5
|
|
|
|
// 54 54 foo #16 6
|
|
|
|
TEST(JsNativeJsSample) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2014-01-17 10:52:00 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
|
2013-12-18 10:31:42 +00:00
|
|
|
env->GetIsolate(), CallJsFunction);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env).ToLocalChecked();
|
2015-02-06 16:50:56 +00:00
|
|
|
func->SetName(v8_str("CallJsFunction"));
|
2015-12-08 18:44:23 +00:00
|
|
|
env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(js_native_js_test_source);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, nullptr, 0, 1000);
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
|
|
|
|
const v8::CpuProfileNode* native_node =
|
|
|
|
GetChild(env, start_node, "CallJsFunction");
|
|
|
|
const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
|
|
|
|
GetChild(env, bar_node, "foo");
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-07-23 15:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char* js_native_js_runtime_js_test_source =
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(foo);\n"
|
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
|
|
|
"function foo(n) {\n"
|
|
|
|
" var s = 0;\n"
|
|
|
|
" for (var i = 0; i < n; i++) s += i * i * i;\n"
|
|
|
|
" return s;\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}\n"
|
|
|
|
"var bound = foo.bind(this);\n"
|
|
|
|
"function bar() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" bound(1000);\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}\n"
|
|
|
|
"function start() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" CallJsFunction(bar);\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}";
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
// [Top down]:
|
|
|
|
// 57 0 (root) #0 1
|
|
|
|
// 55 1 start #16 3
|
|
|
|
// 54 0 CallJsFunction #0 4
|
|
|
|
// 54 3 bar #16 5
|
|
|
|
// 51 51 foo #16 6
|
|
|
|
// 2 2 (program) #0 2
|
|
|
|
TEST(JsNativeJsRuntimeJsSample) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2014-01-17 10:52:00 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
|
2013-12-18 10:31:42 +00:00
|
|
|
env->GetIsolate(), CallJsFunction);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env).ToLocalChecked();
|
2015-02-06 16:50:56 +00:00
|
|
|
func->SetName(v8_str("CallJsFunction"));
|
2015-12-08 18:44:23 +00:00
|
|
|
env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(js_native_js_runtime_js_test_source);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.Run(function, nullptr, 0, 1000);
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
|
|
|
|
const v8::CpuProfileNode* native_node =
|
|
|
|
GetChild(env, start_node, "CallJsFunction");
|
|
|
|
const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
|
|
|
|
GetChild(env, bar_node, "foo");
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-07-23 15:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CallJsFunction2(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
2014-07-01 10:10:12 +00:00
|
|
|
v8::base::OS::Print("In CallJsFunction2\n");
|
2013-07-23 15:01:38 +00:00
|
|
|
CallJsFunction(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* js_native1_js_native2_js_test_source =
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(foo);\n"
|
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"function foo() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" var s = 0;\n"
|
|
|
|
" for (var i = 0; i < 1000; i++) s += i * i * i;\n"
|
|
|
|
" return s;\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}\n"
|
|
|
|
"function bar() {\n"
|
|
|
|
" CallJsFunction2(foo);\n"
|
|
|
|
"}\n"
|
|
|
|
"function start() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" CallJsFunction1(bar);\n"
|
2014-07-11 09:06:12 +00:00
|
|
|
"}";
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
// [Top down]:
|
|
|
|
// 57 0 (root) #0 1
|
|
|
|
// 55 1 start #16 3
|
|
|
|
// 54 0 CallJsFunction1 #0 4
|
|
|
|
// 54 0 bar #16 5
|
|
|
|
// 54 0 CallJsFunction2 #0 6
|
|
|
|
// 54 54 foo #16 7
|
|
|
|
// 2 2 (program) #0 2
|
|
|
|
TEST(JsNative1JsNative2JsSample) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2014-01-17 10:52:00 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func1 =
|
2016-02-08 18:12:04 +00:00
|
|
|
v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction)
|
|
|
|
->GetFunction(env)
|
|
|
|
.ToLocalChecked();
|
2015-02-06 16:50:56 +00:00
|
|
|
func1->SetName(v8_str("CallJsFunction1"));
|
2015-12-08 18:44:23 +00:00
|
|
|
env->Global()->Set(env, v8_str("CallJsFunction1"), func1).FromJust();
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Function> func2 =
|
|
|
|
v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction2)
|
|
|
|
->GetFunction(env)
|
|
|
|
.ToLocalChecked();
|
2015-02-06 16:50:56 +00:00
|
|
|
func2->SetName(v8_str("CallJsFunction2"));
|
2015-12-08 18:44:23 +00:00
|
|
|
env->Global()->Set(env, v8_str("CallJsFunction2"), func2).FromJust();
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
CompileRun(js_native1_js_native2_js_test_source);
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, nullptr, 0, 1000);
|
2013-07-23 15:01:38 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
|
|
|
|
const v8::CpuProfileNode* native_node1 =
|
|
|
|
GetChild(env, start_node, "CallJsFunction1");
|
|
|
|
const v8::CpuProfileNode* bar_node = GetChild(env, native_node1, "bar");
|
|
|
|
const v8::CpuProfileNode* native_node2 =
|
|
|
|
GetChild(env, bar_node, "CallJsFunction2");
|
|
|
|
GetChild(env, native_node2, "foo");
|
2013-07-23 15:01:38 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2013-07-23 15:01:38 +00:00
|
|
|
}
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2016-01-26 20:47:23 +00:00
|
|
|
static const char* js_force_collect_sample_source =
|
|
|
|
"function start() {\n"
|
|
|
|
" CallCollectSample();\n"
|
|
|
|
"}";
|
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
static void CallCollectSample(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
|
i::ProfilerExtension::profiler()->CollectSample();
|
|
|
|
}
|
|
|
|
|
2016-01-26 20:47:23 +00:00
|
|
|
TEST(CollectSampleAPI) {
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> func_template =
|
|
|
|
v8::FunctionTemplate::New(env->GetIsolate(), CallCollectSample);
|
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env).ToLocalChecked();
|
|
|
|
func->SetName(v8_str("CallCollectSample"));
|
|
|
|
env->Global()->Set(env, v8_str("CallCollectSample"), func).FromJust();
|
|
|
|
|
|
|
|
CompileRun(js_force_collect_sample_source);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
2016-01-26 20:47:23 +00:00
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.Run(function, nullptr, 0, 0);
|
2016-01-26 20:47:23 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
|
|
|
|
CHECK_LE(1, start_node->GetChildrenCount());
|
|
|
|
GetChild(env, start_node, "CallCollectSample");
|
2016-01-26 20:47:23 +00:00
|
|
|
|
|
|
|
profile->Delete();
|
|
|
|
}
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2016-02-04 20:00:28 +00:00
|
|
|
static const char* js_native_js_runtime_multiple_test_source =
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(foo);\n"
|
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
2016-02-04 20:00:28 +00:00
|
|
|
"function foo() {\n"
|
|
|
|
" return Math.sin(Math.random());\n"
|
|
|
|
"}\n"
|
|
|
|
"var bound = foo.bind(this);\n"
|
|
|
|
"function bar() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" return bound();\n"
|
2016-02-04 20:00:28 +00:00
|
|
|
"}\n"
|
|
|
|
"function start() {\n"
|
2016-02-08 18:12:04 +00:00
|
|
|
" startProfiling('my_profile');\n"
|
|
|
|
" var startTime = Date.now();\n"
|
|
|
|
" do {\n"
|
|
|
|
" CallJsFunction(bar);\n"
|
|
|
|
" } while (Date.now() - startTime < 200);\n"
|
2016-02-04 20:00:28 +00:00
|
|
|
"}";
|
|
|
|
|
|
|
|
// The test check multiple entrances/exits between JS and native code.
|
|
|
|
//
|
|
|
|
// [Top down]:
|
|
|
|
// (root) #0 1
|
|
|
|
// start #16 3
|
|
|
|
// CallJsFunction #0 4
|
|
|
|
// bar #16 5
|
|
|
|
// foo #16 6
|
|
|
|
// (program) #0 2
|
|
|
|
TEST(JsNativeJsRuntimeJsSampleMultiple) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2016-02-04 20:00:28 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> func_template =
|
|
|
|
v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction);
|
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env).ToLocalChecked();
|
|
|
|
func->SetName(v8_str("CallJsFunction"));
|
|
|
|
env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
|
|
|
|
|
|
|
|
CompileRun(js_native_js_runtime_multiple_test_source);
|
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
|
|
|
v8::CpuProfile* profile = helper.Run(function, nullptr, 0, 500, 500);
|
2016-02-04 20:00:28 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
|
|
|
|
const v8::CpuProfileNode* native_node =
|
|
|
|
GetChild(env, start_node, "CallJsFunction");
|
|
|
|
const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
|
|
|
|
GetChild(env, bar_node, "foo");
|
2016-02-04 20:00:28 +00:00
|
|
|
|
|
|
|
profile->Delete();
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:59:32 +00:00
|
|
|
static const char* inlining_test_source =
|
|
|
|
"%NeverOptimizeFunction(action);\n"
|
|
|
|
"%NeverOptimizeFunction(start);\n"
|
2016-11-23 09:30:11 +00:00
|
|
|
"level1()\n"
|
2016-03-01 05:59:32 +00:00
|
|
|
"%OptimizeFunctionOnNextCall(level1);\n"
|
|
|
|
"%OptimizeFunctionOnNextCall(level2);\n"
|
|
|
|
"%OptimizeFunctionOnNextCall(level3);\n"
|
|
|
|
"var finish = false;\n"
|
|
|
|
"function action(n) {\n"
|
|
|
|
" var s = 0;\n"
|
|
|
|
" for (var i = 0; i < n; ++i) s += i*i*i;\n"
|
|
|
|
" if (finish)\n"
|
|
|
|
" startProfiling('my_profile');\n"
|
|
|
|
" return s;\n"
|
|
|
|
"}\n"
|
|
|
|
"function level3() { return action(100); }\n"
|
|
|
|
"function level2() { return level3() * 2; }\n"
|
|
|
|
"function level1() { return level2(); }\n"
|
|
|
|
"function start() {\n"
|
|
|
|
" var n = 100;\n"
|
|
|
|
" while (--n)\n"
|
|
|
|
" level1();\n"
|
|
|
|
" finish = true;\n"
|
|
|
|
" level1();\n"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
// The test check multiple entrances/exits between JS and native code.
|
|
|
|
//
|
|
|
|
// [Top down]:
|
|
|
|
// (root) #0 1
|
|
|
|
// start #16 3
|
|
|
|
// level1 #0 4
|
|
|
|
// level2 #16 5
|
|
|
|
// level3 #16 6
|
|
|
|
// action #16 7
|
|
|
|
// (program) #0 2
|
|
|
|
TEST(Inlining) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
2016-03-01 05:59:32 +00:00
|
|
|
|
|
|
|
CompileRun(inlining_test_source);
|
|
|
|
v8::Local<v8::Function> function = GetFunction(env, "start");
|
|
|
|
|
|
|
|
v8::Local<v8::String> profile_name = v8_str("my_profile");
|
|
|
|
function->Call(env, env->Global(), 0, NULL).ToLocalChecked();
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfile* profile = helper.profiler()->StopProfiling(profile_name);
|
2016-03-01 05:59:32 +00:00
|
|
|
CHECK(profile);
|
|
|
|
// Dump collected profile to have a better diagnostic in case of failure.
|
|
|
|
reinterpret_cast<i::CpuProfile*>(profile)->Print();
|
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
|
|
|
const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
|
|
|
|
const v8::CpuProfileNode* level1_node = GetChild(env, start_node, "level1");
|
|
|
|
const v8::CpuProfileNode* level2_node = GetChild(env, level1_node, "level2");
|
|
|
|
const v8::CpuProfileNode* level3_node = GetChild(env, level2_node, "level3");
|
|
|
|
GetChild(env, level3_node, "action");
|
|
|
|
|
|
|
|
profile->Delete();
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:04:27 +00:00
|
|
|
// [Top down]:
|
2016-01-26 20:47:23 +00:00
|
|
|
// 0 (root) #0 1
|
|
|
|
// 2 (program) #0 2
|
|
|
|
// 3 (idle) #0 3
|
2013-08-07 17:04:27 +00:00
|
|
|
TEST(IdleTime) {
|
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfiler* cpu_profiler = v8::CpuProfiler::New(env->GetIsolate());
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2015-02-06 16:50:56 +00:00
|
|
|
v8::Local<v8::String> profile_name = v8_str("my_profile");
|
2014-03-28 09:24:49 +00:00
|
|
|
cpu_profiler->StartProfiling(profile_name);
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2013-09-19 09:17:13 +00:00
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
2016-07-08 15:00:23 +00:00
|
|
|
i::ProfilerEventsProcessor* processor =
|
|
|
|
reinterpret_cast<i::CpuProfiler*>(cpu_profiler)->processor();
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
processor->AddCurrentStack(isolate, true);
|
2013-08-07 17:04:27 +00:00
|
|
|
cpu_profiler->SetIdle(true);
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2016-01-26 20:47:23 +00:00
|
|
|
processor->AddCurrentStack(isolate, true);
|
2013-08-07 17:04:27 +00:00
|
|
|
}
|
|
|
|
cpu_profiler->SetIdle(false);
|
2016-01-26 20:47:23 +00:00
|
|
|
processor->AddCurrentStack(isolate, true);
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(profile);
|
2013-08-07 17:04:27 +00:00
|
|
|
// Dump collected profile to have a better diagnostic in case of failure.
|
2014-03-28 09:24:49 +00:00
|
|
|
reinterpret_cast<i::CpuProfile*>(profile)->Print();
|
2013-08-07 17:04:27 +00:00
|
|
|
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* program_node =
|
2016-06-22 16:43:46 +00:00
|
|
|
GetChild(env.local(), root, CodeEntry::kProgramEntryName);
|
2016-02-08 18:12:04 +00:00
|
|
|
CHECK_EQ(0, program_node->GetChildrenCount());
|
|
|
|
CHECK_GE(program_node->GetHitCount(), 2u);
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2016-02-08 18:12:04 +00:00
|
|
|
const v8::CpuProfileNode* idle_node =
|
2016-06-22 16:43:46 +00:00
|
|
|
GetChild(env.local(), root, CodeEntry::kIdleEntryName);
|
2016-02-08 18:12:04 +00:00
|
|
|
CHECK_EQ(0, idle_node->GetChildrenCount());
|
|
|
|
CHECK_GE(idle_node->GetHitCount(), 3u);
|
2013-08-07 17:04:27 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
profile->Delete();
|
2016-07-08 15:00:23 +00:00
|
|
|
cpu_profiler->Dispose();
|
2013-08-07 17:04:27 +00:00
|
|
|
}
|
2013-10-10 13:15:47 +00:00
|
|
|
|
2013-11-22 12:43:17 +00:00
|
|
|
static void CheckFunctionDetails(v8::Isolate* isolate,
|
|
|
|
const v8::CpuProfileNode* node,
|
|
|
|
const char* name, const char* script_name,
|
|
|
|
int script_id, int line, int column) {
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
CHECK(v8_str(name)->Equals(context, node->GetFunctionName()).FromJust());
|
2016-09-09 15:50:23 +00:00
|
|
|
CHECK_EQ(0, strcmp(name, node->GetFunctionNameStr()));
|
2015-12-08 18:44:23 +00:00
|
|
|
CHECK(v8_str(script_name)
|
|
|
|
->Equals(context, node->GetScriptResourceName())
|
|
|
|
.FromJust());
|
2016-09-09 15:50:23 +00:00
|
|
|
CHECK_EQ(0, strcmp(script_name, node->GetScriptResourceNameStr()));
|
2013-10-10 13:15:47 +00:00
|
|
|
CHECK_EQ(script_id, node->GetScriptId());
|
|
|
|
CHECK_EQ(line, node->GetLineNumber());
|
|
|
|
CHECK_EQ(column, node->GetColumnNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FunctionDetails) {
|
2016-02-08 18:12:04 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
2014-01-17 10:52:00 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
2013-10-10 13:15:47 +00:00
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Script> script_a = CompileWithOrigin(
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(foo);\n"
|
|
|
|
"%NeverOptimizeFunction(bar);\n"
|
|
|
|
" function foo\n() { bar(); }\n"
|
2015-12-08 18:44:23 +00:00
|
|
|
" function bar() { startProfiling(); }\n",
|
|
|
|
"script_a");
|
|
|
|
script_a->Run(env).ToLocalChecked();
|
|
|
|
v8::Local<v8::Script> script_b = CompileWithOrigin(
|
2016-02-08 18:12:04 +00:00
|
|
|
"%NeverOptimizeFunction(baz);"
|
|
|
|
"\n\n function baz() { foo(); }\n"
|
2015-12-08 18:44:23 +00:00
|
|
|
"\n\nbaz();\n"
|
|
|
|
"stopProfiling();\n",
|
|
|
|
"script_b");
|
|
|
|
script_b->Run(env).ToLocalChecked();
|
2014-01-17 10:52:00 +00:00
|
|
|
const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
|
2013-10-10 13:15:47 +00:00
|
|
|
const v8::CpuProfileNode* current = profile->GetTopDownRoot();
|
|
|
|
reinterpret_cast<ProfileNode*>(
|
|
|
|
const_cast<v8::CpuProfileNode*>(current))->Print(0);
|
|
|
|
// The tree should look like this:
|
|
|
|
// 0 (root) 0 #1
|
2014-08-05 07:08:39 +00:00
|
|
|
// 0 "" 19 #2 no reason script_b:1
|
2013-10-10 13:15:47 +00:00
|
|
|
// 0 baz 19 #3 TryCatchStatement script_b:3
|
|
|
|
// 0 foo 18 #4 TryCatchStatement script_a:2
|
|
|
|
// 1 bar 18 #5 no reason script_a:3
|
|
|
|
const v8::CpuProfileNode* root = profile->GetTopDownRoot();
|
2015-12-08 18:44:23 +00:00
|
|
|
const v8::CpuProfileNode* script = GetChild(env, root, "");
|
2014-08-05 07:08:39 +00:00
|
|
|
CheckFunctionDetails(env->GetIsolate(), script, "", "script_b",
|
2014-06-05 13:02:18 +00:00
|
|
|
script_b->GetUnboundScript()->GetId(), 1, 1);
|
2015-12-08 18:44:23 +00:00
|
|
|
const v8::CpuProfileNode* baz = GetChild(env, script, "baz");
|
2013-11-22 12:43:17 +00:00
|
|
|
CheckFunctionDetails(env->GetIsolate(), baz, "baz", "script_b",
|
2014-06-05 13:02:18 +00:00
|
|
|
script_b->GetUnboundScript()->GetId(), 3, 16);
|
2015-12-08 18:44:23 +00:00
|
|
|
const v8::CpuProfileNode* foo = GetChild(env, baz, "foo");
|
2013-11-22 12:43:17 +00:00
|
|
|
CheckFunctionDetails(env->GetIsolate(), foo, "foo", "script_a",
|
2016-02-08 18:12:04 +00:00
|
|
|
script_a->GetUnboundScript()->GetId(), 4, 1);
|
2015-12-08 18:44:23 +00:00
|
|
|
const v8::CpuProfileNode* bar = GetChild(env, foo, "bar");
|
2013-11-22 12:43:17 +00:00
|
|
|
CheckFunctionDetails(env->GetIsolate(), bar, "bar", "script_a",
|
2016-02-08 18:12:04 +00:00
|
|
|
script_a->GetUnboundScript()->GetId(), 5, 14);
|
2013-10-10 13:15:47 +00:00
|
|
|
}
|
2013-12-11 14:39:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(DontStopOnFinishedProfileDelete) {
|
2014-01-17 10:52:00 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2013-12-11 14:39:18 +00:00
|
|
|
|
2016-07-08 15:00:23 +00:00
|
|
|
v8::CpuProfiler* profiler = v8::CpuProfiler::New(env->GetIsolate());
|
2013-12-18 08:59:09 +00:00
|
|
|
i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
|
2013-12-11 14:39:18 +00:00
|
|
|
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::String> outer = v8_str("outer");
|
2014-03-28 09:24:49 +00:00
|
|
|
profiler->StartProfiling(outer);
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2013-12-11 14:39:18 +00:00
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::String> inner = v8_str("inner");
|
2014-03-28 09:24:49 +00:00
|
|
|
profiler->StartProfiling(inner);
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2013-12-11 14:39:18 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
v8::CpuProfile* inner_profile = profiler->StopProfiling(inner);
|
2013-12-11 14:39:18 +00:00
|
|
|
CHECK(inner_profile);
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(1, iprofiler->GetProfilesCount());
|
2014-03-28 09:24:49 +00:00
|
|
|
inner_profile->Delete();
|
2013-12-11 14:39:18 +00:00
|
|
|
inner_profile = NULL;
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2013-12-11 14:39:18 +00:00
|
|
|
|
2014-03-28 09:24:49 +00:00
|
|
|
v8::CpuProfile* outer_profile = profiler->StopProfiling(outer);
|
2013-12-11 14:39:18 +00:00
|
|
|
CHECK(outer_profile);
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(1, iprofiler->GetProfilesCount());
|
2014-03-28 09:24:49 +00:00
|
|
|
outer_profile->Delete();
|
2013-12-11 14:39:18 +00:00
|
|
|
outer_profile = NULL;
|
2013-12-18 08:59:09 +00:00
|
|
|
CHECK_EQ(0, iprofiler->GetProfilesCount());
|
2016-07-08 15:00:23 +00:00
|
|
|
profiler->Dispose();
|
2013-12-11 14:39:18 +00:00
|
|
|
}
|
2015-02-10 14:32:42 +00:00
|
|
|
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
const char* GetBranchDeoptReason(v8::Local<v8::Context> context,
|
|
|
|
i::CpuProfile* iprofile, const char* branch[],
|
2015-03-09 14:43:29 +00:00
|
|
|
int length) {
|
|
|
|
v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
|
|
|
|
const ProfileNode* iopt_function = NULL;
|
2015-12-08 18:44:23 +00:00
|
|
|
iopt_function = GetSimpleBranch(context, profile, branch, length);
|
2015-04-10 13:59:39 +00:00
|
|
|
CHECK_EQ(1U, iopt_function->deopt_infos().size());
|
2015-03-09 14:43:29 +00:00
|
|
|
return iopt_function->deopt_infos()[0].deopt_reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-05 10:37:56 +00:00
|
|
|
// deopt at top function
|
2015-02-10 14:32:42 +00:00
|
|
|
TEST(CollectDeoptEvents) {
|
2017-04-28 15:36:46 +00:00
|
|
|
if (!CcTest::i_isolate()->use_optimizer() || i::FLAG_always_opt) return;
|
2015-02-10 14:32:42 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
i::CpuProfiler* iprofiler =
|
|
|
|
reinterpret_cast<i::CpuProfiler*>(helper.profiler());
|
2015-02-10 14:32:42 +00:00
|
|
|
|
2015-03-09 14:43:29 +00:00
|
|
|
const char opt_source[] =
|
|
|
|
"function opt_function%d(value, depth) {\n"
|
|
|
|
" if (depth) return opt_function%d(value, depth - 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
" return 10 / value;\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"}\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"\n";
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
i::EmbeddedVector<char, sizeof(opt_source) + 100> buffer;
|
|
|
|
i::SNPrintF(buffer, opt_source, i, i);
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Script::Compile(env, v8_str(buffer.start()))
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(env)
|
|
|
|
.ToLocalChecked();
|
2015-03-09 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* source =
|
|
|
|
"startProfiling();\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"opt_function0(1, 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"%OptimizeFunctionOnNextCall(opt_function0)\n"
|
|
|
|
"\n"
|
|
|
|
"opt_function0(1, 1);\n"
|
|
|
|
"\n"
|
|
|
|
"opt_function0(undefined, 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"opt_function1(1, 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"%OptimizeFunctionOnNextCall(opt_function1)\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"opt_function1(1, 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"opt_function1(NaN, 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"opt_function2(1, 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"%OptimizeFunctionOnNextCall(opt_function2)\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
2015-03-09 14:43:29 +00:00
|
|
|
"opt_function2(1, 1);\n"
|
|
|
|
"\n"
|
|
|
|
"opt_function2(0, 1);\n"
|
2015-03-05 10:37:56 +00:00
|
|
|
"\n"
|
|
|
|
"stopProfiling();\n"
|
|
|
|
"\n";
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Script::Compile(env, v8_str(source))
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(env)
|
|
|
|
.ToLocalChecked();
|
2015-02-10 14:32:42 +00:00
|
|
|
i::CpuProfile* iprofile = iprofiler->GetProfile(0);
|
|
|
|
iprofile->Print();
|
2015-03-24 12:46:13 +00:00
|
|
|
/* The expected profile
|
|
|
|
[Top down]:
|
|
|
|
0 (root) 0 #1
|
|
|
|
23 32 #2
|
|
|
|
1 opt_function2 31 #7
|
|
|
|
1 opt_function2 31 #8
|
|
|
|
;;; deopted at script_id: 31 position: 106 with reason
|
|
|
|
'division by zero'.
|
|
|
|
2 opt_function0 29 #3
|
|
|
|
4 opt_function0 29 #4
|
|
|
|
;;; deopted at script_id: 29 position: 108 with reason 'not a
|
|
|
|
heap number'.
|
|
|
|
0 opt_function1 30 #5
|
|
|
|
1 opt_function1 30 #6
|
|
|
|
;;; deopted at script_id: 30 position: 108 with reason 'lost
|
|
|
|
precision or NaN'.
|
|
|
|
*/
|
|
|
|
|
2015-03-09 14:43:29 +00:00
|
|
|
{
|
|
|
|
const char* branch[] = {"", "opt_function0", "opt_function0"};
|
2016-12-09 15:05:48 +00:00
|
|
|
const char* deopt_reason =
|
|
|
|
GetBranchDeoptReason(env, iprofile, branch, arraysize(branch));
|
|
|
|
if (deopt_reason != reason(i::DeoptimizeReason::kNotAHeapNumber) &&
|
|
|
|
deopt_reason != reason(i::DeoptimizeReason::kNotASmi)) {
|
|
|
|
FATAL(deopt_reason);
|
|
|
|
}
|
2015-03-09 14:43:29 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char* branch[] = {"", "opt_function1", "opt_function1"};
|
2015-03-10 10:45:07 +00:00
|
|
|
const char* deopt_reason =
|
2015-12-08 18:44:23 +00:00
|
|
|
GetBranchDeoptReason(env, iprofile, branch, arraysize(branch));
|
2016-07-18 09:23:28 +00:00
|
|
|
if (deopt_reason != reason(i::DeoptimizeReason::kNaN) &&
|
2016-12-09 15:05:48 +00:00
|
|
|
deopt_reason != reason(i::DeoptimizeReason::kLostPrecisionOrNaN) &&
|
|
|
|
deopt_reason != reason(i::DeoptimizeReason::kNotASmi)) {
|
2015-03-10 10:45:07 +00:00
|
|
|
FATAL(deopt_reason);
|
|
|
|
}
|
2015-03-09 14:43:29 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const char* branch[] = {"", "opt_function2", "opt_function2"};
|
2016-07-18 09:23:28 +00:00
|
|
|
CHECK_EQ(reason(i::DeoptimizeReason::kDivisionByZero),
|
2015-12-08 18:44:23 +00:00
|
|
|
GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)));
|
2015-03-09 14:43:29 +00:00
|
|
|
}
|
2015-02-10 14:32:42 +00:00
|
|
|
iprofiler->DeleteProfile(iprofile);
|
|
|
|
}
|
2015-02-19 10:07:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(SourceLocation) {
|
|
|
|
i::FLAG_always_opt = true;
|
|
|
|
i::FLAG_hydrogen_track_positions = true;
|
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
|
|
|
|
const char* source =
|
|
|
|
"function CompareStatementWithThis() {\n"
|
|
|
|
" if (this === 1) {}\n"
|
|
|
|
"}\n"
|
|
|
|
"CompareStatementWithThis();\n";
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Script::Compile(env.local(), v8_str(source))
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(env.local())
|
|
|
|
.ToLocalChecked();
|
2015-02-19 10:07:46 +00:00
|
|
|
}
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
static const char* inlined_source =
|
2016-11-22 10:14:36 +00:00
|
|
|
"function opt_function(left, right) { var k = left*right; return k + 1; "
|
|
|
|
"}\n";
|
2015-03-24 12:46:13 +00:00
|
|
|
// 0.........1.........2.........3.........4....*....5.........6......*..7
|
|
|
|
|
|
|
|
|
|
|
|
// deopt at the first level inlined function
|
|
|
|
TEST(DeoptAtFirstLevelInlinedSource) {
|
2017-04-28 15:36:46 +00:00
|
|
|
if (!CcTest::i_isolate()->use_optimizer() || i::FLAG_always_opt) return;
|
2015-03-24 12:46:13 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
i::CpuProfiler* iprofiler =
|
|
|
|
reinterpret_cast<i::CpuProfiler*>(helper.profiler());
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
// 0.........1.........2.........3.........4.........5.........6.........7
|
|
|
|
const char* source =
|
2016-11-22 10:14:36 +00:00
|
|
|
"function test(left, right) { return opt_function(left, right); }\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"startProfiling();\n"
|
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test(10, 10);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"%OptimizeFunctionOnNextCall(test)\n"
|
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test(10, 10);\n"
|
|
|
|
"\n"
|
|
|
|
"test(undefined, 1e9);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"stopProfiling();\n"
|
|
|
|
"\n";
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
|
|
|
|
inlined_script->Run(env).ToLocalChecked();
|
2015-03-24 12:46:13 +00:00
|
|
|
int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Script> script = v8_compile(source);
|
|
|
|
script->Run(env).ToLocalChecked();
|
2015-03-24 12:46:13 +00:00
|
|
|
int script_id = script->GetUnboundScript()->GetId();
|
|
|
|
|
|
|
|
i::CpuProfile* iprofile = iprofiler->GetProfile(0);
|
|
|
|
iprofile->Print();
|
|
|
|
/* The expected profile output
|
|
|
|
[Top down]:
|
|
|
|
0 (root) 0 #1
|
|
|
|
10 30 #2
|
|
|
|
1 test 30 #3
|
|
|
|
;;; deopted at script_id: 29 position: 45 with reason 'not a
|
|
|
|
heap number'.
|
|
|
|
;;; Inline point: script_id 30 position: 36.
|
|
|
|
4 opt_function 29 #4
|
|
|
|
*/
|
|
|
|
v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
|
|
|
|
|
|
|
|
const char* branch[] = {"", "test"};
|
|
|
|
const ProfileNode* itest_node =
|
2015-12-08 18:44:23 +00:00
|
|
|
GetSimpleBranch(env, profile, branch, arraysize(branch));
|
2015-04-08 16:13:24 +00:00
|
|
|
const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
|
|
|
|
itest_node->deopt_infos();
|
2015-04-10 13:59:39 +00:00
|
|
|
CHECK_EQ(1U, deopt_infos.size());
|
2015-03-24 12:46:13 +00:00
|
|
|
|
2015-04-08 16:13:24 +00:00
|
|
|
const v8::CpuProfileDeoptInfo& info = deopt_infos[0];
|
2016-11-22 10:14:36 +00:00
|
|
|
CHECK(reason(i::DeoptimizeReason::kNotASmi) == info.deopt_reason ||
|
|
|
|
reason(i::DeoptimizeReason::kNotAHeapNumber) == info.deopt_reason);
|
2015-04-10 13:59:39 +00:00
|
|
|
CHECK_EQ(2U, info.stack.size());
|
2015-03-24 12:46:13 +00:00
|
|
|
CHECK_EQ(inlined_script_id, info.stack[0].script_id);
|
2016-11-22 10:14:36 +00:00
|
|
|
CHECK_LE(dist(offset(inlined_source, "*right"), info.stack[0].position), 1);
|
2015-03-24 12:46:13 +00:00
|
|
|
CHECK_EQ(script_id, info.stack[1].script_id);
|
2016-11-22 10:14:36 +00:00
|
|
|
CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
iprofiler->DeleteProfile(iprofile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// deopt at the second level inlined function
|
|
|
|
TEST(DeoptAtSecondLevelInlinedSource) {
|
2017-04-28 15:36:46 +00:00
|
|
|
if (!CcTest::i_isolate()->use_optimizer() || i::FLAG_always_opt) return;
|
2015-03-24 12:46:13 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
i::CpuProfiler* iprofiler =
|
|
|
|
reinterpret_cast<i::CpuProfiler*>(helper.profiler());
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
// 0.........1.........2.........3.........4.........5.........6.........7
|
|
|
|
const char* source =
|
2016-11-22 10:14:36 +00:00
|
|
|
"function test2(left, right) { return opt_function(left, right); }\n"
|
|
|
|
"function test1(left, right) { return test2(left, right); } \n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"startProfiling();\n"
|
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test1(10, 10);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"%OptimizeFunctionOnNextCall(test1)\n"
|
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test1(10, 10);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test1(undefined, 1e9);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"stopProfiling();\n"
|
|
|
|
"\n";
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
|
|
|
|
inlined_script->Run(env).ToLocalChecked();
|
2015-03-24 12:46:13 +00:00
|
|
|
int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Script> script = v8_compile(source);
|
|
|
|
script->Run(env).ToLocalChecked();
|
2015-03-24 12:46:13 +00:00
|
|
|
int script_id = script->GetUnboundScript()->GetId();
|
|
|
|
|
|
|
|
i::CpuProfile* iprofile = iprofiler->GetProfile(0);
|
|
|
|
iprofile->Print();
|
|
|
|
/* The expected profile output
|
|
|
|
[Top down]:
|
|
|
|
0 (root) 0 #1
|
|
|
|
11 30 #2
|
|
|
|
1 test1 30 #3
|
|
|
|
;;; deopted at script_id: 29 position: 45 with reason 'not a
|
|
|
|
heap number'.
|
|
|
|
;;; Inline point: script_id 30 position: 37.
|
|
|
|
;;; Inline point: script_id 30 position: 103.
|
|
|
|
1 test2 30 #4
|
|
|
|
3 opt_function 29 #5
|
|
|
|
*/
|
|
|
|
|
|
|
|
v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
|
|
|
|
|
|
|
|
const char* branch[] = {"", "test1"};
|
|
|
|
const ProfileNode* itest_node =
|
2015-12-08 18:44:23 +00:00
|
|
|
GetSimpleBranch(env, profile, branch, arraysize(branch));
|
2015-04-08 16:13:24 +00:00
|
|
|
const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
|
|
|
|
itest_node->deopt_infos();
|
2015-04-10 13:59:39 +00:00
|
|
|
CHECK_EQ(1U, deopt_infos.size());
|
2015-03-24 12:46:13 +00:00
|
|
|
|
2015-04-08 16:13:24 +00:00
|
|
|
const v8::CpuProfileDeoptInfo info = deopt_infos[0];
|
2016-11-22 10:14:36 +00:00
|
|
|
CHECK(reason(i::DeoptimizeReason::kNotASmi) == info.deopt_reason ||
|
|
|
|
reason(i::DeoptimizeReason::kNotAHeapNumber) == info.deopt_reason);
|
2015-04-10 13:59:39 +00:00
|
|
|
CHECK_EQ(3U, info.stack.size());
|
2015-03-24 12:46:13 +00:00
|
|
|
CHECK_EQ(inlined_script_id, info.stack[0].script_id);
|
2016-11-22 10:14:36 +00:00
|
|
|
CHECK_LE(dist(offset(inlined_source, "*right"), info.stack[0].position), 1);
|
2015-03-24 12:46:13 +00:00
|
|
|
CHECK_EQ(script_id, info.stack[1].script_id);
|
2016-11-22 10:14:36 +00:00
|
|
|
CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
|
|
|
|
CHECK_EQ(offset(source, "test2(left, right);"), info.stack[2].position);
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
iprofiler->DeleteProfile(iprofile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// deopt in untracked function
|
|
|
|
TEST(DeoptUntrackedFunction) {
|
2017-04-28 15:36:46 +00:00
|
|
|
if (!CcTest::i_isolate()->use_optimizer() || i::FLAG_always_opt) return;
|
2015-03-24 12:46:13 +00:00
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
|
|
|
|
v8::Context::Scope context_scope(env);
|
2016-07-08 15:00:23 +00:00
|
|
|
ProfilerHelper helper(env);
|
|
|
|
i::CpuProfiler* iprofiler =
|
|
|
|
reinterpret_cast<i::CpuProfiler*>(helper.profiler());
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
// 0.........1.........2.........3.........4.........5.........6.........7
|
|
|
|
const char* source =
|
|
|
|
"function test(left, right) { return opt_function(left, right); }\n"
|
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test(10, 10);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"%OptimizeFunctionOnNextCall(test)\n"
|
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test(10, 10);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"startProfiling();\n" // profiler started after compilation.
|
|
|
|
"\n"
|
2016-11-22 10:14:36 +00:00
|
|
|
"test(undefined, 10);\n"
|
2015-03-24 12:46:13 +00:00
|
|
|
"\n"
|
|
|
|
"stopProfiling();\n"
|
|
|
|
"\n";
|
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
|
|
|
|
inlined_script->Run(env).ToLocalChecked();
|
2015-03-24 12:46:13 +00:00
|
|
|
|
2015-12-08 18:44:23 +00:00
|
|
|
v8::Local<v8::Script> script = v8_compile(source);
|
|
|
|
script->Run(env).ToLocalChecked();
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
i::CpuProfile* iprofile = iprofiler->GetProfile(0);
|
|
|
|
iprofile->Print();
|
|
|
|
v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
|
|
|
|
|
|
|
|
const char* branch[] = {"", "test"};
|
|
|
|
const ProfileNode* itest_node =
|
2015-12-08 18:44:23 +00:00
|
|
|
GetSimpleBranch(env, profile, branch, arraysize(branch));
|
2015-04-10 13:59:39 +00:00
|
|
|
CHECK_EQ(0U, itest_node->deopt_infos().size());
|
2015-03-24 12:46:13 +00:00
|
|
|
|
|
|
|
iprofiler->DeleteProfile(iprofile);
|
|
|
|
}
|
2016-10-06 18:14:03 +00:00
|
|
|
|
|
|
|
using v8::platform::tracing::TraceBuffer;
|
|
|
|
using v8::platform::tracing::TraceConfig;
|
|
|
|
using v8::platform::tracing::TraceObject;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CpuProfileEventChecker : public v8::platform::tracing::TraceWriter {
|
|
|
|
public:
|
|
|
|
void AppendTraceEvent(TraceObject* trace_event) override {
|
2016-10-20 17:46:54 +00:00
|
|
|
if (trace_event->name() != std::string("Profile") &&
|
|
|
|
trace_event->name() != std::string("ProfileChunk"))
|
2016-10-06 18:14:03 +00:00
|
|
|
return;
|
|
|
|
CHECK(!profile_id_ || trace_event->id() == profile_id_);
|
|
|
|
CHECK_EQ(1, trace_event->num_args());
|
|
|
|
CHECK_EQ(TRACE_VALUE_TYPE_CONVERTABLE, trace_event->arg_types()[0]);
|
|
|
|
profile_id_ = trace_event->id();
|
|
|
|
v8::ConvertableToTraceFormat* arg =
|
|
|
|
trace_event->arg_convertables()[0].get();
|
|
|
|
arg->AppendAsTraceFormat(&result_json_);
|
|
|
|
}
|
|
|
|
void Flush() override {}
|
|
|
|
|
|
|
|
std::string result_json() const { return result_json_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string result_json_;
|
|
|
|
uint64_t profile_id_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(TracingCpuProfiler) {
|
|
|
|
v8::Platform* old_platform = i::V8::GetCurrentPlatform();
|
|
|
|
v8::Platform* default_platform = v8::platform::CreateDefaultPlatform();
|
|
|
|
i::V8::SetPlatformForTesting(default_platform);
|
|
|
|
|
|
|
|
v8::platform::tracing::TracingController tracing_controller;
|
|
|
|
v8::platform::SetTracingController(default_platform, &tracing_controller);
|
|
|
|
|
|
|
|
CpuProfileEventChecker* event_checker = new CpuProfileEventChecker();
|
|
|
|
TraceBuffer* ring_buffer =
|
|
|
|
TraceBuffer::CreateTraceBufferRingBuffer(1, event_checker);
|
|
|
|
tracing_controller.Initialize(ring_buffer);
|
|
|
|
TraceConfig* trace_config = new TraceConfig();
|
|
|
|
trace_config->AddIncludedCategory(
|
|
|
|
TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"));
|
|
|
|
|
|
|
|
LocalContext env;
|
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
{
|
|
|
|
tracing_controller.StartTracing(trace_config);
|
|
|
|
auto profiler = v8::TracingCpuProfiler::Create(env->GetIsolate());
|
|
|
|
CompileRun("function foo() { } foo();");
|
|
|
|
tracing_controller.StopTracing();
|
|
|
|
CompileRun("function bar() { } bar();");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* profile_checker =
|
|
|
|
"function checkProfile(profile) {\n"
|
|
|
|
" if (typeof profile['startTime'] !== 'number') return 'startTime';\n"
|
|
|
|
" return '';\n"
|
|
|
|
"}\n"
|
|
|
|
"checkProfile(";
|
|
|
|
std::string profile_json = event_checker->result_json();
|
|
|
|
CHECK_LT(0u, profile_json.length());
|
|
|
|
printf("Profile JSON: %s\n", profile_json.c_str());
|
|
|
|
std::string code = profile_checker + profile_json + ")";
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
CompileRunChecked(CcTest::isolate(), code.c_str());
|
|
|
|
v8::String::Utf8Value value(result);
|
|
|
|
printf("Check result: %*s\n", value.length(), *value);
|
|
|
|
CHECK_EQ(0, value.length());
|
|
|
|
|
|
|
|
i::V8::SetPlatformForTesting(old_platform);
|
|
|
|
}
|