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

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

Powered by Google App Engine
This is Rietveld 408576698