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"
31 #include "Operations.h"
35 #include <sys/resource.h>
37 #include <wtf/CurrentTime.h>
38 #include <wtf/DataLog.h>
39 #include <wtf/Deque.h>
43 double HeapStatistics::s_startTime
= 0.0;
44 double HeapStatistics::s_endTime
= 0.0;
45 Vector
<double>* HeapStatistics::s_pauseTimeStarts
= 0;
46 Vector
<double>* HeapStatistics::s_pauseTimeEnds
= 0;
50 void HeapStatistics::initialize()
52 ASSERT(Options::recordGCPauseTimes());
53 s_startTime
= WTF::monotonicallyIncreasingTime();
54 s_pauseTimeStarts
= new Vector
<double>();
55 s_pauseTimeEnds
= new Vector
<double>();
58 void HeapStatistics::recordGCPauseTime(double start
, double end
)
60 ASSERT(Options::recordGCPauseTimes());
61 ASSERT(s_pauseTimeStarts
);
62 ASSERT(s_pauseTimeEnds
);
63 s_pauseTimeStarts
->append(start
);
64 s_pauseTimeEnds
->append(end
);
67 void HeapStatistics::logStatistics()
70 getrusage(RUSAGE_SELF
, &usage
);
71 #if USE(CF) || OS(UNIX)
72 char* vmName
= getenv("JSVMName");
73 char* suiteName
= getenv("JSSuiteName");
74 char* benchmarkName
= getenv("JSBenchmarkName");
76 #error "The HeapStatistics module is not supported on this platform."
78 if (!vmName
|| !suiteName
|| !benchmarkName
)
79 dataLogF("HeapStatistics: {\"max_rss\": %ld", usage
.ru_maxrss
);
81 dataLogF("HeapStatistics: {\"max_rss\": %ld, \"vm_name\": \"%s\", \"suite_name\": \"%s\", \"benchmark_name\": \"%s\"",
82 usage
.ru_maxrss
, vmName
, suiteName
, benchmarkName
);
84 if (Options::recordGCPauseTimes()) {
85 dataLogF(", \"pause_times\": [");
86 Vector
<double>::iterator startIt
= s_pauseTimeStarts
->begin();
87 Vector
<double>::iterator endIt
= s_pauseTimeEnds
->begin();
88 if (startIt
!= s_pauseTimeStarts
->end() && endIt
!= s_pauseTimeEnds
->end()) {
89 dataLogF("[%f, %f]", *startIt
, *endIt
);
93 while (startIt
!= s_pauseTimeStarts
->end() && endIt
!= s_pauseTimeEnds
->end()) {
94 dataLogF(", [%f, %f]", *startIt
, *endIt
);
98 dataLogF("], \"start_time\": %f, \"end_time\": %f", s_startTime
, s_endTime
);
103 void HeapStatistics::exitWithFailure()
105 ASSERT(Options::logHeapStatisticsAtExit());
106 s_endTime
= WTF::monotonicallyIncreasingTime();
111 void HeapStatistics::reportSuccess()
113 ASSERT(Options::logHeapStatisticsAtExit());
114 s_endTime
= WTF::monotonicallyIncreasingTime();
120 void HeapStatistics::initialize()
124 void HeapStatistics::recordGCPauseTime(double, double)
128 void HeapStatistics::logStatistics()
132 void HeapStatistics::exitWithFailure()
136 void HeapStatistics::reportSuccess()
142 size_t HeapStatistics::parseMemoryAmount(char* s
)
144 size_t multiplier
= 1;
146 size_t value
= strtol(s
, &afterS
, 10);
147 char next
= afterS
[0];
161 return value
* multiplier
;
164 class StorageStatistics
: public MarkedBlock::VoidFunctor
{
168 void operator()(JSCell
*);
170 size_t objectWithOutOfLineStorageCount();
171 size_t objectCount();
173 size_t storageSize();
174 size_t storageCapacity();
177 size_t m_objectWithOutOfLineStorageCount
;
178 size_t m_objectCount
;
179 size_t m_storageSize
;
180 size_t m_storageCapacity
;
183 inline StorageStatistics::StorageStatistics()
184 : m_objectWithOutOfLineStorageCount(0)
187 , m_storageCapacity(0)
191 inline void StorageStatistics::operator()(JSCell
* cell
)
193 if (!cell
->isObject())
196 JSObject
* object
= jsCast
<JSObject
*>(cell
);
197 if (hasIndexedProperties(object
->structure()->indexingType()))
200 if (object
->structure()->isUncacheableDictionary())
204 if (!object
->hasInlineStorage())
205 ++m_objectWithOutOfLineStorageCount
;
206 m_storageSize
+= object
->structure()->totalStorageSize() * sizeof(WriteBarrierBase
<Unknown
>);
207 m_storageCapacity
+= object
->structure()->totalStorageCapacity() * sizeof(WriteBarrierBase
<Unknown
>);
210 inline size_t StorageStatistics::objectWithOutOfLineStorageCount()
212 return m_objectWithOutOfLineStorageCount
;
215 inline size_t StorageStatistics::objectCount()
217 return m_objectCount
;
220 inline size_t StorageStatistics::storageSize()
222 return m_storageSize
;
225 inline size_t StorageStatistics::storageCapacity()
227 return m_storageCapacity
;
230 void HeapStatistics::showObjectStatistics(Heap
* heap
)
232 dataLogF("\n=== Heap Statistics: ===\n");
233 dataLogF("size: %ldkB\n", static_cast<long>(heap
->m_sizeAfterLastCollect
/ KB
));
234 dataLogF("capacity: %ldkB\n", static_cast<long>(heap
->capacity() / KB
));
235 dataLogF("pause time: %lfms\n\n", heap
->m_lastGCLength
);
237 StorageStatistics storageStatistics
;
238 heap
->m_objectSpace
.forEachLiveCell(storageStatistics
);
239 dataLogF("wasted .property storage: %ldkB (%ld%%)\n",
241 (storageStatistics
.storageCapacity() - storageStatistics
.storageSize()) / KB
),
243 (storageStatistics
.storageCapacity() - storageStatistics
.storageSize()) * 100
244 / storageStatistics
.storageCapacity()));
245 dataLogF("objects with out-of-line .property storage: %ld (%ld%%)\n",
247 storageStatistics
.objectWithOutOfLineStorageCount()),
249 storageStatistics
.objectWithOutOfLineStorageCount() * 100
250 / storageStatistics
.objectCount()));