Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: third_party/WebKit/Source/platform/heap/ThreadState.cpp

Issue 2531973002: Simple BlinkGC heap compaction. (Closed)
Patch Set: cros compile fix Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 20 matching lines...) Expand all
31 #include "platform/heap/ThreadState.h" 31 #include "platform/heap/ThreadState.h"
32 32
33 #include "base/trace_event/process_memory_dump.h" 33 #include "base/trace_event/process_memory_dump.h"
34 #include "platform/Histogram.h" 34 #include "platform/Histogram.h"
35 #include "platform/RuntimeEnabledFeatures.h" 35 #include "platform/RuntimeEnabledFeatures.h"
36 #include "platform/ScriptForbiddenScope.h" 36 #include "platform/ScriptForbiddenScope.h"
37 #include "platform/heap/BlinkGCMemoryDumpProvider.h" 37 #include "platform/heap/BlinkGCMemoryDumpProvider.h"
38 #include "platform/heap/CallbackStack.h" 38 #include "platform/heap/CallbackStack.h"
39 #include "platform/heap/Handle.h" 39 #include "platform/heap/Handle.h"
40 #include "platform/heap/Heap.h" 40 #include "platform/heap/Heap.h"
41 #include "platform/heap/HeapCompact.h"
41 #include "platform/heap/PagePool.h" 42 #include "platform/heap/PagePool.h"
42 #include "platform/heap/SafePoint.h" 43 #include "platform/heap/SafePoint.h"
43 #include "platform/heap/Visitor.h" 44 #include "platform/heap/Visitor.h"
44 #include "platform/tracing/TraceEvent.h" 45 #include "platform/tracing/TraceEvent.h"
45 #include "platform/tracing/web_memory_allocator_dump.h" 46 #include "platform/tracing/web_memory_allocator_dump.h"
46 #include "platform/tracing/web_process_memory_dump.h" 47 #include "platform/tracing/web_process_memory_dump.h"
47 #include "public/platform/Platform.h" 48 #include "public/platform/Platform.h"
48 #include "public/platform/WebScheduler.h" 49 #include "public/platform/WebScheduler.h"
49 #include "public/platform/WebThread.h" 50 #include "public/platform/WebThread.h"
50 #include "public/platform/WebTraceLocation.h" 51 #include "public/platform/WebTraceLocation.h"
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 ScriptForbiddenIfMainThreadScope scriptForbiddenScope; 498 ScriptForbiddenIfMainThreadScope scriptForbiddenScope;
498 499
499 // Disallow allocation during weak processing. 500 // Disallow allocation during weak processing.
500 // It would be technically safe to allow allocations, but it is unsafe 501 // It would be technically safe to allow allocations, but it is unsafe
501 // to mutate an object graph in a way in which a dead object gets 502 // to mutate an object graph in a way in which a dead object gets
502 // resurrected or mutate a HashTable (because HashTable's weak processing 503 // resurrected or mutate a HashTable (because HashTable's weak processing
503 // assumes that the HashTable hasn't been mutated since the latest marking). 504 // assumes that the HashTable hasn't been mutated since the latest marking).
504 // Due to the complexity, we just forbid allocations. 505 // Due to the complexity, we just forbid allocations.
505 NoAllocationScope noAllocationScope(this); 506 NoAllocationScope noAllocationScope(this);
506 507
508 GCForbiddenScope gcForbiddenScope(this);
507 std::unique_ptr<Visitor> visitor = 509 std::unique_ptr<Visitor> visitor =
508 Visitor::create(this, BlinkGC::ThreadLocalWeakProcessing); 510 Visitor::create(this, BlinkGC::ThreadLocalWeakProcessing);
509 511
510 // Perform thread-specific weak processing. 512 // Perform thread-specific weak processing.
511 while (popAndInvokeThreadLocalWeakCallback(visitor.get())) { 513 while (popAndInvokeThreadLocalWeakCallback(visitor.get())) {
512 } 514 }
513 515
514 m_threadLocalWeakCallbackStack->decommit(); 516 m_threadLocalWeakCallbackStack->decommit();
515 517
516 if (isMainThread()) { 518 if (isMainThread()) {
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after
1033 } 1035 }
1034 } 1036 }
1035 1037
1036 void ThreadState::makeConsistentForGC() { 1038 void ThreadState::makeConsistentForGC() {
1037 ASSERT(isInGC()); 1039 ASSERT(isInGC());
1038 TRACE_EVENT0("blink_gc", "ThreadState::makeConsistentForGC"); 1040 TRACE_EVENT0("blink_gc", "ThreadState::makeConsistentForGC");
1039 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i) 1041 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i)
1040 m_arenas[i]->makeConsistentForGC(); 1042 m_arenas[i]->makeConsistentForGC();
1041 } 1043 }
1042 1044
1045 void ThreadState::compact() {
1046 if (!heap().compaction()->isCompacting())
1047 return;
1048
1049 SweepForbiddenScope scope(this);
1050 ScriptForbiddenIfMainThreadScope scriptForbiddenScope;
1051
1052 // Compaction is done eagerly and before the mutator threads get
1053 // to run again. Doing it lazily is problematic, as the mutator's
1054 // references to live objects could suddenly be invalidated by
1055 // compaction of a page/heap. We do know all the references to
1056 // the relocating objects just after marking, but won't later.
1057 // (e.g., stack references could have been created, new objects
1058 // created which refer to old collection objects, and so on.)
1059
1060 // Compact the hash table backing store arena first, it usually has
1061 // higher fragmentation and is larger.
1062 //
1063 // TODO: implement bail out wrt any overall deadline, not compacting
1064 // the remaining arenas if the time budget has been exceeded.
1065 heap().compaction()->startThreadCompaction(this);
1066 for (int i = BlinkGC::HashTableArenaIndex; i >= BlinkGC::Vector1ArenaIndex;
1067 --i)
1068 static_cast<NormalPageArena*>(m_arenas[i])->sweepAndCompact();
1069 heap().compaction()->finishedThreadCompaction(this);
1070 }
1071
1043 void ThreadState::makeConsistentForMutator() { 1072 void ThreadState::makeConsistentForMutator() {
1044 ASSERT(isInGC()); 1073 ASSERT(isInGC());
1045 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i) 1074 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i)
1046 m_arenas[i]->makeConsistentForMutator(); 1075 m_arenas[i]->makeConsistentForMutator();
1047 } 1076 }
1048 1077
1049 void ThreadState::preGC() { 1078 void ThreadState::preGC() {
1050 if (RuntimeEnabledFeatures::traceWrappablesEnabled() && m_isolate && 1079 if (RuntimeEnabledFeatures::traceWrappablesEnabled() && m_isolate &&
1051 m_performCleanup) 1080 m_performCleanup)
1052 m_performCleanup(m_isolate); 1081 m_performCleanup(m_isolate);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 1145
1117 // Allocation is allowed during the pre-finalizers and destructors. 1146 // Allocation is allowed during the pre-finalizers and destructors.
1118 // However, they must not mutate an object graph in a way in which 1147 // However, they must not mutate an object graph in a way in which
1119 // a dead object gets resurrected. 1148 // a dead object gets resurrected.
1120 invokePreFinalizers(); 1149 invokePreFinalizers();
1121 1150
1122 m_accumulatedSweepingTime = 0; 1151 m_accumulatedSweepingTime = 0;
1123 1152
1124 eagerSweep(); 1153 eagerSweep();
1125 1154
1155 compact();
1156
1126 #if defined(ADDRESS_SANITIZER) 1157 #if defined(ADDRESS_SANITIZER)
1127 poisonAllHeaps(); 1158 poisonAllHeaps();
1128 #endif 1159 #endif
1160
1129 if (previousGCState == EagerSweepScheduled) { 1161 if (previousGCState == EagerSweepScheduled) {
1130 // Eager sweeping should happen only in testing. 1162 // Eager sweeping should happen only in testing.
1131 completeSweep(); 1163 completeSweep();
1132 } else { 1164 } else {
1133 // The default behavior is lazy sweeping. 1165 // The default behavior is lazy sweeping.
1134 scheduleIdleLazySweep(); 1166 scheduleIdleLazySweep();
1135 } 1167 }
1136 } 1168 }
1137 1169
1138 #if defined(ADDRESS_SANITIZER) 1170 #if defined(ADDRESS_SANITIZER)
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
1667 1699
1668 void ThreadState::collectGarbage(BlinkGC::StackState stackState, 1700 void ThreadState::collectGarbage(BlinkGC::StackState stackState,
1669 BlinkGC::GCType gcType, 1701 BlinkGC::GCType gcType,
1670 BlinkGC::GCReason reason) { 1702 BlinkGC::GCReason reason) {
1671 DCHECK_NE(gcType, BlinkGC::ThreadTerminationGC); 1703 DCHECK_NE(gcType, BlinkGC::ThreadTerminationGC);
1672 1704
1673 // Nested collectGarbage() invocations aren't supported. 1705 // Nested collectGarbage() invocations aren't supported.
1674 RELEASE_ASSERT(!isGCForbidden()); 1706 RELEASE_ASSERT(!isGCForbidden());
1675 completeSweep(); 1707 completeSweep();
1676 1708
1677 std::unique_ptr<Visitor> visitor = Visitor::create(this, gcType); 1709 GCForbiddenScope gcForbiddenScope(this);
1678 1710
1679 SafePointScope safePointScope(stackState, this); 1711 SafePointScope safePointScope(stackState, this);
1680 1712
1681 // Resume all parked threads upon leaving this scope. 1713 // Resume all parked threads upon leaving this scope.
1682 ParkThreadsScope parkThreadsScope(this); 1714 ParkThreadsScope parkThreadsScope(this);
1683 1715
1684 // Try to park the other threads. If we're unable to, bail out of the GC. 1716 // Try to park the other threads. If we're unable to, bail out of the GC.
1685 if (!parkThreadsScope.parkThreads()) 1717 if (!parkThreadsScope.parkThreads())
1686 return; 1718 return;
1687 1719
1720 BlinkGC::GCType visitorType =
1721 heap().compaction()->checkIfCompacting(this, gcType, reason);
haraken 2016/12/05 11:27:47 I'd prefer reorganizing this as follows: bool e
sof 2016/12/06 10:55:59 Divided it up into predicate + config parts.
1722
1723 std::unique_ptr<Visitor> visitor = Visitor::create(this, visitorType);
1724
1688 ScriptForbiddenIfMainThreadScope scriptForbidden; 1725 ScriptForbiddenIfMainThreadScope scriptForbidden;
1689 1726
1690 TRACE_EVENT2("blink_gc,devtools.timeline", "BlinkGCMarking", "lazySweeping", 1727 TRACE_EVENT2("blink_gc,devtools.timeline", "BlinkGCMarking", "lazySweeping",
1691 gcType == BlinkGC::GCWithoutSweep, "gcReason", 1728 gcType == BlinkGC::GCWithoutSweep, "gcReason",
1692 gcReasonString(reason)); 1729 gcReasonString(reason));
1693 double startTime = WTF::currentTimeMS(); 1730 double startTime = WTF::currentTimeMS();
1694 1731
1695 if (gcType == BlinkGC::TakeSnapshot) 1732 if (gcType == BlinkGC::TakeSnapshot)
1696 BlinkGCMemoryDumpProvider::instance()->clearProcessDumpForCurrentGC(); 1733 BlinkGCMemoryDumpProvider::instance()->clearProcessDumpForCurrentGC();
1697 1734
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1778 heap().postGC(gcType); 1815 heap().postGC(gcType);
1779 heap().decommitCallbackStacks(); 1816 heap().decommitCallbackStacks();
1780 } 1817 }
1781 1818
1782 void ThreadState::collectGarbageForTerminatingThread() { 1819 void ThreadState::collectGarbageForTerminatingThread() {
1783 { 1820 {
1784 // A thread-specific termination GC must not allow other global GCs to go 1821 // A thread-specific termination GC must not allow other global GCs to go
1785 // ahead while it is running, hence the termination GC does not enter a 1822 // ahead while it is running, hence the termination GC does not enter a
1786 // safepoint. VisitorScope will not enter also a safepoint scope for 1823 // safepoint. VisitorScope will not enter also a safepoint scope for
1787 // ThreadTerminationGC. 1824 // ThreadTerminationGC.
1825 GCForbiddenScope gcForbiddenScope(this);
1788 std::unique_ptr<Visitor> visitor = 1826 std::unique_ptr<Visitor> visitor =
1789 Visitor::create(this, BlinkGC::ThreadTerminationGC); 1827 Visitor::create(this, BlinkGC::ThreadTerminationGC);
1790 1828
1791 ThreadState::NoAllocationScope noAllocationScope(this); 1829 ThreadState::NoAllocationScope noAllocationScope(this);
1792 1830
1793 heap().commitCallbackStacks(); 1831 heap().commitCallbackStacks();
1794 preGC(); 1832 preGC();
1795 1833
1796 // 1. Trace the thread local persistent roots. For thread local GCs we 1834 // 1. Trace the thread local persistent roots. For thread local GCs we
1797 // don't trace the stack (ie. no conservative scanning) since this is 1835 // don't trace the stack (ie. no conservative scanning) since this is
(...skipping 27 matching lines...) Expand all
1825 collectGarbage(BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep, 1863 collectGarbage(BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep,
1826 BlinkGC::ForcedGC); 1864 BlinkGC::ForcedGC);
1827 size_t liveObjects = heap().heapStats().markedObjectSize(); 1865 size_t liveObjects = heap().heapStats().markedObjectSize();
1828 if (liveObjects == previousLiveObjects) 1866 if (liveObjects == previousLiveObjects)
1829 break; 1867 break;
1830 previousLiveObjects = liveObjects; 1868 previousLiveObjects = liveObjects;
1831 } 1869 }
1832 } 1870 }
1833 1871
1834 } // namespace blink 1872 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698