]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/HeapStatistics.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / heap / HeapStatistics.cpp
1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "HeapStatistics.h"
28
29 #include "Heap.h"
30 #include "HeapIterationScope.h"
31 #include "JSObject.h"
32 #include "JSCInlines.h"
33 #include "Options.h"
34 #include <stdlib.h>
35 #if OS(UNIX)
36 #include <sys/resource.h>
37 #endif
38 #include <wtf/CurrentTime.h>
39 #include <wtf/DataLog.h>
40 #include <wtf/Deque.h>
41
42 namespace JSC {
43
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;
48
49 #if OS(UNIX)
50
51 void HeapStatistics::initialize()
52 {
53 ASSERT(Options::recordGCPauseTimes());
54 s_startTime = WTF::monotonicallyIncreasingTime();
55 s_pauseTimeStarts = new Vector<double>();
56 s_pauseTimeEnds = new Vector<double>();
57 }
58
59 void HeapStatistics::recordGCPauseTime(double start, double end)
60 {
61 ASSERT(Options::recordGCPauseTimes());
62 ASSERT(s_pauseTimeStarts);
63 ASSERT(s_pauseTimeEnds);
64 s_pauseTimeStarts->append(start);
65 s_pauseTimeEnds->append(end);
66 }
67
68 void HeapStatistics::logStatistics()
69 {
70 struct rusage usage;
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");
76 #else
77 #error "The HeapStatistics module is not supported on this platform."
78 #endif
79 if (!vmName || !suiteName || !benchmarkName)
80 dataLogF("HeapStatistics: {\"max_rss\": %ld", usage.ru_maxrss);
81 else
82 dataLogF("HeapStatistics: {\"max_rss\": %ld, \"vm_name\": \"%s\", \"suite_name\": \"%s\", \"benchmark_name\": \"%s\"",
83 usage.ru_maxrss, vmName, suiteName, benchmarkName);
84
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);
91 ++startIt;
92 ++endIt;
93 }
94 while (startIt != s_pauseTimeStarts->end() && endIt != s_pauseTimeEnds->end()) {
95 dataLogF(", [%f, %f]", *startIt, *endIt);
96 ++startIt;
97 ++endIt;
98 }
99 dataLogF("], \"start_time\": %f, \"end_time\": %f", s_startTime, s_endTime);
100 }
101 dataLogF("}\n");
102 }
103
104 void HeapStatistics::exitWithFailure()
105 {
106 ASSERT(Options::logHeapStatisticsAtExit());
107 s_endTime = WTF::monotonicallyIncreasingTime();
108 logStatistics();
109 exit(-1);
110 }
111
112 void HeapStatistics::reportSuccess()
113 {
114 ASSERT(Options::logHeapStatisticsAtExit());
115 s_endTime = WTF::monotonicallyIncreasingTime();
116 logStatistics();
117 }
118
119 #else
120
121 void HeapStatistics::initialize()
122 {
123 }
124
125 void HeapStatistics::recordGCPauseTime(double, double)
126 {
127 }
128
129 void HeapStatistics::logStatistics()
130 {
131 }
132
133 void HeapStatistics::exitWithFailure()
134 {
135 }
136
137 void HeapStatistics::reportSuccess()
138 {
139 }
140
141 #endif // OS(UNIX)
142
143 size_t HeapStatistics::parseMemoryAmount(char* s)
144 {
145 size_t multiplier = 1;
146 char* afterS;
147 size_t value = strtol(s, &afterS, 10);
148 char next = afterS[0];
149 switch (next) {
150 case 'K':
151 multiplier = KB;
152 break;
153 case 'M':
154 multiplier = MB;
155 break;
156 case 'G':
157 multiplier = GB;
158 break;
159 default:
160 break;
161 }
162 return value * multiplier;
163 }
164
165 class StorageStatistics : public MarkedBlock::VoidFunctor {
166 public:
167 StorageStatistics();
168
169 void operator()(JSCell*);
170
171 size_t objectWithOutOfLineStorageCount();
172 size_t objectCount();
173
174 size_t storageSize();
175 size_t storageCapacity();
176
177 private:
178 size_t m_objectWithOutOfLineStorageCount;
179 size_t m_objectCount;
180 size_t m_storageSize;
181 size_t m_storageCapacity;
182 };
183
184 inline StorageStatistics::StorageStatistics()
185 : m_objectWithOutOfLineStorageCount(0)
186 , m_objectCount(0)
187 , m_storageSize(0)
188 , m_storageCapacity(0)
189 {
190 }
191
192 inline void StorageStatistics::operator()(JSCell* cell)
193 {
194 if (!cell->isObject())
195 return;
196
197 JSObject* object = jsCast<JSObject*>(cell);
198 if (hasIndexedProperties(object->indexingType()))
199 return;
200
201 if (object->structure()->isUncacheableDictionary())
202 return;
203
204 ++m_objectCount;
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>);
209 }
210
211 inline size_t StorageStatistics::objectWithOutOfLineStorageCount()
212 {
213 return m_objectWithOutOfLineStorageCount;
214 }
215
216 inline size_t StorageStatistics::objectCount()
217 {
218 return m_objectCount;
219 }
220
221 inline size_t StorageStatistics::storageSize()
222 {
223 return m_storageSize;
224 }
225
226 inline size_t StorageStatistics::storageCapacity()
227 {
228 return m_storageCapacity;
229 }
230
231 void HeapStatistics::showObjectStatistics(Heap* heap)
232 {
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);
237
238 StorageStatistics storageStatistics;
239 {
240 HeapIterationScope iterationScope(*heap);
241 heap->m_objectSpace.forEachLiveCell(iterationScope, storageStatistics);
242 }
243 dataLogF("wasted .property storage: %ldkB (%ld%%)\n",
244 static_cast<long>(
245 (storageStatistics.storageCapacity() - storageStatistics.storageSize()) / KB),
246 static_cast<long>(
247 (storageStatistics.storageCapacity() - storageStatistics.storageSize()) * 100
248 / storageStatistics.storageCapacity()));
249 dataLogF("objects with out-of-line .property storage: %ld (%ld%%)\n",
250 static_cast<long>(
251 storageStatistics.objectWithOutOfLineStorageCount()),
252 static_cast<long>(
253 storageStatistics.objectWithOutOfLineStorageCount() * 100
254 / storageStatistics.objectCount()));
255 }
256
257 } // namespace JSC