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"
32 #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()
137 void HeapStatistics::reportSuccess()
143 size_t HeapStatistics::parseMemoryAmount(char* s
)
145 size_t multiplier
= 1;
147 size_t value
= strtol(s
, &afterS
, 10);
148 char next
= afterS
[0];
162 return value
* multiplier
;
165 class StorageStatistics
: public MarkedBlock::VoidFunctor
{
169 void operator()(JSCell
*);
171 size_t objectWithOutOfLineStorageCount();
172 size_t objectCount();
174 size_t storageSize();
175 size_t storageCapacity();
178 size_t m_objectWithOutOfLineStorageCount
;
179 size_t m_objectCount
;
180 size_t m_storageSize
;
181 size_t m_storageCapacity
;
184 inline StorageStatistics::StorageStatistics()
185 : m_objectWithOutOfLineStorageCount(0)
188 , m_storageCapacity(0)
192 inline void StorageStatistics::operator()(JSCell
* cell
)
194 if (!cell
->isObject())
197 JSObject
* object
= jsCast
<JSObject
*>(cell
);
198 if (hasIndexedProperties(object
->indexingType()))
201 if (object
->structure()->isUncacheableDictionary())
205 if (!object
->hasInlineStorage())
206 ++m_objectWithOutOfLineStorageCount
;
207 m_storageSize
+= object
->structure()->totalStorageSize() * sizeof(WriteBarrierBase
<Unknown
>);
208 m_storageCapacity
+= object
->structure()->totalStorageCapacity() * sizeof(WriteBarrierBase
<Unknown
>);
211 inline size_t StorageStatistics::objectWithOutOfLineStorageCount()
213 return m_objectWithOutOfLineStorageCount
;
216 inline size_t StorageStatistics::objectCount()
218 return m_objectCount
;
221 inline size_t StorageStatistics::storageSize()
223 return m_storageSize
;
226 inline size_t StorageStatistics::storageCapacity()
228 return m_storageCapacity
;
231 void HeapStatistics::showObjectStatistics(Heap
* heap
)
233 dataLogF("\n=== Heap Statistics: ===\n");
234 dataLogF("size: %ldkB\n", static_cast<long>(heap
->m_sizeAfterLastCollect
/ KB
));
235 dataLogF("capacity: %ldkB\n", static_cast<long>(heap
->capacity() / KB
));
236 dataLogF("pause time: %lfms\n\n", heap
->m_lastFullGCLength
);
238 StorageStatistics storageStatistics
;
240 HeapIterationScope
iterationScope(*heap
);
241 heap
->m_objectSpace
.forEachLiveCell(iterationScope
, storageStatistics
);
243 dataLogF("wasted .property storage: %ldkB (%ld%%)\n",
245 (storageStatistics
.storageCapacity() - storageStatistics
.storageSize()) / KB
),
247 (storageStatistics
.storageCapacity() - storageStatistics
.storageSize()) * 100
248 / storageStatistics
.storageCapacity()));
249 dataLogF("objects with out-of-line .property storage: %ld (%ld%%)\n",
251 storageStatistics
.objectWithOutOfLineStorageCount()),
253 storageStatistics
.objectWithOutOfLineStorageCount() * 100
254 / storageStatistics
.objectCount()));