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