]> git.saurik.com Git - apple/javascriptcore.git/blame - bytecode/SamplingTool.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / bytecode / SamplingTool.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 2008, 2009 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
81345200 13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
9dae56ea
A
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "SamplingTool.h"
31
32#include "CodeBlock.h"
33#include "Interpreter.h"
34#include "Opcode.h"
81345200 35#include "JSCInlines.h"
9dae56ea 36
f9bf01c6 37#if !OS(WINDOWS)
9dae56ea
A
38#include <unistd.h>
39#endif
40
41namespace JSC {
42
ba379fdc
A
43#if ENABLE(SAMPLING_FLAGS)
44
45void SamplingFlags::sample()
9dae56ea 46{
4e4e5a6f 47 uint32_t mask = static_cast<uint32_t>(1 << 31);
ba379fdc
A
48 unsigned index;
49
50 for (index = 0; index < 32; ++index) {
51 if (mask & s_flags)
52 break;
53 mask >>= 1;
9dae56ea
A
54 }
55
ba379fdc
A
56 s_flagCounts[32 - index]++;
57}
9dae56ea 58
ba379fdc
A
59void SamplingFlags::start()
60{
61 for (unsigned i = 0; i <= 32; ++i)
62 s_flagCounts[i] = 0;
9dae56ea 63}
ba379fdc
A
64void SamplingFlags::stop()
65{
66 uint64_t total = 0;
67 for (unsigned i = 0; i <= 32; ++i)
68 total += s_flagCounts[i];
69
70 if (total) {
93a37866 71 dataLogF("\nSamplingFlags: sample counts with flags set: (%lld total)\n", total);
ba379fdc
A
72 for (unsigned i = 0; i <= 32; ++i) {
73 if (s_flagCounts[i])
93a37866 74 dataLogF(" [ %02d ] : %lld\t\t(%03.2f%%)\n", i, s_flagCounts[i], (100.0 * s_flagCounts[i]) / total);
ba379fdc 75 }
93a37866 76 dataLogF("\n");
ba379fdc 77 } else
93a37866 78 dataLogF("\nSamplingFlags: no samples.\n\n");
ba379fdc
A
79}
80uint64_t SamplingFlags::s_flagCounts[33];
81
82#else
83void SamplingFlags::start() {}
84void SamplingFlags::stop() {}
85#endif
86
6fe7ccc8
A
87#if ENABLE(SAMPLING_REGIONS)
88volatile uintptr_t SamplingRegion::s_currentOrReserved;
89Spectrum<const char*>* SamplingRegion::s_spectrum;
90unsigned long SamplingRegion::s_noneOfTheAbove;
91unsigned SamplingRegion::s_numberOfSamplesSinceDump;
92
93SamplingRegion::Locker::Locker()
94{
95 uintptr_t previous;
96 while (true) {
97 previous = s_currentOrReserved;
98 if (previous & 1) {
99#if OS(UNIX)
100 sched_yield();
101#endif
102 continue;
103 }
104 if (WTF::weakCompareAndSwapUIntPtr(&s_currentOrReserved, previous, previous | 1))
105 break;
106 }
107}
108
109SamplingRegion::Locker::~Locker()
110{
111 // We don't need the CAS, but we do it out of an
112 // abundance of caution (and because it gives us a memory fence, which is
113 // never bad).
114 uintptr_t previous;
115 do {
116 previous = s_currentOrReserved;
117 } while (!WTF::weakCompareAndSwapUIntPtr(&s_currentOrReserved, previous, previous & ~1));
118}
119
120void SamplingRegion::sample()
121{
122 // Make sure we lock s_current.
123 Locker locker;
124
125 // Create a spectrum if we don't have one already.
126 if (!s_spectrum)
127 s_spectrum = new Spectrum<const char*>();
128
129 ASSERT(s_currentOrReserved & 1);
130
131 // Walk the region stack, and record each region we see.
132 SamplingRegion* region = bitwise_cast<SamplingRegion*>(s_currentOrReserved & ~1);
133 if (region) {
134 for (; region; region = region->m_previous)
135 s_spectrum->add(region->m_name);
136 } else
137 s_noneOfTheAbove++;
138
139 if (s_numberOfSamplesSinceDump++ == SamplingThread::s_hertz) {
140 s_numberOfSamplesSinceDump = 0;
141 dumpInternal();
142 }
143}
144
145void SamplingRegion::dump()
146{
147 Locker locker;
148
149 dumpInternal();
150}
151
152void SamplingRegion::dumpInternal()
153{
154 if (!s_spectrum) {
93a37866 155 dataLogF("\nSamplingRegion: was never sampled.\n\n");
6fe7ccc8
A
156 return;
157 }
158
159 Vector<Spectrum<const char*>::KeyAndCount> list = s_spectrum->buildList();
160
161 unsigned long total = s_noneOfTheAbove;
162 for (unsigned i = list.size(); i--;)
163 total += list[i].count;
164
93a37866 165 dataLogF("\nSamplingRegion: sample counts for regions: (%lu samples)\n", total);
6fe7ccc8
A
166
167 for (unsigned i = list.size(); i--;)
93a37866 168 dataLogF(" %3.2lf%% %s\n", (100.0 * list[i].count) / total, list[i].key);
6fe7ccc8
A
169}
170#else // ENABLE(SAMPLING_REGIONS)
171void SamplingRegion::dump() { }
172#endif // ENABLE(SAMPLING_REGIONS)
173
ba379fdc
A
174/*
175 Start with flag 16 set.
176 By doing this the monitoring of lower valued flags will be masked out
177 until flag 16 is explictly cleared.
178*/
179uint32_t SamplingFlags::s_flags = 1 << 15;
180
9dae56ea 181
f9bf01c6 182#if OS(WINDOWS)
9dae56ea
A
183
184static void sleepForMicroseconds(unsigned us)
185{
186 unsigned ms = us / 1000;
187 if (us && !ms)
188 ms = 1;
189 Sleep(ms);
190}
191
192#else
193
194static void sleepForMicroseconds(unsigned us)
195{
196 usleep(us);
197}
198
199#endif
200
201static inline unsigned hertz2us(unsigned hertz)
202{
203 return 1000000 / hertz;
204}
205
ba379fdc
A
206
207SamplingTool* SamplingTool::s_samplingTool = 0;
208
209
210bool SamplingThread::s_running = false;
211unsigned SamplingThread::s_hertz = 10000;
212ThreadIdentifier SamplingThread::s_samplingThread;
213
6fe7ccc8 214void SamplingThread::threadStartFunc(void*)
9dae56ea 215{
ba379fdc
A
216 while (s_running) {
217 sleepForMicroseconds(hertz2us(s_hertz));
9dae56ea 218
ba379fdc
A
219#if ENABLE(SAMPLING_FLAGS)
220 SamplingFlags::sample();
221#endif
6fe7ccc8
A
222#if ENABLE(SAMPLING_REGIONS)
223 SamplingRegion::sample();
224#endif
ba379fdc
A
225#if ENABLE(OPCODE_SAMPLING)
226 SamplingTool::sample();
227#endif
228 }
ba379fdc 229}
9dae56ea 230
9dae56ea 231
ba379fdc
A
232void SamplingThread::start(unsigned hertz)
233{
234 ASSERT(!s_running);
235 s_running = true;
236 s_hertz = hertz;
9dae56ea 237
ba379fdc
A
238 s_samplingThread = createThread(threadStartFunc, 0, "JavaScriptCore::Sampler");
239}
240
241void SamplingThread::stop()
242{
243 ASSERT(s_running);
244 s_running = false;
6fe7ccc8 245 waitForThreadCompletion(s_samplingThread);
ba379fdc
A
246}
247
248
f9bf01c6 249void ScriptSampleRecord::sample(CodeBlock* codeBlock, Instruction* vPC)
ba379fdc
A
250{
251 if (!m_samples) {
252 m_size = codeBlock->instructions().size();
253 m_samples = static_cast<int*>(calloc(m_size, sizeof(int)));
254 m_codeBlock = codeBlock;
255 }
256
257 ++m_sampleCount;
258
259 unsigned offest = vPC - codeBlock->instructions().begin();
260 // Since we don't read and write codeBlock and vPC atomically, this check
261 // can fail if we sample mid op_call / op_ret.
262 if (offest < m_size) {
263 m_samples[offest]++;
264 m_opcodeSampleCount++;
265 }
266}
267
268void SamplingTool::doRun()
269{
270 Sample sample(m_sample, m_codeBlock);
271 ++m_sampleCount;
272
273 if (sample.isNull())
274 return;
275
276 if (!sample.inHostFunction()) {
277 unsigned opcodeID = m_interpreter->getOpcodeID(sample.vPC()[0].u.opcode);
278
279 ++m_opcodeSampleCount;
280 ++m_opcodeSamples[opcodeID];
281
282 if (sample.inCTIFunction())
283 m_opcodeSamplesInCTIFunctions[opcodeID]++;
284 }
9dae56ea
A
285
286#if ENABLE(CODEBLOCK_SAMPLING)
ba379fdc 287 if (CodeBlock* codeBlock = sample.codeBlock()) {
f9bf01c6
A
288 MutexLocker locker(m_scriptSampleMapMutex);
289 ScriptSampleRecord* record = m_scopeSampleMap->get(codeBlock->ownerExecutable());
9dae56ea 290 ASSERT(record);
ba379fdc 291 record->sample(codeBlock, sample.vPC());
9dae56ea 292 }
ba379fdc 293#endif
9dae56ea
A
294}
295
ba379fdc 296void SamplingTool::sample()
9dae56ea 297{
ba379fdc 298 s_samplingTool->doRun();
9dae56ea
A
299}
300
93a37866 301void SamplingTool::notifyOfScope(VM& vm, ScriptExecutable* script)
9dae56ea 302{
ba379fdc 303#if ENABLE(CODEBLOCK_SAMPLING)
f9bf01c6 304 MutexLocker locker(m_scriptSampleMapMutex);
93a37866 305 m_scopeSampleMap->set(script, adoptPtr(new ScriptSampleRecord(vm, script)));
ba379fdc 306#else
93a37866 307 UNUSED_PARAM(vm);
f9bf01c6 308 UNUSED_PARAM(script);
ba379fdc 309#endif
9dae56ea
A
310}
311
ba379fdc 312void SamplingTool::setup()
9dae56ea 313{
ba379fdc 314 s_samplingTool = this;
9dae56ea
A
315}
316
317#if ENABLE(OPCODE_SAMPLING)
318
319struct OpcodeSampleInfo {
320 OpcodeID opcode;
321 long long count;
322 long long countInCTIFunctions;
323};
324
325struct LineCountInfo {
326 unsigned line;
327 unsigned count;
328};
329
9dae56ea
A
330static int compareOpcodeIndicesSampling(const void* left, const void* right)
331{
332 const OpcodeSampleInfo* leftSampleInfo = reinterpret_cast<const OpcodeSampleInfo*>(left);
333 const OpcodeSampleInfo* rightSampleInfo = reinterpret_cast<const OpcodeSampleInfo*>(right);
334
335 return (leftSampleInfo->count < rightSampleInfo->count) ? 1 : (leftSampleInfo->count > rightSampleInfo->count) ? -1 : 0;
336}
337
ba379fdc
A
338#if ENABLE(CODEBLOCK_SAMPLING)
339static int compareLineCountInfoSampling(const void* left, const void* right)
340{
341 const LineCountInfo* leftLineCount = reinterpret_cast<const LineCountInfo*>(left);
342 const LineCountInfo* rightLineCount = reinterpret_cast<const LineCountInfo*>(right);
343
344 return (leftLineCount->line > rightLineCount->line) ? 1 : (leftLineCount->line < rightLineCount->line) ? -1 : 0;
345}
346
f9bf01c6 347static int compareScriptSampleRecords(const void* left, const void* right)
9dae56ea 348{
f9bf01c6
A
349 const ScriptSampleRecord* const leftValue = *static_cast<const ScriptSampleRecord* const *>(left);
350 const ScriptSampleRecord* const rightValue = *static_cast<const ScriptSampleRecord* const *>(right);
9dae56ea
A
351
352 return (leftValue->m_sampleCount < rightValue->m_sampleCount) ? 1 : (leftValue->m_sampleCount > rightValue->m_sampleCount) ? -1 : 0;
353}
ba379fdc 354#endif
9dae56ea
A
355
356void SamplingTool::dump(ExecState* exec)
357{
358 // Tidies up SunSpider output by removing short scripts - such a small number of samples would likely not be useful anyhow.
359 if (m_sampleCount < 10)
360 return;
361
362 // (1) Build and sort 'opcodeSampleInfo' array.
363
364 OpcodeSampleInfo opcodeSampleInfo[numOpcodeIDs];
365 for (int i = 0; i < numOpcodeIDs; ++i) {
366 opcodeSampleInfo[i].opcode = static_cast<OpcodeID>(i);
367 opcodeSampleInfo[i].count = m_opcodeSamples[i];
368 opcodeSampleInfo[i].countInCTIFunctions = m_opcodeSamplesInCTIFunctions[i];
369 }
370
371 qsort(opcodeSampleInfo, numOpcodeIDs, sizeof(OpcodeSampleInfo), compareOpcodeIndicesSampling);
372
373 // (2) Print Opcode sampling results.
374
93a37866
A
375 dataLogF("\nBytecode samples [*]\n");
376 dataLogF(" sample %% of %% of | cti cti %%\n");
377 dataLogF("opcode count VM total | count of self\n");
378 dataLogF("------------------------------------------------------- | ----------------\n");
9dae56ea
A
379
380 for (int i = 0; i < numOpcodeIDs; ++i) {
381 long long count = opcodeSampleInfo[i].count;
382 if (!count)
383 continue;
384
385 OpcodeID opcodeID = opcodeSampleInfo[i].opcode;
386
387 const char* opcodeName = opcodeNames[opcodeID];
388 const char* opcodePadding = padOpcodeName(opcodeID, 28);
389 double percentOfVM = (static_cast<double>(count) * 100) / m_opcodeSampleCount;
390 double percentOfTotal = (static_cast<double>(count) * 100) / m_sampleCount;
391 long long countInCTIFunctions = opcodeSampleInfo[i].countInCTIFunctions;
392 double percentInCTIFunctions = (static_cast<double>(countInCTIFunctions) * 100) / count;
6fe7ccc8 393 debugDebugPrintf("%s:%s%-6lld %.3f%%\t%.3f%%\t | %-6lld %.3f%%\n", opcodeName, opcodePadding, count, percentOfVM, percentOfTotal, countInCTIFunctions, percentInCTIFunctions);
9dae56ea
A
394 }
395
93a37866
A
396 dataLogF("\n[*] Samples inside host code are not charged to any Bytecode.\n\n");
397 dataLogF("\tSamples inside VM:\t\t%lld / %lld (%.3f%%)\n", m_opcodeSampleCount, m_sampleCount, (static_cast<double>(m_opcodeSampleCount) * 100) / m_sampleCount);
398 dataLogF("\tSamples inside host code:\t%lld / %lld (%.3f%%)\n\n", m_sampleCount - m_opcodeSampleCount, m_sampleCount, (static_cast<double>(m_sampleCount - m_opcodeSampleCount) * 100) / m_sampleCount);
399 dataLogF("\tsample count:\tsamples inside this opcode\n");
400 dataLogF("\t%% of VM:\tsample count / all opcode samples\n");
401 dataLogF("\t%% of total:\tsample count / all samples\n");
402 dataLogF("\t--------------\n");
403 dataLogF("\tcti count:\tsamples inside a CTI function called by this opcode\n");
404 dataLogF("\tcti %% of self:\tcti count / sample count\n");
9dae56ea 405
ba379fdc
A
406#if ENABLE(CODEBLOCK_SAMPLING)
407
9dae56ea
A
408 // (3) Build and sort 'codeBlockSamples' array.
409
410 int scopeCount = m_scopeSampleMap->size();
f9bf01c6
A
411 Vector<ScriptSampleRecord*> codeBlockSamples(scopeCount);
412 ScriptSampleRecordMap::iterator iter = m_scopeSampleMap->begin();
9dae56ea 413 for (int i = 0; i < scopeCount; ++i, ++iter)
93a37866 414 codeBlockSamples[i] = iter->value.get();
9dae56ea 415
f9bf01c6 416 qsort(codeBlockSamples.begin(), scopeCount, sizeof(ScriptSampleRecord*), compareScriptSampleRecords);
9dae56ea
A
417
418 // (4) Print data from 'codeBlockSamples' array.
419
93a37866 420 dataLogF("\nCodeBlock samples\n\n");
9dae56ea
A
421
422 for (int i = 0; i < scopeCount; ++i) {
f9bf01c6 423 ScriptSampleRecord* record = codeBlockSamples[i];
9dae56ea
A
424 CodeBlock* codeBlock = record->m_codeBlock;
425
426 double blockPercent = (record->m_sampleCount * 100.0) / m_sampleCount;
427
428 if (blockPercent >= 1) {
429 //Instruction* code = codeBlock->instructions().begin();
93a37866 430 dataLogF("#%d: %s:%d: %d / %lld (%.3f%%)\n", i + 1, record->m_executable->sourceURL().utf8().data(), codeBlock->lineNumberForBytecodeOffset(0), record->m_sampleCount, m_sampleCount, blockPercent);
9dae56ea
A
431 if (i < 10) {
432 HashMap<unsigned,unsigned> lineCounts;
433 codeBlock->dump(exec);
434
93a37866 435 dataLogF(" Opcode and line number samples [*]\n\n");
9dae56ea
A
436 for (unsigned op = 0; op < record->m_size; ++op) {
437 int count = record->m_samples[op];
438 if (count) {
93a37866 439 dataLogF(" [% 4d] has sample count: % 4d\n", op, count);
14957cd0 440 unsigned line = codeBlock->lineNumberForBytecodeOffset(op);
9dae56ea
A
441 lineCounts.set(line, (lineCounts.contains(line) ? lineCounts.get(line) : 0) + count);
442 }
443 }
93a37866 444 dataLogF("\n");
9dae56ea
A
445
446 int linesCount = lineCounts.size();
447 Vector<LineCountInfo> lineCountInfo(linesCount);
448 int lineno = 0;
449 for (HashMap<unsigned,unsigned>::iterator iter = lineCounts.begin(); iter != lineCounts.end(); ++iter, ++lineno) {
93a37866
A
450 lineCountInfo[lineno].line = iter->key;
451 lineCountInfo[lineno].count = iter->value;
9dae56ea
A
452 }
453
454 qsort(lineCountInfo.begin(), linesCount, sizeof(LineCountInfo), compareLineCountInfoSampling);
455
456 for (lineno = 0; lineno < linesCount; ++lineno) {
93a37866 457 dataLogF(" Line #%d has sample count %d.\n", lineCountInfo[lineno].line, lineCountInfo[lineno].count);
9dae56ea 458 }
93a37866
A
459 dataLogF("\n");
460 dataLogF(" [*] Samples inside host code are charged to the calling Bytecode.\n");
461 dataLogF(" Samples on a call / return boundary are not charged to a specific opcode or line.\n\n");
462 dataLogF(" Samples on a call / return boundary: %d / %d (%.3f%%)\n\n", record->m_sampleCount - record->m_opcodeSampleCount, record->m_sampleCount, (static_cast<double>(record->m_sampleCount - record->m_opcodeSampleCount) * 100) / record->m_sampleCount);
9dae56ea
A
463 }
464 }
465 }
ba379fdc
A
466#else
467 UNUSED_PARAM(exec);
468#endif
9dae56ea
A
469}
470
471#else
472
473void SamplingTool::dump(ExecState*)
474{
475}
476
477#endif
478
479} // namespace JSC