source: webkit/trunk/JavaScriptCore/bytecode/SamplingTool.cpp@ 43105

Last change on this file since 43105 was 43105, checked in by [email protected], 16 years ago

2009-04-30 Gavin Barraclough <[email protected]>

Reviewed by Oliver Hunt.

OPCODE_SAMPLING without CODEBLOCK_SAMPLING is currently broken,
since SamplingTool::Sample::isNull() checks the m_codeBlock
member (which is always null without CODEBLOCK_SAMPLING).

Restructure the checks so make this work again.

  • bytecode/SamplingTool.cpp: (JSC::SamplingTool::doRun):
  • bytecode/SamplingTool.h: (JSC::SamplingTool::Sample::isNull):
File size: 12.6 KB
Line 
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.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
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"
35
36#if !PLATFORM(WIN_OS)
37#include <unistd.h>
38#endif
39
40namespace JSC {
41
42#if ENABLE(SAMPLING_FLAGS)
43
44void SamplingFlags::sample()
45{
46 uint32_t mask = 1 << 31;
47 unsigned index;
48
49 for (index = 0; index < 32; ++index) {
50 if (mask & s_flags)
51 break;
52 mask >>= 1;
53 }
54
55 s_flagCounts[32 - index]++;
56}
57
58void SamplingFlags::start()
59{
60 for (unsigned i = 0; i <= 32; ++i)
61 s_flagCounts[i] = 0;
62}
63void SamplingFlags::stop()
64{
65 uint64_t total = 0;
66 for (unsigned i = 0; i <= 32; ++i)
67 total += s_flagCounts[i];
68
69 if (total) {
70 printf("\nSamplingFlags: sample counts with flags set: (%lld total)\n", total);
71 for (unsigned i = 0; i <= 32; ++i) {
72 if (s_flagCounts[i])
73 printf(" [ %02d ] : %lld\t\t(%03.2f%%)\n", i, s_flagCounts[i], (100.0 * s_flagCounts[i]) / total);
74 }
75 printf("\n");
76 } else
77 printf("\nSamplingFlags: no samples.\n\n");
78}
79uint64_t SamplingFlags::s_flagCounts[33];
80
81#else
82void SamplingFlags::start() {}
83void SamplingFlags::stop() {}
84#endif
85
86/*
87 Start with flag 16 set.
88 By doing this the monitoring of lower valued flags will be masked out
89 until flag 16 is explictly cleared.
90*/
91uint32_t SamplingFlags::s_flags = 1 << 15;
92
93
94#if PLATFORM(WIN_OS)
95
96static void sleepForMicroseconds(unsigned us)
97{
98 unsigned ms = us / 1000;
99 if (us && !ms)
100 ms = 1;
101 Sleep(ms);
102}
103
104#else
105
106static void sleepForMicroseconds(unsigned us)
107{
108 usleep(us);
109}
110
111#endif
112
113static inline unsigned hertz2us(unsigned hertz)
114{
115 return 1000000 / hertz;
116}
117
118
119SamplingTool* SamplingTool::s_samplingTool = 0;
120
121
122bool SamplingThread::s_running = false;
123unsigned SamplingThread::s_hertz = 10000;
124ThreadIdentifier SamplingThread::s_samplingThread;
125
126void* SamplingThread::threadStartFunc(void*)
127{
128 while (s_running) {
129 sleepForMicroseconds(hertz2us(s_hertz));
130
131#if ENABLE(SAMPLING_FLAGS)
132 SamplingFlags::sample();
133#endif
134#if ENABLE(OPCODE_SAMPLING)
135 SamplingTool::sample();
136#endif
137 }
138
139 return 0;
140}
141
142
143void SamplingThread::start(unsigned hertz)
144{
145 ASSERT(!s_running);
146 s_running = true;
147 s_hertz = hertz;
148
149 s_samplingThread = createThread(threadStartFunc, 0, "JavaScriptCore::Sampler");
150}
151
152void SamplingThread::stop()
153{
154 ASSERT(s_running);
155 s_running = false;
156 waitForThreadCompletion(s_samplingThread, 0);
157}
158
159
160void ScopeSampleRecord::sample(CodeBlock* codeBlock, Instruction* vPC)
161{
162 if (!m_samples) {
163 m_size = codeBlock->instructions().size();
164 m_samples = static_cast<int*>(calloc(m_size, sizeof(int)));
165 m_codeBlock = codeBlock;
166 }
167
168 ++m_sampleCount;
169
170 unsigned offest = vPC - codeBlock->instructions().begin();
171 // Since we don't read and write codeBlock and vPC atomically, this check
172 // can fail if we sample mid op_call / op_ret.
173 if (offest < m_size) {
174 m_samples[offest]++;
175 m_opcodeSampleCount++;
176 }
177}
178
179void SamplingTool::doRun()
180{
181 Sample sample(m_sample, m_codeBlock);
182 ++m_sampleCount;
183
184 if (sample.isNull())
185 return;
186
187 if (!sample.inHostFunction()) {
188 unsigned opcodeID = m_interpreter->getOpcodeID(sample.vPC()[0].u.opcode);
189
190 ++m_opcodeSampleCount;
191 ++m_opcodeSamples[opcodeID];
192
193 if (sample.inCTIFunction())
194 m_opcodeSamplesInCTIFunctions[opcodeID]++;
195 }
196
197#if ENABLE(CODEBLOCK_SAMPLING)
198 if (CodeBlock* codeBlock = sample.codeBlock()) {
199 MutexLocker locker(m_scopeSampleMapMutex);
200 ScopeSampleRecord* record = m_scopeSampleMap->get(codeBlock->ownerNode());
201 ASSERT(record);
202 record->sample(codeBlock, sample.vPC());
203 }
204#endif
205}
206
207void SamplingTool::sample()
208{
209 s_samplingTool->doRun();
210}
211
212void SamplingTool::notifyOfScope(ScopeNode* scope)
213{
214 MutexLocker locker(m_scopeSampleMapMutex);
215 m_scopeSampleMap->set(scope, new ScopeSampleRecord(scope));
216}
217
218void SamplingTool::setup()
219{
220 s_samplingTool = this;
221}
222
223#if ENABLE(OPCODE_SAMPLING)
224
225struct OpcodeSampleInfo {
226 OpcodeID opcode;
227 long long count;
228 long long countInCTIFunctions;
229};
230
231struct LineCountInfo {
232 unsigned line;
233 unsigned count;
234};
235
236static int compareLineCountInfoSampling(const void* left, const void* right)
237{
238 const LineCountInfo* leftLineCount = reinterpret_cast<const LineCountInfo*>(left);
239 const LineCountInfo* rightLineCount = reinterpret_cast<const LineCountInfo*>(right);
240
241 return (leftLineCount->line > rightLineCount->line) ? 1 : (leftLineCount->line < rightLineCount->line) ? -1 : 0;
242}
243
244static int compareOpcodeIndicesSampling(const void* left, const void* right)
245{
246 const OpcodeSampleInfo* leftSampleInfo = reinterpret_cast<const OpcodeSampleInfo*>(left);
247 const OpcodeSampleInfo* rightSampleInfo = reinterpret_cast<const OpcodeSampleInfo*>(right);
248
249 return (leftSampleInfo->count < rightSampleInfo->count) ? 1 : (leftSampleInfo->count > rightSampleInfo->count) ? -1 : 0;
250}
251
252static int compareScopeSampleRecords(const void* left, const void* right)
253{
254 const ScopeSampleRecord* const leftValue = *static_cast<const ScopeSampleRecord* const *>(left);
255 const ScopeSampleRecord* const rightValue = *static_cast<const ScopeSampleRecord* const *>(right);
256
257 return (leftValue->m_sampleCount < rightValue->m_sampleCount) ? 1 : (leftValue->m_sampleCount > rightValue->m_sampleCount) ? -1 : 0;
258}
259
260void SamplingTool::dump(ExecState* exec)
261{
262 // Tidies up SunSpider output by removing short scripts - such a small number of samples would likely not be useful anyhow.
263 if (m_sampleCount < 10)
264 return;
265
266 // (1) Build and sort 'opcodeSampleInfo' array.
267
268 OpcodeSampleInfo opcodeSampleInfo[numOpcodeIDs];
269 for (int i = 0; i < numOpcodeIDs; ++i) {
270 opcodeSampleInfo[i].opcode = static_cast<OpcodeID>(i);
271 opcodeSampleInfo[i].count = m_opcodeSamples[i];
272 opcodeSampleInfo[i].countInCTIFunctions = m_opcodeSamplesInCTIFunctions[i];
273 }
274
275 qsort(opcodeSampleInfo, numOpcodeIDs, sizeof(OpcodeSampleInfo), compareOpcodeIndicesSampling);
276
277 // (2) Print Opcode sampling results.
278
279 printf("\nBytecode samples [*]\n");
280 printf(" sample %% of %% of | cti cti %%\n");
281 printf("opcode count VM total | count of self\n");
282 printf("------------------------------------------------------- | ----------------\n");
283
284 for (int i = 0; i < numOpcodeIDs; ++i) {
285 long long count = opcodeSampleInfo[i].count;
286 if (!count)
287 continue;
288
289 OpcodeID opcodeID = opcodeSampleInfo[i].opcode;
290
291 const char* opcodeName = opcodeNames[opcodeID];
292 const char* opcodePadding = padOpcodeName(opcodeID, 28);
293 double percentOfVM = (static_cast<double>(count) * 100) / m_opcodeSampleCount;
294 double percentOfTotal = (static_cast<double>(count) * 100) / m_sampleCount;
295 long long countInCTIFunctions = opcodeSampleInfo[i].countInCTIFunctions;
296 double percentInCTIFunctions = (static_cast<double>(countInCTIFunctions) * 100) / count;
297 fprintf(stdout, "%s:%s%-6lld %.3f%%\t%.3f%%\t | %-6lld %.3f%%\n", opcodeName, opcodePadding, count, percentOfVM, percentOfTotal, countInCTIFunctions, percentInCTIFunctions);
298 }
299
300 printf("\n[*] Samples inside host code are not charged to any Bytecode.\n\n");
301 printf("\tSamples inside VM:\t\t%lld / %lld (%.3f%%)\n", m_opcodeSampleCount, m_sampleCount, (static_cast<double>(m_opcodeSampleCount) * 100) / m_sampleCount);
302 printf("\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);
303 printf("\tsample count:\tsamples inside this opcode\n");
304 printf("\t%% of VM:\tsample count / all opcode samples\n");
305 printf("\t%% of total:\tsample count / all samples\n");
306 printf("\t--------------\n");
307 printf("\tcti count:\tsamples inside a CTI function called by this opcode\n");
308 printf("\tcti %% of self:\tcti count / sample count\n");
309
310 // (3) Build and sort 'codeBlockSamples' array.
311
312 int scopeCount = m_scopeSampleMap->size();
313 Vector<ScopeSampleRecord*> codeBlockSamples(scopeCount);
314 ScopeSampleRecordMap::iterator iter = m_scopeSampleMap->begin();
315 for (int i = 0; i < scopeCount; ++i, ++iter)
316 codeBlockSamples[i] = iter->second;
317
318 qsort(codeBlockSamples.begin(), scopeCount, sizeof(ScopeSampleRecord*), compareScopeSampleRecords);
319
320 // (4) Print data from 'codeBlockSamples' array.
321
322 printf("\nCodeBlock samples\n\n");
323
324 for (int i = 0; i < scopeCount; ++i) {
325 ScopeSampleRecord* record = codeBlockSamples[i];
326 CodeBlock* codeBlock = record->m_codeBlock;
327
328 double blockPercent = (record->m_sampleCount * 100.0) / m_sampleCount;
329
330 if (blockPercent >= 1) {
331 //Instruction* code = codeBlock->instructions().begin();
332 printf("#%d: %s:%d: %d / %lld (%.3f%%)\n", i + 1, record->m_scope->sourceURL().UTF8String().c_str(), codeBlock->lineNumberForBytecodeOffset(exec, 0), record->m_sampleCount, m_sampleCount, blockPercent);
333 if (i < 10) {
334 HashMap<unsigned,unsigned> lineCounts;
335 codeBlock->dump(exec);
336
337 printf(" Opcode and line number samples [*]\n\n");
338 for (unsigned op = 0; op < record->m_size; ++op) {
339 int count = record->m_samples[op];
340 if (count) {
341 printf(" [% 4d] has sample count: % 4d\n", op, count);
342 unsigned line = codeBlock->lineNumberForBytecodeOffset(exec, op);
343 lineCounts.set(line, (lineCounts.contains(line) ? lineCounts.get(line) : 0) + count);
344 }
345 }
346 printf("\n");
347
348 int linesCount = lineCounts.size();
349 Vector<LineCountInfo> lineCountInfo(linesCount);
350 int lineno = 0;
351 for (HashMap<unsigned,unsigned>::iterator iter = lineCounts.begin(); iter != lineCounts.end(); ++iter, ++lineno) {
352 lineCountInfo[lineno].line = iter->first;
353 lineCountInfo[lineno].count = iter->second;
354 }
355
356 qsort(lineCountInfo.begin(), linesCount, sizeof(LineCountInfo), compareLineCountInfoSampling);
357
358 for (lineno = 0; lineno < linesCount; ++lineno) {
359 printf(" Line #%d has sample count %d.\n", lineCountInfo[lineno].line, lineCountInfo[lineno].count);
360 }
361 printf("\n");
362 printf(" [*] Samples inside host code are charged to the calling Bytecode.\n");
363 printf(" Samples on a call / return boundary are not charged to a specific opcode or line.\n\n");
364 printf(" 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);
365 }
366 }
367 }
368}
369
370#else
371
372void SamplingTool::dump(ExecState*)
373{
374}
375
376#endif
377
378} // namespace JSC
Note: See TracBrowser for help on using the repository browser.