Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: src/third_party/vtune/vtune-jit.cc

Issue 11574031: Intel VTune integration for V8/D8 (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "../../factory.h"
63 #include "../../global-handles.h"
64
65 namespace vTune {
66 namespace internal {
67
68
69 // This class is used to record the JITted code position info for JIT
70 // code profiling.
71 class JITCodeLineInfo : public Malloced {
72 public:
73 JITCodeLineInfo() : line_num_info_(10) { }
74
75 void SetPosition(intptr_t pc, int pos) {
76 AddCodeLineInfo(LineNumInfo(pc, pos));
77 }
78
79 struct LineNumInfo {
80 LineNumInfo(intptr_t pc, int pos)
81 : pc_(pc), pos_(pos) { }
82
83 intptr_t pc_;
84 int pos_;
85 };
86
87 List<LineNumInfo>* GetLineNumInfo() {
88 return &line_num_info_;
89 }
90
91 private:
92 void AddCodeLineInfo(const LineNumInfo& line_info) {
93 line_num_info_.Add(line_info);
94 }
95 List<LineNumInfo> line_num_info_;
96 };
97
98 Mutex* VTUNEJITInterface::vtunemutex_ = OS::CreateMutex();
99
100 static bool SameCodeObjects(void* key1, void* key2) {
101 return key1 == key2;
102 }
103
104 static HashMap* GetEntries() {
105 static HashMap* entries;
106 if (entries == NULL) {
107 entries = new HashMap(&SameCodeObjects);
108 }
109 return entries;
110 }
111
112 static uint32_t HashForCodeObject(void* code) {
113 static const uintptr_t kGoldenRatio = 2654435761u;
114 uintptr_t hash = reinterpret_cast<uintptr_t>(code);
115 return static_cast<uint32_t>(hash * kGoldenRatio);
116 }
117
118
119 static const intptr_t kLineInfoTag = 0x1;
120
121 static bool IsSourceInfoAvailable(Handle<Script> script) {
122 return !script.is_null() &&
123 script->source()->IsString() &&
124 script->HasValidSource() &&
125 script->name()->IsString();
126 }
127
128 static bool IsLineInfoTagged(void* ptr) {
129 return 0 != (reinterpret_cast<intptr_t>(ptr));
130 }
131
132 static void* TagLineInfo(JITCodeLineInfo* ptr) {
133 return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(ptr));
134 }
135
136 static JITCodeLineInfo* UntagLineInfo(void* ptr) {
137 return reinterpret_cast<JITCodeLineInfo*>(
138 reinterpret_cast<intptr_t>(ptr));
139 }
140
141 // The parameter str is a mixed pattern which contains the
142 // function name and some other info. It comes from all the
143 // Logger::CodeCreateEvent(...) function. This funtion get the
144 // pure function name from the input parameter.
145 static char* GetFunctionNameFromMixedName(const char* str, int length) {
146 int index = 0;
147 int count = 0;
148 char* start_ptr = NULL;
149
150 while (str[index++] != ':' && (index < length)) {}
151
152 if (str[index] == '*' || str[index] == '~' ) index++;
153 if (index >= length) return NULL;
154
155 start_ptr = const_cast<char*>(str + index);
156
157 while (index < length && str[index++] != ' ') {
158 count++;
159 }
160
161 char* result = NewArray<char>(count + 1);
162 memcpy(result, start_ptr, count);
163 result[count] = '\0';
164
165 return result;
166 }
167
168 // The JitCodeEventHandler for Vtune.
169 void VTUNEJITInterface::event_handler(const v8::JitCodeEvent* event) {
170 CHECK(event != NULL);
171
172 if (VTUNERUNNING) {
173 switch (event->type) {
174 case v8::JitCodeEvent::CODE_ADDED: {
175 ScopedLock lock(vtunemutex_);
176
177 char* temp_file_name = NULL;
178 char* temp_method_name =
179 GetFunctionNameFromMixedName(event->name.str,
180 static_cast<int>(event->name.len));
181 iJIT_Method_Load jmethod;
182 memset(&jmethod, 0, sizeof jmethod);
183 jmethod.method_id = iJIT_GetNewMethodID();
184 jmethod.method_load_address = event->code_start;
185 jmethod.method_size = static_cast<unsigned int>(event->code_len);
186 jmethod.method_name = temp_method_name;
187
188 Script** script_handler =
189 reinterpret_cast<v8::internal::Script**>(*event->script);
190 if (script_handler != NULL) {
191 Script* script = *script_handler;
192
193 if (script != NULL && script->name()->IsString()) {
194 // Get the source file name and set it to jmethod.source_file_name
195 Handle<String> filename =
196 Handle<String>(String::cast(script->name()));
197 SmartArrayPointer<char> filename_cstring =
198 filename->ToCString(DISALLOW_NULLS);
199
200 size_t str_length = strlen(*filename_cstring);
201 temp_file_name = NewArray<char>(str_length + 1);
202 memcpy(temp_file_name, *filename_cstring, str_length +1);
203
204 jmethod.source_file_name = temp_file_name;
205
206 HashMap::Entry* entry =
207 GetEntries()->Lookup(event->code_start,
208 HashForCodeObject(event->code_start),
209 false);
210
211 if (entry != NULL && IsLineInfoTagged(entry->value)
212 && IsSourceInfoAvailable(Handle<Script>(script))) {
213 // Get the line_num_info and set it to jmethod.line_number_table
214 GetScriptLineNumber(Handle<Script>(script), 0);
215 JITCodeLineInfo* line_info = UntagLineInfo(entry->value);
216 List<JITCodeLineInfo::LineNumInfo>* vtunelineinfo =
217 line_info->GetLineNumInfo();
218
219 jmethod.line_number_size = vtunelineinfo->length();
220 jmethod.line_number_table =
221 reinterpret_cast<LineNumberInfo*>(
222 malloc(sizeof(LineNumberInfo)*jmethod.line_number_size));
223 // populate the lineinfo for the code. Filter it before
224 for (int e = 0; e < vtunelineinfo->length(); e++) {
225 JITCodeLineInfo::LineNumInfo line_info = vtunelineinfo->at(e);
226 jmethod.line_number_table[e].Offset =
227 static_cast<unsigned int>(line_info.pc_);
228 jmethod.line_number_table[e].LineNumber =
229 GetScriptLineNumberSafe(
230 Handle<Script>(script), line_info.pos_)+1;
231 }
232 delete line_info;
233 }
234 GetEntries()->Remove(event->code_start,
235 HashForCodeObject(event->code_start));
236 #ifdef DEBUG
237 PrintF("Filename: %s, MethodName: %s\n",
238 jmethod.source_file_name,
239 jmethod.method_name);
240 #endif
241 }
242 }
243
244 iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED,
245 reinterpret_cast<void*>(&jmethod));
246 DeleteArray(temp_method_name);
247 DeleteArray(temp_file_name);
248 break;
249 }
250 // TODO(chunyang.dai@intel.com): code_move will be supported.
251 case v8::JitCodeEvent::CODE_MOVED:
252 break;
253 // Currently the CODE_REMOVED event is not issued.
254 case v8::JitCodeEvent::CODE_REMOVED:
255 break;
256 case v8::JitCodeEvent::CODE_ADD_LINE_POS_INFO: {
257 JITCodeLineInfo* line_info =
258 reinterpret_cast<JITCodeLineInfo*>(event->user_data);
259 if (line_info != NULL) {
260 line_info->SetPosition(static_cast<intptr_t>(event->line_info.offset),
261 static_cast<int>(event->line_info.pos));
262 }
263 break;
264 }
265 case v8::JitCodeEvent::CODE_START_LINE_INFO_RECORDING: {
266 v8::JitCodeEvent* temp_event = const_cast<v8::JitCodeEvent*>(event);
267 temp_event->user_data = new JITCodeLineInfo();
268 break;
269 }
270 case v8::JitCodeEvent::CODE_END_LINE_INFO_RECORDING: {
271 ScopedLock lock(vtunemutex_);
272 HashMap::Entry* entry =
273 GetEntries()->Lookup(event->code_start,
274 HashForCodeObject(event->code_start),
275 true);
276
277 ASSERT(entry->value == NULL);
278 entry->value =
279 TagLineInfo(reinterpret_cast<JITCodeLineInfo*>(event->user_data));
280 break;
281 }
282 default:
283 CHECK(false);
284 break;
285 }
286 }
287 return;
288 }
289
290 } // namespace internal
291
292 void InitilizeVtuneForV8() {
293 if (v8::V8::Initialize()) {
294 v8::V8::SetFlagsFromString("--nocompact_code_space",
295 i::StrLength("--nocompact_code_space"));
296 v8::V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault,
297 vTune::internal::VTUNEJITInterface::event_handler);
298 }
299 }
300
301 } // namespace vTune
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698