OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // | 2 // |
3 // Tests for heap profiler | 3 // Tests for heap profiler |
4 | 4 |
5 #ifdef ENABLE_LOGGING_AND_PROFILING | 5 #ifdef ENABLE_LOGGING_AND_PROFILING |
6 | 6 |
7 #include "v8.h" | 7 #include "v8.h" |
8 #include "heap-profiler.h" | 8 #include "heap-profiler.h" |
9 #include "snapshot.h" | 9 #include "snapshot.h" |
10 #include "string-stream.h" | 10 #include "string-stream.h" |
(...skipping 1240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1251 const v8::HeapSnapshot* snapshot = | 1251 const v8::HeapSnapshot* snapshot = |
1252 v8::HeapProfiler::TakeSnapshot(v8::String::New("full"), | 1252 v8::HeapProfiler::TakeSnapshot(v8::String::New("full"), |
1253 v8::HeapSnapshot::kFull, | 1253 v8::HeapSnapshot::kFull, |
1254 &control); | 1254 &control); |
1255 CHECK_NE(NULL, snapshot); | 1255 CHECK_NE(NULL, snapshot); |
1256 CHECK_EQ(snapshots_count + 1, v8::HeapProfiler::GetSnapshotsCount()); | 1256 CHECK_EQ(snapshots_count + 1, v8::HeapProfiler::GetSnapshotsCount()); |
1257 CHECK_EQ(control.total(), control.done()); | 1257 CHECK_EQ(control.total(), control.done()); |
1258 CHECK_GT(control.total(), 0); | 1258 CHECK_GT(control.total(), 0); |
1259 } | 1259 } |
1260 | 1260 |
| 1261 |
| 1262 namespace { |
| 1263 |
| 1264 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { |
| 1265 public: |
| 1266 TestRetainedObjectInfo(int hash, |
| 1267 const char* label, |
| 1268 intptr_t element_count = -1, |
| 1269 intptr_t size = -1) |
| 1270 : disposed_(false), |
| 1271 hash_(hash), |
| 1272 label_(label), |
| 1273 element_count_(element_count), |
| 1274 size_(size) { |
| 1275 instances.Add(this); |
| 1276 } |
| 1277 virtual ~TestRetainedObjectInfo() {} |
| 1278 virtual void Dispose() { |
| 1279 CHECK(!disposed_); |
| 1280 disposed_ = true; |
| 1281 } |
| 1282 virtual bool IsEquivalent(RetainedObjectInfo* other) { |
| 1283 return GetHash() == other->GetHash(); |
| 1284 } |
| 1285 virtual intptr_t GetHash() { return hash_; } |
| 1286 virtual const char* GetLabel() { return label_; } |
| 1287 virtual intptr_t GetElementCount() { return element_count_; } |
| 1288 virtual intptr_t GetSizeInBytes() { return size_; } |
| 1289 bool disposed() { return disposed_; } |
| 1290 |
| 1291 static v8::RetainedObjectInfo* WrapperInfoCallback( |
| 1292 uint16_t class_id, v8::Handle<v8::Value> wrapper) { |
| 1293 if (class_id == 1) { |
| 1294 if (wrapper->IsString()) { |
| 1295 v8::String::AsciiValue ascii(wrapper); |
| 1296 if (strcmp(*ascii, "AAA") == 0) |
| 1297 return new TestRetainedObjectInfo(1, "aaa", 100); |
| 1298 else if (strcmp(*ascii, "BBB") == 0) |
| 1299 return new TestRetainedObjectInfo(1, "aaa", 100); |
| 1300 } |
| 1301 } else if (class_id == 2) { |
| 1302 if (wrapper->IsString()) { |
| 1303 v8::String::AsciiValue ascii(wrapper); |
| 1304 if (strcmp(*ascii, "CCC") == 0) |
| 1305 return new TestRetainedObjectInfo(2, "ccc"); |
| 1306 } |
| 1307 } |
| 1308 CHECK(false); |
| 1309 return NULL; |
| 1310 } |
| 1311 |
| 1312 static i::List<TestRetainedObjectInfo*> instances; |
| 1313 |
| 1314 private: |
| 1315 bool disposed_; |
| 1316 int category_; |
| 1317 int hash_; |
| 1318 const char* label_; |
| 1319 intptr_t element_count_; |
| 1320 intptr_t size_; |
| 1321 }; |
| 1322 |
| 1323 |
| 1324 i::List<TestRetainedObjectInfo*> TestRetainedObjectInfo::instances; |
| 1325 } |
| 1326 |
| 1327 |
| 1328 static const v8::HeapGraphNode* GetNode(const v8::HeapGraphNode* parent, |
| 1329 v8::HeapGraphNode::Type type, |
| 1330 const char* name) { |
| 1331 for (int i = 0, count = parent->GetChildrenCount(); i < count; ++i) { |
| 1332 const v8::HeapGraphNode* node = parent->GetChild(i)->GetToNode(); |
| 1333 if (node->GetType() == type && strcmp(name, |
| 1334 const_cast<i::HeapEntry*>( |
| 1335 reinterpret_cast<const i::HeapEntry*>(node))->name()) == 0) { |
| 1336 return node; |
| 1337 } |
| 1338 } |
| 1339 return NULL; |
| 1340 } |
| 1341 |
| 1342 |
| 1343 TEST(HeapSnapshotRetainedObjectInfo) { |
| 1344 v8::HandleScope scope; |
| 1345 LocalContext env; |
| 1346 |
| 1347 v8::HeapProfiler::DefineWrapperClass( |
| 1348 1, TestRetainedObjectInfo::WrapperInfoCallback); |
| 1349 v8::HeapProfiler::DefineWrapperClass( |
| 1350 2, TestRetainedObjectInfo::WrapperInfoCallback); |
| 1351 v8::Persistent<v8::String> p_AAA = |
| 1352 v8::Persistent<v8::String>::New(v8_str("AAA")); |
| 1353 p_AAA.SetWrapperClassId(1); |
| 1354 v8::Persistent<v8::String> p_BBB = |
| 1355 v8::Persistent<v8::String>::New(v8_str("BBB")); |
| 1356 p_BBB.SetWrapperClassId(1); |
| 1357 v8::Persistent<v8::String> p_CCC = |
| 1358 v8::Persistent<v8::String>::New(v8_str("CCC")); |
| 1359 p_CCC.SetWrapperClassId(2); |
| 1360 CHECK_EQ(0, TestRetainedObjectInfo::instances.length()); |
| 1361 const v8::HeapSnapshot* snapshot = |
| 1362 v8::HeapProfiler::TakeSnapshot(v8::String::New("retained")); |
| 1363 |
| 1364 CHECK_EQ(3, TestRetainedObjectInfo::instances.length()); |
| 1365 for (int i = 0; i < TestRetainedObjectInfo::instances.length(); ++i) { |
| 1366 CHECK(TestRetainedObjectInfo::instances[i]->disposed()); |
| 1367 delete TestRetainedObjectInfo::instances[i]; |
| 1368 } |
| 1369 |
| 1370 const v8::HeapGraphNode* natives = GetNode( |
| 1371 snapshot->GetRoot(), v8::HeapGraphNode::kObject, "(Native objects)"); |
| 1372 CHECK_NE(NULL, natives); |
| 1373 CHECK_EQ(2, natives->GetChildrenCount()); |
| 1374 const v8::HeapGraphNode* aaa = GetNode( |
| 1375 natives, v8::HeapGraphNode::kNative, "aaa / 100 entries"); |
| 1376 CHECK_NE(NULL, aaa); |
| 1377 const v8::HeapGraphNode* ccc = GetNode( |
| 1378 natives, v8::HeapGraphNode::kNative, "ccc"); |
| 1379 CHECK_NE(NULL, ccc); |
| 1380 |
| 1381 CHECK_EQ(2, aaa->GetChildrenCount()); |
| 1382 const v8::HeapGraphNode* n_AAA = GetNode( |
| 1383 aaa, v8::HeapGraphNode::kString, "AAA"); |
| 1384 CHECK_NE(NULL, n_AAA); |
| 1385 const v8::HeapGraphNode* n_BBB = GetNode( |
| 1386 aaa, v8::HeapGraphNode::kString, "BBB"); |
| 1387 CHECK_NE(NULL, n_BBB); |
| 1388 CHECK_EQ(1, ccc->GetChildrenCount()); |
| 1389 const v8::HeapGraphNode* n_CCC = GetNode( |
| 1390 ccc, v8::HeapGraphNode::kString, "CCC"); |
| 1391 CHECK_NE(NULL, n_CCC); |
| 1392 |
| 1393 CHECK_EQ(aaa, GetProperty(n_AAA, v8::HeapGraphEdge::kInternal, "Native")); |
| 1394 CHECK_EQ(aaa, GetProperty(n_BBB, v8::HeapGraphEdge::kInternal, "Native")); |
| 1395 CHECK_EQ(ccc, GetProperty(n_CCC, v8::HeapGraphEdge::kInternal, "Native")); |
| 1396 } |
| 1397 |
1261 #endif // ENABLE_LOGGING_AND_PROFILING | 1398 #endif // ENABLE_LOGGING_AND_PROFILING |
OLD | NEW |