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); | |
Vitaly Repeshko
2011/03/09 14:15:49
"bbb"?
mnaganov (inactive)
2011/03/09 15:26:32
No. I'm intentionally returning an equivalent obje
| |
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 | |
Søren Thygesen Gjesse
2011/03/09 11:38:47
Missing empty line.
mnaganov (inactive)
2011/03/09 15:26:32
Done.
| |
1323 i::List<TestRetainedObjectInfo*> TestRetainedObjectInfo::instances; | |
1324 } | |
1325 | |
1326 | |
1327 static const v8::HeapGraphNode* GetNode(const v8::HeapGraphNode* parent, | |
1328 v8::HeapGraphNode::Type type, | |
1329 const char* name) { | |
1330 for (int i = 0, count = parent->GetChildrenCount(); i < count; ++i) { | |
1331 const v8::HeapGraphNode* node = parent->GetChild(i)->GetToNode(); | |
1332 if (node->GetType() == type && strcmp(name, | |
1333 const_cast<i::HeapEntry*>( | |
1334 reinterpret_cast<const i::HeapEntry*>(node))->name()) == 0) { | |
1335 return node; | |
1336 } | |
1337 } | |
1338 return NULL; | |
1339 } | |
1340 | |
1341 | |
1342 TEST(HeapSnapshotRetainedObjectInfo) { | |
1343 v8::HandleScope scope; | |
1344 LocalContext env; | |
1345 | |
1346 v8::HeapProfiler::DefineWrapperClass( | |
1347 1, TestRetainedObjectInfo::WrapperInfoCallback); | |
1348 v8::HeapProfiler::DefineWrapperClass( | |
1349 2, TestRetainedObjectInfo::WrapperInfoCallback); | |
1350 v8::Persistent<v8::String> p_AAA = | |
1351 v8::Persistent<v8::String>::New(v8_str("AAA")); | |
1352 p_AAA.SetWrapperClassId(1); | |
1353 v8::Persistent<v8::String> p_BBB = | |
1354 v8::Persistent<v8::String>::New(v8_str("BBB")); | |
1355 p_BBB.SetWrapperClassId(1); | |
1356 v8::Persistent<v8::String> p_CCC = | |
1357 v8::Persistent<v8::String>::New(v8_str("CCC")); | |
1358 p_CCC.SetWrapperClassId(2); | |
1359 CHECK_EQ(0, TestRetainedObjectInfo::instances.length()); | |
1360 const v8::HeapSnapshot* snapshot = | |
1361 v8::HeapProfiler::TakeSnapshot(v8::String::New("retained")); | |
1362 | |
1363 CHECK_EQ(3, TestRetainedObjectInfo::instances.length()); | |
1364 for (int i = 0; i < TestRetainedObjectInfo::instances.length(); ++i) { | |
1365 CHECK(TestRetainedObjectInfo::instances[i]->disposed()); | |
1366 delete TestRetainedObjectInfo::instances[i]; | |
1367 } | |
1368 | |
1369 const v8::HeapGraphNode* natives = GetNode( | |
1370 snapshot->GetRoot(), v8::HeapGraphNode::kObject, "(Native objects)"); | |
1371 CHECK_NE(NULL, natives); | |
1372 CHECK_EQ(2, natives->GetChildrenCount()); | |
1373 const v8::HeapGraphNode* aaa = GetNode( | |
1374 natives, v8::HeapGraphNode::kNative, "aaa / 100 entries"); | |
1375 CHECK_NE(NULL, aaa); | |
1376 const v8::HeapGraphNode* ccc = GetNode( | |
1377 natives, v8::HeapGraphNode::kNative, "ccc"); | |
1378 CHECK_NE(NULL, ccc); | |
1379 | |
1380 CHECK_EQ(2, aaa->GetChildrenCount()); | |
1381 const v8::HeapGraphNode* n_AAA = GetNode( | |
1382 aaa, v8::HeapGraphNode::kString, "AAA"); | |
1383 CHECK_NE(NULL, n_AAA); | |
1384 const v8::HeapGraphNode* n_BBB = GetNode( | |
1385 aaa, v8::HeapGraphNode::kString, "BBB"); | |
1386 CHECK_NE(NULL, n_BBB); | |
1387 CHECK_EQ(1, ccc->GetChildrenCount()); | |
1388 const v8::HeapGraphNode* n_CCC = GetNode( | |
1389 ccc, v8::HeapGraphNode::kString, "CCC"); | |
1390 CHECK_NE(NULL, n_CCC); | |
1391 | |
1392 CHECK_EQ(aaa, GetProperty(n_AAA, v8::HeapGraphEdge::kInternal, "Native")); | |
1393 CHECK_EQ(aaa, GetProperty(n_BBB, v8::HeapGraphEdge::kInternal, "Native")); | |
1394 CHECK_EQ(ccc, GetProperty(n_CCC, v8::HeapGraphEdge::kInternal, "Native")); | |
1395 } | |
1396 | |
1261 #endif // ENABLE_LOGGING_AND_PROFILING | 1397 #endif // ENABLE_LOGGING_AND_PROFILING |
OLD | NEW |