1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
/*
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "Heap.h"
#include "CodeBlock.h"
#include "ConservativeRoots.h"
#include "GCActivityCallback.h"
#include "Interpreter.h"
#include "JSGlobalData.h"
#include "JSGlobalObject.h"
#include "JSLock.h"
#include "JSONObject.h"
#include "Tracing.h"
#include <algorithm>
#define COLLECT_ON_EVERY_SLOW_ALLOCATION 0
using namespace std;
namespace JSC {
const size_t minBytesPerCycle = 512 * 1024;
Heap::Heap(JSGlobalData* globalData)
: m_operationInProgress(NoOperation)
, m_markedSpace(globalData)
, m_markListSet(0)
, m_activityCallback(DefaultGCActivityCallback::create(this))
, m_globalData(globalData)
, m_machineThreads(this)
, m_markStack(globalData->jsArrayVPtr)
, m_handleHeap(globalData)
, m_extraCost(0)
{
m_markedSpace.setHighWaterMark(minBytesPerCycle);
(*m_activityCallback)();
}
Heap::~Heap()
{
// The destroy function must already have been called, so assert this.
ASSERT(!m_globalData);
}
void Heap::destroy()
{
JSLock lock(SilenceAssertionsOnly);
if (!m_globalData)
return;
ASSERT(!m_globalData->dynamicGlobalObject);
ASSERT(m_operationInProgress == NoOperation);
// The global object is not GC protected at this point, so sweeping may delete it
// (and thus the global data) before other objects that may use the global data.
RefPtr<JSGlobalData> protect(m_globalData);
#if ENABLE(JIT)
m_globalData->jitStubs->clearHostFunctionStubs();
#endif
delete m_markListSet;
m_markListSet = 0;
m_markedSpace.clearMarks();
m_handleHeap.finalizeWeakHandles();
m_markedSpace.destroy();
m_globalData = 0;
}
void Heap::reportExtraMemoryCostSlowCase(size_t cost)
{
// Our frequency of garbage collection tries to balance memory use against speed
// by collecting based on the number of newly created values. However, for values
// that hold on to a great deal of memory that's not in the form of other JS values,
// that is not good enough - in some cases a lot of those objects can pile up and
// use crazy amounts of memory without a GC happening. So we track these extra
// memory costs. Only unusually large objects are noted, and we only keep track
// of this extra cost until the next GC. In garbage collected languages, most values
// are either very short lived temporaries, or have extremely long lifetimes. So
// if a large value survives one garbage collection, there is not much point to
// collecting more frequently as long as it stays alive.
if (m_extraCost > maxExtraCost && m_extraCost > m_markedSpace.highWaterMark() / 2)
collectAllGarbage();
m_extraCost += cost;
}
void* Heap::allocateSlowCase(size_t bytes)
{
ASSERT(globalData()->identifierTable == wtfThreadData().currentIdentifierTable());
ASSERT(JSLock::lockCount() > 0);
ASSERT(JSLock::currentThreadIsHoldingLock());
ASSERT(bytes <= MarkedSpace::maxCellSize);
ASSERT(m_operationInProgress == NoOperation);
#if COLLECT_ON_EVERY_SLOW_ALLOCATION
collectAllGarbage();
ASSERT(m_operationInProgress == NoOperation);
#endif
reset(DoNotSweep);
m_operationInProgress = Allocation;
void* result = m_markedSpace.allocate(bytes);
m_operationInProgress = NoOperation;
ASSERT(result);
return result;
}
void Heap::protect(JSValue k)
{
ASSERT(k);
ASSERT(JSLock::currentThreadIsHoldingLock() || !m_globalData->isSharedInstance());
if (!k.isCell())
return;
m_protectedValues.add(k.asCell());
}
bool Heap::unprotect(JSValue k)
{
ASSERT(k);
ASSERT(JSLock::currentThreadIsHoldingLock() || !m_globalData->isSharedInstance());
if (!k.isCell())
return false;
return m_protectedValues.remove(k.asCell());
}
void Heap::markProtectedObjects(HeapRootVisitor& heapRootMarker)
{
ProtectCountSet::iterator end = m_protectedValues.end();
for (ProtectCountSet::iterator it = m_protectedValues.begin(); it != end; ++it)
heapRootMarker.mark(&it->first);
}
void Heap::pushTempSortVector(Vector<ValueStringPair>* tempVector)
{
m_tempSortingVectors.append(tempVector);
}
void Heap::popTempSortVector(Vector<ValueStringPair>* tempVector)
{
ASSERT_UNUSED(tempVector, tempVector == m_tempSortingVectors.last());
m_tempSortingVectors.removeLast();
}
void Heap::markTempSortVectors(HeapRootVisitor& heapRootMarker)
{
typedef Vector<Vector<ValueStringPair>* > VectorOfValueStringVectors;
VectorOfValueStringVectors::iterator end = m_tempSortingVectors.end();
for (VectorOfValueStringVectors::iterator it = m_tempSortingVectors.begin(); it != end; ++it) {
Vector<ValueStringPair>* tempSortingVector = *it;
Vector<ValueStringPair>::iterator vectorEnd = tempSortingVector->end();
for (Vector<ValueStringPair>::iterator vectorIt = tempSortingVector->begin(); vectorIt != vectorEnd; ++vectorIt) {
if (vectorIt->first)
heapRootMarker.mark(&vectorIt->first);
}
}
}
inline RegisterFile& Heap::registerFile()
{
return m_globalData->interpreter->registerFile();
}
void Heap::markRoots()
{
#ifndef NDEBUG
if (m_globalData->isSharedInstance()) {
ASSERT(JSLock::lockCount() > 0);
ASSERT(JSLock::currentThreadIsHoldingLock());
}
#endif
void* dummy;
ASSERT(m_operationInProgress == NoOperation);
if (m_operationInProgress != NoOperation)
CRASH();
m_operationInProgress = Collection;
MarkStack& visitor = m_markStack;
HeapRootVisitor heapRootMarker(visitor);
// We gather conservative roots before clearing mark bits because
// conservative gathering uses the mark bits from our last mark pass to
// determine whether a reference is valid.
ConservativeRoots machineThreadRoots(this);
m_machineThreads.gatherConservativeRoots(machineThreadRoots, &dummy);
ConservativeRoots registerFileRoots(this);
registerFile().gatherConservativeRoots(registerFileRoots);
m_markedSpace.clearMarks();
visitor.append(machineThreadRoots);
visitor.drain();
visitor.append(registerFileRoots);
visitor.drain();
markProtectedObjects(heapRootMarker);
visitor.drain();
markTempSortVectors(heapRootMarker);
visitor.drain();
if (m_markListSet && m_markListSet->size())
MarkedArgumentBuffer::markLists(heapRootMarker, *m_markListSet);
if (m_globalData->exception)
heapRootMarker.mark(&m_globalData->exception);
visitor.drain();
m_handleHeap.markStrongHandles(heapRootMarker);
visitor.drain();
m_handleStack.mark(heapRootMarker);
visitor.drain();
// Mark the small strings cache as late as possible, since it will clear
// itself if nothing else has marked it.
// FIXME: Change the small strings cache to use Weak<T>.
m_globalData->smallStrings.visitChildren(heapRootMarker);
visitor.drain();
// Weak handles must be marked last, because their owners use the set of
// opaque roots to determine reachability.
int lastOpaqueRootCount;
do {
lastOpaqueRootCount = visitor.opaqueRootCount();
m_handleHeap.markWeakHandles(heapRootMarker);
visitor.drain();
// If the set of opaque roots has grown, more weak handles may have become reachable.
} while (lastOpaqueRootCount != visitor.opaqueRootCount());
visitor.reset();
m_operationInProgress = NoOperation;
}
size_t Heap::objectCount() const
{
return m_markedSpace.objectCount();
}
size_t Heap::size() const
{
return m_markedSpace.size();
}
size_t Heap::capacity() const
{
return m_markedSpace.capacity();
}
size_t Heap::globalObjectCount()
{
return m_globalData->globalObjectCount;
}
size_t Heap::protectedGlobalObjectCount()
{
size_t count = m_handleHeap.protectedGlobalObjectCount();
ProtectCountSet::iterator end = m_protectedValues.end();
for (ProtectCountSet::iterator it = m_protectedValues.begin(); it != end; ++it) {
if (it->first->isObject() && asObject(it->first)->isGlobalObject())
count++;
}
return count;
}
size_t Heap::protectedObjectCount()
{
return m_protectedValues.size();
}
class TypeCounter {
public:
TypeCounter();
void operator()(JSCell*);
PassOwnPtr<TypeCountSet> take();
private:
const char* typeName(JSCell*);
OwnPtr<TypeCountSet> m_typeCountSet;
};
inline TypeCounter::TypeCounter()
: m_typeCountSet(adoptPtr(new TypeCountSet))
{
}
inline const char* TypeCounter::typeName(JSCell* cell)
{
if (cell->isString())
return "string";
if (cell->isGetterSetter())
return "Getter-Setter";
if (cell->isAPIValueWrapper())
return "API wrapper";
if (cell->isPropertyNameIterator())
return "For-in iterator";
if (const ClassInfo* info = cell->classInfo())
return info->className;
if (!cell->isObject())
return "[empty cell]";
return "Object";
}
inline void TypeCounter::operator()(JSCell* cell)
{
m_typeCountSet->add(typeName(cell));
}
inline PassOwnPtr<TypeCountSet> TypeCounter::take()
{
return m_typeCountSet.release();
}
PassOwnPtr<TypeCountSet> Heap::protectedObjectTypeCounts()
{
TypeCounter typeCounter;
ProtectCountSet::iterator end = m_protectedValues.end();
for (ProtectCountSet::iterator it = m_protectedValues.begin(); it != end; ++it)
typeCounter(it->first);
m_handleHeap.protectedObjectTypeCounts(typeCounter);
return typeCounter.take();
}
void HandleHeap::protectedObjectTypeCounts(TypeCounter& typeCounter)
{
Node* end = m_strongList.end();
for (Node* node = m_strongList.begin(); node != end; node = node->next()) {
JSValue value = *node->slot();
if (value && value.isCell())
typeCounter(value.asCell());
}
}
PassOwnPtr<TypeCountSet> Heap::objectTypeCounts()
{
TypeCounter typeCounter;
forEach(typeCounter);
return typeCounter.take();
}
bool Heap::isBusy()
{
return m_operationInProgress != NoOperation;
}
void Heap::collectAllGarbage()
{
if (!m_globalData->dynamicGlobalObject)
m_globalData->recompileAllJSFunctions();
reset(DoSweep);
}
void Heap::reset(SweepToggle sweepToggle)
{
ASSERT(globalData()->identifierTable == wtfThreadData().currentIdentifierTable());
JAVASCRIPTCORE_GC_BEGIN();
markRoots();
m_handleHeap.finalizeWeakHandles();
JAVASCRIPTCORE_GC_MARKED();
m_markedSpace.reset();
m_extraCost = 0;
#if ENABLE(JSC_ZOMBIES)
sweepToggle = DoSweep;
#endif
if (sweepToggle == DoSweep) {
m_markedSpace.sweep();
m_markedSpace.shrink();
}
// To avoid pathological GC churn in large heaps, we set the allocation high
// water mark to be proportional to the current size of the heap. The exact
// proportion is a bit arbitrary. A 2X multiplier gives a 1:1 (heap size :
// new bytes allocated) proportion, and seems to work well in benchmarks.
size_t proportionalBytes = 2 * m_markedSpace.size();
m_markedSpace.setHighWaterMark(max(proportionalBytes, minBytesPerCycle));
JAVASCRIPTCORE_GC_END();
(*m_activityCallback)();
}
void Heap::setActivityCallback(PassOwnPtr<GCActivityCallback> activityCallback)
{
m_activityCallback = activityCallback;
}
GCActivityCallback* Heap::activityCallback()
{
return m_activityCallback.get();
}
} // namespace JSC
|