1 | /*
|
---|
2 | * Copyright (C) 2008 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 "Machine.h"
|
---|
34 | #include "Opcode.h"
|
---|
35 |
|
---|
36 | #if !PLATFORM(WIN_OS)
|
---|
37 | #include <unistd.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | namespace KJS {
|
---|
41 |
|
---|
42 | void ScopeSampleRecord::sample(CodeBlock* codeBlock, Instruction* vPC)
|
---|
43 | {
|
---|
44 | m_totalCount++;
|
---|
45 |
|
---|
46 | if (!m_vpcCounts) {
|
---|
47 | m_size = codeBlock->instructions.size();
|
---|
48 | m_vpcCounts = static_cast<int*>(calloc(m_size, sizeof(int)));
|
---|
49 | m_codeBlock = codeBlock;
|
---|
50 | }
|
---|
51 |
|
---|
52 | unsigned codeOffset = static_cast<unsigned>(reinterpret_cast<ptrdiff_t>(vPC) - reinterpret_cast<ptrdiff_t>(codeBlock->instructions.begin())) / sizeof(Instruction*);
|
---|
53 | // This could occur if codeBlock & vPC are not consistent - e.g. sample mid op_call/op_ret.
|
---|
54 | if (codeOffset < m_size)
|
---|
55 | m_vpcCounts[codeOffset]++;
|
---|
56 | }
|
---|
57 |
|
---|
58 | #if PLATFORM(WIN_OS)
|
---|
59 |
|
---|
60 | static void sleepForMicroseconds(unsigned us)
|
---|
61 | {
|
---|
62 | unsigned ms = us / 1000;
|
---|
63 | if (us && !ms)
|
---|
64 | ms = 1;
|
---|
65 | Sleep(ms);
|
---|
66 | }
|
---|
67 |
|
---|
68 | #else
|
---|
69 |
|
---|
70 | static void sleepForMicroseconds(unsigned us)
|
---|
71 | {
|
---|
72 | usleep(us);
|
---|
73 | }
|
---|
74 |
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | static inline unsigned hertz2us(unsigned hertz)
|
---|
78 | {
|
---|
79 | return 1000000 / hertz;
|
---|
80 | }
|
---|
81 |
|
---|
82 | #if ENABLE(SAMPLING_TOOL)
|
---|
83 | extern OpcodeID what;
|
---|
84 | extern unsigned incall;
|
---|
85 | unsigned cowdogs = 0;
|
---|
86 | unsigned sampleCows[numOpcodeIDs] = {0};
|
---|
87 | unsigned sampleDogs[numOpcodeIDs] = {0};
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | void SamplingTool::run()
|
---|
91 | {
|
---|
92 | while (m_running) {
|
---|
93 | sleepForMicroseconds(hertz2us(m_hertz));
|
---|
94 |
|
---|
95 | m_totalSamples++;
|
---|
96 | #if ENABLE(SAMPLING_TOOL)
|
---|
97 | if (what != (OpcodeID)-1) {
|
---|
98 | ++cowdogs;
|
---|
99 | if (incall)
|
---|
100 | sampleDogs[what]++;
|
---|
101 | else
|
---|
102 | sampleCows[what]++;
|
---|
103 | }
|
---|
104 | #endif
|
---|
105 | CodeBlock* codeBlock = m_recordedCodeBlock;
|
---|
106 | Instruction* vPC = m_recordedVPC;
|
---|
107 |
|
---|
108 | if (codeBlock && vPC) {
|
---|
109 | if (ScopeSampleRecord* record = m_scopeSampleMap->get(codeBlock->ownerNode))
|
---|
110 | record->sample(codeBlock, vPC);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | void* SamplingTool::threadStartFunc(void* samplingTool)
|
---|
116 | {
|
---|
117 | reinterpret_cast<SamplingTool*>(samplingTool)->run();
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 |
|
---|
121 | void SamplingTool::notifyOfScope(ScopeNode* scope)
|
---|
122 | {
|
---|
123 | m_scopeSampleMap->set(scope, new ScopeSampleRecord(scope));
|
---|
124 | }
|
---|
125 |
|
---|
126 | void SamplingTool::start(unsigned hertz)
|
---|
127 | {
|
---|
128 | ASSERT(!m_running);
|
---|
129 | m_running = true;
|
---|
130 | m_hertz = hertz;
|
---|
131 |
|
---|
132 | m_samplingThread = createThread(threadStartFunc, this, "JavaScriptCore::Sampler");
|
---|
133 | }
|
---|
134 |
|
---|
135 | void SamplingTool::stop()
|
---|
136 | {
|
---|
137 | ASSERT(m_running);
|
---|
138 | m_running = false;
|
---|
139 | waitForThreadCompletion(m_samplingThread, 0);
|
---|
140 | }
|
---|
141 |
|
---|
142 | #if ENABLE(SAMPLING_TOOL)
|
---|
143 |
|
---|
144 | struct OpcodeSampleInfo
|
---|
145 | {
|
---|
146 | OpcodeID opcode;
|
---|
147 | long long count;
|
---|
148 | long long countincall;
|
---|
149 | };
|
---|
150 |
|
---|
151 | struct LineCountInfo
|
---|
152 | {
|
---|
153 | unsigned line;
|
---|
154 | unsigned count;
|
---|
155 | };
|
---|
156 |
|
---|
157 | static int compareLineCountInfoSampling(const void* left, const void* right)
|
---|
158 | {
|
---|
159 | const LineCountInfo* leftLineCount = reinterpret_cast<const LineCountInfo*>(left);
|
---|
160 | const LineCountInfo* rightLineCount = reinterpret_cast<const LineCountInfo*>(right);
|
---|
161 |
|
---|
162 | return (leftLineCount->line > rightLineCount->line) ? 1 : (leftLineCount->line < rightLineCount->line) ? -1 : 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static int compareOpcodeIndicesSampling(const void* left, const void* right)
|
---|
166 | {
|
---|
167 | const OpcodeSampleInfo* leftSampleInfo = reinterpret_cast<const OpcodeSampleInfo*>(left);
|
---|
168 | const OpcodeSampleInfo* rightSampleInfo = reinterpret_cast<const OpcodeSampleInfo*>(right);
|
---|
169 |
|
---|
170 | return (leftSampleInfo->count < rightSampleInfo->count) ? 1 : (leftSampleInfo->count > rightSampleInfo->count) ? -1 : 0;
|
---|
171 | }
|
---|
172 |
|
---|
173 | static int compareScopeSampleRecords(const void* left, const void* right)
|
---|
174 | {
|
---|
175 | const ScopeSampleRecord* const leftValue = *static_cast<const ScopeSampleRecord* const *>(left);
|
---|
176 | const ScopeSampleRecord* const rightValue = *static_cast<const ScopeSampleRecord* const *>(right);
|
---|
177 |
|
---|
178 | return (leftValue->m_totalCount < rightValue->m_totalCount) ? 1 : (leftValue->m_totalCount > rightValue->m_totalCount) ? -1 : 0;
|
---|
179 | }
|
---|
180 |
|
---|
181 | void SamplingTool::dump(ExecState* exec)
|
---|
182 | {
|
---|
183 | // Tidies up SunSpider output by removing short scripts - such a small number of samples would likely not be useful anyhow.
|
---|
184 | if (m_totalSamples < 10)
|
---|
185 | return;
|
---|
186 |
|
---|
187 | // (1) Calculate 'totalCodeBlockSamples', build and sort 'codeBlockSamples' array.
|
---|
188 |
|
---|
189 | int scopeCount = m_scopeSampleMap->size();
|
---|
190 | long long totalCodeBlockSamples = 0;
|
---|
191 | Vector<ScopeSampleRecord*> codeBlockSamples(scopeCount);
|
---|
192 | ScopeSampleRecordMap::iterator iter = m_scopeSampleMap->begin();
|
---|
193 | for (int i = 0; i < scopeCount; ++i, ++iter) {
|
---|
194 | codeBlockSamples[i] = iter->second;
|
---|
195 | totalCodeBlockSamples += codeBlockSamples[i]->m_totalCount;
|
---|
196 | }
|
---|
197 | #if HAVE(MERGESORT)
|
---|
198 | mergesort(codeBlockSamples.begin(), scopeCount, sizeof(ScopeSampleRecord*), compareScopeSampleRecords);
|
---|
199 | #else
|
---|
200 | qsort(codeBlockSamples.begin(), scopeCount, sizeof(ScopeSampleRecord*), compareScopeSampleRecords);
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | // (2) Print data from 'codeBlockSamples' array, calculate 'totalOpcodeSamples', populate 'opcodeSampleCounts' array.
|
---|
204 |
|
---|
205 | long long totalOpcodeSamples = 0;
|
---|
206 | long long opcodeSampleCounts[numOpcodeIDs] = { 0 };
|
---|
207 |
|
---|
208 | printf("\nBlock sampling results\n\n");
|
---|
209 | printf("Total blocks sampled (total samples): %lld (%lld)\n\n", totalCodeBlockSamples, m_totalSamples);
|
---|
210 |
|
---|
211 | for (int i=0; i < scopeCount; i++) {
|
---|
212 | ScopeSampleRecord* record = codeBlockSamples[i];
|
---|
213 | CodeBlock* codeBlock = record->m_codeBlock;
|
---|
214 |
|
---|
215 | double totalPercent = (record->m_totalCount * 100.0)/m_totalSamples;
|
---|
216 | double blockPercent = (record->m_totalCount * 100.0)/totalCodeBlockSamples;
|
---|
217 |
|
---|
218 | if ((blockPercent >= 1) && codeBlock) {
|
---|
219 | Instruction* code = codeBlock->instructions.begin();
|
---|
220 | printf("#%d: %s:%d: sampled %d times - %.3f%% (%.3f%%)\n", i + 1, record->m_scope->sourceURL().UTF8String().c_str(), codeBlock->lineNumberForVPC(code), record->m_totalCount, blockPercent, totalPercent);
|
---|
221 | if (i < 10) {
|
---|
222 | HashMap<unsigned,unsigned> lineCounts;
|
---|
223 | codeBlock->dump(exec);
|
---|
224 | for (unsigned op = 0; op < record->m_size; ++op) {
|
---|
225 | int count = record->m_vpcCounts[op];
|
---|
226 | if (count) {
|
---|
227 | printf(" [% 4d] has sample count: % 4d\n", op, count);
|
---|
228 | unsigned line = codeBlock->lineNumberForVPC(code+op);
|
---|
229 | lineCounts.set(line, (lineCounts.contains(line) ? lineCounts.get(line) : 0) + count);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | printf("\n");
|
---|
233 | int linesCount = lineCounts.size();
|
---|
234 | Vector<LineCountInfo> lineCountInfo(linesCount);
|
---|
235 | int lineno = 0;
|
---|
236 | for (HashMap<unsigned,unsigned>::iterator iter = lineCounts.begin(); iter != lineCounts.end(); ++iter, ++lineno) {
|
---|
237 | lineCountInfo[lineno].line = iter->first;
|
---|
238 | lineCountInfo[lineno].count = iter->second;
|
---|
239 | }
|
---|
240 | #if HAVE(MERGESORT)
|
---|
241 | mergesort(lineCountInfo.begin(), linesCount, sizeof(LineCountInfo), compareLineCountInfoSampling);
|
---|
242 | #else
|
---|
243 | qsort(lineCountInfo.begin(), linesCount, sizeof(LineCountInfo), compareLineCountInfoSampling);
|
---|
244 | #endif
|
---|
245 | for (lineno = 0; lineno < linesCount; ++lineno) {
|
---|
246 | printf(" Line #%d has sample count %d.\n", lineCountInfo[lineno].line, lineCountInfo[lineno].count);
|
---|
247 | }
|
---|
248 | printf("\n");
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (record->m_vpcCounts && codeBlock) {
|
---|
253 | Instruction* instructions = codeBlock->instructions.begin();
|
---|
254 | for (unsigned op = 0; op < record->m_size; ++op) {
|
---|
255 | Opcode opcode = instructions[op].u.opcode;
|
---|
256 | if (exec->machine()->isOpcode(opcode)) {
|
---|
257 | totalOpcodeSamples += record->m_vpcCounts[op];
|
---|
258 | opcodeSampleCounts[exec->machine()->getOpcodeID(opcode)] += record->m_vpcCounts[op];
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | printf("\n");
|
---|
264 |
|
---|
265 | // (3) Build and sort 'opcodeSampleInfo' array.
|
---|
266 |
|
---|
267 | OpcodeSampleInfo opcodeSampleInfo[numOpcodeIDs];
|
---|
268 | for (int i = 0; i < numOpcodeIDs; ++i) {
|
---|
269 | opcodeSampleInfo[i].opcode = (OpcodeID)i;
|
---|
270 | opcodeSampleInfo[i].count = sampleCows[i]+sampleDogs[i];
|
---|
271 | opcodeSampleInfo[i].countincall = sampleDogs[i];
|
---|
272 | }
|
---|
273 | #if HAVE(MERGESORT)
|
---|
274 | mergesort(opcodeSampleInfo, numOpcodeIDs, sizeof(OpcodeSampleInfo), compareOpcodeIndicesSampling);
|
---|
275 | #else
|
---|
276 | qsort(opcodeSampleInfo, numOpcodeIDs, sizeof(OpcodeSampleInfo), compareOpcodeIndicesSampling);
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | // (4) Print Opcode sampling results.
|
---|
280 |
|
---|
281 |
|
---|
282 | printf("\nOpcode sampling results\n\n");
|
---|
283 |
|
---|
284 | // printf("Total opcodes sampled (total samples): %lld (%lld)\n\n", totalOpcodeSamples, m_totalSamples);
|
---|
285 | // printf("Opcodes in order:\n\n");
|
---|
286 | // for (int i = 0; i < numOpcodeIDs; ++i) {
|
---|
287 | // long long count = opcodeSampleCounts[i];
|
---|
288 | // printf("%s:%s%6lld\t%.3f%%\t(%.3f%%)\n", opcodeNames[i], padOpcodeName(static_cast<OpcodeID>(i), 20), count, (static_cast<double>(count) * 100) / totalOpcodeSamples, (static_cast<double>(count) * 100) / m_totalSamples);
|
---|
289 | // }
|
---|
290 | // printf("\n");
|
---|
291 | // printf("Opcodes by sample count:\n\n");
|
---|
292 |
|
---|
293 | for (int i = 0; i < numOpcodeIDs; ++i) {
|
---|
294 | OpcodeID opcode = opcodeSampleInfo[i].opcode;
|
---|
295 | long long count = opcodeSampleInfo[i].count;
|
---|
296 | // printf("%s:%s%6lld\t%.3f%%\t(%.3f%%)\n", opcodeNames[opcode], padOpcodeName(opcode, 20), count, (static_cast<double>(count) * 100) / totalOpcodeSamples, (static_cast<double>(count) * 100) / m_totalSamples);
|
---|
297 | long long countincall = opcodeSampleInfo[i].countincall;
|
---|
298 | fprintf(stdout, "%s:%s%6lld\t%6lld\t%.3f%%\t%.3f%%\t(%.3f%%)\n", opcodeNames[opcode], padOpcodeName(opcode, 20), count, countincall, ((double)count * 100)/cowdogs, ((double)count * 100)/m_totalSamples, ((double)countincall * 100)/m_totalSamples);
|
---|
299 | }
|
---|
300 | printf("\n");
|
---|
301 | }
|
---|
302 |
|
---|
303 | #else
|
---|
304 |
|
---|
305 | void SamplingTool::dump(ExecState*)
|
---|
306 | {
|
---|
307 | }
|
---|
308 |
|
---|
309 | #endif
|
---|
310 |
|
---|
311 | } // namespace KJS
|
---|