| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/object_graph.h" | 5 #include "vm/object_graph.h" |
| 6 | 6 |
| 7 #include "vm/dart.h" | 7 #include "vm/dart.h" |
| 8 #include "vm/growable_array.h" | 8 #include "vm/growable_array.h" |
| 9 #include "vm/isolate.h" | 9 #include "vm/isolate.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 198 |
| 199 ObjectGraph::ObjectGraph(Thread* thread) : StackResource(thread) { | 199 ObjectGraph::ObjectGraph(Thread* thread) : StackResource(thread) { |
| 200 // The VM isolate has all its objects pre-marked, so iterating over it | 200 // The VM isolate has all its objects pre-marked, so iterating over it |
| 201 // would be a no-op. | 201 // would be a no-op. |
| 202 ASSERT(thread->isolate() != Dart::vm_isolate()); | 202 ASSERT(thread->isolate() != Dart::vm_isolate()); |
| 203 } | 203 } |
| 204 | 204 |
| 205 ObjectGraph::~ObjectGraph() {} | 205 ObjectGraph::~ObjectGraph() {} |
| 206 | 206 |
| 207 void ObjectGraph::IterateObjects(ObjectGraph::Visitor* visitor) { | 207 void ObjectGraph::IterateObjects(ObjectGraph::Visitor* visitor) { |
| 208 NoSafepointScope no_safepoint_scope_; | |
| 209 Stack stack(isolate()); | 208 Stack stack(isolate()); |
| 210 isolate()->VisitObjectPointers(&stack, false); | 209 isolate()->VisitObjectPointers(&stack, false); |
| 211 stack.TraverseGraph(visitor); | 210 stack.TraverseGraph(visitor); |
| 212 Unmarker::UnmarkAll(isolate()); | 211 Unmarker::UnmarkAll(isolate()); |
| 213 } | 212 } |
| 214 | 213 |
| 215 void ObjectGraph::IterateUserObjects(ObjectGraph::Visitor* visitor) { | 214 void ObjectGraph::IterateUserObjects(ObjectGraph::Visitor* visitor) { |
| 216 NoSafepointScope no_safepoint_scope_; | |
| 217 Stack stack(isolate()); | 215 Stack stack(isolate()); |
| 218 IterateUserFields(&stack); | 216 IterateUserFields(&stack); |
| 219 stack.include_vm_objects_ = false; | 217 stack.include_vm_objects_ = false; |
| 220 stack.TraverseGraph(visitor); | 218 stack.TraverseGraph(visitor); |
| 221 Unmarker::UnmarkAll(isolate()); | 219 Unmarker::UnmarkAll(isolate()); |
| 222 } | 220 } |
| 223 | 221 |
| 224 void ObjectGraph::IterateObjectsFrom(const Object& root, | 222 void ObjectGraph::IterateObjectsFrom(const Object& root, |
| 225 ObjectGraph::Visitor* visitor) { | 223 ObjectGraph::Visitor* visitor) { |
| 226 NoSafepointScope no_safepoint_scope_; | |
| 227 Stack stack(isolate()); | 224 Stack stack(isolate()); |
| 228 RawObject* root_raw = root.raw(); | 225 RawObject* root_raw = root.raw(); |
| 229 stack.VisitPointer(&root_raw); | 226 stack.VisitPointer(&root_raw); |
| 230 stack.TraverseGraph(visitor); | 227 stack.TraverseGraph(visitor); |
| 231 Unmarker::UnmarkAll(isolate()); | 228 Unmarker::UnmarkAll(isolate()); |
| 232 } | 229 } |
| 233 | 230 |
| 234 class InstanceAccumulator : public ObjectVisitor { | 231 class InstanceAccumulator : public ObjectVisitor { |
| 235 public: | 232 public: |
| 236 InstanceAccumulator(ObjectGraph::Stack* stack, intptr_t class_id) | 233 InstanceAccumulator(ObjectGraph::Stack* stack, intptr_t class_id) |
| 237 : stack_(stack), class_id_(class_id) {} | 234 : stack_(stack), class_id_(class_id) {} |
| 238 | 235 |
| 239 void VisitObject(RawObject* obj) { | 236 void VisitObject(RawObject* obj) { |
| 240 if (obj->GetClassId() == class_id_) { | 237 if (obj->GetClassId() == class_id_) { |
| 241 RawObject* rawobj = obj; | 238 RawObject* rawobj = obj; |
| 242 stack_->VisitPointer(&rawobj); | 239 stack_->VisitPointer(&rawobj); |
| 243 } | 240 } |
| 244 } | 241 } |
| 245 | 242 |
| 246 private: | 243 private: |
| 247 ObjectGraph::Stack* stack_; | 244 ObjectGraph::Stack* stack_; |
| 248 const intptr_t class_id_; | 245 const intptr_t class_id_; |
| 249 | 246 |
| 250 DISALLOW_COPY_AND_ASSIGN(InstanceAccumulator); | 247 DISALLOW_COPY_AND_ASSIGN(InstanceAccumulator); |
| 251 }; | 248 }; |
| 252 | 249 |
| 253 void ObjectGraph::IterateObjectsFrom(intptr_t class_id, | 250 void ObjectGraph::IterateObjectsFrom(intptr_t class_id, |
| 254 ObjectGraph::Visitor* visitor) { | 251 ObjectGraph::Visitor* visitor) { |
| 255 NoSafepointScope no_safepoint_scope_; | 252 HeapIterationScope iteration(thread()); |
| 256 Stack stack(isolate()); | 253 Stack stack(isolate()); |
| 257 | 254 |
| 258 InstanceAccumulator accumulator(&stack, class_id); | 255 InstanceAccumulator accumulator(&stack, class_id); |
| 259 isolate()->heap()->VisitObjectsNoImagePages(&accumulator); | 256 iteration.IterateObjectsNoImagePages(&accumulator); |
| 260 | 257 |
| 261 stack.TraverseGraph(visitor); | 258 stack.TraverseGraph(visitor); |
| 262 Unmarker::UnmarkAll(isolate()); | 259 Unmarker::UnmarkAll(isolate()); |
| 263 } | 260 } |
| 264 | 261 |
| 265 class SizeVisitor : public ObjectGraph::Visitor { | 262 class SizeVisitor : public ObjectGraph::Visitor { |
| 266 public: | 263 public: |
| 267 SizeVisitor() : size_(0) {} | 264 SizeVisitor() : size_(0) {} |
| 268 intptr_t size() const { return size_; } | 265 intptr_t size() const { return size_; } |
| 269 virtual bool ShouldSkip(RawObject* obj) const { return false; } | 266 virtual bool ShouldSkip(RawObject* obj) const { return false; } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 294 explicit SizeExcludingClassVisitor(intptr_t skip) : skip_(skip) {} | 291 explicit SizeExcludingClassVisitor(intptr_t skip) : skip_(skip) {} |
| 295 virtual bool ShouldSkip(RawObject* obj) const { | 292 virtual bool ShouldSkip(RawObject* obj) const { |
| 296 return obj->GetClassId() == skip_; | 293 return obj->GetClassId() == skip_; |
| 297 } | 294 } |
| 298 | 295 |
| 299 private: | 296 private: |
| 300 const intptr_t skip_; | 297 const intptr_t skip_; |
| 301 }; | 298 }; |
| 302 | 299 |
| 303 intptr_t ObjectGraph::SizeRetainedByInstance(const Object& obj) { | 300 intptr_t ObjectGraph::SizeRetainedByInstance(const Object& obj) { |
| 304 HeapIterationScope iteration_scope(true); | 301 HeapIterationScope iteration_scope(Thread::Current(), true); |
| 305 SizeVisitor total; | 302 SizeVisitor total; |
| 306 IterateObjects(&total); | 303 IterateObjects(&total); |
| 307 intptr_t size_total = total.size(); | 304 intptr_t size_total = total.size(); |
| 308 SizeExcludingObjectVisitor excluding_obj(obj); | 305 SizeExcludingObjectVisitor excluding_obj(obj); |
| 309 IterateObjects(&excluding_obj); | 306 IterateObjects(&excluding_obj); |
| 310 intptr_t size_excluding_obj = excluding_obj.size(); | 307 intptr_t size_excluding_obj = excluding_obj.size(); |
| 311 return size_total - size_excluding_obj; | 308 return size_total - size_excluding_obj; |
| 312 } | 309 } |
| 313 | 310 |
| 314 intptr_t ObjectGraph::SizeReachableByInstance(const Object& obj) { | 311 intptr_t ObjectGraph::SizeReachableByInstance(const Object& obj) { |
| 315 HeapIterationScope iteration_scope(true); | 312 HeapIterationScope iteration_scope(Thread::Current(), true); |
| 316 SizeVisitor total; | 313 SizeVisitor total; |
| 317 IterateObjectsFrom(obj, &total); | 314 IterateObjectsFrom(obj, &total); |
| 318 return total.size(); | 315 return total.size(); |
| 319 } | 316 } |
| 320 | 317 |
| 321 intptr_t ObjectGraph::SizeRetainedByClass(intptr_t class_id) { | 318 intptr_t ObjectGraph::SizeRetainedByClass(intptr_t class_id) { |
| 322 HeapIterationScope iteration_scope(true); | 319 HeapIterationScope iteration_scope(Thread::Current(), true); |
| 323 SizeVisitor total; | 320 SizeVisitor total; |
| 324 IterateObjects(&total); | 321 IterateObjects(&total); |
| 325 intptr_t size_total = total.size(); | 322 intptr_t size_total = total.size(); |
| 326 SizeExcludingClassVisitor excluding_class(class_id); | 323 SizeExcludingClassVisitor excluding_class(class_id); |
| 327 IterateObjects(&excluding_class); | 324 IterateObjects(&excluding_class); |
| 328 intptr_t size_excluding_class = excluding_class.size(); | 325 intptr_t size_excluding_class = excluding_class.size(); |
| 329 return size_total - size_excluding_class; | 326 return size_total - size_excluding_class; |
| 330 } | 327 } |
| 331 | 328 |
| 332 intptr_t ObjectGraph::SizeReachableByClass(intptr_t class_id) { | 329 intptr_t ObjectGraph::SizeReachableByClass(intptr_t class_id) { |
| 333 HeapIterationScope iteration_scope(true); | 330 HeapIterationScope iteration_scope(Thread::Current(), true); |
| 334 SizeVisitor total; | 331 SizeVisitor total; |
| 335 IterateObjectsFrom(class_id, &total); | 332 IterateObjectsFrom(class_id, &total); |
| 336 return total.size(); | 333 return total.size(); |
| 337 } | 334 } |
| 338 | 335 |
| 339 class RetainingPathVisitor : public ObjectGraph::Visitor { | 336 class RetainingPathVisitor : public ObjectGraph::Visitor { |
| 340 public: | 337 public: |
| 341 // We cannot use a GrowableObjectArray, since we must not trigger GC. | 338 // We cannot use a GrowableObjectArray, since we must not trigger GC. |
| 342 RetainingPathVisitor(RawObject* obj, const Array& path) | 339 RetainingPathVisitor(RawObject* obj, const Array& path) |
| 343 : thread_(Thread::Current()), obj_(obj), path_(path), length_(0) { | 340 : thread_(Thread::Current()), obj_(obj), path_(path), length_(0) { |
| 344 ASSERT(Thread::Current()->no_safepoint_scope_depth() != 0); | |
| 345 } | 341 } |
| 346 | 342 |
| 347 intptr_t length() const { return length_; } | 343 intptr_t length() const { return length_; } |
| 348 | 344 |
| 349 bool ShouldSkip(RawObject* obj) { | 345 bool ShouldSkip(RawObject* obj) { |
| 350 // A retaining path through ICData is never the only retaining path, | 346 // A retaining path through ICData is never the only retaining path, |
| 351 // and it is less informative than its alternatives. | 347 // and it is less informative than its alternatives. |
| 352 intptr_t cid = obj->GetClassId(); | 348 intptr_t cid = obj->GetClassId(); |
| 353 switch (cid) { | 349 switch (cid) { |
| 354 case kICDataCid: | 350 case kICDataCid: |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 | 413 |
| 418 private: | 414 private: |
| 419 Thread* thread_; | 415 Thread* thread_; |
| 420 RawObject* obj_; | 416 RawObject* obj_; |
| 421 const Array& path_; | 417 const Array& path_; |
| 422 intptr_t length_; | 418 intptr_t length_; |
| 423 bool was_last_array_; | 419 bool was_last_array_; |
| 424 }; | 420 }; |
| 425 | 421 |
| 426 intptr_t ObjectGraph::RetainingPath(Object* obj, const Array& path) { | 422 intptr_t ObjectGraph::RetainingPath(Object* obj, const Array& path) { |
| 427 NoSafepointScope no_safepoint_scope_; | 423 HeapIterationScope iteration_scope(Thread::Current(), true); |
| 428 HeapIterationScope iteration_scope(true); | |
| 429 // To break the trivial path, the handle 'obj' is temporarily cleared during | 424 // To break the trivial path, the handle 'obj' is temporarily cleared during |
| 430 // the search, but restored before returning. | 425 // the search, but restored before returning. |
| 431 RawObject* raw = obj->raw(); | 426 RawObject* raw = obj->raw(); |
| 432 *obj = Object::null(); | 427 *obj = Object::null(); |
| 433 RetainingPathVisitor visitor(raw, path); | 428 RetainingPathVisitor visitor(raw, path); |
| 434 IterateUserObjects(&visitor); | 429 IterateUserObjects(&visitor); |
| 435 if (visitor.length() == 0) { | 430 if (visitor.length() == 0) { |
| 436 IterateObjects(&visitor); | 431 IterateObjects(&visitor); |
| 437 } | 432 } |
| 438 *obj = raw; | 433 *obj = raw; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 private: | 492 private: |
| 498 RawObject* source_; | 493 RawObject* source_; |
| 499 RawObject* target_; | 494 RawObject* target_; |
| 500 const Array& references_; | 495 const Array& references_; |
| 501 Object* scratch_; | 496 Object* scratch_; |
| 502 intptr_t length_; | 497 intptr_t length_; |
| 503 }; | 498 }; |
| 504 | 499 |
| 505 intptr_t ObjectGraph::InboundReferences(Object* obj, const Array& references) { | 500 intptr_t ObjectGraph::InboundReferences(Object* obj, const Array& references) { |
| 506 Object& scratch = Object::Handle(); | 501 Object& scratch = Object::Handle(); |
| 507 NoSafepointScope no_safepoint_scope; | 502 HeapIterationScope iteration(Thread::Current()); |
| 508 InboundReferencesVisitor visitor(isolate(), obj->raw(), references, &scratch); | 503 InboundReferencesVisitor visitor(isolate(), obj->raw(), references, &scratch); |
| 509 isolate()->heap()->IterateObjects(&visitor); | 504 iteration.IterateObjects(&visitor); |
| 510 return visitor.length(); | 505 return visitor.length(); |
| 511 } | 506 } |
| 512 | 507 |
| 513 static void WritePtr(RawObject* raw, WriteStream* stream) { | 508 static void WritePtr(RawObject* raw, WriteStream* stream) { |
| 514 ASSERT(raw->IsHeapObject()); | 509 ASSERT(raw->IsHeapObject()); |
| 515 ASSERT(raw->IsOldObject()); | 510 ASSERT(raw->IsOldObject()); |
| 516 uword addr = RawObject::ToAddr(raw); | 511 uword addr = RawObject::ToAddr(raw); |
| 517 ASSERT(Utils::IsAligned(addr, kObjectAlignment)); | 512 ASSERT(Utils::IsAligned(addr, kObjectAlignment)); |
| 518 // Using units of kObjectAlignment makes the ids fit into Smis when parsed | 513 // Using units of kObjectAlignment makes the ids fit into Smis when parsed |
| 519 // in the Dart code of the Observatory. | 514 // in the Dart code of the Observatory. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 }; | 597 }; |
| 603 | 598 |
| 604 intptr_t ObjectGraph::Serialize(WriteStream* stream, | 599 intptr_t ObjectGraph::Serialize(WriteStream* stream, |
| 605 SnapshotRoots roots, | 600 SnapshotRoots roots, |
| 606 bool collect_garbage) { | 601 bool collect_garbage) { |
| 607 if (collect_garbage) { | 602 if (collect_garbage) { |
| 608 isolate()->heap()->CollectAllGarbage(); | 603 isolate()->heap()->CollectAllGarbage(); |
| 609 } | 604 } |
| 610 // Current encoding assumes objects do not move, so promote everything to old. | 605 // Current encoding assumes objects do not move, so promote everything to old. |
| 611 isolate()->heap()->new_space()->Evacuate(); | 606 isolate()->heap()->new_space()->Evacuate(); |
| 612 HeapIterationScope iteration_scope(true); | 607 HeapIterationScope iteration_scope(Thread::Current(), true); |
| 613 | 608 |
| 614 RawObject* kRootAddress = reinterpret_cast<RawObject*>(kHeapObjectTag); | 609 RawObject* kRootAddress = reinterpret_cast<RawObject*>(kHeapObjectTag); |
| 615 const intptr_t kRootCid = kIllegalCid; | 610 const intptr_t kRootCid = kIllegalCid; |
| 616 RawObject* kStackAddress = | 611 RawObject* kStackAddress = |
| 617 reinterpret_cast<RawObject*>(kObjectAlignment + kHeapObjectTag); | 612 reinterpret_cast<RawObject*>(kObjectAlignment + kHeapObjectTag); |
| 618 | 613 |
| 619 stream->WriteUnsigned(kObjectAlignment); | 614 stream->WriteUnsigned(kObjectAlignment); |
| 620 stream->WriteUnsigned(kStackCid); | 615 stream->WriteUnsigned(kStackCid); |
| 621 | 616 |
| 622 if (roots == kVM) { | 617 if (roots == kVM) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 650 intptr_t object_count = visitor.count(); | 645 intptr_t object_count = visitor.count(); |
| 651 if (roots == kVM) { | 646 if (roots == kVM) { |
| 652 object_count += 1; // root | 647 object_count += 1; // root |
| 653 } else { | 648 } else { |
| 654 object_count += 2; // root and stack | 649 object_count += 2; // root and stack |
| 655 } | 650 } |
| 656 return object_count; | 651 return object_count; |
| 657 } | 652 } |
| 658 | 653 |
| 659 } // namespace dart | 654 } // namespace dart |
| OLD | NEW |