Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // |
| 3 | // Tests of profiles generator and utilities. |
| 4 | |
| 5 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 6 | |
| 7 | #include "v8.h" |
| 8 | #include "cpu-profiler-inl.h" |
| 9 | #include "cctest.h" |
| 10 | |
| 11 | namespace i = v8::internal; |
| 12 | |
| 13 | using i::CodeEntry; |
| 14 | using i::CpuProfile; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 15 | using i::CpuProfiler; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 16 | using i::CpuProfilesCollection; |
| 17 | using i::ProfileGenerator; |
| 18 | using i::ProfileNode; |
| 19 | using i::ProfilerEventsProcessor; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 20 | using i::TokenEnumerator; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 21 | |
| 22 | |
| 23 | TEST(StartStop) { |
| 24 | CpuProfilesCollection profiles; |
| 25 | ProfileGenerator generator(&profiles); |
| 26 | ProfilerEventsProcessor processor(&generator); |
| 27 | processor.Start(); |
| 28 | while (!processor.running()) { |
| 29 | i::Thread::YieldCPU(); |
| 30 | } |
| 31 | processor.Stop(); |
| 32 | processor.Join(); |
| 33 | } |
| 34 | |
| 35 | static v8::Persistent<v8::Context> env; |
| 36 | |
| 37 | static void InitializeVM() { |
| 38 | if (env.IsEmpty()) env = v8::Context::New(); |
| 39 | v8::HandleScope scope; |
| 40 | env->Enter(); |
| 41 | } |
| 42 | |
| 43 | static inline i::Address ToAddress(int n) { |
| 44 | return reinterpret_cast<i::Address>(n); |
| 45 | } |
| 46 | |
| 47 | static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc, |
| 48 | i::Address frame1, |
| 49 | i::Address frame2 = NULL, |
| 50 | i::Address frame3 = NULL) { |
| 51 | i::TickSample* sample = proc->TickSampleEvent(); |
| 52 | sample->pc = frame1; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame^] | 53 | sample->tos = frame1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 54 | sample->frames_count = 0; |
| 55 | if (frame2 != NULL) { |
| 56 | sample->stack[0] = frame2; |
| 57 | sample->frames_count = 1; |
| 58 | } |
| 59 | if (frame3 != NULL) { |
| 60 | sample->stack[1] = frame3; |
| 61 | sample->frames_count = 2; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | class TestSetup { |
| 68 | public: |
| 69 | TestSetup() |
| 70 | : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) { |
| 71 | i::FLAG_prof_browser_mode = false; |
| 72 | } |
| 73 | |
| 74 | ~TestSetup() { |
| 75 | i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | bool old_flag_prof_browser_mode_; |
| 80 | }; |
| 81 | |
| 82 | } // namespace |
| 83 | |
| 84 | TEST(CodeEvents) { |
| 85 | InitializeVM(); |
| 86 | TestSetup test_setup; |
| 87 | CpuProfilesCollection profiles; |
| 88 | profiles.StartProfiling("", 1); |
| 89 | ProfileGenerator generator(&profiles); |
| 90 | ProfilerEventsProcessor processor(&generator); |
| 91 | processor.Start(); |
| 92 | while (!processor.running()) { |
| 93 | i::Thread::YieldCPU(); |
| 94 | } |
| 95 | |
| 96 | // Enqueue code creation events. |
| 97 | i::HandleScope scope; |
| 98 | const char* aaa_str = "aaa"; |
| 99 | i::Handle<i::String> aaa_name = i::Factory::NewStringFromAscii( |
| 100 | i::Vector<const char>(aaa_str, i::StrLength(aaa_str))); |
| 101 | processor.CodeCreateEvent(i::Logger::FUNCTION_TAG, |
| 102 | *aaa_name, |
| 103 | i::Heap::empty_string(), |
| 104 | 0, |
| 105 | ToAddress(0x1000), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame^] | 106 | 0x100, |
| 107 | ToAddress(0x10000)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 108 | processor.CodeCreateEvent(i::Logger::BUILTIN_TAG, |
| 109 | "bbb", |
| 110 | ToAddress(0x1200), |
| 111 | 0x80); |
| 112 | processor.CodeCreateEvent(i::Logger::STUB_TAG, 5, ToAddress(0x1300), 0x10); |
| 113 | processor.CodeCreateEvent(i::Logger::BUILTIN_TAG, |
| 114 | "ddd", |
| 115 | ToAddress(0x1400), |
| 116 | 0x80); |
| 117 | processor.CodeMoveEvent(ToAddress(0x1400), ToAddress(0x1500)); |
| 118 | processor.CodeCreateEvent(i::Logger::STUB_TAG, 3, ToAddress(0x1600), 0x10); |
| 119 | processor.CodeDeleteEvent(ToAddress(0x1600)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 120 | // Enqueue a tick event to enable code events processing. |
| 121 | EnqueueTickSampleEvent(&processor, ToAddress(0x1000)); |
| 122 | |
| 123 | processor.Stop(); |
| 124 | processor.Join(); |
| 125 | |
| 126 | // Check the state of profile generator. |
| 127 | CodeEntry* entry1 = generator.code_map()->FindEntry(ToAddress(0x1000)); |
| 128 | CHECK_NE(NULL, entry1); |
| 129 | CHECK_EQ(aaa_str, entry1->name()); |
| 130 | CodeEntry* entry2 = generator.code_map()->FindEntry(ToAddress(0x1200)); |
| 131 | CHECK_NE(NULL, entry2); |
| 132 | CHECK_EQ("bbb", entry2->name()); |
| 133 | CodeEntry* entry3 = generator.code_map()->FindEntry(ToAddress(0x1300)); |
| 134 | CHECK_NE(NULL, entry3); |
| 135 | CHECK_EQ("5", entry3->name()); |
| 136 | CHECK_EQ(NULL, generator.code_map()->FindEntry(ToAddress(0x1400))); |
| 137 | CodeEntry* entry4 = generator.code_map()->FindEntry(ToAddress(0x1500)); |
| 138 | CHECK_NE(NULL, entry4); |
| 139 | CHECK_EQ("ddd", entry4->name()); |
| 140 | CHECK_EQ(NULL, generator.code_map()->FindEntry(ToAddress(0x1600))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | template<typename T> |
| 145 | static int CompareProfileNodes(const T* p1, const T* p2) { |
| 146 | return strcmp((*p1)->entry()->name(), (*p2)->entry()->name()); |
| 147 | } |
| 148 | |
| 149 | TEST(TickEvents) { |
| 150 | TestSetup test_setup; |
| 151 | CpuProfilesCollection profiles; |
| 152 | profiles.StartProfiling("", 1); |
| 153 | ProfileGenerator generator(&profiles); |
| 154 | ProfilerEventsProcessor processor(&generator); |
| 155 | processor.Start(); |
| 156 | while (!processor.running()) { |
| 157 | i::Thread::YieldCPU(); |
| 158 | } |
| 159 | |
| 160 | processor.CodeCreateEvent(i::Logger::BUILTIN_TAG, |
| 161 | "bbb", |
| 162 | ToAddress(0x1200), |
| 163 | 0x80); |
| 164 | processor.CodeCreateEvent(i::Logger::STUB_TAG, 5, ToAddress(0x1300), 0x10); |
| 165 | processor.CodeCreateEvent(i::Logger::BUILTIN_TAG, |
| 166 | "ddd", |
| 167 | ToAddress(0x1400), |
| 168 | 0x80); |
| 169 | EnqueueTickSampleEvent(&processor, ToAddress(0x1210)); |
| 170 | EnqueueTickSampleEvent(&processor, ToAddress(0x1305), ToAddress(0x1220)); |
| 171 | EnqueueTickSampleEvent(&processor, |
| 172 | ToAddress(0x1404), |
| 173 | ToAddress(0x1305), |
| 174 | ToAddress(0x1230)); |
| 175 | |
| 176 | processor.Stop(); |
| 177 | processor.Join(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 178 | CpuProfile* profile = |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 179 | profiles.StopProfiling(TokenEnumerator::kNoSecurityToken, "", 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 180 | CHECK_NE(NULL, profile); |
| 181 | |
| 182 | // Check call trees. |
| 183 | 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()); |
| 198 | |
| 199 | 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); |
| 204 | bottom_up_root_children.Sort(&CompareProfileNodes); |
| 205 | CHECK_EQ("5", bottom_up_root_children[0]->entry()->name()); |
| 206 | CHECK_EQ("bbb", bottom_up_root_children[1]->entry()->name()); |
| 207 | CHECK_EQ("ddd", bottom_up_root_children[2]->entry()->name()); |
| 208 | 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()); |
| 223 | } |
| 224 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 225 | |
| 226 | // http://crbug/51594 |
| 227 | // This test must not crash. |
| 228 | TEST(CrashIfStoppingLastNonExistentProfile) { |
| 229 | InitializeVM(); |
| 230 | TestSetup test_setup; |
| 231 | CpuProfiler::Setup(); |
| 232 | CpuProfiler::StartProfiling("1"); |
| 233 | CpuProfiler::StopProfiling("2"); |
| 234 | CpuProfiler::StartProfiling("1"); |
| 235 | CpuProfiler::StopProfiling(""); |
| 236 | CpuProfiler::TearDown(); |
| 237 | } |
| 238 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 239 | #endif // ENABLE_LOGGING_AND_PROFILING |