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