blob: 4e4cf28c5cc888b7365129531f675e8d1e67bba1 [file] [log] [blame]
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001// Copyright 2010 the V8 project authors. All rights reserved.
2//
3// Tests of profiles generator and utilities.
4
lrn@chromium.org25156de2010-04-06 13:10:27 +00005#ifdef ENABLE_CPP_PROFILES_PROCESSOR
6
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00007#include "v8.h"
8#include "cpu-profiler-inl.h"
9#include "cctest.h"
10
11namespace i = v8::internal;
12
13using i::CodeEntry;
lrn@chromium.org25156de2010-04-06 13:10:27 +000014using i::CpuProfile;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000015using i::CpuProfilesCollection;
16using i::ProfileGenerator;
17using i::ProfileNode;
18using i::ProfilerEventsProcessor;
19
20
21TEST(StartStop) {
22 CpuProfilesCollection profiles;
23 ProfileGenerator generator(&profiles);
24 ProfilerEventsProcessor processor(&generator);
25 processor.Start();
26 while (!processor.running()) {
27 i::Thread::YieldCPU();
28 }
29 processor.Stop();
30 processor.Join();
31}
32
33static v8::Persistent<v8::Context> env;
34
35static void InitializeVM() {
36 if (env.IsEmpty()) env = v8::Context::New();
37 v8::HandleScope scope;
38 env->Enter();
39}
40
41static inline i::Address ToAddress(int n) {
42 return reinterpret_cast<i::Address>(n);
43}
44
45static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc,
46 i::Address frame1,
47 i::Address frame2 = NULL,
48 i::Address frame3 = NULL) {
49 i::TickSample* sample = proc->TickSampleEvent();
50 sample->pc = frame1;
51 sample->function = frame1;
52 sample->frames_count = 0;
53 if (frame2 != NULL) {
54 sample->stack[0] = frame2;
55 sample->frames_count = 1;
56 }
57 if (frame3 != NULL) {
58 sample->stack[1] = frame3;
59 sample->frames_count = 2;
60 }
61}
62
ager@chromium.org357bf652010-04-12 11:30:10 +000063namespace {
64
65class TestSetup {
66 public:
67 TestSetup()
68 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
69 i::FLAG_prof_browser_mode = false;
70 }
71
72 ~TestSetup() {
73 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
74 }
75
76 private:
77 bool old_flag_prof_browser_mode_;
78};
79
80} // namespace
81
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000082TEST(CodeEvents) {
83 InitializeVM();
ager@chromium.org357bf652010-04-12 11:30:10 +000084 TestSetup test_setup;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000085 CpuProfilesCollection profiles;
lrn@chromium.org25156de2010-04-06 13:10:27 +000086 profiles.StartProfiling("", 1);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000087 ProfileGenerator generator(&profiles);
88 ProfilerEventsProcessor processor(&generator);
89 processor.Start();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000090 while (!processor.running()) {
91 i::Thread::YieldCPU();
92 }
93
94 // Enqueue code creation events.
95 i::HandleScope scope;
96 const char* aaa_str = "aaa";
97 i::Handle<i::String> aaa_name = i::Factory::NewStringFromAscii(
98 i::Vector<const char>(aaa_str, strlen(aaa_str)));
99 processor.CodeCreateEvent(i::Logger::FUNCTION_TAG,
100 *aaa_name,
101 i::Heap::empty_string(),
102 0,
103 ToAddress(0x1000),
104 0x100);
105 processor.CodeCreateEvent(i::Logger::BUILTIN_TAG,
106 "bbb",
107 ToAddress(0x1200),
108 0x80);
109 processor.CodeCreateEvent(i::Logger::STUB_TAG, 5, ToAddress(0x1300), 0x10);
110 processor.CodeCreateEvent(i::Logger::BUILTIN_TAG,
111 "ddd",
112 ToAddress(0x1400),
113 0x80);
114 processor.CodeMoveEvent(ToAddress(0x1400), ToAddress(0x1500));
115 processor.CodeCreateEvent(i::Logger::STUB_TAG, 3, ToAddress(0x1600), 0x10);
116 processor.CodeDeleteEvent(ToAddress(0x1600));
117 processor.FunctionCreateEvent(ToAddress(0x1700), ToAddress(0x1000));
118 // Enqueue a tick event to enable code events processing.
119 EnqueueTickSampleEvent(&processor, ToAddress(0x1000));
120
121 processor.Stop();
122 processor.Join();
123
124 // Check the state of profile generator.
125 CodeEntry* entry1 = generator.code_map()->FindEntry(ToAddress(0x1000));
126 CHECK_NE(NULL, entry1);
127 CHECK_EQ(aaa_str, entry1->name());
128 CodeEntry* entry2 = generator.code_map()->FindEntry(ToAddress(0x1200));
129 CHECK_NE(NULL, entry2);
130 CHECK_EQ("bbb", entry2->name());
131 CodeEntry* entry3 = generator.code_map()->FindEntry(ToAddress(0x1300));
132 CHECK_NE(NULL, entry3);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000133 CHECK_EQ("5", entry3->name());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000134 CHECK_EQ(NULL, generator.code_map()->FindEntry(ToAddress(0x1400)));
135 CodeEntry* entry4 = generator.code_map()->FindEntry(ToAddress(0x1500));
136 CHECK_NE(NULL, entry4);
137 CHECK_EQ("ddd", entry4->name());
138 CHECK_EQ(NULL, generator.code_map()->FindEntry(ToAddress(0x1600)));
139 CodeEntry* entry5 = generator.code_map()->FindEntry(ToAddress(0x1700));
140 CHECK_NE(NULL, entry5);
141 CHECK_EQ(aaa_str, entry5->name());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000142}
143
144
145template<typename T>
146static int CompareProfileNodes(const T* p1, const T* p2) {
147 return strcmp((*p1)->entry()->name(), (*p2)->entry()->name());
148}
149
150TEST(TickEvents) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000151 TestSetup test_setup;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000152 CpuProfilesCollection profiles;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000153 profiles.StartProfiling("", 1);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000154 ProfileGenerator generator(&profiles);
155 ProfilerEventsProcessor processor(&generator);
156 processor.Start();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000157 while (!processor.running()) {
158 i::Thread::YieldCPU();
159 }
160
161 processor.CodeCreateEvent(i::Logger::BUILTIN_TAG,
162 "bbb",
163 ToAddress(0x1200),
164 0x80);
165 processor.CodeCreateEvent(i::Logger::STUB_TAG, 5, ToAddress(0x1300), 0x10);
166 processor.CodeCreateEvent(i::Logger::BUILTIN_TAG,
167 "ddd",
168 ToAddress(0x1400),
169 0x80);
170 EnqueueTickSampleEvent(&processor, ToAddress(0x1210));
171 EnqueueTickSampleEvent(&processor, ToAddress(0x1305), ToAddress(0x1220));
172 EnqueueTickSampleEvent(&processor,
173 ToAddress(0x1404),
174 ToAddress(0x1305),
175 ToAddress(0x1230));
176
177 processor.Stop();
178 processor.Join();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000179 CpuProfile* profile = profiles.StopProfiling("");
180 CHECK_NE(NULL, profile);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000181
182 // Check call trees.
lrn@chromium.org25156de2010-04-06 13:10:27 +0000183 const i::List<ProfileNode*>* top_down_root_children =
184 profile->top_down()->root()->children();
185 CHECK_EQ(1, top_down_root_children->length());
186 CHECK_EQ("bbb", top_down_root_children->last()->entry()->name());
187 const i::List<ProfileNode*>* top_down_bbb_children =
188 top_down_root_children->last()->children();
189 CHECK_EQ(1, top_down_bbb_children->length());
190 CHECK_EQ("5", top_down_bbb_children->last()->entry()->name());
191 const i::List<ProfileNode*>* top_down_stub_children =
192 top_down_bbb_children->last()->children();
193 CHECK_EQ(1, top_down_stub_children->length());
194 CHECK_EQ("ddd", top_down_stub_children->last()->entry()->name());
195 const i::List<ProfileNode*>* top_down_ddd_children =
196 top_down_stub_children->last()->children();
197 CHECK_EQ(0, top_down_ddd_children->length());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000198
lrn@chromium.org25156de2010-04-06 13:10:27 +0000199 const i::List<ProfileNode*>* bottom_up_root_children_unsorted =
200 profile->bottom_up()->root()->children();
201 CHECK_EQ(3, bottom_up_root_children_unsorted->length());
202 i::List<ProfileNode*> bottom_up_root_children(3);
203 bottom_up_root_children.AddAll(*bottom_up_root_children_unsorted);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000204 bottom_up_root_children.Sort(&CompareProfileNodes);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000205 CHECK_EQ("5", bottom_up_root_children[0]->entry()->name());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000206 CHECK_EQ("bbb", bottom_up_root_children[1]->entry()->name());
207 CHECK_EQ("ddd", bottom_up_root_children[2]->entry()->name());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000208 const i::List<ProfileNode*>* bottom_up_stub_children =
209 bottom_up_root_children[0]->children();
210 CHECK_EQ(1, bottom_up_stub_children->length());
211 CHECK_EQ("bbb", bottom_up_stub_children->last()->entry()->name());
212 const i::List<ProfileNode*>* bottom_up_bbb_children =
213 bottom_up_root_children[1]->children();
214 CHECK_EQ(0, bottom_up_bbb_children->length());
215 const i::List<ProfileNode*>* bottom_up_ddd_children =
216 bottom_up_root_children[2]->children();
217 CHECK_EQ(1, bottom_up_ddd_children->length());
218 CHECK_EQ("5", bottom_up_ddd_children->last()->entry()->name());
219 const i::List<ProfileNode*>* bottom_up_ddd_stub_children =
220 bottom_up_ddd_children->last()->children();
221 CHECK_EQ(1, bottom_up_ddd_stub_children->length());
222 CHECK_EQ("bbb", bottom_up_ddd_stub_children->last()->entry()->name());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000223}
lrn@chromium.org25156de2010-04-06 13:10:27 +0000224
225#endif // ENABLE_CPP_PROFILES_PROCESSOR