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