2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "HeapStatistics.h"
30 #include "HeapIterationScope.h"
31 #include "JSCInlines.h"
36 #include <sys/resource.h>
38 #include <wtf/CurrentTime.h>
39 #include <wtf/DataLog.h>
40 #include <wtf/Deque.h>
44 double HeapStatistics::s_startTime
= 0.0;
45 double HeapStatistics::s_endTime
= 0.0;
46 Vector
<double>* HeapStatistics::s_pauseTimeStarts
= 0;
47 Vector
<double>* HeapStatistics::s_pauseTimeEnds
= 0;
51 void HeapStatistics::initialize()
53 ASSERT(Options::recordGCPauseTimes());
54 s_startTime
= WTF::monotonicallyIncreasingTime();
55 s_pauseTimeStarts
= new Vector
<double>();
56 s_pauseTimeEnds
= new Vector
<double>();
59 void HeapStatistics::recordGCPauseTime(double start
, double end
)
61 ASSERT(Options::recordGCPauseTimes());
62 ASSERT(s_pauseTimeStarts
);
63 ASSERT(s_pauseTimeEnds
);
64 s_pauseTimeStarts
->append(start
);
65 s_pauseTimeEnds
->append(end
);
68 void HeapStatistics::logStatistics()
71 getrusage(RUSAGE_SELF
, &usage
);
72 #if USE(CF) || OS(UNIX)
73 char* vmName
= getenv("JSVMName");
74 char* suiteName
= getenv("JSSuiteName");
75 char* benchmarkName
= getenv("JSBenchmarkName");
77 #error "The HeapStatistics module is not supported on this platform."
79 if (!vmName
|| !suiteName
|| !benchmarkName
)
80 dataLogF("HeapStatistics: {\"max_rss\": %ld", usage
.ru_maxrss
);
82 dataLogF("HeapStatistics: {\"max_rss\": %ld, \"vm_name\": \"%s\", \"suite_name\": \"%s\", \"benchmark_name\": \"%s\"",
83 usage
.ru_maxrss
, vmName
, suiteName
, benchmarkName
);
85 if (Options::recordGCPauseTimes()) {
86 dataLogF(", \"pause_times\": [");
87 Vector
<double>::iterator startIt
= s_pauseTimeStarts
->begin();
88 Vector
<double>::iterator endIt
= s_pauseTimeEnds
->begin();
89 if (startIt
!= s_pauseTimeStarts
->end() && endIt
!= s_pauseTimeEnds
->end()) {
90 dataLogF("[%f, %f]", *startIt
, *endIt
);
94 while (startIt
!= s_pauseTimeStarts
->end() && endIt
!= s_pauseTimeEnds
->end()) {
95 dataLogF(", [%f, %f]", *startIt
, *endIt
);
99 dataLogF("], \"start_time\": %f, \"end_time\": %f", s_startTime
, s_endTime
);
104 void HeapStatistics::exitWithFailure()
106 ASSERT(Options::logHeapStatisticsAtExit());
107 s_endTime
= WTF::monotonicallyIncreasingTime();
112 void HeapStatistics::reportSuccess()
114 ASSERT(Options::logHeapStatisticsAtExit());
115 s_endTime
= WTF::monotonicallyIncreasingTime();
121 void HeapStatistics::initialize()
125 void HeapStatistics::recordGCPauseTime(double, double)
129 void HeapStatistics::logStatistics()
133 void HeapStatistics::exitWithFailure()
138 void HeapStatistics::reportSuccess()
144 size_t HeapStatistics::parseMemoryAmount(char* s
)
146 size_t multiplier
= 1;
148 size_t value
= strtol(s
, &afterS
, 10);
149 char next
= afterS
[0];
163 return value
* multiplier
;
166 class StorageStatistics
: public MarkedBlock::VoidFunctor
{
170 IterationStatus
operator()(JSCell
*);
172 size_t objectWithOutOfLineStorageCount();
173 size_t objectCount();
175 size_t storageSize();
176 size_t storageCapacity();
181 size_t m_objectWithOutOfLineStorageCount
;
182 size_t m_objectCount
;
183 size_t m_storageSize
;
184 size_t m_storageCapacity
;
187 inline StorageStatistics::StorageStatistics()
188 : m_objectWithOutOfLineStorageCount(0)
191 , m_storageCapacity(0)
195 inline void StorageStatistics::visit(JSCell
* cell
)
197 if (!cell
->isObject())
200 JSObject
* object
= jsCast
<JSObject
*>(cell
);
201 if (hasIndexedProperties(object
->indexingType()))
204 if (object
->structure()->isUncacheableDictionary())
208 if (!object
->hasInlineStorage())
209 ++m_objectWithOutOfLineStorageCount
;
210 m_storageSize
+= object
->structure()->totalStorageSize() * sizeof(WriteBarrierBase
<Unknown
>);
211 m_storageCapacity
+= object
->structure()->totalStorageCapacity() * sizeof(WriteBarrierBase
<Unknown
>);
214 inline IterationStatus
StorageStatistics::operator()(JSCell
* cell
)
217 return IterationStatus::Continue
;
220 inline size_t StorageStatistics::objectWithOutOfLineStorageCount()
222 return m_objectWithOutOfLineStorageCount
;
225 inline size_t StorageStatistics::objectCount()
227 return m_objectCount
;
230 inline size_t StorageStatistics::storageSize()
232 return m_storageSize
;
235 inline size_t StorageStatistics::storageCapacity()
237 return m_storageCapacity
;
240 void HeapStatistics::showObjectStatistics(Heap
* heap
)
242 dataLogF("\n=== Heap Statistics: ===\n");
243 dataLogF("size: %ldkB\n", static_cast<long>(heap
->m_sizeAfterLastCollect
/ KB
));
244 dataLogF("capacity: %ldkB\n", static_cast<long>(heap
->capacity() / KB
));
245 dataLogF("pause time: %lfs\n\n", heap
->m_lastFullGCLength
);
247 StorageStatistics storageStatistics
;
249 HeapIterationScope
iterationScope(*heap
);
250 heap
->m_objectSpace
.forEachLiveCell(iterationScope
, storageStatistics
);
252 long wastedPropertyStorageBytes
= 0;
253 long wastedPropertyStoragePercent
= 0;
254 long objectWithOutOfLineStorageCount
= 0;
255 long objectsWithOutOfLineStoragePercent
= 0;
256 if ((storageStatistics
.storageCapacity() > 0) && (storageStatistics
.objectCount() > 0)) {
257 wastedPropertyStorageBytes
= static_cast<long>((storageStatistics
.storageCapacity() - storageStatistics
.storageSize()) / KB
);
258 wastedPropertyStoragePercent
= static_cast<long>(
259 (storageStatistics
.storageCapacity() - storageStatistics
.storageSize()) * 100 / storageStatistics
.storageCapacity());
260 objectWithOutOfLineStorageCount
= static_cast<long>(storageStatistics
.objectWithOutOfLineStorageCount());
261 objectsWithOutOfLineStoragePercent
= objectWithOutOfLineStorageCount
* 100 / storageStatistics
.objectCount();
263 dataLogF("wasted .property storage: %ldkB (%ld%%)\n", wastedPropertyStorageBytes
, wastedPropertyStoragePercent
);
264 dataLogF("objects with out-of-line .property storage: %ld (%ld%%)\n", objectWithOutOfLineStorageCount
, objectsWithOutOfLineStoragePercent
);