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

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

Issue 2531973002: Simple BlinkGC heap compaction. (Closed)
Patch Set: synchronize on compaction finish 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 976 matching lines...) Expand 10 before | Expand all | Expand 10 after
1027 } 1028 }
1028 1029
1029 void ThreadState::flushHeapDoesNotContainCacheIfNeeded() { 1030 void ThreadState::flushHeapDoesNotContainCacheIfNeeded() {
1030 if (m_shouldFlushHeapDoesNotContainCache) { 1031 if (m_shouldFlushHeapDoesNotContainCache) {
1031 m_heap->flushHeapDoesNotContainCache(); 1032 m_heap->flushHeapDoesNotContainCache();
1032 m_shouldFlushHeapDoesNotContainCache = false; 1033 m_shouldFlushHeapDoesNotContainCache = false;
1033 } 1034 }
1034 } 1035 }
1035 1036
1036 void ThreadState::makeConsistentForGC() { 1037 void ThreadState::makeConsistentForGC() {
1038 if (isMainThread()) {
haraken 2016/12/02 12:43:20 Maybe can we move this code into checkIfCompacting
sof 2016/12/04 14:55:38 Moved the size sampling into the heap compaction c
1039 // For the main thread, report heap + freelist residency to the
1040 // heap compactor. It uses the data to determine if compactions
1041 // is now worthwhile for one or more of the sub heaps/arenas it can
1042 // compact.
1043 size_t heapSize = 0;
1044 size_t freeSize = 0;
haraken 2016/12/02 12:43:21 heapSize => totalArenaSize freeSize => totalFreeLi
sof 2016/12/04 14:55:38 Done.
1045 using Residency = std::pair<size_t, size_t>;
1046 Vector<Residency> residencies;
1047 NormalPageArena* arena;
1048 for (int i = BlinkGC::Vector1ArenaIndex; i <= BlinkGC::HashTableArenaIndex;
1049 ++i) {
haraken 2016/12/02 12:43:20 Add an assertion somewhere to check that all backi
sof 2016/12/04 14:55:38 Added (in HeapCompact::updateHeapResidency())
1050 arena = static_cast<NormalPageArena*>(m_arenas[i]);
1051 heapSize += arena->arenaSize();
1052 freeSize += arena->freeListSize();
1053 residencies.append(Residency(arena->arenaSize(), arena->freeListSize()));
1054 }
1055 heap().compaction()->setHeapResidency(heapSize, freeSize, residencies);
1056 }
1037 ASSERT(isInGC()); 1057 ASSERT(isInGC());
1038 TRACE_EVENT0("blink_gc", "ThreadState::makeConsistentForGC"); 1058 TRACE_EVENT0("blink_gc", "ThreadState::makeConsistentForGC");
1039 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i) 1059 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i)
1040 m_arenas[i]->makeConsistentForGC(); 1060 m_arenas[i]->makeConsistentForGC();
1041 } 1061 }
1042 1062
1063 void ThreadState::compact() {
haraken 2016/12/02 12:43:20 Add a TODO that the heap compaction should probabl
sof 2016/12/04 14:55:38 checkIfCompacting() is who decides this; added a T
1064 if (!heap().compaction()->isCompacting())
haraken 2016/12/02 12:43:20 When can this happen?
sof 2016/12/04 14:55:38 Most of the time? :) compact() is called unconditi
1065 return;
1066
haraken 2016/12/02 12:43:20 Let's enter SweepForbiddenScope and ScriptForbidde
sof 2016/12/04 14:55:38 Makes good sense, like the other sweeping passes.
1067 // Compaction is done eagerly and before the mutator threads get
1068 // to run again. Doing it lazily is problematic, as the mutator's
1069 // references to live objects could suddenly be invalidated by
1070 // compaction of a page/heap. We do know all the references to
1071 // the relocating objects just after marking, but won't later.
1072 // (e.g., stack references could have been created, new objects
1073 // created which refer to old collection objects, and so on.)
1074 //
1075 heap().compaction()->startCompacting(this);
1076 // TODO: implement bail out wrt any overall deadline, not
1077 // compacting heaps if the time budget has been exceeded.
1078 static_cast<NormalPageArena*>(m_arenas[BlinkGC::HashTableArenaIndex])
1079 ->sweepCompact();
haraken 2016/12/02 12:43:20 Can we move BlinkGC::HashTableArenaIndex into the
sof 2016/12/04 14:55:38 Done.
1080 for (int i = BlinkGC::Vector1ArenaIndex; i <= BlinkGC::InlineVectorArenaIndex;
1081 ++i)
1082 static_cast<NormalPageArena*>(m_arenas[i])->sweepCompact();
1083 heap().compaction()->finishedCompacting(this);
1084 }
1085
1043 void ThreadState::makeConsistentForMutator() { 1086 void ThreadState::makeConsistentForMutator() {
1044 ASSERT(isInGC()); 1087 ASSERT(isInGC());
1045 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i) 1088 for (int i = 0; i < BlinkGC::NumberOfArenas; ++i)
1046 m_arenas[i]->makeConsistentForMutator(); 1089 m_arenas[i]->makeConsistentForMutator();
1047 } 1090 }
1048 1091
1049 void ThreadState::preGC() { 1092 void ThreadState::preGC() {
1050 if (RuntimeEnabledFeatures::traceWrappablesEnabled() && m_isolate && 1093 if (RuntimeEnabledFeatures::traceWrappablesEnabled() && m_isolate &&
1051 m_performCleanup) 1094 m_performCleanup)
1052 m_performCleanup(m_isolate); 1095 m_performCleanup(m_isolate);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 1159
1117 // Allocation is allowed during the pre-finalizers and destructors. 1160 // Allocation is allowed during the pre-finalizers and destructors.
1118 // However, they must not mutate an object graph in a way in which 1161 // However, they must not mutate an object graph in a way in which
1119 // a dead object gets resurrected. 1162 // a dead object gets resurrected.
1120 invokePreFinalizers(); 1163 invokePreFinalizers();
1121 1164
1122 m_accumulatedSweepingTime = 0; 1165 m_accumulatedSweepingTime = 0;
1123 1166
1124 eagerSweep(); 1167 eagerSweep();
1125 1168
1169 compact();
1170
1126 #if defined(ADDRESS_SANITIZER) 1171 #if defined(ADDRESS_SANITIZER)
1127 poisonAllHeaps(); 1172 poisonAllHeaps();
1128 #endif 1173 #endif
1174
1129 if (previousGCState == EagerSweepScheduled) { 1175 if (previousGCState == EagerSweepScheduled) {
1130 // Eager sweeping should happen only in testing. 1176 // Eager sweeping should happen only in testing.
1131 completeSweep(); 1177 completeSweep();
1132 } else { 1178 } else {
1133 // The default behavior is lazy sweeping. 1179 // The default behavior is lazy sweeping.
1134 scheduleIdleLazySweep(); 1180 scheduleIdleLazySweep();
1135 } 1181 }
1136 } 1182 }
1137 1183
1138 #if defined(ADDRESS_SANITIZER) 1184 #if defined(ADDRESS_SANITIZER)
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
1667 1713
1668 void ThreadState::collectGarbage(BlinkGC::StackState stackState, 1714 void ThreadState::collectGarbage(BlinkGC::StackState stackState,
1669 BlinkGC::GCType gcType, 1715 BlinkGC::GCType gcType,
1670 BlinkGC::GCReason reason) { 1716 BlinkGC::GCReason reason) {
1671 DCHECK_NE(gcType, BlinkGC::ThreadTerminationGC); 1717 DCHECK_NE(gcType, BlinkGC::ThreadTerminationGC);
1672 1718
1673 // Nested collectGarbage() invocations aren't supported. 1719 // Nested collectGarbage() invocations aren't supported.
1674 RELEASE_ASSERT(!isGCForbidden()); 1720 RELEASE_ASSERT(!isGCForbidden());
1675 completeSweep(); 1721 completeSweep();
1676 1722
1677 std::unique_ptr<Visitor> visitor = Visitor::create(this, gcType); 1723 std::unique_ptr<Visitor> visitor = Visitor::create(this, gcType);
haraken 2016/12/02 12:43:20 Maybe can we use this visitor creation down to lin
sof 2016/12/04 14:55:38 I've teased apart the steps a bit to allow that, b
1678 1724
1679 SafePointScope safePointScope(stackState, this); 1725 SafePointScope safePointScope(stackState, this);
1680 1726
1681 // Resume all parked threads upon leaving this scope. 1727 // Resume all parked threads upon leaving this scope.
1682 ParkThreadsScope parkThreadsScope(this); 1728 ParkThreadsScope parkThreadsScope(this);
1683 1729
1684 // Try to park the other threads. If we're unable to, bail out of the GC. 1730 // Try to park the other threads. If we're unable to, bail out of the GC.
1685 if (!parkThreadsScope.parkThreads()) 1731 if (!parkThreadsScope.parkThreads())
1686 return; 1732 return;
1687 1733
1734 heap().compaction()->checkIfCompacting(&heap(), visitor.get(), gcType,
1735 reason);
1736
1688 ScriptForbiddenIfMainThreadScope scriptForbidden; 1737 ScriptForbiddenIfMainThreadScope scriptForbidden;
1689 1738
1690 TRACE_EVENT2("blink_gc,devtools.timeline", "BlinkGCMarking", "lazySweeping", 1739 TRACE_EVENT2("blink_gc,devtools.timeline", "BlinkGCMarking", "lazySweeping",
1691 gcType == BlinkGC::GCWithoutSweep, "gcReason", 1740 gcType == BlinkGC::GCWithoutSweep, "gcReason",
1692 gcReasonString(reason)); 1741 gcReasonString(reason));
1693 double startTime = WTF::currentTimeMS(); 1742 double startTime = WTF::currentTimeMS();
1694 1743
1695 if (gcType == BlinkGC::TakeSnapshot) 1744 if (gcType == BlinkGC::TakeSnapshot)
1696 BlinkGCMemoryDumpProvider::instance()->clearProcessDumpForCurrentGC(); 1745 BlinkGCMemoryDumpProvider::instance()->clearProcessDumpForCurrentGC();
1697 1746
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 collectGarbage(BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep, 1874 collectGarbage(BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep,
1826 BlinkGC::ForcedGC); 1875 BlinkGC::ForcedGC);
1827 size_t liveObjects = heap().heapStats().markedObjectSize(); 1876 size_t liveObjects = heap().heapStats().markedObjectSize();
1828 if (liveObjects == previousLiveObjects) 1877 if (liveObjects == previousLiveObjects)
1829 break; 1878 break;
1830 previousLiveObjects = liveObjects; 1879 previousLiveObjects = liveObjects;
1831 } 1880 }
1832 } 1881 }
1833 1882
1834 } // namespace blink 1883 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698