1 | /* |
2 | * Copyright (C) 2008 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2008 Cameron Zwarich <[email protected]> |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
15 | * its contributors may be used to endorse or promote products derived |
16 | * from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #include "config.h" |
31 | #include "Opcode.h" |
32 | |
33 | #include "BytecodeStructs.h" |
34 | #include <wtf/PrintStream.h> |
35 | |
36 | #if ENABLE(OPCODE_STATS) |
37 | #include <array> |
38 | #include <wtf/DataLog.h> |
39 | #endif |
40 | |
41 | namespace JSC { |
42 | |
43 | const unsigned opcodeLengths[] = { |
44 | #define OPCODE_LENGTH(opcode, length) length, |
45 | FOR_EACH_OPCODE_ID(OPCODE_LENGTH) |
46 | #undef OPCODE_LENGTH |
47 | }; |
48 | |
49 | const char* const opcodeNames[] = { |
50 | #define OPCODE_NAME_ENTRY(opcode, size) #opcode, |
51 | FOR_EACH_OPCODE_ID(OPCODE_NAME_ENTRY) |
52 | #undef OPCODE_NAME_ENTRY |
53 | }; |
54 | |
55 | #if ENABLE(OPCODE_STATS) |
56 | |
57 | inline const char* padOpcodeName(OpcodeID op, unsigned width) |
58 | { |
59 | auto padding = " " ; |
60 | auto paddingLength = strlen(padding); |
61 | auto opcodeNameLength = strlen(opcodeNames[op]); |
62 | if (opcodeNameLength >= width) |
63 | return "" ; |
64 | if (paddingLength + opcodeNameLength < width) |
65 | return padding; |
66 | return &padding[paddingLength + opcodeNameLength - width]; |
67 | } |
68 | |
69 | long long OpcodeStats::opcodeCounts[numOpcodeIDs]; |
70 | long long OpcodeStats::opcodePairCounts[numOpcodeIDs][numOpcodeIDs]; |
71 | int OpcodeStats::lastOpcode = -1; |
72 | |
73 | static OpcodeStats logger; |
74 | |
75 | OpcodeStats::OpcodeStats() |
76 | { |
77 | for (int i = 0; i < numOpcodeIDs; ++i) |
78 | opcodeCounts[i] = 0; |
79 | |
80 | for (int i = 0; i < numOpcodeIDs; ++i) |
81 | for (int j = 0; j < numOpcodeIDs; ++j) |
82 | opcodePairCounts[i][j] = 0; |
83 | } |
84 | |
85 | static int compareOpcodeIndices(const void* left, const void* right) |
86 | { |
87 | long long leftValue = OpcodeStats::opcodeCounts[*(int*) left]; |
88 | long long rightValue = OpcodeStats::opcodeCounts[*(int*) right]; |
89 | |
90 | if (leftValue < rightValue) |
91 | return 1; |
92 | else if (leftValue > rightValue) |
93 | return -1; |
94 | else |
95 | return 0; |
96 | } |
97 | |
98 | static int compareOpcodePairIndices(const void* left, const void* right) |
99 | { |
100 | std::pair<int, int> leftPair = *(std::pair<int, int>*) left; |
101 | long long leftValue = OpcodeStats::opcodePairCounts[leftPair.first][leftPair.second]; |
102 | std::pair<int, int> rightPair = *(std::pair<int, int>*) right; |
103 | long long rightValue = OpcodeStats::opcodePairCounts[rightPair.first][rightPair.second]; |
104 | |
105 | if (leftValue < rightValue) |
106 | return 1; |
107 | else if (leftValue > rightValue) |
108 | return -1; |
109 | else |
110 | return 0; |
111 | } |
112 | |
113 | OpcodeStats::~OpcodeStats() |
114 | { |
115 | long long totalInstructions = 0; |
116 | for (int i = 0; i < numOpcodeIDs; ++i) |
117 | totalInstructions += opcodeCounts[i]; |
118 | |
119 | long long totalInstructionPairs = 0; |
120 | for (int i = 0; i < numOpcodeIDs; ++i) |
121 | for (int j = 0; j < numOpcodeIDs; ++j) |
122 | totalInstructionPairs += opcodePairCounts[i][j]; |
123 | |
124 | std::array<int, numOpcodeIDs> sortedIndices; |
125 | for (int i = 0; i < numOpcodeIDs; ++i) |
126 | sortedIndices[i] = i; |
127 | qsort(sortedIndices.data(), numOpcodeIDs, sizeof(int), compareOpcodeIndices); |
128 | |
129 | std::pair<int, int> sortedPairIndices[numOpcodeIDs * numOpcodeIDs]; |
130 | std::pair<int, int>* currentPairIndex = sortedPairIndices; |
131 | for (int i = 0; i < numOpcodeIDs; ++i) |
132 | for (int j = 0; j < numOpcodeIDs; ++j) |
133 | *(currentPairIndex++) = std::make_pair(i, j); |
134 | qsort(sortedPairIndices, numOpcodeIDs * numOpcodeIDs, sizeof(std::pair<int, int>), compareOpcodePairIndices); |
135 | |
136 | dataLogF("\nExecuted opcode statistics\n" ); |
137 | |
138 | dataLogF("Total instructions executed: %lld\n\n" , totalInstructions); |
139 | |
140 | dataLogF("All opcodes by frequency:\n\n" ); |
141 | |
142 | for (int i = 0; i < numOpcodeIDs; ++i) { |
143 | int index = sortedIndices[i]; |
144 | dataLogF("%s:%s %lld - %.2f%%\n" , opcodeNames[index], padOpcodeName((OpcodeID)index, 28), opcodeCounts[index], ((double) opcodeCounts[index]) / ((double) totalInstructions) * 100.0); |
145 | } |
146 | |
147 | dataLogF("\n" ); |
148 | dataLogF("2-opcode sequences by frequency: %lld\n\n" , totalInstructions); |
149 | |
150 | for (int i = 0; i < numOpcodeIDs * numOpcodeIDs; ++i) { |
151 | std::pair<int, int> indexPair = sortedPairIndices[i]; |
152 | long long count = opcodePairCounts[indexPair.first][indexPair.second]; |
153 | |
154 | if (!count) |
155 | break; |
156 | |
157 | dataLogF("%s%s %s:%s %lld %.2f%%\n" , opcodeNames[indexPair.first], padOpcodeName((OpcodeID)indexPair.first, 28), opcodeNames[indexPair.second], padOpcodeName((OpcodeID)indexPair.second, 28), count, ((double) count) / ((double) totalInstructionPairs) * 100.0); |
158 | } |
159 | |
160 | dataLogF("\n" ); |
161 | dataLogF("Most common opcodes and sequences:\n" ); |
162 | |
163 | for (int i = 0; i < numOpcodeIDs; ++i) { |
164 | int index = sortedIndices[i]; |
165 | long long opcodeCount = opcodeCounts[index]; |
166 | double opcodeProportion = ((double) opcodeCount) / ((double) totalInstructions); |
167 | if (opcodeProportion < 0.0001) |
168 | break; |
169 | dataLogF("\n%s:%s %lld - %.2f%%\n" , opcodeNames[index], padOpcodeName((OpcodeID)index, 28), opcodeCount, opcodeProportion * 100.0); |
170 | |
171 | for (int j = 0; j < numOpcodeIDs * numOpcodeIDs; ++j) { |
172 | std::pair<int, int> indexPair = sortedPairIndices[j]; |
173 | long long pairCount = opcodePairCounts[indexPair.first][indexPair.second]; |
174 | double pairProportion = ((double) pairCount) / ((double) totalInstructionPairs); |
175 | |
176 | if (!pairCount || pairProportion < 0.0001 || pairProportion < opcodeProportion / 100) |
177 | break; |
178 | |
179 | if (indexPair.first != index && indexPair.second != index) |
180 | continue; |
181 | |
182 | dataLogF(" %s%s %s:%s %lld - %.2f%%\n" , opcodeNames[indexPair.first], padOpcodeName((OpcodeID)indexPair.first, 28), opcodeNames[indexPair.second], padOpcodeName((OpcodeID)indexPair.second, 28), pairCount, pairProportion * 100.0); |
183 | } |
184 | |
185 | } |
186 | dataLogF("\n" ); |
187 | } |
188 | |
189 | void OpcodeStats::recordInstruction(int opcode) |
190 | { |
191 | opcodeCounts[opcode]++; |
192 | |
193 | if (lastOpcode != -1) |
194 | opcodePairCounts[lastOpcode][opcode]++; |
195 | |
196 | lastOpcode = opcode; |
197 | } |
198 | |
199 | void OpcodeStats::resetLastInstruction() |
200 | { |
201 | lastOpcode = -1; |
202 | } |
203 | |
204 | #endif |
205 | |
206 | static const unsigned metadataSizes[] = { |
207 | |
208 | #define METADATA_SIZE(size) size, |
209 | FOR_EACH_BYTECODE_METADATA_SIZE(METADATA_SIZE) |
210 | #undef METADATA_SIZE |
211 | |
212 | }; |
213 | |
214 | static const unsigned metadataAlignments[] = { |
215 | |
216 | #define METADATA_ALIGNMENT(size) size, |
217 | FOR_EACH_BYTECODE_METADATA_ALIGNMENT(METADATA_ALIGNMENT) |
218 | #undef METADATA_ALIGNMENT |
219 | |
220 | }; |
221 | |
222 | unsigned metadataSize(OpcodeID opcodeID) |
223 | { |
224 | return metadataSizes[opcodeID]; |
225 | } |
226 | |
227 | unsigned metadataAlignment(OpcodeID opcodeID) |
228 | { |
229 | return metadataAlignments[opcodeID]; |
230 | } |
231 | |
232 | } // namespace JSC |
233 | |
234 | namespace WTF { |
235 | |
236 | using namespace JSC; |
237 | |
238 | void printInternal(PrintStream& out, OpcodeID opcode) |
239 | { |
240 | out.print(opcodeNames[opcode]); |
241 | } |
242 | |
243 | } // namespace WTF |
244 | |