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 | #ifndef SamplingTool_h
|
---|
30 | #define SamplingTool_h
|
---|
31 |
|
---|
32 | #include <wtf/Assertions.h>
|
---|
33 | #include <wtf/HashMap.h>
|
---|
34 | #include <wtf/Threading.h>
|
---|
35 |
|
---|
36 | #include <nodes.h>
|
---|
37 | #include <Opcode.h>
|
---|
38 |
|
---|
39 | namespace JSC {
|
---|
40 |
|
---|
41 | class ExecState;
|
---|
42 | class ScopeNode;
|
---|
43 | class CodeBlock;
|
---|
44 | struct Instruction;
|
---|
45 |
|
---|
46 | #if ENABLE(SAMPLING_TOOL)
|
---|
47 | extern OpcodeID currentOpcodeID;
|
---|
48 | extern unsigned inCalledCode;
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | struct ScopeSampleRecord {
|
---|
52 | RefPtr<ScopeNode> m_scope;
|
---|
53 | CodeBlock* m_codeBlock;
|
---|
54 | int m_totalCount;
|
---|
55 | int* m_vpcCounts;
|
---|
56 | unsigned m_size;
|
---|
57 |
|
---|
58 | ScopeSampleRecord(ScopeNode* scope)
|
---|
59 | : m_scope(scope)
|
---|
60 | , m_codeBlock(0)
|
---|
61 | , m_totalCount(0)
|
---|
62 | , m_vpcCounts(0)
|
---|
63 | , m_size(0)
|
---|
64 | {
|
---|
65 | }
|
---|
66 |
|
---|
67 | ~ScopeSampleRecord()
|
---|
68 | {
|
---|
69 | if (m_vpcCounts)
|
---|
70 | free(m_vpcCounts);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void sample(CodeBlock* codeBlock, Instruction* vPC);
|
---|
74 | };
|
---|
75 |
|
---|
76 | typedef WTF::HashMap<ScopeNode*, ScopeSampleRecord*> ScopeSampleRecordMap;
|
---|
77 |
|
---|
78 | class SamplingTool {
|
---|
79 | public:
|
---|
80 | SamplingTool()
|
---|
81 | : m_running(false)
|
---|
82 | , m_recordedCodeBlock(0)
|
---|
83 | , m_recordedVPC(0)
|
---|
84 | , m_totalSamples(0)
|
---|
85 | , m_scopeSampleMap(new ScopeSampleRecordMap())
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 | ~SamplingTool()
|
---|
90 | {
|
---|
91 | deleteAllValues(*m_scopeSampleMap);
|
---|
92 | }
|
---|
93 |
|
---|
94 | void start(unsigned hertz=10000);
|
---|
95 | void stop();
|
---|
96 | void dump(ExecState*);
|
---|
97 |
|
---|
98 | void notifyOfScope(ScopeNode* scope);
|
---|
99 |
|
---|
100 | void sample(CodeBlock* recordedCodeBlock, Instruction* recordedVPC)
|
---|
101 | {
|
---|
102 | m_recordedCodeBlock = recordedCodeBlock;
|
---|
103 | m_recordedVPC = recordedVPC;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void privateExecuteReturned()
|
---|
107 | {
|
---|
108 | m_recordedCodeBlock = 0;
|
---|
109 | m_recordedVPC = 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void callingHostFunction()
|
---|
113 | {
|
---|
114 | m_recordedCodeBlock = 0;
|
---|
115 | m_recordedVPC = 0;
|
---|
116 | #if ENABLE(SAMPLING_TOOL)
|
---|
117 | currentOpcodeID = static_cast<OpcodeID>(-1);
|
---|
118 | #endif
|
---|
119 | }
|
---|
120 |
|
---|
121 | private:
|
---|
122 | static void* threadStartFunc(void*);
|
---|
123 | void run();
|
---|
124 |
|
---|
125 | // Sampling thread state.
|
---|
126 | bool m_running;
|
---|
127 | unsigned m_hertz;
|
---|
128 | ThreadIdentifier m_samplingThread;
|
---|
129 |
|
---|
130 | // State tracked by the main thread, used by the sampling thread.
|
---|
131 | CodeBlock* m_recordedCodeBlock;
|
---|
132 | Instruction* m_recordedVPC;
|
---|
133 |
|
---|
134 | // Gathered sample data.
|
---|
135 | long long m_totalSamples;
|
---|
136 | OwnPtr<ScopeSampleRecordMap> m_scopeSampleMap;
|
---|
137 | };
|
---|
138 |
|
---|
139 | // SCOPENODE_ / MACHINE_ macros for use from within member methods on ScopeNode / Machine respectively.
|
---|
140 | #if ENABLE(SAMPLING_TOOL)
|
---|
141 | #define SCOPENODE_SAMPLING_notifyOfScope(sampler) sampler->notifyOfScope(this)
|
---|
142 | #define MACHINE_SAMPLING_sample(codeBlock, vPC) m_sampler->sample(codeBlock, vPC)
|
---|
143 | #define MACHINE_SAMPLING_privateExecuteReturned() m_sampler->privateExecuteReturned()
|
---|
144 | #define MACHINE_SAMPLING_callingHostFunction() m_sampler->callingHostFunction()
|
---|
145 | #define CTI_MACHINE_SAMPLING_callingHostFunction() machine->m_sampler->callingHostFunction()
|
---|
146 | #else
|
---|
147 | #define SCOPENODE_SAMPLING_notifyOfScope(sampler)
|
---|
148 | #define MACHINE_SAMPLING_sample(codeBlock, vPC)
|
---|
149 | #define MACHINE_SAMPLING_privateExecuteReturned()
|
---|
150 | #define MACHINE_SAMPLING_callingHostFunction()
|
---|
151 | #define CTI_MACHINE_SAMPLING_callingHostFunction()
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | } // namespace JSC
|
---|
155 |
|
---|
156 | #endif // SamplingTool_h
|
---|