Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 27 | // |
| 28 | // Tests of profiles generator and utilities. |
| 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | #include "src/v8.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | #include "include/v8-profiler.h" |
| 33 | #include "src/base/platform/platform.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | #include "src/base/smart-pointers.h" |
| 35 | #include "src/deoptimizer.h" |
| 36 | #include "src/profiler/cpu-profiler-inl.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | #include "src/utils.h" |
| 38 | #include "test/cctest/cctest.h" |
| 39 | #include "test/cctest/profiler-extension.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 40 | using i::CodeEntry; |
| 41 | using i::CpuProfile; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 42 | using i::CpuProfiler; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 43 | using i::CpuProfilesCollection; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | using i::Heap; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 45 | using i::ProfileGenerator; |
| 46 | using i::ProfileNode; |
| 47 | using i::ProfilerEventsProcessor; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | using i::ScopedVector; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 49 | using i::Vector; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 50 | using v8::base::SmartPointer; |
| 51 | |
| 52 | |
| 53 | // Helper methods |
| 54 | static v8::Local<v8::Function> GetFunction(v8::Local<v8::Context> env, |
| 55 | const char* name) { |
| 56 | return v8::Local<v8::Function>::Cast( |
| 57 | env->Global()->Get(env, v8_str(name)).ToLocalChecked()); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static size_t offset(const char* src, const char* substring) { |
| 62 | const char* it = strstr(src, substring); |
| 63 | CHECK(it); |
| 64 | return static_cast<size_t>(it - src); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static const char* reason(const i::Deoptimizer::DeoptReason reason) { |
| 69 | return i::Deoptimizer::GetDeoptReason(reason); |
| 70 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 71 | |
| 72 | |
| 73 | TEST(StartStop) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | i::Isolate* isolate = CcTest::i_isolate(); |
| 75 | CpuProfilesCollection profiles(isolate->heap()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 76 | ProfileGenerator generator(&profiles); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 78 | &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | processor->Start(); |
| 80 | processor->StopSynchronously(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 83 | |
| 84 | static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc, |
| 85 | i::Address frame1, |
| 86 | i::Address frame2 = NULL, |
| 87 | i::Address frame3 = NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | i::TickSample* sample = proc->StartTickSample(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 89 | sample->pc = frame1; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 90 | sample->tos = frame1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 91 | sample->frames_count = 0; |
| 92 | if (frame2 != NULL) { |
| 93 | sample->stack[0] = frame2; |
| 94 | sample->frames_count = 1; |
| 95 | } |
| 96 | if (frame3 != NULL) { |
| 97 | sample->stack[1] = frame3; |
| 98 | sample->frames_count = 2; |
| 99 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | proc->FinishTickSample(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | namespace { |
| 104 | |
| 105 | class TestSetup { |
| 106 | public: |
| 107 | TestSetup() |
| 108 | : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) { |
| 109 | i::FLAG_prof_browser_mode = false; |
| 110 | } |
| 111 | |
| 112 | ~TestSetup() { |
| 113 | i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_; |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | bool old_flag_prof_browser_mode_; |
| 118 | }; |
| 119 | |
| 120 | } // namespace |
| 121 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 122 | i::AbstractCode* CreateCode(LocalContext* env) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | static int counter = 0; |
| 124 | i::EmbeddedVector<char, 256> script; |
| 125 | i::EmbeddedVector<char, 32> name; |
| 126 | |
| 127 | i::SNPrintF(name, "function_%d", ++counter); |
| 128 | const char* name_start = name.start(); |
| 129 | i::SNPrintF(script, |
| 130 | "function %s() {\n" |
| 131 | "var counter = 0;\n" |
| 132 | "for (var i = 0; i < %d; ++i) counter += i;\n" |
| 133 | "return '%s_' + counter;\n" |
| 134 | "}\n" |
| 135 | "%s();\n", name_start, counter, name_start, name_start); |
| 136 | CompileRun(script.start()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 137 | |
| 138 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast( |
| 139 | v8::Utils::OpenHandle(*GetFunction(env->local(), name_start))); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 140 | return fun->abstract_code(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 143 | TEST(CodeEvents) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | CcTest::InitializeVM(); |
| 145 | LocalContext env; |
| 146 | i::Isolate* isolate = CcTest::i_isolate(); |
| 147 | i::Factory* factory = isolate->factory(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 148 | TestSetup test_setup; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | |
| 150 | i::HandleScope scope(isolate); |
| 151 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 152 | i::AbstractCode* aaa_code = CreateCode(&env); |
| 153 | i::AbstractCode* comment_code = CreateCode(&env); |
| 154 | i::AbstractCode* args5_code = CreateCode(&env); |
| 155 | i::AbstractCode* comment2_code = CreateCode(&env); |
| 156 | i::AbstractCode* moved_code = CreateCode(&env); |
| 157 | i::AbstractCode* args3_code = CreateCode(&env); |
| 158 | i::AbstractCode* args4_code = CreateCode(&env); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 159 | |
| 160 | CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap()); |
| 161 | profiles->StartProfiling("", false); |
| 162 | ProfileGenerator generator(profiles); |
| 163 | SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor( |
| 164 | &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100))); |
| 165 | processor->Start(); |
| 166 | CpuProfiler profiler(isolate, profiles, &generator, processor.get()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 167 | |
| 168 | // Enqueue code creation events. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 169 | const char* aaa_str = "aaa"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 170 | i::Handle<i::String> aaa_name = factory->NewStringFromAsciiChecked(aaa_str); |
| 171 | profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, aaa_code, *aaa_name); |
| 172 | profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment_code, "comment"); |
| 173 | profiler.CodeCreateEvent(i::Logger::STUB_TAG, args5_code, 5); |
| 174 | profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment2_code, "comment2"); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 175 | profiler.CodeMoveEvent(comment2_code, moved_code->address()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | profiler.CodeCreateEvent(i::Logger::STUB_TAG, args3_code, 3); |
| 177 | profiler.CodeCreateEvent(i::Logger::STUB_TAG, args4_code, 4); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 178 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | // Enqueue a tick event to enable code events processing. |
| 180 | EnqueueTickSampleEvent(processor.get(), aaa_code->address()); |
| 181 | |
| 182 | processor->StopSynchronously(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 183 | |
| 184 | // Check the state of profile generator. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 185 | CodeEntry* aaa = generator.code_map()->FindEntry(aaa_code->address()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 186 | CHECK(aaa); |
| 187 | CHECK_EQ(0, strcmp(aaa_str, aaa->name())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 188 | |
| 189 | CodeEntry* comment = generator.code_map()->FindEntry(comment_code->address()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 190 | CHECK(comment); |
| 191 | CHECK_EQ(0, strcmp("comment", comment->name())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 192 | |
| 193 | CodeEntry* args5 = generator.code_map()->FindEntry(args5_code->address()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 194 | CHECK(args5); |
| 195 | CHECK_EQ(0, strcmp("5", args5->name())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 196 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 197 | CHECK(!generator.code_map()->FindEntry(comment2_code->address())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 198 | |
| 199 | CodeEntry* comment2 = generator.code_map()->FindEntry(moved_code->address()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 200 | CHECK(comment2); |
| 201 | CHECK_EQ(0, strcmp("comment2", comment2->name())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 204 | template<typename T> |
| 205 | static int CompareProfileNodes(const T* p1, const T* p2) { |
| 206 | return strcmp((*p1)->entry()->name(), (*p2)->entry()->name()); |
| 207 | } |
| 208 | |
| 209 | TEST(TickEvents) { |
| 210 | TestSetup test_setup; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 211 | LocalContext env; |
| 212 | i::Isolate* isolate = CcTest::i_isolate(); |
| 213 | i::HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 214 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 215 | i::AbstractCode* frame1_code = CreateCode(&env); |
| 216 | i::AbstractCode* frame2_code = CreateCode(&env); |
| 217 | i::AbstractCode* frame3_code = CreateCode(&env); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 218 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 219 | CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap()); |
| 220 | profiles->StartProfiling("", false); |
| 221 | ProfileGenerator generator(profiles); |
| 222 | SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor( |
| 223 | &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100))); |
| 224 | processor->Start(); |
| 225 | CpuProfiler profiler(isolate, profiles, &generator, processor.get()); |
| 226 | |
| 227 | profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame1_code, "bbb"); |
| 228 | profiler.CodeCreateEvent(i::Logger::STUB_TAG, frame2_code, 5); |
| 229 | profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame3_code, "ddd"); |
| 230 | |
| 231 | EnqueueTickSampleEvent(processor.get(), frame1_code->instruction_start()); |
| 232 | EnqueueTickSampleEvent( |
| 233 | processor.get(), |
| 234 | frame2_code->instruction_start() + frame2_code->ExecutableSize() / 2, |
| 235 | frame1_code->instruction_start() + frame2_code->ExecutableSize() / 2); |
| 236 | EnqueueTickSampleEvent( |
| 237 | processor.get(), |
| 238 | frame3_code->instruction_end() - 1, |
| 239 | frame2_code->instruction_end() - 1, |
| 240 | frame1_code->instruction_end() - 1); |
| 241 | |
| 242 | processor->StopSynchronously(); |
| 243 | CpuProfile* profile = profiles->StopProfiling(""); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 244 | CHECK(profile); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 245 | |
| 246 | // Check call trees. |
| 247 | const i::List<ProfileNode*>* top_down_root_children = |
| 248 | profile->top_down()->root()->children(); |
| 249 | CHECK_EQ(1, top_down_root_children->length()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 250 | CHECK_EQ(0, strcmp("bbb", top_down_root_children->last()->entry()->name())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 251 | const i::List<ProfileNode*>* top_down_bbb_children = |
| 252 | top_down_root_children->last()->children(); |
| 253 | CHECK_EQ(1, top_down_bbb_children->length()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 254 | CHECK_EQ(0, strcmp("5", top_down_bbb_children->last()->entry()->name())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 255 | const i::List<ProfileNode*>* top_down_stub_children = |
| 256 | top_down_bbb_children->last()->children(); |
| 257 | CHECK_EQ(1, top_down_stub_children->length()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 258 | CHECK_EQ(0, strcmp("ddd", top_down_stub_children->last()->entry()->name())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 259 | const i::List<ProfileNode*>* top_down_ddd_children = |
| 260 | top_down_stub_children->last()->children(); |
| 261 | CHECK_EQ(0, top_down_ddd_children->length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 264 | // http://crbug/51594 |
| 265 | // This test must not crash. |
| 266 | TEST(CrashIfStoppingLastNonExistentProfile) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 267 | CcTest::InitializeVM(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 268 | TestSetup test_setup; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 269 | CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler(); |
| 270 | profiler->StartProfiling("1"); |
| 271 | profiler->StopProfiling("2"); |
| 272 | profiler->StartProfiling("1"); |
| 273 | profiler->StopProfiling(""); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 276 | // http://code.google.com/p/v8/issues/detail?id=1398 |
| 277 | // Long stacks (exceeding max frames limit) must not be erased. |
| 278 | TEST(Issue1398) { |
| 279 | TestSetup test_setup; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 280 | LocalContext env; |
| 281 | i::Isolate* isolate = CcTest::i_isolate(); |
| 282 | i::HandleScope scope(isolate); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 283 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 284 | i::AbstractCode* code = CreateCode(&env); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 285 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 286 | CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap()); |
| 287 | profiles->StartProfiling("", false); |
| 288 | ProfileGenerator generator(profiles); |
| 289 | SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor( |
| 290 | &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100))); |
| 291 | processor->Start(); |
| 292 | CpuProfiler profiler(isolate, profiles, &generator, processor.get()); |
| 293 | |
| 294 | profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, code, "bbb"); |
| 295 | |
| 296 | i::TickSample* sample = processor->StartTickSample(); |
| 297 | sample->pc = code->address(); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 298 | sample->tos = 0; |
| 299 | sample->frames_count = i::TickSample::kMaxFramesCount; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 300 | for (unsigned i = 0; i < sample->frames_count; ++i) { |
| 301 | sample->stack[i] = code->address(); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 302 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 303 | processor->FinishTickSample(); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 304 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 305 | processor->StopSynchronously(); |
| 306 | CpuProfile* profile = profiles->StopProfiling(""); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 307 | CHECK(profile); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 308 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | unsigned actual_depth = 0; |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 310 | const ProfileNode* node = profile->top_down()->root(); |
| 311 | while (node->children()->length() > 0) { |
| 312 | node = node->children()->last(); |
| 313 | ++actual_depth; |
| 314 | } |
| 315 | |
| 316 | CHECK_EQ(1 + i::TickSample::kMaxFramesCount, actual_depth); // +1 for PC. |
| 317 | } |
| 318 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 319 | TEST(DeleteAllCpuProfiles) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 320 | CcTest::InitializeVM(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 321 | TestSetup test_setup; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 322 | CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler(); |
| 323 | CHECK_EQ(0, profiler->GetProfilesCount()); |
| 324 | profiler->DeleteAllProfiles(); |
| 325 | CHECK_EQ(0, profiler->GetProfilesCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 326 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 327 | profiler->StartProfiling("1"); |
| 328 | profiler->StopProfiling("1"); |
| 329 | CHECK_EQ(1, profiler->GetProfilesCount()); |
| 330 | profiler->DeleteAllProfiles(); |
| 331 | CHECK_EQ(0, profiler->GetProfilesCount()); |
| 332 | profiler->StartProfiling("1"); |
| 333 | profiler->StartProfiling("2"); |
| 334 | profiler->StopProfiling("2"); |
| 335 | profiler->StopProfiling("1"); |
| 336 | CHECK_EQ(2, profiler->GetProfilesCount()); |
| 337 | profiler->DeleteAllProfiles(); |
| 338 | CHECK_EQ(0, profiler->GetProfilesCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 339 | |
| 340 | // Test profiling cancellation by the 'delete' command. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 341 | profiler->StartProfiling("1"); |
| 342 | profiler->StartProfiling("2"); |
| 343 | CHECK_EQ(0, profiler->GetProfilesCount()); |
| 344 | profiler->DeleteAllProfiles(); |
| 345 | CHECK_EQ(0, profiler->GetProfilesCount()); |
| 346 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 347 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | |
| 349 | static bool FindCpuProfile(v8::CpuProfiler* v8profiler, |
| 350 | const v8::CpuProfile* v8profile) { |
| 351 | i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(v8profiler); |
| 352 | const i::CpuProfile* profile = |
| 353 | reinterpret_cast<const i::CpuProfile*>(v8profile); |
| 354 | int length = profiler->GetProfilesCount(); |
| 355 | for (int i = 0; i < length; i++) { |
| 356 | if (profile == profiler->GetProfile(i)) |
| 357 | return true; |
| 358 | } |
| 359 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
| 363 | TEST(DeleteCpuProfile) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 364 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 365 | v8::HandleScope scope(env->GetIsolate()); |
| 366 | v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
| 367 | i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(cpu_profiler); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 368 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 369 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 370 | v8::Local<v8::String> name1 = v8_str("1"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 371 | cpu_profiler->StartProfiling(name1); |
| 372 | v8::CpuProfile* p1 = cpu_profiler->StopProfiling(name1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 373 | CHECK(p1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 374 | CHECK_EQ(1, iprofiler->GetProfilesCount()); |
| 375 | CHECK(FindCpuProfile(cpu_profiler, p1)); |
| 376 | p1->Delete(); |
| 377 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 378 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 379 | v8::Local<v8::String> name2 = v8_str("2"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 380 | cpu_profiler->StartProfiling(name2); |
| 381 | v8::CpuProfile* p2 = cpu_profiler->StopProfiling(name2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 382 | CHECK(p2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 383 | CHECK_EQ(1, iprofiler->GetProfilesCount()); |
| 384 | CHECK(FindCpuProfile(cpu_profiler, p2)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 385 | v8::Local<v8::String> name3 = v8_str("3"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 386 | cpu_profiler->StartProfiling(name3); |
| 387 | v8::CpuProfile* p3 = cpu_profiler->StopProfiling(name3); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 388 | CHECK(p3); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 389 | CHECK_EQ(2, iprofiler->GetProfilesCount()); |
| 390 | CHECK_NE(p2, p3); |
| 391 | CHECK(FindCpuProfile(cpu_profiler, p3)); |
| 392 | CHECK(FindCpuProfile(cpu_profiler, p2)); |
| 393 | p2->Delete(); |
| 394 | CHECK_EQ(1, iprofiler->GetProfilesCount()); |
| 395 | CHECK(!FindCpuProfile(cpu_profiler, p2)); |
| 396 | CHECK(FindCpuProfile(cpu_profiler, p3)); |
| 397 | p3->Delete(); |
| 398 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 402 | TEST(ProfileStartEndTime) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 403 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 404 | v8::HandleScope scope(env->GetIsolate()); |
| 405 | v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 406 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 407 | v8::Local<v8::String> profile_name = v8_str("test"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 408 | cpu_profiler->StartProfiling(profile_name); |
| 409 | const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); |
| 410 | CHECK(profile->GetStartTime() <= profile->GetEndTime()); |
| 411 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 412 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 413 | static v8::CpuProfile* RunProfiler(v8::Local<v8::Context> env, |
| 414 | v8::Local<v8::Function> function, |
| 415 | v8::Local<v8::Value> argv[], int argc, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 416 | unsigned min_js_samples = 0, |
| 417 | unsigned min_external_samples = 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 418 | bool collect_samples = false) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 419 | v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 420 | v8::Local<v8::String> profile_name = v8_str("my_profile"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 421 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 422 | cpu_profiler->SetSamplingInterval(100); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 423 | cpu_profiler->StartProfiling(profile_name, collect_samples); |
| 424 | |
| 425 | i::Sampler* sampler = |
| 426 | reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler(); |
| 427 | sampler->StartCountingSamples(); |
| 428 | do { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 429 | function->Call(env, env->Global(), argc, argv).ToLocalChecked(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 430 | } while (sampler->js_sample_count() < min_js_samples || |
| 431 | sampler->external_sample_count() < min_external_samples); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 432 | |
| 433 | v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); |
| 434 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 435 | CHECK(profile); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 436 | // Dump collected profile to have a better diagnostic in case of failure. |
| 437 | reinterpret_cast<i::CpuProfile*>(profile)->Print(); |
| 438 | |
| 439 | return profile; |
| 440 | } |
| 441 | |
| 442 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 443 | static const v8::CpuProfileNode* FindChild(v8::Local<v8::Context> context, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 444 | const v8::CpuProfileNode* node, |
| 445 | const char* name) { |
| 446 | int count = node->GetChildrenCount(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 447 | v8::Local<v8::String> name_handle = v8_str(name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 448 | for (int i = 0; i < count; i++) { |
| 449 | const v8::CpuProfileNode* child = node->GetChild(i); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 450 | if (name_handle->Equals(context, child->GetFunctionName()).FromJust()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 451 | return child; |
| 452 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 453 | } |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 458 | static const v8::CpuProfileNode* GetChild(v8::Local<v8::Context> context, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 459 | const v8::CpuProfileNode* node, |
| 460 | const char* name) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 461 | const v8::CpuProfileNode* result = FindChild(context, node, name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 462 | if (!result) { |
| 463 | char buffer[100]; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 464 | i::SNPrintF(i::ArrayVector(buffer), "Failed to GetChild: %s", name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 465 | FATAL(buffer); |
| 466 | } |
| 467 | return result; |
| 468 | } |
| 469 | |
| 470 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 471 | static void CheckSimpleBranch(v8::Local<v8::Context> context, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 472 | const v8::CpuProfileNode* node, |
| 473 | const char* names[], int length) { |
| 474 | for (int i = 0; i < length; i++) { |
| 475 | const char* name = names[i]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 476 | node = GetChild(context, node, name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 481 | static const ProfileNode* GetSimpleBranch(v8::Local<v8::Context> context, |
| 482 | v8::CpuProfile* profile, |
| 483 | const char* names[], int length) { |
| 484 | const v8::CpuProfileNode* node = profile->GetTopDownRoot(); |
| 485 | for (int i = 0; i < length; i++) { |
| 486 | node = GetChild(context, node, names[i]); |
| 487 | } |
| 488 | return reinterpret_cast<const ProfileNode*>(node); |
| 489 | } |
| 490 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 491 | static void CallCollectSample(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 492 | info.GetIsolate()->GetCpuProfiler()->CollectSample(); |
| 493 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 494 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 495 | static const char* cpu_profiler_test_source = |
| 496 | "%NeverOptimizeFunction(loop);\n" |
| 497 | "%NeverOptimizeFunction(delay);\n" |
| 498 | "%NeverOptimizeFunction(bar);\n" |
| 499 | "%NeverOptimizeFunction(baz);\n" |
| 500 | "%NeverOptimizeFunction(foo);\n" |
| 501 | "%NeverOptimizeFunction(start);\n" |
| 502 | "function loop(timeout) {\n" |
| 503 | " this.mmm = 0;\n" |
| 504 | " var start = Date.now();\n" |
| 505 | " do {\n" |
| 506 | " var n = 1000;\n" |
| 507 | " while(n > 1) {\n" |
| 508 | " n--;\n" |
| 509 | " this.mmm += n * n * n;\n" |
| 510 | " }\n" |
| 511 | " } while (Date.now() - start < timeout);\n" |
| 512 | "}\n" |
| 513 | "function delay() { loop(10); }\n" |
| 514 | "function bar() { delay(); }\n" |
| 515 | "function baz() { delay(); }\n" |
| 516 | "function foo() {\n" |
| 517 | " delay();\n" |
| 518 | " bar();\n" |
| 519 | " delay();\n" |
| 520 | " baz();\n" |
| 521 | "}\n" |
| 522 | "function start(duration) {\n" |
| 523 | " var start = Date.now();\n" |
| 524 | " do {\n" |
| 525 | " foo();\n" |
| 526 | " } while (Date.now() - start < duration);\n" |
| 527 | "}\n"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 528 | |
| 529 | // Check that the profile tree for the script above will look like the |
| 530 | // following: |
| 531 | // |
| 532 | // [Top down]: |
| 533 | // 1062 0 (root) [-1] |
| 534 | // 1054 0 start [-1] |
| 535 | // 1054 1 foo [-1] |
| 536 | // 265 0 baz [-1] |
| 537 | // 265 1 delay [-1] |
| 538 | // 264 264 loop [-1] |
| 539 | // 525 3 delay [-1] |
| 540 | // 522 522 loop [-1] |
| 541 | // 263 0 bar [-1] |
| 542 | // 263 1 delay [-1] |
| 543 | // 262 262 loop [-1] |
| 544 | // 2 2 (program) [-1] |
| 545 | // 6 6 (garbage collector) [-1] |
| 546 | TEST(CollectCpuProfile) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 547 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 548 | LocalContext env; |
| 549 | v8::HandleScope scope(env->GetIsolate()); |
| 550 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 551 | CompileRun(cpu_profiler_test_source); |
| 552 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 553 | |
| 554 | int32_t profiling_interval_ms = 200; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 555 | v8::Local<v8::Value> args[] = { |
| 556 | v8::Integer::New(env->GetIsolate(), profiling_interval_ms)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 557 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 558 | RunProfiler(env.local(), function, args, arraysize(args), 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 559 | |
| 560 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 561 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 562 | const v8::CpuProfileNode* foo_node = GetChild(env.local(), start_node, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 563 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 564 | const char* bar_branch[] = {"bar", "delay", "loop"}; |
| 565 | CheckSimpleBranch(env.local(), foo_node, bar_branch, arraysize(bar_branch)); |
| 566 | const char* baz_branch[] = {"baz", "delay", "loop"}; |
| 567 | CheckSimpleBranch(env.local(), foo_node, baz_branch, arraysize(baz_branch)); |
| 568 | const char* delay_branch[] = {"delay", "loop"}; |
| 569 | CheckSimpleBranch(env.local(), foo_node, delay_branch, |
| 570 | arraysize(delay_branch)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 571 | |
| 572 | profile->Delete(); |
| 573 | } |
| 574 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 575 | static const char* hot_deopt_no_frame_entry_test_source = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 576 | "%NeverOptimizeFunction(foo);\n" |
| 577 | "%NeverOptimizeFunction(start);\n" |
| 578 | "function foo(a, b) {\n" |
| 579 | " return a + b;\n" |
| 580 | "}\n" |
| 581 | "function start(timeout) {\n" |
| 582 | " var start = Date.now();\n" |
| 583 | " do {\n" |
| 584 | " for (var i = 1; i < 1000; ++i) foo(1, i);\n" |
| 585 | " var duration = Date.now() - start;\n" |
| 586 | " } while (duration < timeout);\n" |
| 587 | " return duration;\n" |
| 588 | "}\n"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 589 | |
| 590 | // Check that the profile tree for the script above will look like the |
| 591 | // following: |
| 592 | // |
| 593 | // [Top down]: |
| 594 | // 1062 0 (root) [-1] |
| 595 | // 1054 0 start [-1] |
| 596 | // 1054 1 foo [-1] |
| 597 | // 2 2 (program) [-1] |
| 598 | // 6 6 (garbage collector) [-1] |
| 599 | // |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 600 | // The test checks no FP ranges are present in a deoptimized function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 601 | // If 'foo' has no ranges the samples falling into the prologue will miss the |
| 602 | // 'start' function on the stack, so 'foo' will be attached to the (root). |
| 603 | TEST(HotDeoptNoFrameEntry) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 604 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 605 | LocalContext env; |
| 606 | v8::HandleScope scope(env->GetIsolate()); |
| 607 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 608 | CompileRun(hot_deopt_no_frame_entry_test_source); |
| 609 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 610 | |
| 611 | int32_t profiling_interval_ms = 200; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 612 | v8::Local<v8::Value> args[] = { |
| 613 | v8::Integer::New(env->GetIsolate(), profiling_interval_ms)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 614 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 615 | RunProfiler(env.local(), function, args, arraysize(args), 1000); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 616 | function->Call(env.local(), env->Global(), arraysize(args), args) |
| 617 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 618 | |
| 619 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 620 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 621 | GetChild(env.local(), start_node, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 622 | |
| 623 | profile->Delete(); |
| 624 | } |
| 625 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 626 | TEST(CollectCpuProfileSamples) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 627 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 628 | LocalContext env; |
| 629 | v8::HandleScope scope(env->GetIsolate()); |
| 630 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 631 | CompileRun(cpu_profiler_test_source); |
| 632 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 633 | |
| 634 | int32_t profiling_interval_ms = 200; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 635 | v8::Local<v8::Value> args[] = { |
| 636 | v8::Integer::New(env->GetIsolate(), profiling_interval_ms)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 637 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 638 | RunProfiler(env.local(), function, args, arraysize(args), 1000, 0, true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 639 | |
| 640 | CHECK_LE(200, profile->GetSamplesCount()); |
| 641 | uint64_t end_time = profile->GetEndTime(); |
| 642 | uint64_t current_time = profile->GetStartTime(); |
| 643 | CHECK_LE(current_time, end_time); |
| 644 | for (int i = 0; i < profile->GetSamplesCount(); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 645 | CHECK(profile->GetSample(i)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 646 | uint64_t timestamp = profile->GetSampleTimestamp(i); |
| 647 | CHECK_LE(current_time, timestamp); |
| 648 | CHECK_LE(timestamp, end_time); |
| 649 | current_time = timestamp; |
| 650 | } |
| 651 | |
| 652 | profile->Delete(); |
| 653 | } |
| 654 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 655 | static const char* cpu_profiler_test_source2 = |
| 656 | "%NeverOptimizeFunction(loop);\n" |
| 657 | "%NeverOptimizeFunction(delay);\n" |
| 658 | "%NeverOptimizeFunction(start);\n" |
| 659 | "function loop() {}\n" |
| 660 | "function delay() { loop(); }\n" |
| 661 | "function start(duration) {\n" |
| 662 | " var start = Date.now();\n" |
| 663 | " do {\n" |
| 664 | " for (var i = 0; i < 10000; ++i) delay();\n" |
| 665 | " } while (Date.now() - start < duration);\n" |
| 666 | "}"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 667 | |
| 668 | // Check that the profile tree doesn't contain unexpected traces: |
| 669 | // - 'loop' can be called only by 'delay' |
| 670 | // - 'delay' may be called only by 'start' |
| 671 | // The profile will look like the following: |
| 672 | // |
| 673 | // [Top down]: |
| 674 | // 135 0 (root) [-1] #1 |
| 675 | // 121 72 start [-1] #3 |
| 676 | // 49 33 delay [-1] #4 |
| 677 | // 16 16 loop [-1] #5 |
| 678 | // 14 14 (program) [-1] #2 |
| 679 | TEST(SampleWhenFrameIsNotSetup) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 680 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 681 | LocalContext env; |
| 682 | v8::HandleScope scope(env->GetIsolate()); |
| 683 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 684 | CompileRun(cpu_profiler_test_source2); |
| 685 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 686 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 687 | int32_t duration_ms = 100; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 688 | v8::Local<v8::Value> args[] = { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 689 | v8::Integer::New(env->GetIsolate(), duration_ms)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 690 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 691 | RunProfiler(env.local(), function, args, arraysize(args), 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 692 | |
| 693 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 694 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 695 | const v8::CpuProfileNode* delay_node = |
| 696 | GetChild(env.local(), start_node, "delay"); |
| 697 | GetChild(env.local(), delay_node, "loop"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 698 | |
| 699 | profile->Delete(); |
| 700 | } |
| 701 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 702 | static const char* native_accessor_test_source = "function start(count) {\n" |
| 703 | " for (var i = 0; i < count; i++) {\n" |
| 704 | " var o = instance.foo;\n" |
| 705 | " instance.foo = o + 1;\n" |
| 706 | " }\n" |
| 707 | "}\n"; |
| 708 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 709 | class TestApiCallbacks { |
| 710 | public: |
| 711 | explicit TestApiCallbacks(int min_duration_ms) |
| 712 | : min_duration_ms_(min_duration_ms), |
| 713 | is_warming_up_(false) {} |
| 714 | |
| 715 | static void Getter(v8::Local<v8::String> name, |
| 716 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 717 | TestApiCallbacks* data = FromInfo(info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 718 | data->Wait(); |
| 719 | } |
| 720 | |
| 721 | static void Setter(v8::Local<v8::String> name, |
| 722 | v8::Local<v8::Value> value, |
| 723 | const v8::PropertyCallbackInfo<void>& info) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 724 | TestApiCallbacks* data = FromInfo(info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 725 | data->Wait(); |
| 726 | } |
| 727 | |
| 728 | static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 729 | TestApiCallbacks* data = FromInfo(info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 730 | data->Wait(); |
| 731 | } |
| 732 | |
| 733 | void set_warming_up(bool value) { is_warming_up_ = value; } |
| 734 | |
| 735 | private: |
| 736 | void Wait() { |
| 737 | if (is_warming_up_) return; |
| 738 | double start = v8::base::OS::TimeCurrentMillis(); |
| 739 | double duration = 0; |
| 740 | while (duration < min_duration_ms_) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 741 | v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 742 | duration = v8::base::OS::TimeCurrentMillis() - start; |
| 743 | } |
| 744 | } |
| 745 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 746 | template <typename T> |
| 747 | static TestApiCallbacks* FromInfo(const T& info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 748 | void* data = v8::External::Cast(*info.Data())->Value(); |
| 749 | return reinterpret_cast<TestApiCallbacks*>(data); |
| 750 | } |
| 751 | |
| 752 | int min_duration_ms_; |
| 753 | bool is_warming_up_; |
| 754 | }; |
| 755 | |
| 756 | |
| 757 | // Test that native accessors are properly reported in the CPU profile. |
| 758 | // This test checks the case when the long-running accessors are called |
| 759 | // only once and the optimizer doesn't have chance to change the invocation |
| 760 | // code. |
| 761 | TEST(NativeAccessorUninitializedIC) { |
| 762 | LocalContext env; |
| 763 | v8::Isolate* isolate = env->GetIsolate(); |
| 764 | v8::HandleScope scope(isolate); |
| 765 | |
| 766 | v8::Local<v8::FunctionTemplate> func_template = |
| 767 | v8::FunctionTemplate::New(isolate); |
| 768 | v8::Local<v8::ObjectTemplate> instance_template = |
| 769 | func_template->InstanceTemplate(); |
| 770 | |
| 771 | TestApiCallbacks accessors(100); |
| 772 | v8::Local<v8::External> data = |
| 773 | v8::External::New(isolate, &accessors); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 774 | instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter, |
| 775 | &TestApiCallbacks::Setter, data); |
| 776 | v8::Local<v8::Function> func = |
| 777 | func_template->GetFunction(env.local()).ToLocalChecked(); |
| 778 | v8::Local<v8::Object> instance = |
| 779 | func->NewInstance(env.local()).ToLocalChecked(); |
| 780 | env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 781 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 782 | CompileRun(native_accessor_test_source); |
| 783 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 784 | |
| 785 | int32_t repeat_count = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 786 | v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 787 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 788 | RunProfiler(env.local(), function, args, arraysize(args), 0, 100); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 789 | |
| 790 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 791 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 792 | GetChild(env.local(), start_node, "get foo"); |
| 793 | GetChild(env.local(), start_node, "set foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 794 | |
| 795 | profile->Delete(); |
| 796 | } |
| 797 | |
| 798 | |
| 799 | // Test that native accessors are properly reported in the CPU profile. |
| 800 | // This test makes sure that the accessors are called enough times to become |
| 801 | // hot and to trigger optimizations. |
| 802 | TEST(NativeAccessorMonomorphicIC) { |
| 803 | LocalContext env; |
| 804 | v8::Isolate* isolate = env->GetIsolate(); |
| 805 | v8::HandleScope scope(isolate); |
| 806 | |
| 807 | v8::Local<v8::FunctionTemplate> func_template = |
| 808 | v8::FunctionTemplate::New(isolate); |
| 809 | v8::Local<v8::ObjectTemplate> instance_template = |
| 810 | func_template->InstanceTemplate(); |
| 811 | |
| 812 | TestApiCallbacks accessors(1); |
| 813 | v8::Local<v8::External> data = |
| 814 | v8::External::New(isolate, &accessors); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 815 | instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter, |
| 816 | &TestApiCallbacks::Setter, data); |
| 817 | v8::Local<v8::Function> func = |
| 818 | func_template->GetFunction(env.local()).ToLocalChecked(); |
| 819 | v8::Local<v8::Object> instance = |
| 820 | func->NewInstance(env.local()).ToLocalChecked(); |
| 821 | env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 822 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 823 | CompileRun(native_accessor_test_source); |
| 824 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 825 | |
| 826 | { |
| 827 | // Make sure accessors ICs are in monomorphic state before starting |
| 828 | // profiling. |
| 829 | accessors.set_warming_up(true); |
| 830 | int32_t warm_up_iterations = 3; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 831 | v8::Local<v8::Value> args[] = { |
| 832 | v8::Integer::New(isolate, warm_up_iterations)}; |
| 833 | function->Call(env.local(), env->Global(), arraysize(args), args) |
| 834 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 835 | accessors.set_warming_up(false); |
| 836 | } |
| 837 | |
| 838 | int32_t repeat_count = 100; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 839 | v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 840 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 841 | RunProfiler(env.local(), function, args, arraysize(args), 0, 100); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 842 | |
| 843 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 844 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 845 | GetChild(env.local(), start_node, "get foo"); |
| 846 | GetChild(env.local(), start_node, "set foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 847 | |
| 848 | profile->Delete(); |
| 849 | } |
| 850 | |
| 851 | |
| 852 | static const char* native_method_test_source = "function start(count) {\n" |
| 853 | " for (var i = 0; i < count; i++) {\n" |
| 854 | " instance.fooMethod();\n" |
| 855 | " }\n" |
| 856 | "}\n"; |
| 857 | |
| 858 | |
| 859 | TEST(NativeMethodUninitializedIC) { |
| 860 | LocalContext env; |
| 861 | v8::Isolate* isolate = env->GetIsolate(); |
| 862 | v8::HandleScope scope(isolate); |
| 863 | |
| 864 | TestApiCallbacks callbacks(100); |
| 865 | v8::Local<v8::External> data = |
| 866 | v8::External::New(isolate, &callbacks); |
| 867 | |
| 868 | v8::Local<v8::FunctionTemplate> func_template = |
| 869 | v8::FunctionTemplate::New(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 870 | func_template->SetClassName(v8_str("Test_InstanceCostructor")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 871 | v8::Local<v8::ObjectTemplate> proto_template = |
| 872 | func_template->PrototypeTemplate(); |
| 873 | v8::Local<v8::Signature> signature = |
| 874 | v8::Signature::New(isolate, func_template); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 875 | proto_template->Set( |
| 876 | v8_str("fooMethod"), |
| 877 | v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data, |
| 878 | signature, 0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 879 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 880 | v8::Local<v8::Function> func = |
| 881 | func_template->GetFunction(env.local()).ToLocalChecked(); |
| 882 | v8::Local<v8::Object> instance = |
| 883 | func->NewInstance(env.local()).ToLocalChecked(); |
| 884 | env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 885 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 886 | CompileRun(native_method_test_source); |
| 887 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 888 | |
| 889 | int32_t repeat_count = 1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 890 | v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 891 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 892 | RunProfiler(env.local(), function, args, arraysize(args), 0, 100); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 893 | |
| 894 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 895 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 896 | GetChild(env.local(), start_node, "fooMethod"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 897 | |
| 898 | profile->Delete(); |
| 899 | } |
| 900 | |
| 901 | |
| 902 | TEST(NativeMethodMonomorphicIC) { |
| 903 | LocalContext env; |
| 904 | v8::Isolate* isolate = env->GetIsolate(); |
| 905 | v8::HandleScope scope(isolate); |
| 906 | |
| 907 | TestApiCallbacks callbacks(1); |
| 908 | v8::Local<v8::External> data = |
| 909 | v8::External::New(isolate, &callbacks); |
| 910 | |
| 911 | v8::Local<v8::FunctionTemplate> func_template = |
| 912 | v8::FunctionTemplate::New(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 913 | func_template->SetClassName(v8_str("Test_InstanceCostructor")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 914 | v8::Local<v8::ObjectTemplate> proto_template = |
| 915 | func_template->PrototypeTemplate(); |
| 916 | v8::Local<v8::Signature> signature = |
| 917 | v8::Signature::New(isolate, func_template); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 918 | proto_template->Set( |
| 919 | v8_str("fooMethod"), |
| 920 | v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data, |
| 921 | signature, 0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 922 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 923 | v8::Local<v8::Function> func = |
| 924 | func_template->GetFunction(env.local()).ToLocalChecked(); |
| 925 | v8::Local<v8::Object> instance = |
| 926 | func->NewInstance(env.local()).ToLocalChecked(); |
| 927 | env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 928 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 929 | CompileRun(native_method_test_source); |
| 930 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 931 | { |
| 932 | // Make sure method ICs are in monomorphic state before starting |
| 933 | // profiling. |
| 934 | callbacks.set_warming_up(true); |
| 935 | int32_t warm_up_iterations = 3; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 936 | v8::Local<v8::Value> args[] = { |
| 937 | v8::Integer::New(isolate, warm_up_iterations)}; |
| 938 | function->Call(env.local(), env->Global(), arraysize(args), args) |
| 939 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 940 | callbacks.set_warming_up(false); |
| 941 | } |
| 942 | |
| 943 | int32_t repeat_count = 100; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 944 | v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 945 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 946 | RunProfiler(env.local(), function, args, arraysize(args), 0, 200); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 947 | |
| 948 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 949 | GetChild(env.local(), root, "start"); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 950 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 951 | GetChild(env.local(), start_node, "fooMethod"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 952 | |
| 953 | profile->Delete(); |
| 954 | } |
| 955 | |
| 956 | |
| 957 | static const char* bound_function_test_source = |
| 958 | "function foo() {\n" |
| 959 | " startProfiling('my_profile');\n" |
| 960 | "}\n" |
| 961 | "function start() {\n" |
| 962 | " var callback = foo.bind(this);\n" |
| 963 | " callback();\n" |
| 964 | "}"; |
| 965 | |
| 966 | |
| 967 | TEST(BoundFunctionCall) { |
| 968 | v8::HandleScope scope(CcTest::isolate()); |
| 969 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 970 | v8::Context::Scope context_scope(env); |
| 971 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 972 | CompileRun(bound_function_test_source); |
| 973 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 974 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 975 | v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 976 | |
| 977 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 978 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 979 | const v8::CpuProfileNode* start_node = GetChild(env, root, "start"); |
| 980 | GetChild(env, start_node, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 981 | |
| 982 | profile->Delete(); |
| 983 | } |
| 984 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 985 | // This tests checks distribution of the samples through the source lines. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 986 | static void TickLines(bool optimize) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 987 | CcTest::InitializeVM(); |
| 988 | LocalContext env; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 989 | i::FLAG_allow_natives_syntax = true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 990 | i::FLAG_turbo_source_positions = true; |
| 991 | i::Isolate* isolate = CcTest::i_isolate(); |
| 992 | i::Factory* factory = isolate->factory(); |
| 993 | i::HandleScope scope(isolate); |
| 994 | |
| 995 | i::EmbeddedVector<char, 512> script; |
| 996 | |
| 997 | const char* func_name = "func"; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 998 | const char* opt_func = |
| 999 | optimize ? "%OptimizeFunctionOnNextCall" : "%NeverOptimizeFunction"; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1000 | i::SNPrintF(script, |
| 1001 | "function %s() {\n" |
| 1002 | " var n = 0;\n" |
| 1003 | " var m = 100*100;\n" |
| 1004 | " while (m > 1) {\n" |
| 1005 | " m--;\n" |
| 1006 | " n += m * m * m;\n" |
| 1007 | " }\n" |
| 1008 | "}\n" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1009 | "%s(%s);\n" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1010 | "%s();\n", |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1011 | func_name, opt_func, func_name, func_name); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1012 | |
| 1013 | CompileRun(script.start()); |
| 1014 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1015 | i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast( |
| 1016 | v8::Utils::OpenHandle(*GetFunction(env.local(), func_name))); |
| 1017 | CHECK(func->shared()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1018 | CHECK(func->shared()->abstract_code()); |
| 1019 | i::AbstractCode* code = func->abstract_code(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1020 | CHECK(code); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1021 | i::Address code_address = code->instruction_start(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1022 | CHECK(code_address); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1023 | |
| 1024 | CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap()); |
| 1025 | profiles->StartProfiling("", false); |
| 1026 | ProfileGenerator generator(profiles); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1027 | SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor( |
| 1028 | &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100))); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1029 | processor->Start(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1030 | CpuProfiler profiler(isolate, profiles, &generator, processor.get()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1031 | |
| 1032 | // Enqueue code creation events. |
| 1033 | i::Handle<i::String> str = factory->NewStringFromAsciiChecked(func_name); |
| 1034 | int line = 1; |
| 1035 | int column = 1; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1036 | profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, code, func->shared(), *str, |
| 1037 | line, column); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1038 | |
| 1039 | // Enqueue a tick event to enable code events processing. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1040 | EnqueueTickSampleEvent(processor.get(), code_address); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1041 | |
| 1042 | processor->StopSynchronously(); |
| 1043 | |
| 1044 | CpuProfile* profile = profiles->StopProfiling(""); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1045 | CHECK(profile); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1046 | |
| 1047 | // Check the state of profile generator. |
| 1048 | CodeEntry* func_entry = generator.code_map()->FindEntry(code_address); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1049 | CHECK(func_entry); |
| 1050 | CHECK_EQ(0, strcmp(func_name, func_entry->name())); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1051 | const i::JITLineInfoTable* line_info = func_entry->line_info(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1052 | CHECK(line_info); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1053 | CHECK(!line_info->empty()); |
| 1054 | |
| 1055 | // Check the hit source lines using V8 Public APIs. |
| 1056 | const i::ProfileTree* tree = profile->top_down(); |
| 1057 | ProfileNode* root = tree->root(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1058 | CHECK(root); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1059 | ProfileNode* func_node = root->FindChild(func_entry); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1060 | CHECK(func_node); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1061 | |
| 1062 | // Add 10 faked ticks to source line #5. |
| 1063 | int hit_line = 5; |
| 1064 | int hit_count = 10; |
| 1065 | for (int i = 0; i < hit_count; i++) func_node->IncrementLineTicks(hit_line); |
| 1066 | |
| 1067 | unsigned int line_count = func_node->GetHitLineCount(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1068 | CHECK_EQ(2u, line_count); // Expect two hit source lines - #1 and #5. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1069 | ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count); |
| 1070 | CHECK(func_node->GetLineTicks(&entries[0], line_count)); |
| 1071 | int value = 0; |
| 1072 | for (int i = 0; i < entries.length(); i++) |
| 1073 | if (entries[i].line == hit_line) { |
| 1074 | value = entries[i].hit_count; |
| 1075 | break; |
| 1076 | } |
| 1077 | CHECK_EQ(hit_count, value); |
| 1078 | } |
| 1079 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1080 | TEST(TickLinesBaseline) { TickLines(false); } |
| 1081 | |
| 1082 | TEST(TickLinesOptimized) { TickLines(true); } |
| 1083 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1084 | static const char* call_function_test_source = |
| 1085 | "%NeverOptimizeFunction(bar);\n" |
| 1086 | "%NeverOptimizeFunction(start);\n" |
| 1087 | "function bar(n) {\n" |
| 1088 | " var s = 0;\n" |
| 1089 | " for (var i = 0; i < n; i++) s += i * i * i;\n" |
| 1090 | " return s;\n" |
| 1091 | "}\n" |
| 1092 | "function start(duration) {\n" |
| 1093 | " var start = Date.now();\n" |
| 1094 | " do {\n" |
| 1095 | " for (var i = 0; i < 100; ++i)\n" |
| 1096 | " bar.call(this, 1000);\n" |
| 1097 | " } while (Date.now() - start < duration);\n" |
| 1098 | "}"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1099 | |
| 1100 | // Test that if we sampled thread when it was inside FunctionCall buitin then |
| 1101 | // its caller frame will be '(unresolved function)' as we have no reliable way |
| 1102 | // to resolve it. |
| 1103 | // |
| 1104 | // [Top down]: |
| 1105 | // 96 0 (root) [-1] #1 |
| 1106 | // 1 1 (garbage collector) [-1] #4 |
| 1107 | // 5 0 (unresolved function) [-1] #5 |
| 1108 | // 5 5 call [-1] #6 |
| 1109 | // 71 70 start [-1] #3 |
| 1110 | // 1 1 bar [-1] #7 |
| 1111 | // 19 19 (program) [-1] #2 |
| 1112 | TEST(FunctionCallSample) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1113 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1114 | LocalContext env; |
| 1115 | v8::HandleScope scope(env->GetIsolate()); |
| 1116 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1117 | // Collect garbage that might have be generated while installing |
| 1118 | // extensions. |
| 1119 | CcTest::heap()->CollectAllGarbage(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1120 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1121 | CompileRun(call_function_test_source); |
| 1122 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1123 | |
| 1124 | int32_t duration_ms = 100; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1125 | v8::Local<v8::Value> args[] = { |
| 1126 | v8::Integer::New(env->GetIsolate(), duration_ms)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1127 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1128 | RunProfiler(env.local(), function, args, arraysize(args), 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1129 | |
| 1130 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1131 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 1132 | GetChild(env.local(), start_node, "bar"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1133 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1134 | const v8::CpuProfileNode* unresolved_node = FindChild( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1135 | env.local(), root, i::ProfileGenerator::kUnresolvedFunctionName); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1136 | CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "call")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1137 | |
| 1138 | profile->Delete(); |
| 1139 | } |
| 1140 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1141 | static const char* function_apply_test_source = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1142 | "%NeverOptimizeFunction(bar);\n" |
| 1143 | "%NeverOptimizeFunction(test);\n" |
| 1144 | "%NeverOptimizeFunction(start);\n" |
| 1145 | "function bar(n) {\n" |
| 1146 | " var s = 0;\n" |
| 1147 | " for (var i = 0; i < n; i++) s += i * i * i;\n" |
| 1148 | " return s;\n" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1149 | "}\n" |
| 1150 | "function test() {\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1151 | " bar.apply(this, [1000]);\n" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1152 | "}\n" |
| 1153 | "function start(duration) {\n" |
| 1154 | " var start = Date.now();\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1155 | " do {\n" |
| 1156 | " for (var i = 0; i < 100; ++i) test();\n" |
| 1157 | " } while (Date.now() - start < duration);\n" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1158 | "}"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1159 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1160 | // [Top down]: |
| 1161 | // 94 0 (root) [-1] #0 1 |
| 1162 | // 2 2 (garbage collector) [-1] #0 7 |
| 1163 | // 82 49 start [-1] #16 3 |
| 1164 | // 1 0 (unresolved function) [-1] #0 8 |
| 1165 | // 1 1 apply [-1] #0 9 |
| 1166 | // 32 21 test [-1] #16 4 |
| 1167 | // 2 2 bar [-1] #16 6 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1168 | // 10 10 (program) [-1] #0 2 |
| 1169 | TEST(FunctionApplySample) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1170 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1171 | LocalContext env; |
| 1172 | v8::HandleScope scope(env->GetIsolate()); |
| 1173 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1174 | CompileRun(function_apply_test_source); |
| 1175 | v8::Local<v8::Function> function = GetFunction(env.local(), "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1176 | |
| 1177 | int32_t duration_ms = 100; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1178 | v8::Local<v8::Value> args[] = { |
| 1179 | v8::Integer::New(env->GetIsolate(), duration_ms)}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1180 | |
| 1181 | v8::CpuProfile* profile = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1182 | RunProfiler(env.local(), function, args, arraysize(args), 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1183 | |
| 1184 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1185 | const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start"); |
| 1186 | const v8::CpuProfileNode* test_node = |
| 1187 | GetChild(env.local(), start_node, "test"); |
| 1188 | GetChild(env.local(), test_node, "bar"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1189 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1190 | const v8::CpuProfileNode* unresolved_node = FindChild( |
| 1191 | env.local(), start_node, ProfileGenerator::kUnresolvedFunctionName); |
| 1192 | CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "apply")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1193 | |
| 1194 | profile->Delete(); |
| 1195 | } |
| 1196 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1197 | static const char* cpu_profiler_deep_stack_test_source = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1198 | "function foo(n) {\n" |
| 1199 | " if (n)\n" |
| 1200 | " foo(n - 1);\n" |
| 1201 | " else\n" |
| 1202 | " collectSample();\n" |
| 1203 | "}\n" |
| 1204 | "function start() {\n" |
| 1205 | " startProfiling('my_profile');\n" |
| 1206 | " foo(250);\n" |
| 1207 | "}\n"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Check a deep stack |
| 1210 | // |
| 1211 | // [Top down]: |
| 1212 | // 0 (root) 0 #1 |
| 1213 | // 2 (program) 0 #2 |
| 1214 | // 0 start 21 #3 no reason |
| 1215 | // 0 foo 21 #4 no reason |
| 1216 | // 0 foo 21 #5 no reason |
| 1217 | // .... |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1218 | // 0 foo 21 #254 no reason |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1219 | TEST(CpuProfileDeepStack) { |
| 1220 | v8::HandleScope scope(CcTest::isolate()); |
| 1221 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1222 | v8::Context::Scope context_scope(env); |
| 1223 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1224 | CompileRun(cpu_profiler_deep_stack_test_source); |
| 1225 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1226 | |
| 1227 | v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1228 | v8::Local<v8::String> profile_name = v8_str("my_profile"); |
| 1229 | function->Call(env, env->Global(), 0, NULL).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1230 | v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1231 | CHECK(profile); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1232 | // Dump collected profile to have a better diagnostic in case of failure. |
| 1233 | reinterpret_cast<i::CpuProfile*>(profile)->Print(); |
| 1234 | |
| 1235 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1236 | const v8::CpuProfileNode* node = GetChild(env, root, "start"); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1237 | for (int i = 0; i <= 250; ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1238 | node = GetChild(env, node, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1239 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1240 | CHECK(!FindChild(env, node, "foo")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1241 | |
| 1242 | profile->Delete(); |
| 1243 | } |
| 1244 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1245 | static const char* js_native_js_test_source = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1246 | "%NeverOptimizeFunction(foo);\n" |
| 1247 | "%NeverOptimizeFunction(bar);\n" |
| 1248 | "%NeverOptimizeFunction(start);\n" |
| 1249 | "function foo(n) {\n" |
| 1250 | " var s = 0;\n" |
| 1251 | " for (var i = 0; i < n; i++) s += i * i * i;\n" |
| 1252 | " return s;\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1253 | "}\n" |
| 1254 | "function bar() {\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1255 | " foo(1000);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1256 | "}\n" |
| 1257 | "function start() {\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1258 | " CallJsFunction(bar);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1259 | "}"; |
| 1260 | |
| 1261 | static void CallJsFunction(const v8::FunctionCallbackInfo<v8::Value>& info) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1262 | v8::Local<v8::Function> function = info[0].As<v8::Function>(); |
| 1263 | v8::Local<v8::Value> argv[] = {info[1]}; |
| 1264 | function->Call(info.GetIsolate()->GetCurrentContext(), info.This(), |
| 1265 | arraysize(argv), argv) |
| 1266 | .ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1269 | // [Top down]: |
| 1270 | // 58 0 (root) #0 1 |
| 1271 | // 2 2 (program) #0 2 |
| 1272 | // 56 1 start #16 3 |
| 1273 | // 55 0 CallJsFunction #0 4 |
| 1274 | // 55 1 bar #16 5 |
| 1275 | // 54 54 foo #16 6 |
| 1276 | TEST(JsNativeJsSample) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1277 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1278 | v8::HandleScope scope(CcTest::isolate()); |
| 1279 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1280 | v8::Context::Scope context_scope(env); |
| 1281 | |
| 1282 | v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New( |
| 1283 | env->GetIsolate(), CallJsFunction); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1284 | v8::Local<v8::Function> func = |
| 1285 | func_template->GetFunction(env).ToLocalChecked(); |
| 1286 | func->SetName(v8_str("CallJsFunction")); |
| 1287 | env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1288 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1289 | CompileRun(js_native_js_test_source); |
| 1290 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1291 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1292 | v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1293 | |
| 1294 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1295 | const v8::CpuProfileNode* start_node = GetChild(env, root, "start"); |
| 1296 | const v8::CpuProfileNode* native_node = |
| 1297 | GetChild(env, start_node, "CallJsFunction"); |
| 1298 | const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar"); |
| 1299 | GetChild(env, bar_node, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1300 | |
| 1301 | profile->Delete(); |
| 1302 | } |
| 1303 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1304 | static const char* js_native_js_runtime_js_test_source = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1305 | "%NeverOptimizeFunction(foo);\n" |
| 1306 | "%NeverOptimizeFunction(bar);\n" |
| 1307 | "%NeverOptimizeFunction(start);\n" |
| 1308 | "function foo(n) {\n" |
| 1309 | " var s = 0;\n" |
| 1310 | " for (var i = 0; i < n; i++) s += i * i * i;\n" |
| 1311 | " return s;\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1312 | "}\n" |
| 1313 | "var bound = foo.bind(this);\n" |
| 1314 | "function bar() {\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1315 | " bound(1000);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1316 | "}\n" |
| 1317 | "function start() {\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1318 | " CallJsFunction(bar);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1319 | "}"; |
| 1320 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1321 | // [Top down]: |
| 1322 | // 57 0 (root) #0 1 |
| 1323 | // 55 1 start #16 3 |
| 1324 | // 54 0 CallJsFunction #0 4 |
| 1325 | // 54 3 bar #16 5 |
| 1326 | // 51 51 foo #16 6 |
| 1327 | // 2 2 (program) #0 2 |
| 1328 | TEST(JsNativeJsRuntimeJsSample) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1329 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1330 | v8::HandleScope scope(CcTest::isolate()); |
| 1331 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1332 | v8::Context::Scope context_scope(env); |
| 1333 | |
| 1334 | v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New( |
| 1335 | env->GetIsolate(), CallJsFunction); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1336 | v8::Local<v8::Function> func = |
| 1337 | func_template->GetFunction(env).ToLocalChecked(); |
| 1338 | func->SetName(v8_str("CallJsFunction")); |
| 1339 | env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1340 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1341 | CompileRun(js_native_js_runtime_js_test_source); |
| 1342 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1343 | v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1344 | |
| 1345 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1346 | const v8::CpuProfileNode* start_node = GetChild(env, root, "start"); |
| 1347 | const v8::CpuProfileNode* native_node = |
| 1348 | GetChild(env, start_node, "CallJsFunction"); |
| 1349 | const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar"); |
| 1350 | GetChild(env, bar_node, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1351 | |
| 1352 | profile->Delete(); |
| 1353 | } |
| 1354 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1355 | static void CallJsFunction2(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 1356 | v8::base::OS::Print("In CallJsFunction2\n"); |
| 1357 | CallJsFunction(info); |
| 1358 | } |
| 1359 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1360 | static const char* js_native1_js_native2_js_test_source = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1361 | "%NeverOptimizeFunction(foo);\n" |
| 1362 | "%NeverOptimizeFunction(bar);\n" |
| 1363 | "%NeverOptimizeFunction(start);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1364 | "function foo() {\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1365 | " var s = 0;\n" |
| 1366 | " for (var i = 0; i < 1000; i++) s += i * i * i;\n" |
| 1367 | " return s;\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1368 | "}\n" |
| 1369 | "function bar() {\n" |
| 1370 | " CallJsFunction2(foo);\n" |
| 1371 | "}\n" |
| 1372 | "function start() {\n" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1373 | " CallJsFunction1(bar);\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1374 | "}"; |
| 1375 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1376 | // [Top down]: |
| 1377 | // 57 0 (root) #0 1 |
| 1378 | // 55 1 start #16 3 |
| 1379 | // 54 0 CallJsFunction1 #0 4 |
| 1380 | // 54 0 bar #16 5 |
| 1381 | // 54 0 CallJsFunction2 #0 6 |
| 1382 | // 54 54 foo #16 7 |
| 1383 | // 2 2 (program) #0 2 |
| 1384 | TEST(JsNative1JsNative2JsSample) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1385 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1386 | v8::HandleScope scope(CcTest::isolate()); |
| 1387 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1388 | v8::Context::Scope context_scope(env); |
| 1389 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1390 | v8::Local<v8::Function> func1 = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1391 | v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction) |
| 1392 | ->GetFunction(env) |
| 1393 | .ToLocalChecked(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1394 | func1->SetName(v8_str("CallJsFunction1")); |
| 1395 | env->Global()->Set(env, v8_str("CallJsFunction1"), func1).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1396 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1397 | v8::Local<v8::Function> func2 = |
| 1398 | v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction2) |
| 1399 | ->GetFunction(env) |
| 1400 | .ToLocalChecked(); |
| 1401 | func2->SetName(v8_str("CallJsFunction2")); |
| 1402 | env->Global()->Set(env, v8_str("CallJsFunction2"), func2).FromJust(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1403 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1404 | CompileRun(js_native1_js_native2_js_test_source); |
| 1405 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1406 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1407 | v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1408 | |
| 1409 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1410 | const v8::CpuProfileNode* start_node = GetChild(env, root, "start"); |
| 1411 | const v8::CpuProfileNode* native_node1 = |
| 1412 | GetChild(env, start_node, "CallJsFunction1"); |
| 1413 | const v8::CpuProfileNode* bar_node = GetChild(env, native_node1, "bar"); |
| 1414 | const v8::CpuProfileNode* native_node2 = |
| 1415 | GetChild(env, bar_node, "CallJsFunction2"); |
| 1416 | GetChild(env, native_node2, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1417 | |
| 1418 | profile->Delete(); |
| 1419 | } |
| 1420 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1421 | static const char* js_force_collect_sample_source = |
| 1422 | "function start() {\n" |
| 1423 | " CallCollectSample();\n" |
| 1424 | "}"; |
| 1425 | |
| 1426 | TEST(CollectSampleAPI) { |
| 1427 | v8::HandleScope scope(CcTest::isolate()); |
| 1428 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1429 | v8::Context::Scope context_scope(env); |
| 1430 | |
| 1431 | v8::Local<v8::FunctionTemplate> func_template = |
| 1432 | v8::FunctionTemplate::New(env->GetIsolate(), CallCollectSample); |
| 1433 | v8::Local<v8::Function> func = |
| 1434 | func_template->GetFunction(env).ToLocalChecked(); |
| 1435 | func->SetName(v8_str("CallCollectSample")); |
| 1436 | env->Global()->Set(env, v8_str("CallCollectSample"), func).FromJust(); |
| 1437 | |
| 1438 | CompileRun(js_force_collect_sample_source); |
| 1439 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
| 1440 | v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0); |
| 1441 | |
| 1442 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
| 1443 | const v8::CpuProfileNode* start_node = GetChild(env, root, "start"); |
| 1444 | CHECK_LE(1, start_node->GetChildrenCount()); |
| 1445 | GetChild(env, start_node, "CallCollectSample"); |
| 1446 | |
| 1447 | profile->Delete(); |
| 1448 | } |
| 1449 | |
| 1450 | static const char* js_native_js_runtime_multiple_test_source = |
| 1451 | "%NeverOptimizeFunction(foo);\n" |
| 1452 | "%NeverOptimizeFunction(bar);\n" |
| 1453 | "%NeverOptimizeFunction(start);\n" |
| 1454 | "function foo() {\n" |
| 1455 | " return Math.sin(Math.random());\n" |
| 1456 | "}\n" |
| 1457 | "var bound = foo.bind(this);\n" |
| 1458 | "function bar() {\n" |
| 1459 | " return bound();\n" |
| 1460 | "}\n" |
| 1461 | "function start() {\n" |
| 1462 | " startProfiling('my_profile');\n" |
| 1463 | " var startTime = Date.now();\n" |
| 1464 | " do {\n" |
| 1465 | " CallJsFunction(bar);\n" |
| 1466 | " } while (Date.now() - startTime < 200);\n" |
| 1467 | "}"; |
| 1468 | |
| 1469 | // The test check multiple entrances/exits between JS and native code. |
| 1470 | // |
| 1471 | // [Top down]: |
| 1472 | // (root) #0 1 |
| 1473 | // start #16 3 |
| 1474 | // CallJsFunction #0 4 |
| 1475 | // bar #16 5 |
| 1476 | // foo #16 6 |
| 1477 | // (program) #0 2 |
| 1478 | TEST(JsNativeJsRuntimeJsSampleMultiple) { |
| 1479 | i::FLAG_allow_natives_syntax = true; |
| 1480 | v8::HandleScope scope(CcTest::isolate()); |
| 1481 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1482 | v8::Context::Scope context_scope(env); |
| 1483 | |
| 1484 | v8::Local<v8::FunctionTemplate> func_template = |
| 1485 | v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction); |
| 1486 | v8::Local<v8::Function> func = |
| 1487 | func_template->GetFunction(env).ToLocalChecked(); |
| 1488 | func->SetName(v8_str("CallJsFunction")); |
| 1489 | env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust(); |
| 1490 | |
| 1491 | CompileRun(js_native_js_runtime_multiple_test_source); |
| 1492 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
| 1493 | |
| 1494 | v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 500, 500); |
| 1495 | |
| 1496 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
| 1497 | const v8::CpuProfileNode* start_node = GetChild(env, root, "start"); |
| 1498 | const v8::CpuProfileNode* native_node = |
| 1499 | GetChild(env, start_node, "CallJsFunction"); |
| 1500 | const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar"); |
| 1501 | GetChild(env, bar_node, "foo"); |
| 1502 | |
| 1503 | profile->Delete(); |
| 1504 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1505 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1506 | static const char* inlining_test_source = |
| 1507 | "%NeverOptimizeFunction(action);\n" |
| 1508 | "%NeverOptimizeFunction(start);\n" |
| 1509 | "%OptimizeFunctionOnNextCall(level1);\n" |
| 1510 | "%OptimizeFunctionOnNextCall(level2);\n" |
| 1511 | "%OptimizeFunctionOnNextCall(level3);\n" |
| 1512 | "var finish = false;\n" |
| 1513 | "function action(n) {\n" |
| 1514 | " var s = 0;\n" |
| 1515 | " for (var i = 0; i < n; ++i) s += i*i*i;\n" |
| 1516 | " if (finish)\n" |
| 1517 | " startProfiling('my_profile');\n" |
| 1518 | " return s;\n" |
| 1519 | "}\n" |
| 1520 | "function level3() { return action(100); }\n" |
| 1521 | "function level2() { return level3() * 2; }\n" |
| 1522 | "function level1() { return level2(); }\n" |
| 1523 | "function start() {\n" |
| 1524 | " var n = 100;\n" |
| 1525 | " while (--n)\n" |
| 1526 | " level1();\n" |
| 1527 | " finish = true;\n" |
| 1528 | " level1();\n" |
| 1529 | "}"; |
| 1530 | |
| 1531 | // The test check multiple entrances/exits between JS and native code. |
| 1532 | // |
| 1533 | // [Top down]: |
| 1534 | // (root) #0 1 |
| 1535 | // start #16 3 |
| 1536 | // level1 #0 4 |
| 1537 | // level2 #16 5 |
| 1538 | // level3 #16 6 |
| 1539 | // action #16 7 |
| 1540 | // (program) #0 2 |
| 1541 | TEST(Inlining) { |
| 1542 | i::FLAG_allow_natives_syntax = true; |
| 1543 | v8::HandleScope scope(CcTest::isolate()); |
| 1544 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1545 | v8::Context::Scope context_scope(env); |
| 1546 | |
| 1547 | CompileRun(inlining_test_source); |
| 1548 | v8::Local<v8::Function> function = GetFunction(env, "start"); |
| 1549 | |
| 1550 | v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
| 1551 | v8::Local<v8::String> profile_name = v8_str("my_profile"); |
| 1552 | function->Call(env, env->Global(), 0, NULL).ToLocalChecked(); |
| 1553 | v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); |
| 1554 | CHECK(profile); |
| 1555 | // Dump collected profile to have a better diagnostic in case of failure. |
| 1556 | reinterpret_cast<i::CpuProfile*>(profile)->Print(); |
| 1557 | |
| 1558 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
| 1559 | const v8::CpuProfileNode* start_node = GetChild(env, root, "start"); |
| 1560 | const v8::CpuProfileNode* level1_node = GetChild(env, start_node, "level1"); |
| 1561 | const v8::CpuProfileNode* level2_node = GetChild(env, level1_node, "level2"); |
| 1562 | const v8::CpuProfileNode* level3_node = GetChild(env, level2_node, "level3"); |
| 1563 | GetChild(env, level3_node, "action"); |
| 1564 | |
| 1565 | profile->Delete(); |
| 1566 | } |
| 1567 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1568 | // [Top down]: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1569 | // 0 (root) #0 1 |
| 1570 | // 2 (program) #0 2 |
| 1571 | // 3 (idle) #0 3 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1572 | TEST(IdleTime) { |
| 1573 | LocalContext env; |
| 1574 | v8::HandleScope scope(env->GetIsolate()); |
| 1575 | v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
| 1576 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1577 | v8::Local<v8::String> profile_name = v8_str("my_profile"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1578 | cpu_profiler->StartProfiling(profile_name); |
| 1579 | |
| 1580 | i::Isolate* isolate = CcTest::i_isolate(); |
| 1581 | i::ProfilerEventsProcessor* processor = isolate->cpu_profiler()->processor(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1582 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1583 | processor->AddCurrentStack(isolate, true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1584 | cpu_profiler->SetIdle(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1585 | for (int i = 0; i < 3; i++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1586 | processor->AddCurrentStack(isolate, true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1587 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1588 | cpu_profiler->SetIdle(false); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1589 | processor->AddCurrentStack(isolate, true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1590 | |
| 1591 | v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1592 | CHECK(profile); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1593 | // Dump collected profile to have a better diagnostic in case of failure. |
| 1594 | reinterpret_cast<i::CpuProfile*>(profile)->Print(); |
| 1595 | |
| 1596 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1597 | const v8::CpuProfileNode* program_node = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1598 | GetChild(env.local(), root, ProfileGenerator::kProgramEntryName); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1599 | CHECK_EQ(0, program_node->GetChildrenCount()); |
| 1600 | CHECK_GE(program_node->GetHitCount(), 2u); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1601 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1602 | const v8::CpuProfileNode* idle_node = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1603 | GetChild(env.local(), root, ProfileGenerator::kIdleEntryName); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1604 | CHECK_EQ(0, idle_node->GetChildrenCount()); |
| 1605 | CHECK_GE(idle_node->GetHitCount(), 3u); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1606 | |
| 1607 | profile->Delete(); |
| 1608 | } |
| 1609 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1610 | static void CheckFunctionDetails(v8::Isolate* isolate, |
| 1611 | const v8::CpuProfileNode* node, |
| 1612 | const char* name, const char* script_name, |
| 1613 | int script_id, int line, int column) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1614 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 1615 | CHECK(v8_str(name)->Equals(context, node->GetFunctionName()).FromJust()); |
| 1616 | CHECK(v8_str(script_name) |
| 1617 | ->Equals(context, node->GetScriptResourceName()) |
| 1618 | .FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1619 | CHECK_EQ(script_id, node->GetScriptId()); |
| 1620 | CHECK_EQ(line, node->GetLineNumber()); |
| 1621 | CHECK_EQ(column, node->GetColumnNumber()); |
| 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | TEST(FunctionDetails) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1626 | i::FLAG_allow_natives_syntax = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1627 | v8::HandleScope scope(CcTest::isolate()); |
| 1628 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1629 | v8::Context::Scope context_scope(env); |
| 1630 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1631 | v8::Local<v8::Script> script_a = CompileWithOrigin( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1632 | "%NeverOptimizeFunction(foo);\n" |
| 1633 | "%NeverOptimizeFunction(bar);\n" |
| 1634 | " function foo\n() { bar(); }\n" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1635 | " function bar() { startProfiling(); }\n", |
| 1636 | "script_a"); |
| 1637 | script_a->Run(env).ToLocalChecked(); |
| 1638 | v8::Local<v8::Script> script_b = CompileWithOrigin( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1639 | "%NeverOptimizeFunction(baz);" |
| 1640 | "\n\n function baz() { foo(); }\n" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1641 | "\n\nbaz();\n" |
| 1642 | "stopProfiling();\n", |
| 1643 | "script_b"); |
| 1644 | script_b->Run(env).ToLocalChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1645 | const v8::CpuProfile* profile = i::ProfilerExtension::last_profile; |
| 1646 | const v8::CpuProfileNode* current = profile->GetTopDownRoot(); |
| 1647 | reinterpret_cast<ProfileNode*>( |
| 1648 | const_cast<v8::CpuProfileNode*>(current))->Print(0); |
| 1649 | // The tree should look like this: |
| 1650 | // 0 (root) 0 #1 |
| 1651 | // 0 "" 19 #2 no reason script_b:1 |
| 1652 | // 0 baz 19 #3 TryCatchStatement script_b:3 |
| 1653 | // 0 foo 18 #4 TryCatchStatement script_a:2 |
| 1654 | // 1 bar 18 #5 no reason script_a:3 |
| 1655 | const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1656 | const v8::CpuProfileNode* script = GetChild(env, root, ""); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1657 | CheckFunctionDetails(env->GetIsolate(), script, "", "script_b", |
| 1658 | script_b->GetUnboundScript()->GetId(), 1, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1659 | const v8::CpuProfileNode* baz = GetChild(env, script, "baz"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1660 | CheckFunctionDetails(env->GetIsolate(), baz, "baz", "script_b", |
| 1661 | script_b->GetUnboundScript()->GetId(), 3, 16); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1662 | const v8::CpuProfileNode* foo = GetChild(env, baz, "foo"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1663 | CheckFunctionDetails(env->GetIsolate(), foo, "foo", "script_a", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1664 | script_a->GetUnboundScript()->GetId(), 4, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1665 | const v8::CpuProfileNode* bar = GetChild(env, foo, "bar"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1666 | CheckFunctionDetails(env->GetIsolate(), bar, "bar", "script_a", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1667 | script_a->GetUnboundScript()->GetId(), 5, 14); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | TEST(DontStopOnFinishedProfileDelete) { |
| 1672 | v8::HandleScope scope(CcTest::isolate()); |
| 1673 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1674 | v8::Context::Scope context_scope(env); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1675 | |
| 1676 | v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler(); |
| 1677 | i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler); |
| 1678 | |
| 1679 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1680 | v8::Local<v8::String> outer = v8_str("outer"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1681 | profiler->StartProfiling(outer); |
| 1682 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
| 1683 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1684 | v8::Local<v8::String> inner = v8_str("inner"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1685 | profiler->StartProfiling(inner); |
| 1686 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
| 1687 | |
| 1688 | v8::CpuProfile* inner_profile = profiler->StopProfiling(inner); |
| 1689 | CHECK(inner_profile); |
| 1690 | CHECK_EQ(1, iprofiler->GetProfilesCount()); |
| 1691 | inner_profile->Delete(); |
| 1692 | inner_profile = NULL; |
| 1693 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
| 1694 | |
| 1695 | v8::CpuProfile* outer_profile = profiler->StopProfiling(outer); |
| 1696 | CHECK(outer_profile); |
| 1697 | CHECK_EQ(1, iprofiler->GetProfilesCount()); |
| 1698 | outer_profile->Delete(); |
| 1699 | outer_profile = NULL; |
| 1700 | CHECK_EQ(0, iprofiler->GetProfilesCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1701 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1702 | |
| 1703 | |
| 1704 | const char* GetBranchDeoptReason(v8::Local<v8::Context> context, |
| 1705 | i::CpuProfile* iprofile, const char* branch[], |
| 1706 | int length) { |
| 1707 | v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile); |
| 1708 | const ProfileNode* iopt_function = NULL; |
| 1709 | iopt_function = GetSimpleBranch(context, profile, branch, length); |
| 1710 | CHECK_EQ(1U, iopt_function->deopt_infos().size()); |
| 1711 | return iopt_function->deopt_infos()[0].deopt_reason; |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | // deopt at top function |
| 1716 | TEST(CollectDeoptEvents) { |
| 1717 | if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return; |
| 1718 | i::FLAG_allow_natives_syntax = true; |
| 1719 | v8::HandleScope scope(CcTest::isolate()); |
| 1720 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1721 | v8::Context::Scope context_scope(env); |
| 1722 | v8::Isolate* isolate = env->GetIsolate(); |
| 1723 | v8::CpuProfiler* profiler = isolate->GetCpuProfiler(); |
| 1724 | i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler); |
| 1725 | |
| 1726 | const char opt_source[] = |
| 1727 | "function opt_function%d(value, depth) {\n" |
| 1728 | " if (depth) return opt_function%d(value, depth - 1);\n" |
| 1729 | "\n" |
| 1730 | " return 10 / value;\n" |
| 1731 | "}\n" |
| 1732 | "\n"; |
| 1733 | |
| 1734 | for (int i = 0; i < 3; ++i) { |
| 1735 | i::EmbeddedVector<char, sizeof(opt_source) + 100> buffer; |
| 1736 | i::SNPrintF(buffer, opt_source, i, i); |
| 1737 | v8::Script::Compile(env, v8_str(buffer.start())) |
| 1738 | .ToLocalChecked() |
| 1739 | ->Run(env) |
| 1740 | .ToLocalChecked(); |
| 1741 | } |
| 1742 | |
| 1743 | const char* source = |
| 1744 | "startProfiling();\n" |
| 1745 | "\n" |
| 1746 | "opt_function0(1, 1);\n" |
| 1747 | "\n" |
| 1748 | "%OptimizeFunctionOnNextCall(opt_function0)\n" |
| 1749 | "\n" |
| 1750 | "opt_function0(1, 1);\n" |
| 1751 | "\n" |
| 1752 | "opt_function0(undefined, 1);\n" |
| 1753 | "\n" |
| 1754 | "opt_function1(1, 1);\n" |
| 1755 | "\n" |
| 1756 | "%OptimizeFunctionOnNextCall(opt_function1)\n" |
| 1757 | "\n" |
| 1758 | "opt_function1(1, 1);\n" |
| 1759 | "\n" |
| 1760 | "opt_function1(NaN, 1);\n" |
| 1761 | "\n" |
| 1762 | "opt_function2(1, 1);\n" |
| 1763 | "\n" |
| 1764 | "%OptimizeFunctionOnNextCall(opt_function2)\n" |
| 1765 | "\n" |
| 1766 | "opt_function2(1, 1);\n" |
| 1767 | "\n" |
| 1768 | "opt_function2(0, 1);\n" |
| 1769 | "\n" |
| 1770 | "stopProfiling();\n" |
| 1771 | "\n"; |
| 1772 | |
| 1773 | v8::Script::Compile(env, v8_str(source)) |
| 1774 | .ToLocalChecked() |
| 1775 | ->Run(env) |
| 1776 | .ToLocalChecked(); |
| 1777 | i::CpuProfile* iprofile = iprofiler->GetProfile(0); |
| 1778 | iprofile->Print(); |
| 1779 | /* The expected profile |
| 1780 | [Top down]: |
| 1781 | 0 (root) 0 #1 |
| 1782 | 23 32 #2 |
| 1783 | 1 opt_function2 31 #7 |
| 1784 | 1 opt_function2 31 #8 |
| 1785 | ;;; deopted at script_id: 31 position: 106 with reason |
| 1786 | 'division by zero'. |
| 1787 | 2 opt_function0 29 #3 |
| 1788 | 4 opt_function0 29 #4 |
| 1789 | ;;; deopted at script_id: 29 position: 108 with reason 'not a |
| 1790 | heap number'. |
| 1791 | 0 opt_function1 30 #5 |
| 1792 | 1 opt_function1 30 #6 |
| 1793 | ;;; deopted at script_id: 30 position: 108 with reason 'lost |
| 1794 | precision or NaN'. |
| 1795 | */ |
| 1796 | |
| 1797 | { |
| 1798 | const char* branch[] = {"", "opt_function0", "opt_function0"}; |
| 1799 | CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), |
| 1800 | GetBranchDeoptReason(env, iprofile, branch, arraysize(branch))); |
| 1801 | } |
| 1802 | { |
| 1803 | const char* branch[] = {"", "opt_function1", "opt_function1"}; |
| 1804 | const char* deopt_reason = |
| 1805 | GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)); |
| 1806 | if (deopt_reason != reason(i::Deoptimizer::kNaN) && |
| 1807 | deopt_reason != reason(i::Deoptimizer::kLostPrecisionOrNaN)) { |
| 1808 | FATAL(deopt_reason); |
| 1809 | } |
| 1810 | } |
| 1811 | { |
| 1812 | const char* branch[] = {"", "opt_function2", "opt_function2"}; |
| 1813 | CHECK_EQ(reason(i::Deoptimizer::kDivisionByZero), |
| 1814 | GetBranchDeoptReason(env, iprofile, branch, arraysize(branch))); |
| 1815 | } |
| 1816 | iprofiler->DeleteProfile(iprofile); |
| 1817 | } |
| 1818 | |
| 1819 | |
| 1820 | TEST(SourceLocation) { |
| 1821 | i::FLAG_always_opt = true; |
| 1822 | i::FLAG_hydrogen_track_positions = true; |
| 1823 | LocalContext env; |
| 1824 | v8::HandleScope scope(CcTest::isolate()); |
| 1825 | |
| 1826 | const char* source = |
| 1827 | "function CompareStatementWithThis() {\n" |
| 1828 | " if (this === 1) {}\n" |
| 1829 | "}\n" |
| 1830 | "CompareStatementWithThis();\n"; |
| 1831 | |
| 1832 | v8::Script::Compile(env.local(), v8_str(source)) |
| 1833 | .ToLocalChecked() |
| 1834 | ->Run(env.local()) |
| 1835 | .ToLocalChecked(); |
| 1836 | } |
| 1837 | |
| 1838 | |
| 1839 | static const char* inlined_source = |
| 1840 | "function opt_function(left, right) { var k = left / 10; var r = 10 / " |
| 1841 | "right; return k + r; }\n"; |
| 1842 | // 0.........1.........2.........3.........4....*....5.........6......*..7 |
| 1843 | |
| 1844 | |
| 1845 | // deopt at the first level inlined function |
| 1846 | TEST(DeoptAtFirstLevelInlinedSource) { |
| 1847 | if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return; |
| 1848 | i::FLAG_allow_natives_syntax = true; |
| 1849 | v8::HandleScope scope(CcTest::isolate()); |
| 1850 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1851 | v8::Context::Scope context_scope(env); |
| 1852 | v8::Isolate* isolate = env->GetIsolate(); |
| 1853 | v8::CpuProfiler* profiler = isolate->GetCpuProfiler(); |
| 1854 | i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler); |
| 1855 | |
| 1856 | // 0.........1.........2.........3.........4.........5.........6.........7 |
| 1857 | const char* source = |
| 1858 | "function test(left, right) { return opt_function(left, right); }\n" |
| 1859 | "\n" |
| 1860 | "startProfiling();\n" |
| 1861 | "\n" |
| 1862 | "test(10, 10);\n" |
| 1863 | "\n" |
| 1864 | "%OptimizeFunctionOnNextCall(test)\n" |
| 1865 | "\n" |
| 1866 | "test(10, 10);\n" |
| 1867 | "\n" |
| 1868 | "test(undefined, 10);\n" |
| 1869 | "\n" |
| 1870 | "stopProfiling();\n" |
| 1871 | "\n"; |
| 1872 | |
| 1873 | v8::Local<v8::Script> inlined_script = v8_compile(inlined_source); |
| 1874 | inlined_script->Run(env).ToLocalChecked(); |
| 1875 | int inlined_script_id = inlined_script->GetUnboundScript()->GetId(); |
| 1876 | |
| 1877 | v8::Local<v8::Script> script = v8_compile(source); |
| 1878 | script->Run(env).ToLocalChecked(); |
| 1879 | int script_id = script->GetUnboundScript()->GetId(); |
| 1880 | |
| 1881 | i::CpuProfile* iprofile = iprofiler->GetProfile(0); |
| 1882 | iprofile->Print(); |
| 1883 | /* The expected profile output |
| 1884 | [Top down]: |
| 1885 | 0 (root) 0 #1 |
| 1886 | 10 30 #2 |
| 1887 | 1 test 30 #3 |
| 1888 | ;;; deopted at script_id: 29 position: 45 with reason 'not a |
| 1889 | heap number'. |
| 1890 | ;;; Inline point: script_id 30 position: 36. |
| 1891 | 4 opt_function 29 #4 |
| 1892 | */ |
| 1893 | v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile); |
| 1894 | |
| 1895 | const char* branch[] = {"", "test"}; |
| 1896 | const ProfileNode* itest_node = |
| 1897 | GetSimpleBranch(env, profile, branch, arraysize(branch)); |
| 1898 | const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos = |
| 1899 | itest_node->deopt_infos(); |
| 1900 | CHECK_EQ(1U, deopt_infos.size()); |
| 1901 | |
| 1902 | const v8::CpuProfileDeoptInfo& info = deopt_infos[0]; |
| 1903 | CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason); |
| 1904 | CHECK_EQ(2U, info.stack.size()); |
| 1905 | CHECK_EQ(inlined_script_id, info.stack[0].script_id); |
| 1906 | CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position); |
| 1907 | CHECK_EQ(script_id, info.stack[1].script_id); |
| 1908 | CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position); |
| 1909 | |
| 1910 | iprofiler->DeleteProfile(iprofile); |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | // deopt at the second level inlined function |
| 1915 | TEST(DeoptAtSecondLevelInlinedSource) { |
| 1916 | if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return; |
| 1917 | i::FLAG_allow_natives_syntax = true; |
| 1918 | v8::HandleScope scope(CcTest::isolate()); |
| 1919 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1920 | v8::Context::Scope context_scope(env); |
| 1921 | v8::Isolate* isolate = env->GetIsolate(); |
| 1922 | v8::CpuProfiler* profiler = isolate->GetCpuProfiler(); |
| 1923 | i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler); |
| 1924 | |
| 1925 | // 0.........1.........2.........3.........4.........5.........6.........7 |
| 1926 | const char* source = |
| 1927 | "function test2(left, right) { return opt_function(left, right); }\n" |
| 1928 | "function test1(left, right) { return test2(left, right); }\n" |
| 1929 | "\n" |
| 1930 | "startProfiling();\n" |
| 1931 | "\n" |
| 1932 | "test1(10, 10);\n" |
| 1933 | "\n" |
| 1934 | "%OptimizeFunctionOnNextCall(test1)\n" |
| 1935 | "\n" |
| 1936 | "test1(10, 10);\n" |
| 1937 | "\n" |
| 1938 | "test1(undefined, 10);\n" |
| 1939 | "\n" |
| 1940 | "stopProfiling();\n" |
| 1941 | "\n"; |
| 1942 | |
| 1943 | v8::Local<v8::Script> inlined_script = v8_compile(inlined_source); |
| 1944 | inlined_script->Run(env).ToLocalChecked(); |
| 1945 | int inlined_script_id = inlined_script->GetUnboundScript()->GetId(); |
| 1946 | |
| 1947 | v8::Local<v8::Script> script = v8_compile(source); |
| 1948 | script->Run(env).ToLocalChecked(); |
| 1949 | int script_id = script->GetUnboundScript()->GetId(); |
| 1950 | |
| 1951 | i::CpuProfile* iprofile = iprofiler->GetProfile(0); |
| 1952 | iprofile->Print(); |
| 1953 | /* The expected profile output |
| 1954 | [Top down]: |
| 1955 | 0 (root) 0 #1 |
| 1956 | 11 30 #2 |
| 1957 | 1 test1 30 #3 |
| 1958 | ;;; deopted at script_id: 29 position: 45 with reason 'not a |
| 1959 | heap number'. |
| 1960 | ;;; Inline point: script_id 30 position: 37. |
| 1961 | ;;; Inline point: script_id 30 position: 103. |
| 1962 | 1 test2 30 #4 |
| 1963 | 3 opt_function 29 #5 |
| 1964 | */ |
| 1965 | |
| 1966 | v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile); |
| 1967 | |
| 1968 | const char* branch[] = {"", "test1"}; |
| 1969 | const ProfileNode* itest_node = |
| 1970 | GetSimpleBranch(env, profile, branch, arraysize(branch)); |
| 1971 | const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos = |
| 1972 | itest_node->deopt_infos(); |
| 1973 | CHECK_EQ(1U, deopt_infos.size()); |
| 1974 | |
| 1975 | const v8::CpuProfileDeoptInfo info = deopt_infos[0]; |
| 1976 | CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason); |
| 1977 | CHECK_EQ(3U, info.stack.size()); |
| 1978 | CHECK_EQ(inlined_script_id, info.stack[0].script_id); |
| 1979 | CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position); |
| 1980 | CHECK_EQ(script_id, info.stack[1].script_id); |
| 1981 | CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position); |
| 1982 | CHECK_EQ(offset(source, "test2(left, right);"), info.stack[2].position); |
| 1983 | |
| 1984 | iprofiler->DeleteProfile(iprofile); |
| 1985 | } |
| 1986 | |
| 1987 | |
| 1988 | // deopt in untracked function |
| 1989 | TEST(DeoptUntrackedFunction) { |
| 1990 | if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return; |
| 1991 | i::FLAG_allow_natives_syntax = true; |
| 1992 | v8::HandleScope scope(CcTest::isolate()); |
| 1993 | v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION); |
| 1994 | v8::Context::Scope context_scope(env); |
| 1995 | v8::Isolate* isolate = env->GetIsolate(); |
| 1996 | v8::CpuProfiler* profiler = isolate->GetCpuProfiler(); |
| 1997 | i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler); |
| 1998 | |
| 1999 | // 0.........1.........2.........3.........4.........5.........6.........7 |
| 2000 | const char* source = |
| 2001 | "function test(left, right) { return opt_function(left, right); }\n" |
| 2002 | "\n" |
| 2003 | "test(10, 10);\n" |
| 2004 | "\n" |
| 2005 | "%OptimizeFunctionOnNextCall(test)\n" |
| 2006 | "\n" |
| 2007 | "test(10, 10);\n" |
| 2008 | "\n" |
| 2009 | "startProfiling();\n" // profiler started after compilation. |
| 2010 | "\n" |
| 2011 | "test(undefined, 10);\n" |
| 2012 | "\n" |
| 2013 | "stopProfiling();\n" |
| 2014 | "\n"; |
| 2015 | |
| 2016 | v8::Local<v8::Script> inlined_script = v8_compile(inlined_source); |
| 2017 | inlined_script->Run(env).ToLocalChecked(); |
| 2018 | |
| 2019 | v8::Local<v8::Script> script = v8_compile(source); |
| 2020 | script->Run(env).ToLocalChecked(); |
| 2021 | |
| 2022 | i::CpuProfile* iprofile = iprofiler->GetProfile(0); |
| 2023 | iprofile->Print(); |
| 2024 | v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile); |
| 2025 | |
| 2026 | const char* branch[] = {"", "test"}; |
| 2027 | const ProfileNode* itest_node = |
| 2028 | GetSimpleBranch(env, profile, branch, arraysize(branch)); |
| 2029 | CHECK_EQ(0U, itest_node->deopt_infos().size()); |
| 2030 | |
| 2031 | iprofiler->DeleteProfile(iprofile); |
| 2032 | } |