OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 This file is provided under a dual BSD/GPLv2 license. When using or | |
3 redistributing this file, you may do so under either license. | |
4 | |
5 GPL LICENSE SUMMARY | |
6 | |
7 Copyright(c) 2005-2012 Intel Corporation. All rights reserved. | |
8 | |
9 This program is free software; you can redistribute it and/or modify | |
10 it under the terms of version 2 of the GNU General Public License as | |
11 published by the Free Software Foundation. | |
12 | |
13 This program is distributed in the hope that it will be useful, but | |
14 WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program; if not, write to the Free Software | |
20 Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
21 The full GNU General Public License is included in this distribution | |
22 in the file called LICENSE.GPL. | |
23 | |
24 Contact Information: | |
25 http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/ | |
26 | |
27 BSD LICENSE | |
28 | |
29 Copyright(c) 2005-2012 Intel Corporation. All rights reserved. | |
30 All rights reserved. | |
31 | |
32 Redistribution and use in source and binary forms, with or without | |
33 modification, are permitted provided that the following conditions | |
34 are met: | |
35 | |
36 * Redistributions of source code must retain the above copyright | |
37 notice, this list of conditions and the following disclaimer. | |
38 * Redistributions in binary form must reproduce the above copyright | |
39 notice, this list of conditions and the following disclaimer in | |
40 the documentation and/or other materials provided with the | |
41 distribution. | |
42 * Neither the name of Intel Corporation nor the names of its | |
43 contributors may be used to endorse or promote products derived | |
44 from this software without specific prior written permission. | |
45 | |
46 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
47 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
48 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
49 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
50 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
51 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
52 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
53 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
54 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
55 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
56 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
57 */ | |
58 #include <string.h> | |
59 | |
60 #include "third_party/vtune/v8-vtune.h" | |
61 #include "third_party/vtune/vtune-jit.h" | |
62 | |
63 namespace vTune { | |
64 namespace internal { | |
65 | |
66 | |
67 // This class is used to record the JITted code position info for JIT | |
68 // code profiling. | |
69 class JITCodeLineInfo : public Malloced { | |
70 public: | |
71 JITCodeLineInfo() : line_num_info_(10) { } | |
72 | |
73 void SetPosition(intptr_t pc, int pos) { | |
74 AddCodeLineInfo(LineNumInfo(pc, pos)); | |
75 } | |
76 | |
77 struct LineNumInfo { | |
78 LineNumInfo(intptr_t pc, int pos) | |
79 : pc_(pc), pos_(pos) { } | |
80 | |
81 intptr_t pc_; | |
82 int pos_; | |
83 }; | |
84 | |
85 List<LineNumInfo>* GetLineNumInfo() { | |
86 return &line_num_info_; | |
87 } | |
88 | |
89 private: | |
90 void AddCodeLineInfo(const LineNumInfo& line_info) { | |
91 line_num_info_.Add(line_info); | |
92 } | |
93 List<LineNumInfo> line_num_info_; | |
94 }; | |
95 | |
96 Mutex* VTUNEJITInterface::vtunemutex_ = OS::CreateMutex(); | |
97 | |
98 static bool SameCodeObjects(void* key1, void* key2) { | |
99 return key1 == key2; | |
100 } | |
101 | |
102 static HashMap* GetEntries() { | |
103 static HashMap* entries; | |
104 if (entries == NULL) { | |
105 entries = new HashMap(&SameCodeObjects); | |
106 } | |
107 return entries; | |
108 } | |
109 | |
110 static uint32_t HashForCodeObject(void* code) { | |
111 static const uintptr_t kGoldenRatio = 2654435761u; | |
112 uintptr_t hash = reinterpret_cast<uintptr_t>(code); | |
113 return static_cast<uint32_t>(hash * kGoldenRatio); | |
114 } | |
115 | |
116 | |
117 static const intptr_t kLineInfoTag = 0x1; | |
118 | |
119 static bool IsSourceInfoAvailable(Handle<Script> script) { | |
120 return !script.is_null() && | |
121 script->source()->IsString() && | |
122 script->HasValidSource() && | |
123 script->name()->IsString(); | |
124 } | |
125 | |
126 static bool IsLineInfoTagged(void* ptr) { | |
127 return 0 != (reinterpret_cast<intptr_t>(ptr)); | |
128 } | |
129 | |
130 static void* TagLineInfo(JITCodeLineInfo* ptr) { | |
131 return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(ptr)); | |
132 } | |
133 | |
134 static JITCodeLineInfo* UntagLineInfo(void* ptr) { | |
135 return reinterpret_cast<JITCodeLineInfo*>( | |
136 reinterpret_cast<intptr_t>(ptr)); | |
137 } | |
138 | |
139 // The parameter str is a mixed pattern which contains the | |
140 // function name and some other info. It comes from all the | |
141 // Logger::CodeCreateEvent(...) function. This funtion get the | |
142 // pure function name from the input parameter. | |
143 static char* GetFunctionNameFromMixedName(const char* str, int length) { | |
144 int index = 0; | |
145 int count = 0; | |
146 char* start_ptr = NULL; | |
147 | |
148 while (str[index++] != ':' && (index < length)) {} | |
149 | |
150 if (str[index] == '*' || str[index] == '~' ) index++; | |
151 if (index >= length) return NULL; | |
152 | |
153 start_ptr = const_cast<char*>(str + index); | |
154 | |
155 while (index < length && str[index++] != ' ') { | |
156 count++; | |
157 } | |
158 | |
159 char* result = NewArray<char>(count + 1); | |
160 memcpy(result, start_ptr, count); | |
161 result[count] = '\0'; | |
162 | |
163 return result; | |
164 } | |
165 | |
166 // The JitCodeEventHandler for Vtune. | |
167 void VTUNEJITInterface::event_handler(const v8::JitCodeEvent* event) { | |
168 CHECK(event != NULL); | |
169 | |
170 if (VTUNERUNNING) { | |
171 switch (event->type) { | |
172 case v8::JitCodeEvent::CODE_ADDED: { | |
173 ScopedLock lock(vtunemutex_); | |
174 | |
175 char* temp_file_name = NULL; | |
176 char* temp_method_name = | |
177 GetFunctionNameFromMixedName(event->name.str, | |
178 static_cast<int>(event->name.len)); | |
179 iJIT_Method_Load jmethod; | |
180 memset(&jmethod, 0, sizeof jmethod); | |
181 jmethod.method_id = iJIT_GetNewMethodID(); | |
182 jmethod.method_load_address = event->code_start; | |
183 jmethod.method_size = static_cast<unsigned int>(event->code_len); | |
184 jmethod.method_name = temp_method_name; | |
185 | |
186 Script** script_handler = | |
187 reinterpret_cast<v8::internal::Script**>(*event->script); | |
danno
2013/03/27 12:33:48
You should at no point access anything in the v8::
| |
188 if (script_handler != NULL) { | |
189 Script* script = *script_handler; | |
190 | |
191 if (script != NULL && script->name()->IsString()) { | |
192 // Get the source file name and set it to jmethod.source_file_name | |
193 Handle<String> filename = | |
194 Handle<String>(String::cast(script->name())); | |
195 SmartArrayPointer<char> filename_cstring = | |
196 filename->ToCString(DISALLOW_NULLS); | |
197 | |
198 size_t str_length = strlen(*filename_cstring); | |
199 temp_file_name = NewArray<char>(str_length + 1); | |
200 memcpy(temp_file_name, *filename_cstring, str_length +1); | |
201 | |
202 jmethod.source_file_name = temp_file_name; | |
203 | |
204 HashMap::Entry* entry = | |
205 GetEntries()->Lookup(event->code_start, | |
206 HashForCodeObject(event->code_start), | |
207 false); | |
208 | |
209 if (entry != NULL && IsLineInfoTagged(entry->value) | |
210 && IsSourceInfoAvailable(Handle<Script>(script))) { | |
211 // Get the line_num_info and set it to jmethod.line_number_table | |
212 GetScriptLineNumber(Handle<Script>(script), 0); | |
213 JITCodeLineInfo* line_info = UntagLineInfo(entry->value); | |
214 List<JITCodeLineInfo::LineNumInfo>* vtunelineinfo = | |
215 line_info->GetLineNumInfo(); | |
216 | |
217 jmethod.line_number_size = vtunelineinfo->length(); | |
218 jmethod.line_number_table = | |
219 reinterpret_cast<LineNumberInfo*>( | |
220 malloc(sizeof(LineNumberInfo)*jmethod.line_number_size)); | |
221 // populate the lineinfo for the code. Filter it before | |
222 for (int e = 0; e < vtunelineinfo->length(); e++) { | |
223 JITCodeLineInfo::LineNumInfo line_info = vtunelineinfo->at(e); | |
224 jmethod.line_number_table[e].Offset = | |
225 static_cast<unsigned int>(line_info.pc_); | |
226 jmethod.line_number_table[e].LineNumber = | |
227 GetScriptLineNumberSafe( | |
228 Handle<Script>(script), line_info.pos_)+1; | |
229 } | |
230 delete line_info; | |
231 } | |
232 GetEntries()->Remove(event->code_start, | |
233 HashForCodeObject(event->code_start)); | |
234 #ifdef DEBUG | |
235 PrintF("Filename: %s, MethodName: %s\n", | |
236 jmethod.source_file_name, | |
237 jmethod.method_name); | |
238 #endif | |
239 } | |
240 } | |
241 | |
242 iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, | |
243 reinterpret_cast<void*>(&jmethod)); | |
244 DeleteArray(temp_method_name); | |
245 DeleteArray(temp_file_name); | |
246 break; | |
247 } | |
248 // TODO(chunyang.dai@intel.com): code_move will be supported. | |
249 case v8::JitCodeEvent::CODE_MOVED: | |
250 break; | |
251 // Currently the CODE_REMOVED event is not issued. | |
252 case v8::JitCodeEvent::CODE_REMOVED: | |
253 break; | |
254 case v8::JitCodeEvent::CODE_ADD_LINE_POS_INFO: { | |
255 JITCodeLineInfo* line_info = | |
256 reinterpret_cast<JITCodeLineInfo*>(event->user_data); | |
257 if (line_info != NULL) { | |
258 line_info->SetPosition(static_cast<intptr_t>(event->line_info.offset), | |
259 static_cast<int>(event->line_info.pos)); | |
260 } | |
261 break; | |
262 } | |
263 case v8::JitCodeEvent::CODE_START_LINE_INFO_RECORDING: { | |
264 v8::JitCodeEvent* temp_event = const_cast<v8::JitCodeEvent*>(event); | |
265 temp_event->user_data = new JITCodeLineInfo(); | |
266 break; | |
267 } | |
268 case v8::JitCodeEvent::CODE_END_LINE_INFO_RECORDING: { | |
269 ScopedLock lock(vtunemutex_); | |
270 HashMap::Entry* entry = | |
271 GetEntries()->Lookup(event->code_start, | |
272 HashForCodeObject(event->code_start), | |
273 true); | |
274 | |
275 ASSERT(entry->value == NULL); | |
276 entry->value = | |
277 TagLineInfo(reinterpret_cast<JITCodeLineInfo*>(event->user_data)); | |
278 break; | |
279 } | |
280 default: | |
281 CHECK(false); | |
282 break; | |
283 } | |
284 } | |
285 return; | |
286 } | |
287 | |
288 } // namespace internal | |
289 | |
290 void InitilizeVtuneForV8() { | |
291 if (v8::V8::Initialize()) { | |
292 v8::V8::SetFlagsFromString("--nocompact_code_space", | |
293 i::StrLength("--nocompact_code_space")); | |
294 v8::V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, | |
295 vTune::internal::VTUNEJITInterface::event_handler); | |
296 } | |
297 } | |
298 | |
299 } // namespace vTune | |
OLD | NEW |