fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // |
| 3 | // Tests of profiles generator and utilities. |
| 4 | |
| 5 | #include "v8.h" |
| 6 | #include "profile-generator-inl.h" |
| 7 | #include "cctest.h" |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 8 | #include "../include/v8-profiler.h" |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 9 | |
| 10 | namespace i = v8::internal; |
| 11 | |
| 12 | using i::CodeEntry; |
| 13 | using i::CodeMap; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 14 | using i::CpuProfile; |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 15 | using i::CpuProfiler; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 16 | using i::CpuProfilesCollection; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 17 | using i::ProfileNode; |
| 18 | using i::ProfileTree; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 19 | using i::ProfileGenerator; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 20 | using i::SampleRateCalculator; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 21 | using i::TickSample; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 22 | using i::TokenEnumerator; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 23 | using i::Vector; |
| 24 | |
| 25 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 26 | namespace v8 { |
| 27 | namespace internal { |
| 28 | |
| 29 | class TokenEnumeratorTester { |
| 30 | public: |
| 31 | static i::List<bool>* token_removed(TokenEnumerator* te) { |
| 32 | return &te->token_removed_; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | } } // namespace v8::internal |
| 37 | |
| 38 | TEST(TokenEnumerator) { |
| 39 | TokenEnumerator te; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 40 | CHECK_EQ(TokenEnumerator::kNoSecurityToken, te.GetTokenId(NULL)); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 41 | v8::HandleScope hs; |
| 42 | v8::Local<v8::String> token1(v8::String::New("1")); |
| 43 | CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1))); |
| 44 | CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1))); |
| 45 | v8::Local<v8::String> token2(v8::String::New("2")); |
| 46 | CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2))); |
| 47 | CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2))); |
| 48 | CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1))); |
| 49 | { |
| 50 | v8::HandleScope hs; |
| 51 | v8::Local<v8::String> token3(v8::String::New("3")); |
| 52 | CHECK_EQ(2, te.GetTokenId(*v8::Utils::OpenHandle(*token3))); |
| 53 | CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2))); |
| 54 | CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1))); |
| 55 | } |
| 56 | CHECK(!i::TokenEnumeratorTester::token_removed(&te)->at(2)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 57 | HEAP->CollectAllGarbage(false); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 58 | CHECK(i::TokenEnumeratorTester::token_removed(&te)->at(2)); |
| 59 | CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2))); |
| 60 | CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1))); |
| 61 | } |
| 62 | |
| 63 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 64 | TEST(ProfileNodeFindOrAddChild) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 65 | ProfileNode node(NULL, NULL); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 66 | CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, |
| 67 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 68 | ProfileNode* childNode1 = node.FindOrAddChild(&entry1); |
| 69 | CHECK_NE(NULL, childNode1); |
| 70 | CHECK_EQ(childNode1, node.FindOrAddChild(&entry1)); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 71 | CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, |
| 72 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 73 | ProfileNode* childNode2 = node.FindOrAddChild(&entry2); |
| 74 | CHECK_NE(NULL, childNode2); |
| 75 | CHECK_NE(childNode1, childNode2); |
| 76 | CHECK_EQ(childNode1, node.FindOrAddChild(&entry1)); |
| 77 | CHECK_EQ(childNode2, node.FindOrAddChild(&entry2)); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 78 | CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, |
| 79 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 80 | ProfileNode* childNode3 = node.FindOrAddChild(&entry3); |
| 81 | CHECK_NE(NULL, childNode3); |
| 82 | CHECK_NE(childNode1, childNode3); |
| 83 | CHECK_NE(childNode2, childNode3); |
| 84 | CHECK_EQ(childNode1, node.FindOrAddChild(&entry1)); |
| 85 | CHECK_EQ(childNode2, node.FindOrAddChild(&entry2)); |
| 86 | CHECK_EQ(childNode3, node.FindOrAddChild(&entry3)); |
| 87 | } |
| 88 | |
| 89 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 90 | TEST(ProfileNodeFindOrAddChildForSameFunction) { |
| 91 | const char* empty = ""; |
| 92 | const char* aaa = "aaa"; |
| 93 | ProfileNode node(NULL, NULL); |
| 94 | CodeEntry entry1(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0, |
| 95 | TokenEnumerator::kNoSecurityToken); |
| 96 | ProfileNode* childNode1 = node.FindOrAddChild(&entry1); |
| 97 | CHECK_NE(NULL, childNode1); |
| 98 | CHECK_EQ(childNode1, node.FindOrAddChild(&entry1)); |
| 99 | // The same function again. |
| 100 | CodeEntry entry2(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0, |
| 101 | TokenEnumerator::kNoSecurityToken); |
| 102 | CHECK_EQ(childNode1, node.FindOrAddChild(&entry2)); |
| 103 | // Now with a different security token. |
| 104 | CodeEntry entry3(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0, |
| 105 | TokenEnumerator::kNoSecurityToken + 1); |
| 106 | CHECK_EQ(childNode1, node.FindOrAddChild(&entry3)); |
| 107 | } |
| 108 | |
| 109 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 110 | namespace { |
| 111 | |
| 112 | class ProfileTreeTestHelper { |
| 113 | public: |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 114 | explicit ProfileTreeTestHelper(const ProfileTree* tree) |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 115 | : tree_(tree) { } |
| 116 | |
| 117 | ProfileNode* Walk(CodeEntry* entry1, |
| 118 | CodeEntry* entry2 = NULL, |
| 119 | CodeEntry* entry3 = NULL) { |
| 120 | ProfileNode* node = tree_->root(); |
| 121 | node = node->FindChild(entry1); |
| 122 | if (node == NULL) return NULL; |
| 123 | if (entry2 != NULL) { |
| 124 | node = node->FindChild(entry2); |
| 125 | if (node == NULL) return NULL; |
| 126 | } |
| 127 | if (entry3 != NULL) { |
| 128 | node = node->FindChild(entry3); |
| 129 | } |
| 130 | return node; |
| 131 | } |
| 132 | |
| 133 | private: |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 134 | const ProfileTree* tree_; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace |
| 138 | |
| 139 | TEST(ProfileTreeAddPathFromStart) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 140 | CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, |
| 141 | TokenEnumerator::kNoSecurityToken); |
| 142 | CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, |
| 143 | TokenEnumerator::kNoSecurityToken); |
| 144 | CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, |
| 145 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 146 | ProfileTree tree; |
| 147 | ProfileTreeTestHelper helper(&tree); |
| 148 | CHECK_EQ(NULL, helper.Walk(&entry1)); |
| 149 | CHECK_EQ(NULL, helper.Walk(&entry2)); |
| 150 | CHECK_EQ(NULL, helper.Walk(&entry3)); |
| 151 | |
| 152 | CodeEntry* path[] = {NULL, &entry1, NULL, &entry2, NULL, NULL, &entry3, NULL}; |
| 153 | Vector<CodeEntry*> path_vec(path, sizeof(path) / sizeof(path[0])); |
| 154 | tree.AddPathFromStart(path_vec); |
| 155 | CHECK_EQ(NULL, helper.Walk(&entry2)); |
| 156 | CHECK_EQ(NULL, helper.Walk(&entry3)); |
| 157 | ProfileNode* node1 = helper.Walk(&entry1); |
| 158 | CHECK_NE(NULL, node1); |
| 159 | CHECK_EQ(0, node1->total_ticks()); |
| 160 | CHECK_EQ(0, node1->self_ticks()); |
| 161 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry1)); |
| 162 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry3)); |
| 163 | ProfileNode* node2 = helper.Walk(&entry1, &entry2); |
| 164 | CHECK_NE(NULL, node2); |
| 165 | CHECK_NE(node1, node2); |
| 166 | CHECK_EQ(0, node2->total_ticks()); |
| 167 | CHECK_EQ(0, node2->self_ticks()); |
| 168 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1)); |
| 169 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry2)); |
| 170 | ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3); |
| 171 | CHECK_NE(NULL, node3); |
| 172 | CHECK_NE(node1, node3); |
| 173 | CHECK_NE(node2, node3); |
| 174 | CHECK_EQ(0, node3->total_ticks()); |
| 175 | CHECK_EQ(1, node3->self_ticks()); |
| 176 | |
| 177 | tree.AddPathFromStart(path_vec); |
| 178 | CHECK_EQ(node1, helper.Walk(&entry1)); |
| 179 | CHECK_EQ(node2, helper.Walk(&entry1, &entry2)); |
| 180 | CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3)); |
| 181 | CHECK_EQ(0, node1->total_ticks()); |
| 182 | CHECK_EQ(0, node1->self_ticks()); |
| 183 | CHECK_EQ(0, node2->total_ticks()); |
| 184 | CHECK_EQ(0, node2->self_ticks()); |
| 185 | CHECK_EQ(0, node3->total_ticks()); |
| 186 | CHECK_EQ(2, node3->self_ticks()); |
| 187 | |
| 188 | CodeEntry* path2[] = {&entry1, &entry2, &entry2}; |
| 189 | Vector<CodeEntry*> path2_vec(path2, sizeof(path2) / sizeof(path2[0])); |
| 190 | tree.AddPathFromStart(path2_vec); |
| 191 | CHECK_EQ(NULL, helper.Walk(&entry2)); |
| 192 | CHECK_EQ(NULL, helper.Walk(&entry3)); |
| 193 | CHECK_EQ(node1, helper.Walk(&entry1)); |
| 194 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry1)); |
| 195 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry3)); |
| 196 | CHECK_EQ(node2, helper.Walk(&entry1, &entry2)); |
| 197 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1)); |
| 198 | CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3)); |
| 199 | CHECK_EQ(0, node3->total_ticks()); |
| 200 | CHECK_EQ(2, node3->self_ticks()); |
| 201 | ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2); |
| 202 | CHECK_NE(NULL, node4); |
| 203 | CHECK_NE(node3, node4); |
| 204 | CHECK_EQ(0, node4->total_ticks()); |
| 205 | CHECK_EQ(1, node4->self_ticks()); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | TEST(ProfileTreeAddPathFromEnd) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 210 | CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, |
| 211 | TokenEnumerator::kNoSecurityToken); |
| 212 | CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, |
| 213 | TokenEnumerator::kNoSecurityToken); |
| 214 | CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, |
| 215 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 216 | ProfileTree tree; |
| 217 | ProfileTreeTestHelper helper(&tree); |
| 218 | CHECK_EQ(NULL, helper.Walk(&entry1)); |
| 219 | CHECK_EQ(NULL, helper.Walk(&entry2)); |
| 220 | CHECK_EQ(NULL, helper.Walk(&entry3)); |
| 221 | |
| 222 | CodeEntry* path[] = {NULL, &entry3, NULL, &entry2, NULL, NULL, &entry1, NULL}; |
| 223 | Vector<CodeEntry*> path_vec(path, sizeof(path) / sizeof(path[0])); |
| 224 | tree.AddPathFromEnd(path_vec); |
| 225 | CHECK_EQ(NULL, helper.Walk(&entry2)); |
| 226 | CHECK_EQ(NULL, helper.Walk(&entry3)); |
| 227 | ProfileNode* node1 = helper.Walk(&entry1); |
| 228 | CHECK_NE(NULL, node1); |
| 229 | CHECK_EQ(0, node1->total_ticks()); |
| 230 | CHECK_EQ(0, node1->self_ticks()); |
| 231 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry1)); |
| 232 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry3)); |
| 233 | ProfileNode* node2 = helper.Walk(&entry1, &entry2); |
| 234 | CHECK_NE(NULL, node2); |
| 235 | CHECK_NE(node1, node2); |
| 236 | CHECK_EQ(0, node2->total_ticks()); |
| 237 | CHECK_EQ(0, node2->self_ticks()); |
| 238 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1)); |
| 239 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry2)); |
| 240 | ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3); |
| 241 | CHECK_NE(NULL, node3); |
| 242 | CHECK_NE(node1, node3); |
| 243 | CHECK_NE(node2, node3); |
| 244 | CHECK_EQ(0, node3->total_ticks()); |
| 245 | CHECK_EQ(1, node3->self_ticks()); |
| 246 | |
| 247 | tree.AddPathFromEnd(path_vec); |
| 248 | CHECK_EQ(node1, helper.Walk(&entry1)); |
| 249 | CHECK_EQ(node2, helper.Walk(&entry1, &entry2)); |
| 250 | CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3)); |
| 251 | CHECK_EQ(0, node1->total_ticks()); |
| 252 | CHECK_EQ(0, node1->self_ticks()); |
| 253 | CHECK_EQ(0, node2->total_ticks()); |
| 254 | CHECK_EQ(0, node2->self_ticks()); |
| 255 | CHECK_EQ(0, node3->total_ticks()); |
| 256 | CHECK_EQ(2, node3->self_ticks()); |
| 257 | |
| 258 | CodeEntry* path2[] = {&entry2, &entry2, &entry1}; |
| 259 | Vector<CodeEntry*> path2_vec(path2, sizeof(path2) / sizeof(path2[0])); |
| 260 | tree.AddPathFromEnd(path2_vec); |
| 261 | CHECK_EQ(NULL, helper.Walk(&entry2)); |
| 262 | CHECK_EQ(NULL, helper.Walk(&entry3)); |
| 263 | CHECK_EQ(node1, helper.Walk(&entry1)); |
| 264 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry1)); |
| 265 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry3)); |
| 266 | CHECK_EQ(node2, helper.Walk(&entry1, &entry2)); |
| 267 | CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1)); |
| 268 | CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3)); |
| 269 | CHECK_EQ(0, node3->total_ticks()); |
| 270 | CHECK_EQ(2, node3->self_ticks()); |
| 271 | ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2); |
| 272 | CHECK_NE(NULL, node4); |
| 273 | CHECK_NE(node3, node4); |
| 274 | CHECK_EQ(0, node4->total_ticks()); |
| 275 | CHECK_EQ(1, node4->self_ticks()); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | TEST(ProfileTreeCalculateTotalTicks) { |
| 280 | ProfileTree empty_tree; |
| 281 | CHECK_EQ(0, empty_tree.root()->total_ticks()); |
| 282 | CHECK_EQ(0, empty_tree.root()->self_ticks()); |
| 283 | empty_tree.CalculateTotalTicks(); |
| 284 | CHECK_EQ(0, empty_tree.root()->total_ticks()); |
| 285 | CHECK_EQ(0, empty_tree.root()->self_ticks()); |
| 286 | empty_tree.root()->IncrementSelfTicks(); |
| 287 | CHECK_EQ(0, empty_tree.root()->total_ticks()); |
| 288 | CHECK_EQ(1, empty_tree.root()->self_ticks()); |
| 289 | empty_tree.CalculateTotalTicks(); |
| 290 | CHECK_EQ(1, empty_tree.root()->total_ticks()); |
| 291 | CHECK_EQ(1, empty_tree.root()->self_ticks()); |
| 292 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 293 | CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, |
| 294 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 295 | CodeEntry* e1_path[] = {&entry1}; |
| 296 | Vector<CodeEntry*> e1_path_vec( |
| 297 | e1_path, sizeof(e1_path) / sizeof(e1_path[0])); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 298 | |
| 299 | ProfileTree single_child_tree; |
| 300 | single_child_tree.AddPathFromStart(e1_path_vec); |
| 301 | single_child_tree.root()->IncrementSelfTicks(); |
| 302 | CHECK_EQ(0, single_child_tree.root()->total_ticks()); |
| 303 | CHECK_EQ(1, single_child_tree.root()->self_ticks()); |
| 304 | ProfileTreeTestHelper single_child_helper(&single_child_tree); |
| 305 | ProfileNode* node1 = single_child_helper.Walk(&entry1); |
| 306 | CHECK_NE(NULL, node1); |
| 307 | CHECK_EQ(0, node1->total_ticks()); |
| 308 | CHECK_EQ(1, node1->self_ticks()); |
| 309 | single_child_tree.CalculateTotalTicks(); |
| 310 | CHECK_EQ(2, single_child_tree.root()->total_ticks()); |
| 311 | CHECK_EQ(1, single_child_tree.root()->self_ticks()); |
| 312 | CHECK_EQ(1, node1->total_ticks()); |
| 313 | CHECK_EQ(1, node1->self_ticks()); |
| 314 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 315 | CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, |
| 316 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 317 | CodeEntry* e1_e2_path[] = {&entry1, &entry2}; |
| 318 | Vector<CodeEntry*> e1_e2_path_vec( |
| 319 | e1_e2_path, sizeof(e1_e2_path) / sizeof(e1_e2_path[0])); |
| 320 | |
| 321 | ProfileTree flat_tree; |
| 322 | ProfileTreeTestHelper flat_helper(&flat_tree); |
| 323 | flat_tree.AddPathFromStart(e1_path_vec); |
| 324 | flat_tree.AddPathFromStart(e1_path_vec); |
| 325 | flat_tree.AddPathFromStart(e1_e2_path_vec); |
| 326 | flat_tree.AddPathFromStart(e1_e2_path_vec); |
| 327 | flat_tree.AddPathFromStart(e1_e2_path_vec); |
| 328 | // Results in {root,0,0} -> {entry1,0,2} -> {entry2,0,3} |
| 329 | CHECK_EQ(0, flat_tree.root()->total_ticks()); |
| 330 | CHECK_EQ(0, flat_tree.root()->self_ticks()); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 331 | node1 = flat_helper.Walk(&entry1); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 332 | CHECK_NE(NULL, node1); |
| 333 | CHECK_EQ(0, node1->total_ticks()); |
| 334 | CHECK_EQ(2, node1->self_ticks()); |
| 335 | ProfileNode* node2 = flat_helper.Walk(&entry1, &entry2); |
| 336 | CHECK_NE(NULL, node2); |
| 337 | CHECK_EQ(0, node2->total_ticks()); |
| 338 | CHECK_EQ(3, node2->self_ticks()); |
| 339 | flat_tree.CalculateTotalTicks(); |
| 340 | // Must calculate {root,5,0} -> {entry1,5,2} -> {entry2,3,3} |
| 341 | CHECK_EQ(5, flat_tree.root()->total_ticks()); |
| 342 | CHECK_EQ(0, flat_tree.root()->self_ticks()); |
| 343 | CHECK_EQ(5, node1->total_ticks()); |
| 344 | CHECK_EQ(2, node1->self_ticks()); |
| 345 | CHECK_EQ(3, node2->total_ticks()); |
| 346 | CHECK_EQ(3, node2->self_ticks()); |
| 347 | |
| 348 | CodeEntry* e2_path[] = {&entry2}; |
| 349 | Vector<CodeEntry*> e2_path_vec( |
| 350 | e2_path, sizeof(e2_path) / sizeof(e2_path[0])); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 351 | CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, |
| 352 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 353 | CodeEntry* e3_path[] = {&entry3}; |
| 354 | Vector<CodeEntry*> e3_path_vec( |
| 355 | e3_path, sizeof(e3_path) / sizeof(e3_path[0])); |
| 356 | |
| 357 | ProfileTree wide_tree; |
| 358 | ProfileTreeTestHelper wide_helper(&wide_tree); |
| 359 | wide_tree.AddPathFromStart(e1_path_vec); |
| 360 | wide_tree.AddPathFromStart(e1_path_vec); |
| 361 | wide_tree.AddPathFromStart(e1_e2_path_vec); |
| 362 | wide_tree.AddPathFromStart(e2_path_vec); |
| 363 | wide_tree.AddPathFromStart(e2_path_vec); |
| 364 | wide_tree.AddPathFromStart(e2_path_vec); |
| 365 | wide_tree.AddPathFromStart(e3_path_vec); |
| 366 | wide_tree.AddPathFromStart(e3_path_vec); |
| 367 | wide_tree.AddPathFromStart(e3_path_vec); |
| 368 | wide_tree.AddPathFromStart(e3_path_vec); |
| 369 | // Results in -> {entry1,0,2} -> {entry2,0,1} |
| 370 | // {root,0,0} -> {entry2,0,3} |
| 371 | // -> {entry3,0,4} |
| 372 | CHECK_EQ(0, wide_tree.root()->total_ticks()); |
| 373 | CHECK_EQ(0, wide_tree.root()->self_ticks()); |
| 374 | node1 = wide_helper.Walk(&entry1); |
| 375 | CHECK_NE(NULL, node1); |
| 376 | CHECK_EQ(0, node1->total_ticks()); |
| 377 | CHECK_EQ(2, node1->self_ticks()); |
| 378 | ProfileNode* node1_2 = wide_helper.Walk(&entry1, &entry2); |
| 379 | CHECK_NE(NULL, node1_2); |
| 380 | CHECK_EQ(0, node1_2->total_ticks()); |
| 381 | CHECK_EQ(1, node1_2->self_ticks()); |
| 382 | node2 = wide_helper.Walk(&entry2); |
| 383 | CHECK_NE(NULL, node2); |
| 384 | CHECK_EQ(0, node2->total_ticks()); |
| 385 | CHECK_EQ(3, node2->self_ticks()); |
| 386 | ProfileNode* node3 = wide_helper.Walk(&entry3); |
| 387 | CHECK_NE(NULL, node3); |
| 388 | CHECK_EQ(0, node3->total_ticks()); |
| 389 | CHECK_EQ(4, node3->self_ticks()); |
| 390 | wide_tree.CalculateTotalTicks(); |
| 391 | // Calculates -> {entry1,3,2} -> {entry2,1,1} |
| 392 | // {root,10,0} -> {entry2,3,3} |
| 393 | // -> {entry3,4,4} |
| 394 | CHECK_EQ(10, wide_tree.root()->total_ticks()); |
| 395 | CHECK_EQ(0, wide_tree.root()->self_ticks()); |
| 396 | CHECK_EQ(3, node1->total_ticks()); |
| 397 | CHECK_EQ(2, node1->self_ticks()); |
| 398 | CHECK_EQ(1, node1_2->total_ticks()); |
| 399 | CHECK_EQ(1, node1_2->self_ticks()); |
| 400 | CHECK_EQ(3, node2->total_ticks()); |
| 401 | CHECK_EQ(3, node2->self_ticks()); |
| 402 | CHECK_EQ(4, node3->total_ticks()); |
| 403 | CHECK_EQ(4, node3->self_ticks()); |
| 404 | } |
| 405 | |
| 406 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 407 | TEST(ProfileTreeFilteredClone) { |
| 408 | ProfileTree source_tree; |
| 409 | const int token0 = 0, token1 = 1, token2 = 2; |
| 410 | CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, token0); |
| 411 | CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, token1); |
| 412 | CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, token0); |
| 413 | CodeEntry entry4( |
| 414 | i::Logger::FUNCTION_TAG, "", "ddd", "", 0, |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 415 | TokenEnumerator::kInheritsSecurityToken); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 416 | |
| 417 | { |
| 418 | CodeEntry* e1_e2_path[] = {&entry1, &entry2}; |
| 419 | Vector<CodeEntry*> e1_e2_path_vec( |
| 420 | e1_e2_path, sizeof(e1_e2_path) / sizeof(e1_e2_path[0])); |
| 421 | source_tree.AddPathFromStart(e1_e2_path_vec); |
| 422 | CodeEntry* e2_e4_path[] = {&entry2, &entry4}; |
| 423 | Vector<CodeEntry*> e2_e4_path_vec( |
| 424 | e2_e4_path, sizeof(e2_e4_path) / sizeof(e2_e4_path[0])); |
| 425 | source_tree.AddPathFromStart(e2_e4_path_vec); |
| 426 | CodeEntry* e3_e1_path[] = {&entry3, &entry1}; |
| 427 | Vector<CodeEntry*> e3_e1_path_vec( |
| 428 | e3_e1_path, sizeof(e3_e1_path) / sizeof(e3_e1_path[0])); |
| 429 | source_tree.AddPathFromStart(e3_e1_path_vec); |
| 430 | CodeEntry* e3_e2_path[] = {&entry3, &entry2}; |
| 431 | Vector<CodeEntry*> e3_e2_path_vec( |
| 432 | e3_e2_path, sizeof(e3_e2_path) / sizeof(e3_e2_path[0])); |
| 433 | source_tree.AddPathFromStart(e3_e2_path_vec); |
| 434 | source_tree.CalculateTotalTicks(); |
| 435 | // Results in -> {entry1,0,1,0} -> {entry2,1,1,1} |
| 436 | // {root,0,4,-1} -> {entry2,0,1,1} -> {entry4,1,1,inherits} |
| 437 | // -> {entry3,0,2,0} -> {entry1,1,1,0} |
| 438 | // -> {entry2,1,1,1} |
| 439 | CHECK_EQ(4, source_tree.root()->total_ticks()); |
| 440 | CHECK_EQ(0, source_tree.root()->self_ticks()); |
| 441 | } |
| 442 | |
| 443 | { |
| 444 | ProfileTree token0_tree; |
| 445 | token0_tree.FilteredClone(&source_tree, token0); |
| 446 | // Should be -> {entry1,1,1,0} |
| 447 | // {root,1,4,-1} -> {entry3,1,2,0} -> {entry1,1,1,0} |
| 448 | // [self ticks from filtered nodes are attributed to their parents] |
| 449 | CHECK_EQ(4, token0_tree.root()->total_ticks()); |
| 450 | CHECK_EQ(1, token0_tree.root()->self_ticks()); |
| 451 | ProfileTreeTestHelper token0_helper(&token0_tree); |
| 452 | ProfileNode* node1 = token0_helper.Walk(&entry1); |
| 453 | CHECK_NE(NULL, node1); |
| 454 | CHECK_EQ(1, node1->total_ticks()); |
| 455 | CHECK_EQ(1, node1->self_ticks()); |
| 456 | CHECK_EQ(NULL, token0_helper.Walk(&entry2)); |
| 457 | ProfileNode* node3 = token0_helper.Walk(&entry3); |
| 458 | CHECK_NE(NULL, node3); |
| 459 | CHECK_EQ(2, node3->total_ticks()); |
| 460 | CHECK_EQ(1, node3->self_ticks()); |
| 461 | ProfileNode* node3_1 = token0_helper.Walk(&entry3, &entry1); |
| 462 | CHECK_NE(NULL, node3_1); |
| 463 | CHECK_EQ(1, node3_1->total_ticks()); |
| 464 | CHECK_EQ(1, node3_1->self_ticks()); |
| 465 | CHECK_EQ(NULL, token0_helper.Walk(&entry3, &entry2)); |
| 466 | } |
| 467 | |
| 468 | { |
| 469 | ProfileTree token1_tree; |
| 470 | token1_tree.FilteredClone(&source_tree, token1); |
| 471 | // Should be |
| 472 | // {root,1,4,-1} -> {entry2,2,3,1} -> {entry4,1,1,inherits} |
| 473 | // [child nodes referring to the same entry get merged and |
| 474 | // their self times summed up] |
| 475 | CHECK_EQ(4, token1_tree.root()->total_ticks()); |
| 476 | CHECK_EQ(1, token1_tree.root()->self_ticks()); |
| 477 | ProfileTreeTestHelper token1_helper(&token1_tree); |
| 478 | CHECK_EQ(NULL, token1_helper.Walk(&entry1)); |
| 479 | CHECK_EQ(NULL, token1_helper.Walk(&entry3)); |
| 480 | ProfileNode* node2 = token1_helper.Walk(&entry2); |
| 481 | CHECK_NE(NULL, node2); |
| 482 | CHECK_EQ(3, node2->total_ticks()); |
| 483 | CHECK_EQ(2, node2->self_ticks()); |
| 484 | ProfileNode* node2_4 = token1_helper.Walk(&entry2, &entry4); |
| 485 | CHECK_NE(NULL, node2_4); |
| 486 | CHECK_EQ(1, node2_4->total_ticks()); |
| 487 | CHECK_EQ(1, node2_4->self_ticks()); |
| 488 | } |
| 489 | |
| 490 | { |
| 491 | ProfileTree token2_tree; |
| 492 | token2_tree.FilteredClone(&source_tree, token2); |
| 493 | // Should be |
| 494 | // {root,4,4,-1} |
| 495 | // [no nodes, all ticks get migrated into root node] |
| 496 | CHECK_EQ(4, token2_tree.root()->total_ticks()); |
| 497 | CHECK_EQ(4, token2_tree.root()->self_ticks()); |
| 498 | ProfileTreeTestHelper token2_helper(&token2_tree); |
| 499 | CHECK_EQ(NULL, token2_helper.Walk(&entry1)); |
| 500 | CHECK_EQ(NULL, token2_helper.Walk(&entry2)); |
| 501 | CHECK_EQ(NULL, token2_helper.Walk(&entry3)); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 506 | static inline i::Address ToAddress(int n) { |
| 507 | return reinterpret_cast<i::Address>(n); |
| 508 | } |
| 509 | |
| 510 | TEST(CodeMapAddCode) { |
| 511 | CodeMap code_map; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 512 | CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, |
| 513 | TokenEnumerator::kNoSecurityToken); |
| 514 | CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, |
| 515 | TokenEnumerator::kNoSecurityToken); |
| 516 | CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, |
| 517 | TokenEnumerator::kNoSecurityToken); |
| 518 | CodeEntry entry4(i::Logger::FUNCTION_TAG, "", "ddd", "", 0, |
| 519 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 520 | code_map.AddCode(ToAddress(0x1500), &entry1, 0x200); |
| 521 | code_map.AddCode(ToAddress(0x1700), &entry2, 0x100); |
| 522 | code_map.AddCode(ToAddress(0x1900), &entry3, 0x50); |
| 523 | code_map.AddCode(ToAddress(0x1950), &entry4, 0x10); |
| 524 | CHECK_EQ(NULL, code_map.FindEntry(0)); |
| 525 | CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1500 - 1))); |
| 526 | CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500))); |
| 527 | CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x100))); |
| 528 | CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x200 - 1))); |
| 529 | CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700))); |
| 530 | CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x50))); |
| 531 | CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x100 - 1))); |
| 532 | CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1700 + 0x100))); |
| 533 | CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1900 - 1))); |
| 534 | CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900))); |
| 535 | CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900 + 0x28))); |
| 536 | CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950))); |
| 537 | CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x7))); |
| 538 | CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x10 - 1))); |
| 539 | CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1950 + 0x10))); |
| 540 | CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0xFFFFFFFF))); |
| 541 | } |
| 542 | |
| 543 | |
| 544 | TEST(CodeMapMoveAndDeleteCode) { |
| 545 | CodeMap code_map; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 546 | CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, |
| 547 | TokenEnumerator::kNoSecurityToken); |
| 548 | CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, |
| 549 | TokenEnumerator::kNoSecurityToken); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 550 | code_map.AddCode(ToAddress(0x1500), &entry1, 0x200); |
| 551 | code_map.AddCode(ToAddress(0x1700), &entry2, 0x100); |
| 552 | CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500))); |
| 553 | CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700))); |
| 554 | code_map.MoveCode(ToAddress(0x1500), ToAddress(0x1800)); |
| 555 | CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1500))); |
| 556 | CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700))); |
| 557 | CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1800))); |
| 558 | code_map.DeleteCode(ToAddress(0x1700)); |
| 559 | CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1700))); |
| 560 | CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1800))); |
| 561 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 562 | |
| 563 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 564 | namespace { |
| 565 | |
| 566 | class TestSetup { |
| 567 | public: |
| 568 | TestSetup() |
| 569 | : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) { |
| 570 | i::FLAG_prof_browser_mode = false; |
| 571 | } |
| 572 | |
| 573 | ~TestSetup() { |
| 574 | i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_; |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | bool old_flag_prof_browser_mode_; |
| 579 | }; |
| 580 | |
| 581 | } // namespace |
| 582 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 583 | TEST(RecordTickSample) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 584 | TestSetup test_setup; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 585 | CpuProfilesCollection profiles; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 586 | profiles.StartProfiling("", 1); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 587 | ProfileGenerator generator(&profiles); |
| 588 | CodeEntry* entry1 = generator.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa"); |
| 589 | CodeEntry* entry2 = generator.NewCodeEntry(i::Logger::FUNCTION_TAG, "bbb"); |
| 590 | CodeEntry* entry3 = generator.NewCodeEntry(i::Logger::FUNCTION_TAG, "ccc"); |
| 591 | generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200); |
| 592 | generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100); |
| 593 | generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50); |
| 594 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 595 | // We are building the following calls tree: |
| 596 | // -> aaa - sample1 |
| 597 | // aaa -> bbb -> ccc - sample2 |
| 598 | // -> ccc -> aaa - sample3 |
| 599 | TickSample sample1; |
| 600 | sample1.pc = ToAddress(0x1600); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 601 | sample1.tos = ToAddress(0x1500); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 602 | sample1.stack[0] = ToAddress(0x1510); |
| 603 | sample1.frames_count = 1; |
| 604 | generator.RecordTickSample(sample1); |
| 605 | TickSample sample2; |
| 606 | sample2.pc = ToAddress(0x1925); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 607 | sample2.tos = ToAddress(0x1900); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 608 | sample2.stack[0] = ToAddress(0x1780); |
| 609 | sample2.stack[1] = ToAddress(0x10000); // non-existent. |
| 610 | sample2.stack[2] = ToAddress(0x1620); |
| 611 | sample2.frames_count = 3; |
| 612 | generator.RecordTickSample(sample2); |
| 613 | TickSample sample3; |
| 614 | sample3.pc = ToAddress(0x1510); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 615 | sample3.tos = ToAddress(0x1500); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 616 | sample3.stack[0] = ToAddress(0x1910); |
| 617 | sample3.stack[1] = ToAddress(0x1610); |
| 618 | sample3.frames_count = 2; |
| 619 | generator.RecordTickSample(sample3); |
| 620 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 621 | CpuProfile* profile = |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 622 | profiles.StopProfiling(TokenEnumerator::kNoSecurityToken, "", 1); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 623 | CHECK_NE(NULL, profile); |
| 624 | ProfileTreeTestHelper top_down_test_helper(profile->top_down()); |
| 625 | CHECK_EQ(NULL, top_down_test_helper.Walk(entry2)); |
| 626 | CHECK_EQ(NULL, top_down_test_helper.Walk(entry3)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 627 | ProfileNode* node1 = top_down_test_helper.Walk(entry1); |
| 628 | CHECK_NE(NULL, node1); |
| 629 | CHECK_EQ(entry1, node1->entry()); |
| 630 | ProfileNode* node2 = top_down_test_helper.Walk(entry1, entry1); |
| 631 | CHECK_NE(NULL, node2); |
| 632 | CHECK_EQ(entry1, node2->entry()); |
| 633 | ProfileNode* node3 = top_down_test_helper.Walk(entry1, entry2, entry3); |
| 634 | CHECK_NE(NULL, node3); |
| 635 | CHECK_EQ(entry3, node3->entry()); |
| 636 | ProfileNode* node4 = top_down_test_helper.Walk(entry1, entry3, entry1); |
| 637 | CHECK_NE(NULL, node4); |
| 638 | CHECK_EQ(entry1, node4->entry()); |
| 639 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 640 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 641 | |
| 642 | TEST(SampleRateCalculator) { |
| 643 | const double kSamplingIntervalMs = i::Logger::kSamplingIntervalMs; |
| 644 | |
| 645 | // Verify that ticking exactly in query intervals results in the |
| 646 | // initial sampling interval. |
| 647 | double time = 0.0; |
| 648 | SampleRateCalculator calc1; |
| 649 | CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms()); |
| 650 | calc1.UpdateMeasurements(time); |
| 651 | CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms()); |
| 652 | time += SampleRateCalculator::kWallTimeQueryIntervalMs; |
| 653 | calc1.UpdateMeasurements(time); |
| 654 | CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms()); |
| 655 | time += SampleRateCalculator::kWallTimeQueryIntervalMs; |
| 656 | calc1.UpdateMeasurements(time); |
| 657 | CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms()); |
| 658 | time += SampleRateCalculator::kWallTimeQueryIntervalMs; |
| 659 | calc1.UpdateMeasurements(time); |
| 660 | CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms()); |
| 661 | |
| 662 | SampleRateCalculator calc2; |
| 663 | time = 0.0; |
| 664 | CHECK_EQ(kSamplingIntervalMs, calc2.ticks_per_ms()); |
| 665 | calc2.UpdateMeasurements(time); |
| 666 | CHECK_EQ(kSamplingIntervalMs, calc2.ticks_per_ms()); |
| 667 | time += SampleRateCalculator::kWallTimeQueryIntervalMs * 0.5; |
| 668 | calc2.UpdateMeasurements(time); |
| 669 | // (1.0 + 2.0) / 2 |
| 670 | CHECK_EQ(kSamplingIntervalMs * 1.5, calc2.ticks_per_ms()); |
| 671 | time += SampleRateCalculator::kWallTimeQueryIntervalMs * 0.75; |
| 672 | calc2.UpdateMeasurements(time); |
| 673 | // (1.0 + 2.0 + 2.0) / 3 |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 674 | CHECK_EQ(kSamplingIntervalMs * 5.0, floor(calc2.ticks_per_ms() * 3.0 + 0.5)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 675 | |
| 676 | SampleRateCalculator calc3; |
| 677 | time = 0.0; |
| 678 | CHECK_EQ(kSamplingIntervalMs, calc3.ticks_per_ms()); |
| 679 | calc3.UpdateMeasurements(time); |
| 680 | CHECK_EQ(kSamplingIntervalMs, calc3.ticks_per_ms()); |
| 681 | time += SampleRateCalculator::kWallTimeQueryIntervalMs * 2; |
| 682 | calc3.UpdateMeasurements(time); |
| 683 | // (1.0 + 0.5) / 2 |
| 684 | CHECK_EQ(kSamplingIntervalMs * 0.75, calc3.ticks_per_ms()); |
| 685 | time += SampleRateCalculator::kWallTimeQueryIntervalMs * 1.5; |
| 686 | calc3.UpdateMeasurements(time); |
| 687 | // (1.0 + 0.5 + 0.5) / 3 |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 688 | CHECK_EQ(kSamplingIntervalMs * 2.0, floor(calc3.ticks_per_ms() * 3.0 + 0.5)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 689 | } |
| 690 | |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 691 | |
| 692 | // --- P r o f i l e r E x t e n s i o n --- |
| 693 | |
| 694 | class ProfilerExtension : public v8::Extension { |
| 695 | public: |
| 696 | ProfilerExtension() : v8::Extension("v8/profiler", kSource) { } |
| 697 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 698 | v8::Handle<v8::String> name); |
| 699 | static v8::Handle<v8::Value> StartProfiling(const v8::Arguments& args); |
| 700 | static v8::Handle<v8::Value> StopProfiling(const v8::Arguments& args); |
| 701 | private: |
| 702 | static const char* kSource; |
| 703 | }; |
| 704 | |
| 705 | |
| 706 | const char* ProfilerExtension::kSource = |
| 707 | "native function startProfiling();" |
| 708 | "native function stopProfiling();"; |
| 709 | |
| 710 | v8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunction( |
| 711 | v8::Handle<v8::String> name) { |
| 712 | if (name->Equals(v8::String::New("startProfiling"))) { |
| 713 | return v8::FunctionTemplate::New(ProfilerExtension::StartProfiling); |
| 714 | } else if (name->Equals(v8::String::New("stopProfiling"))) { |
| 715 | return v8::FunctionTemplate::New(ProfilerExtension::StopProfiling); |
| 716 | } else { |
| 717 | CHECK(false); |
| 718 | return v8::Handle<v8::FunctionTemplate>(); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | |
| 723 | v8::Handle<v8::Value> ProfilerExtension::StartProfiling( |
| 724 | const v8::Arguments& args) { |
| 725 | if (args.Length() > 0) |
| 726 | v8::CpuProfiler::StartProfiling(args[0].As<v8::String>()); |
| 727 | else |
| 728 | v8::CpuProfiler::StartProfiling(v8::String::New("")); |
| 729 | return v8::Undefined(); |
| 730 | } |
| 731 | |
| 732 | |
| 733 | v8::Handle<v8::Value> ProfilerExtension::StopProfiling( |
| 734 | const v8::Arguments& args) { |
| 735 | if (args.Length() > 0) |
| 736 | v8::CpuProfiler::StopProfiling(args[0].As<v8::String>()); |
| 737 | else |
| 738 | v8::CpuProfiler::StopProfiling(v8::String::New("")); |
| 739 | return v8::Undefined(); |
| 740 | } |
| 741 | |
| 742 | |
| 743 | static ProfilerExtension kProfilerExtension; |
| 744 | v8::DeclareExtension kProfilerExtensionDeclaration(&kProfilerExtension); |
| 745 | static v8::Persistent<v8::Context> env; |
| 746 | |
| 747 | static const ProfileNode* PickChild(const ProfileNode* parent, |
| 748 | const char* name) { |
| 749 | for (int i = 0; i < parent->children()->length(); ++i) { |
| 750 | const ProfileNode* child = parent->children()->at(i); |
| 751 | if (strcmp(child->entry()->name(), name) == 0) return child; |
| 752 | } |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | |
| 757 | TEST(RecordStackTraceAtStartProfiling) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 758 | // This test does not pass with inlining enabled since inlined functions |
| 759 | // don't appear in the stack trace. |
| 760 | i::FLAG_use_inlining = false; |
| 761 | |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 762 | if (env.IsEmpty()) { |
| 763 | v8::HandleScope scope; |
| 764 | const char* extensions[] = { "v8/profiler" }; |
| 765 | v8::ExtensionConfiguration config(1, extensions); |
| 766 | env = v8::Context::New(&config); |
| 767 | } |
| 768 | v8::HandleScope scope; |
| 769 | env->Enter(); |
| 770 | |
| 771 | CHECK_EQ(0, CpuProfiler::GetProfilesCount()); |
| 772 | CompileRun( |
| 773 | "function c() { startProfiling(); }\n" |
| 774 | "function b() { c(); }\n" |
| 775 | "function a() { b(); }\n" |
| 776 | "a();\n" |
| 777 | "stopProfiling();"); |
| 778 | CHECK_EQ(1, CpuProfiler::GetProfilesCount()); |
| 779 | CpuProfile* profile = |
| 780 | CpuProfiler::GetProfile(NULL, 0); |
| 781 | const ProfileTree* topDown = profile->top_down(); |
| 782 | const ProfileNode* current = topDown->root(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 783 | const_cast<ProfileNode*>(current)->Print(0); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 784 | // The tree should look like this: |
| 785 | // (root) |
| 786 | // (anonymous function) |
| 787 | // a |
| 788 | // b |
| 789 | // c |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 790 | // There can also be: |
| 791 | // startProfiling |
| 792 | // if the sampler managed to get a tick. |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 793 | current = PickChild(current, "(anonymous function)"); |
| 794 | CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
| 795 | current = PickChild(current, "a"); |
| 796 | CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
| 797 | current = PickChild(current, "b"); |
| 798 | CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
| 799 | current = PickChild(current, "c"); |
| 800 | CHECK_NE(NULL, const_cast<ProfileNode*>(current)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 801 | CHECK(current->children()->length() == 0 || |
| 802 | current->children()->length() == 1); |
| 803 | if (current->children()->length() == 1) { |
| 804 | current = PickChild(current, "startProfiling"); |
| 805 | CHECK_EQ(0, current->children()->length()); |
| 806 | } |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 807 | } |
| 808 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 809 | |
| 810 | TEST(Issue51919) { |
| 811 | CpuProfilesCollection collection; |
| 812 | i::EmbeddedVector<char*, |
| 813 | CpuProfilesCollection::kMaxSimultaneousProfiles> titles; |
| 814 | for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) { |
| 815 | i::Vector<char> title = i::Vector<char>::New(16); |
| 816 | i::OS::SNPrintF(title, "%d", i); |
| 817 | CHECK(collection.StartProfiling(title.start(), i + 1)); // UID must be > 0. |
| 818 | titles[i] = title.start(); |
| 819 | } |
| 820 | CHECK(!collection.StartProfiling( |
| 821 | "maximum", CpuProfilesCollection::kMaxSimultaneousProfiles + 1)); |
| 822 | for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) |
| 823 | i::DeleteArray(titles[i]); |
| 824 | } |