OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/profiler/sampler.h" | 5 #include "src/libsampler/v8-sampler.h" |
jochen (gone - plz use gerrit)
2016/05/04 07:29:07
please add a DEPS file to this directory that cont
lpy
2016/05/05 22:24:17
Will do after finishing moving atomic-utils.h into
| |
6 | 6 |
7 #if V8_OS_POSIX && !V8_OS_CYGWIN | 7 #if V8_OS_POSIX && !V8_OS_CYGWIN |
8 | 8 |
9 #define USE_SIGNALS | 9 #define USE_SIGNALS |
10 | 10 |
11 #include <errno.h> | 11 #include <errno.h> |
12 #include <pthread.h> | 12 #include <pthread.h> |
13 #include <signal.h> | 13 #include <signal.h> |
14 #include <sys/time.h> | 14 #include <sys/time.h> |
15 | 15 |
(...skipping 19 matching lines...) Expand all Loading... | |
35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) | 35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) |
36 #include <asm/sigcontext.h> // NOLINT | 36 #include <asm/sigcontext.h> // NOLINT |
37 #endif | 37 #endif |
38 | 38 |
39 #elif V8_OS_WIN || V8_OS_CYGWIN | 39 #elif V8_OS_WIN || V8_OS_CYGWIN |
40 | 40 |
41 #include "src/base/win32-headers.h" | 41 #include "src/base/win32-headers.h" |
42 | 42 |
43 #endif | 43 #endif |
44 | 44 |
45 #include "src/atomic-utils.h" | 45 #include "src/atomic-utils.h" |
jochen (gone - plz use gerrit)
2016/05/04 07:29:07
I think this file can be moved to src/base
lpy
2016/05/05 22:24:17
atomic-utils.h will be moved into base/ in this CL
| |
46 #include "src/base/platform/platform.h" | 46 #include "src/base/platform/platform.h" |
47 #include "src/flags.h" | 47 #include "src/hashmap.h" |
48 #include "src/frames-inl.h" | 48 #include "src/list-inl.h" |
jochen (gone - plz use gerrit)
2016/05/04 07:29:07
please use stl container instead
lpy
2016/05/05 22:24:17
Done.
| |
49 #include "src/log.h" | 49 #include "src/isolate.h" |
jochen (gone - plz use gerrit)
2016/05/04 07:29:07
we should only use v8::Isolate via include/v8.h
lpy
2016/05/05 22:24:17
Done.
| |
50 #include "src/profiler/cpu-profiler-inl.h" | |
51 #include "src/simulator.h" | |
52 #include "src/v8threads.h" | |
53 #include "src/vm-state-inl.h" | |
54 | 50 |
55 | 51 |
56 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | 52 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) |
57 | 53 |
58 // Not all versions of Android's C library provide ucontext_t. | 54 // Not all versions of Android's C library provide ucontext_t. |
59 // Detect this and provide custom but compatible definitions. Note that these | 55 // Detect this and provide custom but compatible definitions. Note that these |
60 // follow the GLibc naming convention to access register values from | 56 // follow the GLibc naming convention to access register values from |
61 // mcontext_t. | 57 // mcontext_t. |
62 // | 58 // |
63 // See http://code.google.com/p/android/issues/detail?id=34784 | 59 // See http://code.google.com/p/android/issues/detail?id=34784 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 mcontext_t uc_mcontext; | 147 mcontext_t uc_mcontext; |
152 // Other fields are not used by V8, don't define them here. | 148 // Other fields are not used by V8, don't define them here. |
153 } ucontext_t; | 149 } ucontext_t; |
154 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; | 150 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; |
155 #endif | 151 #endif |
156 | 152 |
157 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | 153 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) |
158 | 154 |
159 | 155 |
160 namespace v8 { | 156 namespace v8 { |
161 namespace internal { | 157 namespace sampler { |
162 | 158 |
163 namespace { | 159 namespace { |
164 | 160 |
165 class PlatformDataCommon : public Malloced { | 161 class PlatformDataCommon : public i::Malloced { |
166 public: | 162 public: |
167 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} | 163 PlatformDataCommon() : profiled_thread_id_(i::ThreadId::Current()) {} |
168 ThreadId profiled_thread_id() { return profiled_thread_id_; } | 164 i::ThreadId profiled_thread_id() { return profiled_thread_id_; } |
169 | 165 |
170 protected: | 166 protected: |
171 ~PlatformDataCommon() {} | 167 ~PlatformDataCommon() {} |
172 | 168 |
173 private: | 169 private: |
174 ThreadId profiled_thread_id_; | 170 i::ThreadId profiled_thread_id_; |
175 }; | 171 }; |
176 | 172 |
177 | 173 |
178 bool IsSamePage(byte* ptr1, byte* ptr2) { | 174 #if defined(USE_SIGNALS) |
179 const uint32_t kPageSize = 4096; | 175 typedef internal::List<Sampler*> SamplerList; |
180 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); | |
181 return (reinterpret_cast<uintptr_t>(ptr1) & mask) == | |
182 (reinterpret_cast<uintptr_t>(ptr2) & mask); | |
183 } | |
184 | 176 |
185 | |
186 // Check if the code at specified address could potentially be a | |
187 // frame setup code. | |
188 bool IsNoFrameRegion(Address address) { | |
189 struct Pattern { | |
190 int bytes_count; | |
191 byte bytes[8]; | |
192 int offsets[4]; | |
193 }; | |
194 byte* pc = reinterpret_cast<byte*>(address); | |
195 static Pattern patterns[] = { | |
196 #if V8_HOST_ARCH_IA32 | |
197 // push %ebp | |
198 // mov %esp,%ebp | |
199 {3, {0x55, 0x89, 0xe5}, {0, 1, -1}}, | |
200 // pop %ebp | |
201 // ret N | |
202 {2, {0x5d, 0xc2}, {0, 1, -1}}, | |
203 // pop %ebp | |
204 // ret | |
205 {2, {0x5d, 0xc3}, {0, 1, -1}}, | |
206 #elif V8_HOST_ARCH_X64 | |
207 // pushq %rbp | |
208 // movq %rsp,%rbp | |
209 {4, {0x55, 0x48, 0x89, 0xe5}, {0, 1, -1}}, | |
210 // popq %rbp | |
211 // ret N | |
212 {2, {0x5d, 0xc2}, {0, 1, -1}}, | |
213 // popq %rbp | |
214 // ret | |
215 {2, {0x5d, 0xc3}, {0, 1, -1}}, | |
216 #endif | |
217 {0, {}, {}} | |
218 }; | |
219 for (Pattern* pattern = patterns; pattern->bytes_count; ++pattern) { | |
220 for (int* offset_ptr = pattern->offsets; *offset_ptr != -1; ++offset_ptr) { | |
221 int offset = *offset_ptr; | |
222 if (!offset || IsSamePage(pc, pc - offset)) { | |
223 MSAN_MEMORY_IS_INITIALIZED(pc - offset, pattern->bytes_count); | |
224 if (!memcmp(pc - offset, pattern->bytes, pattern->bytes_count)) | |
225 return true; | |
226 } else { | |
227 // It is not safe to examine bytes on another page as it might not be | |
228 // allocated thus causing a SEGFAULT. | |
229 // Check the pattern part that's on the same page and | |
230 // pessimistically assume it could be the entire pattern match. | |
231 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset); | |
232 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) | |
233 return true; | |
234 } | |
235 } | |
236 } | |
237 return false; | |
238 } | |
239 | |
240 typedef List<Sampler*> SamplerList; | |
241 | |
242 #if defined(USE_SIGNALS) | |
243 class AtomicGuard { | 177 class AtomicGuard { |
244 public: | 178 public: |
245 explicit AtomicGuard(AtomicValue<int>* atomic, bool is_block = true) | 179 explicit AtomicGuard(i::AtomicValue<int>* atomic, bool is_block = true) |
246 : atomic_(atomic), | 180 : atomic_(atomic), |
247 is_success_(false) { | 181 is_success_(false) { |
248 do { | 182 do { |
249 // Use Acquire_Load to gain mutual exclusion. | 183 // Use Acquire_Load to gain mutual exclusion. |
250 USE(atomic_->Value()); | 184 USE(atomic_->Value()); |
251 is_success_ = atomic_->TrySetValue(0, 1); | 185 is_success_ = atomic_->TrySetValue(0, 1); |
252 } while (is_block && !is_success_); | 186 } while (is_block && !is_success_); |
253 } | 187 } |
254 | 188 |
255 bool is_success() { return is_success_; } | 189 bool is_success() { return is_success_; } |
256 | 190 |
257 ~AtomicGuard() { | 191 ~AtomicGuard() { |
258 if (is_success_) { | 192 if (is_success_) { |
259 atomic_->SetValue(0); | 193 atomic_->SetValue(0); |
260 } | 194 } |
261 atomic_ = NULL; | 195 atomic_ = NULL; |
262 } | 196 } |
263 | 197 |
264 private: | 198 private: |
265 AtomicValue<int>* atomic_; | 199 i::AtomicValue<int>* atomic_; |
266 bool is_success_; | 200 bool is_success_; |
267 }; | 201 }; |
268 | 202 |
269 | 203 |
270 // Returns key for hash map. | 204 // Returns key for hash map. |
271 void* ThreadKey(pthread_t thread_id) { | 205 void* ThreadKey(pthread_t thread_id) { |
272 return reinterpret_cast<void*>(thread_id); | 206 return reinterpret_cast<void*>(thread_id); |
273 } | 207 } |
274 | 208 |
275 | 209 |
(...skipping 13 matching lines...) Expand all Loading... | |
289 | 223 |
290 class Sampler::PlatformData : public PlatformDataCommon { | 224 class Sampler::PlatformData : public PlatformDataCommon { |
291 public: | 225 public: |
292 PlatformData() : vm_tid_(pthread_self()) {} | 226 PlatformData() : vm_tid_(pthread_self()) {} |
293 pthread_t vm_tid() const { return vm_tid_; } | 227 pthread_t vm_tid() const { return vm_tid_; } |
294 | 228 |
295 private: | 229 private: |
296 pthread_t vm_tid_; | 230 pthread_t vm_tid_; |
297 }; | 231 }; |
298 | 232 |
233 | |
234 class SamplerManager { | |
235 public: | |
236 static void AddSampler(Sampler* sampler) { | |
237 AtomicGuard atomic_guard(&samplers_access_counter_); | |
238 DCHECK(sampler->IsActive()); | |
239 // Add sampler into map if needed. | |
240 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
241 i::HashMap::Entry *entry = | |
242 thread_id_to_samplers_.Pointer()->LookupOrInsert(ThreadKey(thread_id), | |
243 ThreadHash(thread_id)); | |
244 if (entry->value == NULL) { | |
245 SamplerList* samplers = new SamplerList(); | |
246 samplers->Add(sampler); | |
247 entry->value = samplers; | |
248 } else { | |
249 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
250 if (!samplers->Contains(sampler)) { | |
251 samplers->Add(sampler); | |
252 } | |
253 } | |
254 } | |
255 | |
256 static void RemoveSampler(Sampler* sampler) { | |
257 AtomicGuard atomic_guard(&samplers_access_counter_); | |
258 DCHECK(sampler->IsActive()); | |
259 // Remove sampler from map. | |
260 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
261 void* thread_key = ThreadKey(thread_id); | |
262 uint32_t thread_hash = ThreadHash(thread_id); | |
263 i::HashMap::Entry* entry = | |
264 thread_id_to_samplers_.Get().Lookup(thread_key, thread_hash); | |
265 DCHECK(entry != NULL); | |
266 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
267 samplers->RemoveElement(sampler); | |
268 if (samplers->is_empty()) { | |
269 thread_id_to_samplers_.Pointer()->Remove(thread_key, thread_hash); | |
270 delete samplers; | |
271 } | |
272 } | |
273 | |
274 private: | |
275 struct HashMapCreateTrait { | |
276 static void Construct(internal::HashMap* allocated_ptr) { | |
277 new (allocated_ptr) internal::HashMap(internal::HashMap::PointersMatch); | |
278 } | |
279 }; | |
280 friend class SignalHandler; | |
281 static base::LazyInstance<internal::HashMap, HashMapCreateTrait>::type | |
282 thread_id_to_samplers_; | |
283 static i::AtomicValue<int> samplers_access_counter_; | |
284 }; | |
285 | |
286 | |
287 base::LazyInstance<i::HashMap, SamplerManager::HashMapCreateTrait>::type | |
288 SamplerManager::thread_id_to_samplers_ = LAZY_INSTANCE_INITIALIZER; | |
289 i::AtomicValue<int> SamplerManager::samplers_access_counter_(0); | |
290 | |
291 | |
299 #elif V8_OS_WIN || V8_OS_CYGWIN | 292 #elif V8_OS_WIN || V8_OS_CYGWIN |
300 | 293 |
301 // ---------------------------------------------------------------------------- | 294 // ---------------------------------------------------------------------------- |
302 // Win32 profiler support. On Cygwin we use the same sampler implementation as | 295 // Win32 profiler support. On Cygwin we use the same sampler implementation as |
303 // on Win32. | 296 // on Win32. |
304 | 297 |
305 class Sampler::PlatformData : public PlatformDataCommon { | 298 class Sampler::PlatformData : public PlatformDataCommon { |
306 public: | 299 public: |
307 // Get a handle to the calling thread. This is the thread that we are | 300 // Get a handle to the calling thread. This is the thread that we are |
308 // going to profile. We need to make a copy of the handle because we are | 301 // going to profile. We need to make a copy of the handle because we are |
(...skipping 12 matching lines...) Expand all Loading... | |
321 CloseHandle(profiled_thread_); | 314 CloseHandle(profiled_thread_); |
322 profiled_thread_ = NULL; | 315 profiled_thread_ = NULL; |
323 } | 316 } |
324 } | 317 } |
325 | 318 |
326 HANDLE profiled_thread() { return profiled_thread_; } | 319 HANDLE profiled_thread() { return profiled_thread_; } |
327 | 320 |
328 private: | 321 private: |
329 HANDLE profiled_thread_; | 322 HANDLE profiled_thread_; |
330 }; | 323 }; |
331 #endif | 324 #endif // USE_SIGNALS |
332 | |
333 | |
334 #if defined(USE_SIMULATOR) | |
335 bool SimulatorHelper::FillRegisters(Isolate* isolate, | |
336 v8::RegisterState* state) { | |
337 Simulator *simulator = isolate->thread_local_top()->simulator_; | |
338 // Check if there is active simulator. | |
339 if (simulator == NULL) return false; | |
340 #if V8_TARGET_ARCH_ARM | |
341 if (!simulator->has_bad_pc()) { | |
342 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
343 } | |
344 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
345 state->fp = reinterpret_cast<Address>(simulator->get_register( | |
346 Simulator::r11)); | |
347 #elif V8_TARGET_ARCH_ARM64 | |
348 state->pc = reinterpret_cast<Address>(simulator->pc()); | |
349 state->sp = reinterpret_cast<Address>(simulator->sp()); | |
350 state->fp = reinterpret_cast<Address>(simulator->fp()); | |
351 #elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 | |
352 if (!simulator->has_bad_pc()) { | |
353 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
354 } | |
355 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
356 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); | |
357 #elif V8_TARGET_ARCH_PPC | |
358 if (!simulator->has_bad_pc()) { | |
359 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
360 } | |
361 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
362 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); | |
363 #elif V8_TARGET_ARCH_S390 | |
364 if (!simulator->has_bad_pc()) { | |
365 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
366 } | |
367 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
368 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); | |
369 #endif | |
370 if (state->sp == 0 || state->fp == 0) { | |
371 // It possible that the simulator is interrupted while it is updating | |
372 // the sp or fp register. ARM64 simulator does this in two steps: | |
373 // first setting it to zero and then setting it to the new value. | |
374 // Bailout if sp/fp doesn't contain the new value. | |
375 // | |
376 // FIXME: The above doesn't really solve the issue. | |
377 // If a 64-bit target is executed on a 32-bit host even the final | |
378 // write is non-atomic, so it might obtain a half of the result. | |
379 // Moreover as long as the register set code uses memcpy (as of now), | |
380 // it is not guaranteed to be atomic even when both host and target | |
381 // are of same bitness. | |
382 return false; | |
383 } | |
384 return true; | |
385 } | |
386 #endif // USE_SIMULATOR | |
387 | 325 |
388 | 326 |
389 #if defined(USE_SIGNALS) | 327 #if defined(USE_SIGNALS) |
390 | 328 class SignalHandler : public i::AllStatic { |
391 class SignalHandler : public AllStatic { | |
392 public: | 329 public: |
393 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | 330 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } |
394 static void TearDown() { delete mutex_; mutex_ = NULL; } | 331 static void TearDown() { delete mutex_; mutex_ = NULL; } |
395 | 332 |
396 static void IncreaseSamplerCount() { | 333 static void IncreaseSamplerCount() { |
397 base::LockGuard<base::Mutex> lock_guard(mutex_); | 334 base::LockGuard<base::Mutex> lock_guard(mutex_); |
398 if (++client_count_ == 1) Install(); | 335 if (++client_count_ == 1) Install(); |
399 } | 336 } |
400 | 337 |
401 static void DecreaseSamplerCount() { | 338 static void DecreaseSamplerCount() { |
402 base::LockGuard<base::Mutex> lock_guard(mutex_); | 339 base::LockGuard<base::Mutex> lock_guard(mutex_); |
403 if (--client_count_ == 0) Restore(); | 340 if (--client_count_ == 0) Restore(); |
404 } | 341 } |
405 | 342 |
406 static bool Installed() { | 343 static bool Installed() { |
407 return signal_handler_installed_; | 344 return signal_handler_installed_; |
408 } | 345 } |
409 | 346 |
410 #if !V8_OS_NACL | |
411 static void CollectSample(void* context, Sampler* sampler); | |
412 #endif | |
413 | |
414 private: | 347 private: |
415 static void Install() { | 348 static void Install() { |
416 #if !V8_OS_NACL | 349 #if !V8_OS_NACL |
417 struct sigaction sa; | 350 struct sigaction sa; |
418 sa.sa_sigaction = &HandleProfilerSignal; | 351 sa.sa_sigaction = &HandleProfilerSignal; |
419 sigemptyset(&sa.sa_mask); | 352 sigemptyset(&sa.sa_mask); |
420 #if V8_OS_QNX | 353 #if V8_OS_QNX |
421 sa.sa_flags = SA_SIGINFO; | 354 sa.sa_flags = SA_SIGINFO; |
422 #else | 355 #else |
423 sa.sa_flags = SA_RESTART | SA_SIGINFO; | 356 sa.sa_flags = SA_RESTART | SA_SIGINFO; |
424 #endif | 357 #endif |
425 signal_handler_installed_ = | 358 signal_handler_installed_ = |
426 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | 359 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
427 #endif | 360 #endif // !V8_OS_NACL |
428 } | 361 } |
429 | 362 |
430 static void Restore() { | 363 static void Restore() { |
431 #if !V8_OS_NACL | 364 #if !V8_OS_NACL |
432 if (signal_handler_installed_) { | 365 if (signal_handler_installed_) { |
433 sigaction(SIGPROF, &old_signal_handler_, 0); | 366 sigaction(SIGPROF, &old_signal_handler_, 0); |
434 signal_handler_installed_ = false; | 367 signal_handler_installed_ = false; |
435 } | 368 } |
436 #endif | 369 #endif |
437 } | 370 } |
438 | 371 |
439 #if !V8_OS_NACL | 372 #if !V8_OS_NACL |
373 static void FillRegisterState(void* context, RegisterState* regs); | |
440 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); | 374 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); |
441 #endif | 375 #endif |
442 // Protects the process wide state below. | 376 // Protects the process wide state below. |
443 static base::Mutex* mutex_; | 377 static base::Mutex* mutex_; |
444 static int client_count_; | 378 static int client_count_; |
445 static bool signal_handler_installed_; | 379 static bool signal_handler_installed_; |
446 static struct sigaction old_signal_handler_; | 380 static struct sigaction old_signal_handler_; |
447 }; | 381 }; |
448 | 382 |
449 | 383 |
450 base::Mutex* SignalHandler::mutex_ = NULL; | 384 base::Mutex* SignalHandler::mutex_ = NULL; |
451 int SignalHandler::client_count_ = 0; | 385 int SignalHandler::client_count_ = 0; |
452 struct sigaction SignalHandler::old_signal_handler_; | 386 struct sigaction SignalHandler::old_signal_handler_; |
453 bool SignalHandler::signal_handler_installed_ = false; | 387 bool SignalHandler::signal_handler_installed_ = false; |
454 | 388 |
455 | 389 |
456 // As Native Client does not support signal handling, profiling is disabled. | 390 // As Native Client does not support signal handling, profiling is disabled. |
457 #if !V8_OS_NACL | 391 #if !V8_OS_NACL |
458 void SignalHandler::CollectSample(void* context, Sampler* sampler) { | 392 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
459 if (sampler == NULL || (!sampler->IsProfiling() && | 393 void* context) { |
460 !sampler->IsRegistered())) { | 394 USE(info); |
395 if (signal != SIGPROF) return; | |
396 AtomicGuard atomic_guard(&SamplerManager::samplers_access_counter_, false); | |
397 if (!atomic_guard.is_success()) return; | |
398 pthread_t thread_id = pthread_self(); | |
399 i::HashMap::Entry* entry = | |
400 SamplerManager::thread_id_to_samplers_.Pointer()->Lookup( | |
401 ThreadKey(thread_id), ThreadHash(thread_id)); | |
402 if (entry == NULL) | |
461 return; | 403 return; |
404 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
405 v8::RegisterState state; | |
406 SignalHandler::FillRegisterState(context, &state); | |
407 | |
408 for (int i = 0; i < samplers->length(); ++i) { | |
409 Sampler* sampler = samplers->at(i); | |
410 if (sampler == NULL || !sampler->IsProfiling()) { | |
411 return; | |
412 } | |
413 Isolate* isolate = sampler->isolate(); | |
414 | |
415 // We require a fully initialized and entered isolate. | |
416 if (isolate == NULL || !isolate->IsInUse()) return; | |
417 | |
418 if (v8::Locker::IsActive() && !Locker::IsLocked(isolate)) { | |
419 return; | |
420 } | |
421 | |
422 sampler->SampleStack(state); | |
462 } | 423 } |
463 Isolate* isolate = sampler->isolate(); | 424 } |
464 | 425 |
465 // We require a fully initialized and entered isolate. | 426 void SignalHandler::FillRegisterState(void* context, RegisterState* state) { |
466 if (isolate == NULL || !isolate->IsInUse()) return; | |
467 | |
468 if (v8::Locker::IsActive() && | |
469 !isolate->thread_manager()->IsLockedByCurrentThread()) { | |
470 return; | |
471 } | |
472 | |
473 v8::RegisterState state; | |
474 | |
475 #if defined(USE_SIMULATOR) | |
476 if (!SimulatorHelper::FillRegisters(isolate, &state)) return; | |
477 #else | |
478 // Extracting the sample from the context is extremely machine dependent. | 427 // Extracting the sample from the context is extremely machine dependent. |
479 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | 428 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
480 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_S390))) | 429 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_S390))) |
481 mcontext_t& mcontext = ucontext->uc_mcontext; | 430 mcontext_t& mcontext = ucontext->uc_mcontext; |
482 #endif | 431 #endif |
483 #if V8_OS_LINUX | 432 #if V8_OS_LINUX |
484 #if V8_HOST_ARCH_IA32 | 433 #if V8_HOST_ARCH_IA32 |
485 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); | 434 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_EIP]); |
486 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); | 435 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_ESP]); |
487 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); | 436 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_EBP]); |
488 #elif V8_HOST_ARCH_X64 | 437 #elif V8_HOST_ARCH_X64 |
489 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); | 438 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_RIP]); |
490 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); | 439 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_RSP]); |
491 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); | 440 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_RBP]); |
492 #elif V8_HOST_ARCH_ARM | 441 #elif V8_HOST_ARCH_ARM |
493 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | 442 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
494 // Old GLibc ARM versions used a gregs[] array to access the register | 443 // Old GLibc ARM versions used a gregs[] array to access the register |
495 // values from mcontext_t. | 444 // values from mcontext_t. |
496 state.pc = reinterpret_cast<Address>(mcontext.gregs[R15]); | 445 state->pc = reinterpret_cast<void*>(mcontext.gregs[R15]); |
497 state.sp = reinterpret_cast<Address>(mcontext.gregs[R13]); | 446 state->sp = reinterpret_cast<void*>(mcontext.gregs[R13]); |
498 state.fp = reinterpret_cast<Address>(mcontext.gregs[R11]); | 447 state->fp = reinterpret_cast<void*>(mcontext.gregs[R11]); |
499 #else | 448 #else |
500 state.pc = reinterpret_cast<Address>(mcontext.arm_pc); | 449 state->pc = reinterpret_cast<void*>(mcontext.arm_pc); |
501 state.sp = reinterpret_cast<Address>(mcontext.arm_sp); | 450 state->sp = reinterpret_cast<void*>(mcontext.arm_sp); |
502 state.fp = reinterpret_cast<Address>(mcontext.arm_fp); | 451 state->fp = reinterpret_cast<void*>(mcontext.arm_fp); |
503 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | 452 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
504 #elif V8_HOST_ARCH_ARM64 | 453 #elif V8_HOST_ARCH_ARM64 |
505 state.pc = reinterpret_cast<Address>(mcontext.pc); | 454 state->pc = reinterpret_cast<void*>(mcontext.pc); |
506 state.sp = reinterpret_cast<Address>(mcontext.sp); | 455 state->sp = reinterpret_cast<void*>(mcontext.sp); |
507 // FP is an alias for x29. | 456 // FP is an alias for x29. |
508 state.fp = reinterpret_cast<Address>(mcontext.regs[29]); | 457 state->fp = reinterpret_cast<void*>(mcontext.regs[29]); |
509 #elif V8_HOST_ARCH_MIPS | 458 #elif V8_HOST_ARCH_MIPS |
510 state.pc = reinterpret_cast<Address>(mcontext.pc); | 459 state->pc = reinterpret_cast<void*>(mcontext.pc); |
511 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | 460 state->sp = reinterpret_cast<void*>(mcontext.gregs[29]); |
512 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | 461 state->fp = reinterpret_cast<void*>(mcontext.gregs[30]); |
513 #elif V8_HOST_ARCH_MIPS64 | 462 #elif V8_HOST_ARCH_MIPS64 |
514 state.pc = reinterpret_cast<Address>(mcontext.pc); | 463 state->pc = reinterpret_cast<void*>(mcontext.pc); |
515 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | 464 state->sp = reinterpret_cast<void*>(mcontext.gregs[29]); |
516 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | 465 state->fp = reinterpret_cast<void*>(mcontext.gregs[30]); |
517 #elif V8_HOST_ARCH_PPC | 466 #elif V8_HOST_ARCH_PPC |
518 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->nip); | 467 state->pc = reinterpret_cast<void*>(ucontext->uc_mcontext.regs->nip); |
519 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R1]); | 468 state->sp = |
520 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R31]); | 469 reinterpret_cast<void*>(ucontext->uc_mcontext.regs->gpr[PT_R1]); |
470 state->fp = | |
471 reinterpret_cast<void*>(ucontext->uc_mcontext.regs->gpr[PT_R31]); | |
521 #elif V8_HOST_ARCH_S390 | 472 #elif V8_HOST_ARCH_S390 |
522 #if V8_TARGET_ARCH_32_BIT | 473 #if V8_TARGET_ARCH_32_BIT |
523 // 31-bit target will have bit 0 (MSB) of the PSW set to denote addressing | 474 // 31-bit target will have bit 0 (MSB) of the PSW set to denote addressing |
524 // mode. This bit needs to be masked out to resolve actual address. | 475 // mode. This bit needs to be masked out to resolve actual address. |
525 state.pc = | 476 state->pc = |
526 reinterpret_cast<Address>(ucontext->uc_mcontext.psw.addr & 0x7FFFFFFF); | 477 reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr & 0x7FFFFFFF); |
527 #else | 478 #else |
528 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.psw.addr); | 479 state->pc = reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr); |
529 #endif // V8_TARGET_ARCH_32_BIT | 480 #endif // V8_TARGET_ARCH_32_BIT |
530 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.gregs[15]); | 481 state->sp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[15]); |
531 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.gregs[11]); | 482 state->fp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[11]); |
532 #endif // V8_HOST_ARCH_* | 483 #endif // V8_HOST_ARCH_* |
533 #elif V8_OS_MACOSX | 484 #elif V8_OS_MACOSX |
534 #if V8_HOST_ARCH_X64 | 485 #if V8_HOST_ARCH_X64 |
535 #if __DARWIN_UNIX03 | 486 #if __DARWIN_UNIX03 |
536 state.pc = reinterpret_cast<Address>(mcontext->__ss.__rip); | 487 state->pc = reinterpret_cast<void*>(mcontext->__ss.__rip); |
537 state.sp = reinterpret_cast<Address>(mcontext->__ss.__rsp); | 488 state->sp = reinterpret_cast<void*>(mcontext->__ss.__rsp); |
538 state.fp = reinterpret_cast<Address>(mcontext->__ss.__rbp); | 489 state->fp = reinterpret_cast<void*>(mcontext->__ss.__rbp); |
539 #else // !__DARWIN_UNIX03 | 490 #else // !__DARWIN_UNIX03 |
540 state.pc = reinterpret_cast<Address>(mcontext->ss.rip); | 491 state->pc = reinterpret_cast<void*>(mcontext->ss.rip); |
541 state.sp = reinterpret_cast<Address>(mcontext->ss.rsp); | 492 state->sp = reinterpret_cast<void*>(mcontext->ss.rsp); |
542 state.fp = reinterpret_cast<Address>(mcontext->ss.rbp); | 493 state->fp = reinterpret_cast<void*>(mcontext->ss.rbp); |
543 #endif // __DARWIN_UNIX03 | 494 #endif // __DARWIN_UNIX03 |
544 #elif V8_HOST_ARCH_IA32 | 495 #elif V8_HOST_ARCH_IA32 |
545 #if __DARWIN_UNIX03 | 496 #if __DARWIN_UNIX03 |
546 state.pc = reinterpret_cast<Address>(mcontext->__ss.__eip); | 497 state->pc = reinterpret_cast<void*>(mcontext->__ss.__eip); |
547 state.sp = reinterpret_cast<Address>(mcontext->__ss.__esp); | 498 state->sp = reinterpret_cast<void*>(mcontext->__ss.__esp); |
548 state.fp = reinterpret_cast<Address>(mcontext->__ss.__ebp); | 499 state->fp = reinterpret_cast<void*>(mcontext->__ss.__ebp); |
549 #else // !__DARWIN_UNIX03 | 500 #else // !__DARWIN_UNIX03 |
550 state.pc = reinterpret_cast<Address>(mcontext->ss.eip); | 501 state->pc = reinterpret_cast<void*>(mcontext->ss.eip); |
551 state.sp = reinterpret_cast<Address>(mcontext->ss.esp); | 502 state->sp = reinterpret_cast<void*>(mcontext->ss.esp); |
552 state.fp = reinterpret_cast<Address>(mcontext->ss.ebp); | 503 state->fp = reinterpret_cast<void*>(mcontext->ss.ebp); |
553 #endif // __DARWIN_UNIX03 | 504 #endif // __DARWIN_UNIX03 |
554 #endif // V8_HOST_ARCH_IA32 | 505 #endif // V8_HOST_ARCH_IA32 |
555 #elif V8_OS_FREEBSD | 506 #elif V8_OS_FREEBSD |
556 #if V8_HOST_ARCH_IA32 | 507 #if V8_HOST_ARCH_IA32 |
557 state.pc = reinterpret_cast<Address>(mcontext.mc_eip); | 508 state->pc = reinterpret_cast<void*>(mcontext.mc_eip); |
558 state.sp = reinterpret_cast<Address>(mcontext.mc_esp); | 509 state->sp = reinterpret_cast<void*>(mcontext.mc_esp); |
559 state.fp = reinterpret_cast<Address>(mcontext.mc_ebp); | 510 state->fp = reinterpret_cast<void*>(mcontext.mc_ebp); |
560 #elif V8_HOST_ARCH_X64 | 511 #elif V8_HOST_ARCH_X64 |
561 state.pc = reinterpret_cast<Address>(mcontext.mc_rip); | 512 state->pc = reinterpret_cast<void*>(mcontext.mc_rip); |
562 state.sp = reinterpret_cast<Address>(mcontext.mc_rsp); | 513 state->sp = reinterpret_cast<void*>(mcontext.mc_rsp); |
563 state.fp = reinterpret_cast<Address>(mcontext.mc_rbp); | 514 state->fp = reinterpret_cast<void*>(mcontext.mc_rbp); |
564 #elif V8_HOST_ARCH_ARM | 515 #elif V8_HOST_ARCH_ARM |
565 state.pc = reinterpret_cast<Address>(mcontext.mc_r15); | 516 state->pc = reinterpret_cast<void*>(mcontext.mc_r15); |
566 state.sp = reinterpret_cast<Address>(mcontext.mc_r13); | 517 state->sp = reinterpret_cast<void*>(mcontext.mc_r13); |
567 state.fp = reinterpret_cast<Address>(mcontext.mc_r11); | 518 state->fp = reinterpret_cast<void*>(mcontext.mc_r11); |
568 #endif // V8_HOST_ARCH_* | 519 #endif // V8_HOST_ARCH_* |
569 #elif V8_OS_NETBSD | 520 #elif V8_OS_NETBSD |
570 #if V8_HOST_ARCH_IA32 | 521 #if V8_HOST_ARCH_IA32 |
571 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]); | 522 state->pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_EIP]); |
572 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]); | 523 state->sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_ESP]); |
573 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]); | 524 state->fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_EBP]); |
574 #elif V8_HOST_ARCH_X64 | 525 #elif V8_HOST_ARCH_X64 |
575 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]); | 526 state->pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_RIP]); |
576 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]); | 527 state->sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RSP]); |
577 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]); | 528 state->fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RBP]); |
578 #endif // V8_HOST_ARCH_* | 529 #endif // V8_HOST_ARCH_* |
579 #elif V8_OS_OPENBSD | 530 #elif V8_OS_OPENBSD |
580 #if V8_HOST_ARCH_IA32 | 531 #if V8_HOST_ARCH_IA32 |
581 state.pc = reinterpret_cast<Address>(ucontext->sc_eip); | 532 state->pc = reinterpret_cast<void*>(ucontext->sc_eip); |
582 state.sp = reinterpret_cast<Address>(ucontext->sc_esp); | 533 state->sp = reinterpret_cast<void*>(ucontext->sc_esp); |
583 state.fp = reinterpret_cast<Address>(ucontext->sc_ebp); | 534 state->fp = reinterpret_cast<void*>(ucontext->sc_ebp); |
584 #elif V8_HOST_ARCH_X64 | 535 #elif V8_HOST_ARCH_X64 |
585 state.pc = reinterpret_cast<Address>(ucontext->sc_rip); | 536 state->pc = reinterpret_cast<void*>(ucontext->sc_rip); |
586 state.sp = reinterpret_cast<Address>(ucontext->sc_rsp); | 537 state->sp = reinterpret_cast<void*>(ucontext->sc_rsp); |
587 state.fp = reinterpret_cast<Address>(ucontext->sc_rbp); | 538 state->fp = reinterpret_cast<void*>(ucontext->sc_rbp); |
588 #endif // V8_HOST_ARCH_* | 539 #endif // V8_HOST_ARCH_* |
589 #elif V8_OS_SOLARIS | 540 #elif V8_OS_SOLARIS |
590 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); | 541 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_PC]); |
591 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); | 542 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_SP]); |
592 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); | 543 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_FP]); |
593 #elif V8_OS_QNX | 544 #elif V8_OS_QNX |
594 #if V8_HOST_ARCH_IA32 | 545 #if V8_HOST_ARCH_IA32 |
595 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); | 546 state->pc = reinterpret_cast<void*>(mcontext.cpu.eip); |
596 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); | 547 state->sp = reinterpret_cast<void*>(mcontext.cpu.esp); |
597 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); | 548 state->fp = reinterpret_cast<void*>(mcontext.cpu.ebp); |
598 #elif V8_HOST_ARCH_ARM | 549 #elif V8_HOST_ARCH_ARM |
599 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); | 550 state->pc = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_PC]); |
600 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); | 551 state->sp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_SP]); |
601 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); | 552 state->fp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_FP]); |
602 #endif // V8_HOST_ARCH_* | 553 #endif // V8_HOST_ARCH_* |
603 #elif V8_OS_AIX | 554 #elif V8_OS_AIX |
604 state.pc = reinterpret_cast<Address>(mcontext.jmp_context.iar); | 555 state->pc = reinterpret_cast<void*>(mcontext.jmp_context.iar); |
605 state.sp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[1]); | 556 state->sp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[1]); |
606 state.fp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[31]); | 557 state->fp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[31]); |
607 #endif // V8_OS_AIX | 558 #endif // V8_OS_AIX |
608 #endif // USE_SIMULATOR | |
609 sampler->SampleStack(state); | |
610 } | 559 } |
611 #endif // V8_OS_NACL | 560 |
561 #endif // !V8_OS_NACL | |
612 | 562 |
613 #endif // USE_SIGNALS | 563 #endif // USE_SIGNALS |
614 | 564 |
615 | 565 |
616 class SamplerThread : public base::Thread { | |
617 public: | |
618 static const int kSamplerThreadStackSize = 64 * KB; | |
619 | |
620 explicit SamplerThread(int interval) | |
621 : Thread(base::Thread::Options("SamplerThread", kSamplerThreadStackSize)), | |
622 interval_(interval) {} | |
623 | |
624 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | |
625 static void TearDown() { delete mutex_; mutex_ = NULL; } | |
626 | |
627 static void AddActiveSampler(Sampler* sampler) { | |
628 bool need_to_start = false; | |
629 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
630 if (instance_ == NULL) { | |
631 // Start a thread that will send SIGPROF signal to VM threads, | |
632 // when CPU profiling will be enabled. | |
633 instance_ = new SamplerThread(sampler->interval()); | |
634 need_to_start = true; | |
635 } | |
636 | |
637 DCHECK(sampler->IsActive()); | |
638 DCHECK(instance_->interval_ == sampler->interval()); | |
639 | |
640 #if defined(USE_SIGNALS) | |
641 AddSampler(sampler); | |
642 #else | |
643 DCHECK(!instance_->active_samplers_.Contains(sampler)); | |
644 instance_->active_samplers_.Add(sampler); | |
645 #endif // USE_SIGNALS | |
646 | |
647 if (need_to_start) instance_->StartSynchronously(); | |
648 } | |
649 | |
650 static void RemoveSampler(Sampler* sampler) { | |
651 SamplerThread* instance_to_remove = NULL; | |
652 { | |
653 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
654 | |
655 DCHECK(sampler->IsActive() || sampler->IsRegistered()); | |
656 #if defined(USE_SIGNALS) | |
657 { | |
658 AtomicGuard atomic_guard(&sampler_list_access_counter_); | |
659 // Remove sampler from map. | |
660 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
661 void* thread_key = ThreadKey(thread_id); | |
662 uint32_t thread_hash = ThreadHash(thread_id); | |
663 HashMap::Entry* entry = | |
664 thread_id_to_samplers_.Get().Lookup(thread_key, thread_hash); | |
665 DCHECK(entry != NULL); | |
666 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
667 samplers->RemoveElement(sampler); | |
668 if (samplers->is_empty()) { | |
669 thread_id_to_samplers_.Pointer()->Remove(thread_key, thread_hash); | |
670 delete samplers; | |
671 } | |
672 if (thread_id_to_samplers_.Get().occupancy() == 0) { | |
673 instance_to_remove = instance_; | |
674 instance_ = NULL; | |
675 } | |
676 } | |
677 #else | |
678 bool removed = instance_->active_samplers_.RemoveElement(sampler); | |
679 DCHECK(removed); | |
680 USE(removed); | |
681 | |
682 // We cannot delete the instance immediately as we need to Join() the | |
683 // thread but we are holding mutex_ and the thread may try to acquire it. | |
684 if (instance_->active_samplers_.is_empty()) { | |
685 instance_to_remove = instance_; | |
686 instance_ = NULL; | |
687 } | |
688 #endif // USE_SIGNALS | |
689 } | |
690 | |
691 if (!instance_to_remove) return; | |
692 instance_to_remove->Join(); | |
693 delete instance_to_remove; | |
694 } | |
695 | |
696 // Unlike AddActiveSampler, this method only adds a sampler, | |
697 // but won't start the sampler thread. | |
698 static void RegisterSampler(Sampler* sampler) { | |
699 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
700 #if defined(USE_SIGNALS) | |
701 AddSampler(sampler); | |
702 #endif // USE_SIGNALS | |
703 } | |
704 | |
705 // Implement Thread::Run(). | |
706 virtual void Run() { | |
707 while (true) { | |
708 { | |
709 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
710 #if defined(USE_SIGNALS) | |
711 if (thread_id_to_samplers_.Get().occupancy() == 0) break; | |
712 if (SignalHandler::Installed()) { | |
713 for (HashMap::Entry *p = thread_id_to_samplers_.Get().Start(); | |
714 p != NULL; p = thread_id_to_samplers_.Get().Next(p)) { | |
715 pthread_t thread_id = reinterpret_cast<pthread_t>(p->key); | |
716 pthread_kill(thread_id, SIGPROF); | |
717 } | |
718 } | |
719 #else | |
720 if (active_samplers_.is_empty()) break; | |
721 // When CPU profiling is enabled both JavaScript and C++ code is | |
722 // profiled. We must not suspend. | |
723 for (int i = 0; i < active_samplers_.length(); ++i) { | |
724 Sampler* sampler = active_samplers_.at(i); | |
725 if (!sampler->IsProfiling()) continue; | |
726 sampler->DoSample(); | |
727 } | |
728 #endif // USE_SIGNALS | |
729 } | |
730 base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_)); | |
731 } | |
732 } | |
733 | |
734 private: | |
735 // Protects the process wide state below. | |
736 static base::Mutex* mutex_; | |
737 static SamplerThread* instance_; | |
738 | |
739 const int interval_; | |
740 | |
741 #if defined(USE_SIGNALS) | |
742 struct HashMapCreateTrait { | |
743 static void Construct(HashMap* allocated_ptr) { | |
744 new (allocated_ptr) HashMap(HashMap::PointersMatch); | |
745 } | |
746 }; | |
747 friend class SignalHandler; | |
748 static base::LazyInstance<HashMap, HashMapCreateTrait>::type | |
749 thread_id_to_samplers_; | |
750 static AtomicValue<int> sampler_list_access_counter_; | |
751 static void AddSampler(Sampler* sampler) { | |
752 AtomicGuard atomic_guard(&sampler_list_access_counter_); | |
753 // Add sampler into map if needed. | |
754 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
755 HashMap::Entry *entry = | |
756 thread_id_to_samplers_.Pointer()->LookupOrInsert(ThreadKey(thread_id), | |
757 ThreadHash(thread_id)); | |
758 if (entry->value == NULL) { | |
759 SamplerList* samplers = new SamplerList(); | |
760 samplers->Add(sampler); | |
761 entry->value = samplers; | |
762 } else { | |
763 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
764 if (!samplers->Contains(sampler)) { | |
765 samplers->Add(sampler); | |
766 } | |
767 } | |
768 } | |
769 #else | |
770 SamplerList active_samplers_; | |
771 #endif // USE_SIGNALS | |
772 | |
773 DISALLOW_COPY_AND_ASSIGN(SamplerThread); | |
774 }; | |
775 | |
776 | |
777 base::Mutex* SamplerThread::mutex_ = NULL; | |
778 SamplerThread* SamplerThread::instance_ = NULL; | |
779 #if defined(USE_SIGNALS) | |
780 base::LazyInstance<HashMap, SamplerThread::HashMapCreateTrait>::type | |
781 SamplerThread::thread_id_to_samplers_ = LAZY_INSTANCE_INITIALIZER; | |
782 AtomicValue<int> SamplerThread::sampler_list_access_counter_(0); | |
783 | |
784 // As Native Client does not support signal handling, profiling is disabled. | |
785 #if !V8_OS_NACL | |
786 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, | |
787 void* context) { | |
788 USE(info); | |
789 if (signal != SIGPROF) return; | |
790 AtomicGuard atomic_guard(&SamplerThread::sampler_list_access_counter_, false); | |
791 if (!atomic_guard.is_success()) return; | |
792 pthread_t thread_id = pthread_self(); | |
793 HashMap::Entry* entry = | |
794 SamplerThread::thread_id_to_samplers_.Pointer()->Lookup( | |
795 ThreadKey(thread_id), ThreadHash(thread_id)); | |
796 if (entry == NULL) | |
797 return; | |
798 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
799 for (int i = 0; i < samplers->length(); ++i) { | |
800 Sampler* sampler = samplers->at(i); | |
801 CollectSample(context, sampler); | |
802 } | |
803 } | |
804 #endif // !V8_OS_NACL | |
805 #endif // USE_SIGNALs | |
806 | |
807 | |
808 // | |
809 // StackTracer implementation | |
810 // | |
811 DISABLE_ASAN void TickSample::Init(Isolate* isolate, | |
812 const v8::RegisterState& regs, | |
813 RecordCEntryFrame record_c_entry_frame, | |
814 bool update_stats) { | |
815 timestamp = base::TimeTicks::HighResolutionNow(); | |
816 pc = reinterpret_cast<Address>(regs.pc); | |
817 state = isolate->current_vm_state(); | |
818 this->update_stats = update_stats; | |
819 | |
820 // Avoid collecting traces while doing GC. | |
821 if (state == GC) return; | |
822 | |
823 Address js_entry_sp = isolate->js_entry_sp(); | |
824 if (js_entry_sp == 0) return; // Not executing JS now. | |
825 | |
826 if (pc && IsNoFrameRegion(pc)) { | |
827 // Can't collect stack. Mark the sample as spoiled. | |
828 timestamp = base::TimeTicks(); | |
829 pc = 0; | |
830 return; | |
831 } | |
832 | |
833 ExternalCallbackScope* scope = isolate->external_callback_scope(); | |
834 Address handler = Isolate::handler(isolate->thread_local_top()); | |
835 // If there is a handler on top of the external callback scope then | |
836 // we have already entrered JavaScript again and the external callback | |
837 // is not the top function. | |
838 if (scope && scope->scope_address() < handler) { | |
839 external_callback_entry = *scope->callback_entrypoint_address(); | |
840 has_external_callback = true; | |
841 } else { | |
842 // sp register may point at an arbitrary place in memory, make | |
843 // sure MSAN doesn't complain about it. | |
844 MSAN_MEMORY_IS_INITIALIZED(regs.sp, sizeof(Address)); | |
845 // Sample potential return address value for frameless invocation of | |
846 // stubs (we'll figure out later, if this value makes sense). | |
847 tos = Memory::Address_at(reinterpret_cast<Address>(regs.sp)); | |
848 has_external_callback = false; | |
849 } | |
850 | |
851 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), | |
852 reinterpret_cast<Address>(regs.sp), js_entry_sp); | |
853 top_frame_type = it.top_frame_type(); | |
854 | |
855 SampleInfo info; | |
856 GetStackSample(isolate, regs, record_c_entry_frame, | |
857 reinterpret_cast<void**>(&stack[0]), kMaxFramesCount, &info); | |
858 frames_count = static_cast<unsigned>(info.frames_count); | |
859 if (!frames_count) { | |
860 // It is executing JS but failed to collect a stack trace. | |
861 // Mark the sample as spoiled. | |
862 timestamp = base::TimeTicks(); | |
863 pc = 0; | |
864 } | |
865 } | |
866 | |
867 | |
868 void TickSample::GetStackSample(Isolate* isolate, const v8::RegisterState& regs, | |
869 RecordCEntryFrame record_c_entry_frame, | |
870 void** frames, size_t frames_limit, | |
871 v8::SampleInfo* sample_info) { | |
872 sample_info->frames_count = 0; | |
873 sample_info->vm_state = isolate->current_vm_state(); | |
874 if (sample_info->vm_state == GC) return; | |
875 | |
876 Address js_entry_sp = isolate->js_entry_sp(); | |
877 if (js_entry_sp == 0) return; // Not executing JS now. | |
878 | |
879 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), | |
880 reinterpret_cast<Address>(regs.sp), js_entry_sp); | |
881 size_t i = 0; | |
882 if (record_c_entry_frame == kIncludeCEntryFrame && !it.done() && | |
883 it.top_frame_type() == StackFrame::EXIT) { | |
884 frames[i++] = isolate->c_function(); | |
885 } | |
886 while (!it.done() && i < frames_limit) { | |
887 if (it.frame()->is_interpreted()) { | |
888 // For interpreted frames use the bytecode array pointer as the pc. | |
889 InterpretedFrame* frame = static_cast<InterpretedFrame*>(it.frame()); | |
890 // Since the sampler can interrupt execution at any point the | |
891 // bytecode_array might be garbage, so don't dereference it. | |
892 Address bytecode_array = | |
893 reinterpret_cast<Address>(frame->GetBytecodeArray()) - kHeapObjectTag; | |
894 frames[i++] = bytecode_array + BytecodeArray::kHeaderSize + | |
895 frame->GetBytecodeOffset(); | |
896 } else { | |
897 frames[i++] = it.frame()->pc(); | |
898 } | |
899 it.Advance(); | |
900 } | |
901 sample_info->frames_count = i; | |
902 } | |
903 | |
904 | |
905 void Sampler::SetUp() { | 566 void Sampler::SetUp() { |
906 #if defined(USE_SIGNALS) | 567 #if defined(USE_SIGNALS) |
907 SignalHandler::SetUp(); | 568 SignalHandler::SetUp(); |
908 #endif | 569 #endif |
909 SamplerThread::SetUp(); | |
910 } | 570 } |
911 | 571 |
912 | 572 |
913 void Sampler::TearDown() { | 573 void Sampler::TearDown() { |
914 SamplerThread::TearDown(); | |
915 #if defined(USE_SIGNALS) | 574 #if defined(USE_SIGNALS) |
916 SignalHandler::TearDown(); | 575 SignalHandler::TearDown(); |
917 #endif | 576 #endif |
918 } | 577 } |
919 | 578 |
920 Sampler::Sampler(Isolate* isolate, int interval) | 579 Sampler::Sampler(Isolate* isolate) |
921 : isolate_(isolate), | 580 : is_counting_samples_(false), |
922 interval_(interval), | 581 js_sample_count_(0), |
582 external_sample_count_(0), | |
583 isolate_(isolate), | |
923 profiling_(false), | 584 profiling_(false), |
924 has_processing_thread_(false), | 585 active_(false) { |
925 active_(false), | |
926 registered_(false), | |
927 is_counting_samples_(false), | |
928 js_sample_count_(0), | |
929 external_sample_count_(0) { | |
930 data_ = new PlatformData; | 586 data_ = new PlatformData; |
931 } | 587 } |
932 | 588 |
933 Sampler::~Sampler() { | 589 Sampler::~Sampler() { |
934 DCHECK(!IsActive()); | 590 DCHECK(!IsActive()); |
935 if (IsRegistered()) { | |
936 SamplerThread::RemoveSampler(this); | |
937 } | |
938 delete data_; | 591 delete data_; |
939 } | 592 } |
940 | 593 |
941 void Sampler::Start() { | 594 void Sampler::Start() { |
942 DCHECK(!IsActive()); | 595 DCHECK(!IsActive()); |
943 SetActive(true); | 596 SetActive(true); |
944 SamplerThread::AddActiveSampler(this); | 597 SamplerManager::AddSampler(this); |
945 } | 598 } |
946 | 599 |
947 | 600 |
948 void Sampler::Stop() { | 601 void Sampler::Stop() { |
602 SamplerManager::RemoveSampler(this); | |
949 DCHECK(IsActive()); | 603 DCHECK(IsActive()); |
950 SamplerThread::RemoveSampler(this); | |
951 SetActive(false); | 604 SetActive(false); |
952 SetRegistered(false); | |
953 } | 605 } |
954 | 606 |
955 | 607 |
956 void Sampler::IncreaseProfilingDepth() { | 608 void Sampler::IncreaseProfilingDepth() { |
957 base::NoBarrier_AtomicIncrement(&profiling_, 1); | 609 base::NoBarrier_AtomicIncrement(&profiling_, 1); |
958 #if defined(USE_SIGNALS) | 610 #if defined(USE_SIGNALS) |
959 SignalHandler::IncreaseSamplerCount(); | 611 SignalHandler::IncreaseSamplerCount(); |
960 #endif | 612 #endif |
961 } | 613 } |
962 | 614 |
963 | 615 |
964 void Sampler::DecreaseProfilingDepth() { | 616 void Sampler::DecreaseProfilingDepth() { |
965 #if defined(USE_SIGNALS) | 617 #if defined(USE_SIGNALS) |
966 SignalHandler::DecreaseSamplerCount(); | 618 SignalHandler::DecreaseSamplerCount(); |
967 #endif | 619 #endif |
968 base::NoBarrier_AtomicIncrement(&profiling_, -1); | 620 base::NoBarrier_AtomicIncrement(&profiling_, -1); |
969 } | 621 } |
970 | 622 |
971 | 623 |
972 void Sampler::SampleStack(const v8::RegisterState& state) { | |
973 TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); | |
974 TickSample sample_obj; | |
975 if (sample == NULL) sample = &sample_obj; | |
976 sample->Init(isolate_, state, TickSample::kIncludeCEntryFrame, true); | |
977 if (is_counting_samples_ && !sample->timestamp.IsNull()) { | |
978 if (sample->state == JS) ++js_sample_count_; | |
979 if (sample->state == EXTERNAL) ++external_sample_count_; | |
980 } | |
981 Tick(sample); | |
982 if (sample != &sample_obj) { | |
983 isolate_->cpu_profiler()->FinishTickSample(); | |
984 } | |
985 } | |
986 | |
987 | |
988 #if defined(USE_SIGNALS) | 624 #if defined(USE_SIGNALS) |
989 | 625 |
990 void Sampler::DoSample() { | 626 void Sampler::DoSample() { |
991 if (!SignalHandler::Installed()) return; | 627 if (!SignalHandler::Installed()) return; |
992 if (!IsActive() && !IsRegistered()) { | |
993 SamplerThread::RegisterSampler(this); | |
994 SetRegistered(true); | |
995 } | |
996 pthread_kill(platform_data()->vm_tid(), SIGPROF); | 628 pthread_kill(platform_data()->vm_tid(), SIGPROF); |
997 } | 629 } |
998 | 630 |
999 #elif V8_OS_WIN || V8_OS_CYGWIN | 631 #elif V8_OS_WIN || V8_OS_CYGWIN |
1000 | 632 |
1001 void Sampler::DoSample() { | 633 void Sampler::DoSample() { |
1002 HANDLE profiled_thread = platform_data()->profiled_thread(); | 634 HANDLE profiled_thread = platform_data()->profiled_thread(); |
1003 if (profiled_thread == NULL) return; | 635 if (profiled_thread == NULL) return; |
1004 | 636 |
1005 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 637 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
1006 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 638 if (SuspendThread(profiled_thread) == kSuspendFailed) return; |
1007 | 639 |
1008 // Context used for sampling the register state of the profiled thread. | 640 // Context used for sampling the register state of the profiled thread. |
1009 CONTEXT context; | 641 CONTEXT context; |
1010 memset(&context, 0, sizeof(context)); | 642 memset(&context, 0, sizeof(context)); |
1011 context.ContextFlags = CONTEXT_FULL; | 643 context.ContextFlags = CONTEXT_FULL; |
1012 if (GetThreadContext(profiled_thread, &context) != 0) { | 644 if (GetThreadContext(profiled_thread, &context) != 0) { |
1013 v8::RegisterState state; | 645 v8::RegisterState state; |
1014 #if defined(USE_SIMULATOR) | 646 #if V8_HOST_ARCH_X64 |
1015 if (!SimulatorHelper::FillRegisters(isolate(), &state)) { | 647 state.pc = reinterpret_cast<void*>(context.Rip); |
1016 ResumeThread(profiled_thread); | 648 state.sp = reinterpret_cast<void*>(context.Rsp); |
1017 return; | 649 state.fp = reinterpret_cast<void*>(context.Rbp); |
1018 } | |
1019 #else | 650 #else |
1020 #if V8_HOST_ARCH_X64 | 651 state.pc = reinterpret_cast<void*>(context.Eip); |
1021 state.pc = reinterpret_cast<Address>(context.Rip); | 652 state.sp = reinterpret_cast<void*>(context.Esp); |
1022 state.sp = reinterpret_cast<Address>(context.Rsp); | 653 state.fp = reinterpret_cast<void*>(context.Ebp); |
1023 state.fp = reinterpret_cast<Address>(context.Rbp); | |
1024 #else | |
1025 state.pc = reinterpret_cast<Address>(context.Eip); | |
1026 state.sp = reinterpret_cast<Address>(context.Esp); | |
1027 state.fp = reinterpret_cast<Address>(context.Ebp); | |
1028 #endif | 654 #endif |
1029 #endif // USE_SIMULATOR | |
1030 SampleStack(state); | 655 SampleStack(state); |
1031 } | 656 } |
1032 ResumeThread(profiled_thread); | 657 ResumeThread(profiled_thread); |
1033 } | 658 } |
1034 | 659 |
1035 #endif // USE_SIGNALS | 660 #endif // USE_SIGNALS |
1036 | 661 |
1037 | 662 } // namespace sampler |
1038 } // namespace internal | |
1039 } // namespace v8 | 663 } // namespace v8 |
OLD | NEW |