OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_WINDOWS) |
| 7 |
| 8 #include "vm/isolate.h" |
| 9 #include "vm/profiler.h" |
| 10 |
| 11 namespace dart { |
| 12 |
| 13 #define kThreadError -1 |
| 14 |
| 15 DECLARE_FLAG(bool, profile); |
| 16 |
| 17 static void CollectSample(IsolateProfilerData* profiler_data, |
| 18 uintptr_t pc, |
| 19 uintptr_t fp, |
| 20 uintptr_t stack_lower, |
| 21 uintptr_t stack_upper) { |
| 22 uintptr_t sp = stack_lower; |
| 23 SampleBuffer* sample_buffer = profiler_data->sample_buffer(); |
| 24 Sample* sample = sample_buffer->ReserveSample(); |
| 25 ASSERT(sample != NULL); |
| 26 sample->timestamp = OS::GetCurrentTimeMicros(); |
| 27 // TODO(johnmccutchan): Make real use of vm_tags and runtime_tags. |
| 28 // Issue # 14777 |
| 29 sample->vm_tags = Sample::kExecuting; |
| 30 sample->runtime_tags = 0; |
| 31 int64_t cpu_usage; |
| 32 Thread::GetThreadCpuUsage(profiler_data->thread_id(), &cpu_usage); |
| 33 sample->cpu_usage = profiler_data->ComputeDeltaAndSetCpuUsage(cpu_usage); |
| 34 ProfilerSampleStackWalker stackWalker(sample, stack_lower, stack_upper, |
| 35 pc, fp, sp); |
| 36 stackWalker.walk(); |
| 37 } |
| 38 |
| 39 |
| 40 static bool GrabRegisters(ThreadId thread, uintptr_t* pc, uintptr_t* fp, |
| 41 uintptr_t* sp) { |
| 42 CONTEXT context; |
| 43 memset(&context, 0, sizeof(context)); |
| 44 context.ContextFlags = CONTEXT_FULL; |
| 45 if (GetThreadContext(thread, &context) != 0) { |
| 46 #if defined(TARGET_ARCH_IA32) |
| 47 *pc = static_cast<uintptr_t>(context.Eip); |
| 48 *fp = static_cast<uintptr_t>(context.Ebp); |
| 49 *sp = static_cast<uintptr_t>(context.Esp); |
| 50 #elif defined(TARGET_ARCH_X64) |
| 51 *pc = reinterpret_cast<uintptr_t>(context.Rip); |
| 52 *fp = reinterpret_cast<uintptr_t>(context.Rbp); |
| 53 *sp = reinterpret_cast<uintptr_t>(context.Rsp); |
| 54 #else |
| 55 UNIMPLEMENTED(); |
| 56 #endif |
| 57 return true; |
| 58 } |
| 59 return false; |
| 60 } |
| 61 |
| 62 |
| 63 static void SuspendAndSample(Isolate* isolate, |
| 64 IsolateProfilerData* profiler_data) { |
| 65 ASSERT(GetCurrentThread() != profiler_data->thread_id()); |
| 66 DWORD result = SuspendThread(profiler_data->thread_id()); |
| 67 if (result == kThreadError) { |
| 68 return; |
| 69 } |
| 70 uintptr_t PC; |
| 71 uintptr_t FP; |
| 72 uintptr_t stack_lower; |
| 73 uintptr_t stack_upper; |
| 74 bool r = isolate->GetStackBounds(&stack_lower, &stack_upper); |
| 75 if (r) { |
| 76 r = GrabRegisters(profiler_data->thread_id(), &PC, &FP, &stack_lower); |
| 77 if (r) { |
| 78 int64_t sample_time = OS::GetCurrentTimeMicros(); |
| 79 profiler_data->SampledAt(sample_time); |
| 80 CollectSample(profiler_data, PC, FP, stack_lower, stack_upper); |
| 81 } |
| 82 } |
| 83 |
| 84 ResumeThread(profiler_data->thread_id()); |
| 85 } |
| 86 |
| 87 |
| 88 static void Reschedule(IsolateProfilerData* profiler_data) { |
| 89 profiler_data->Scheduled(OS::GetCurrentTimeMicros(), |
| 90 profiler_data->thread_id()); |
| 91 } |
| 92 |
| 93 |
| 94 int64_t ProfilerManager::SampleAndRescheduleIsolates(int64_t current_time) { |
| 95 if (isolates_size_ == 0) { |
| 96 return 0; |
| 97 } |
| 98 static const int64_t max_time = 0x7fffffffffffffffLL; |
| 99 int64_t lowest = max_time; |
| 100 for (intptr_t i = 0; i < isolates_size_; i++) { |
| 101 Isolate* isolate = isolates_[i]; |
| 102 ScopedMutex isolate_lock(isolate->profiler_data_mutex()); |
| 103 IsolateProfilerData* profiler_data = isolate->profiler_data(); |
| 104 ASSERT(profiler_data != NULL); |
| 105 if (profiler_data->ShouldSample(current_time)) { |
| 106 SuspendAndSample(isolate, profiler_data); |
| 107 Reschedule(profiler_data); |
| 108 } |
| 109 if (profiler_data->CanExpire()) { |
| 110 int64_t isolate_time_left = |
| 111 profiler_data->TimeUntilExpiration(current_time); |
| 112 if (isolate_time_left < 0) { |
| 113 continue; |
| 114 } |
| 115 if (isolate_time_left < lowest) { |
| 116 lowest = isolate_time_left; |
| 117 } |
| 118 } |
| 119 } |
| 120 if (isolates_size_ == 0) { |
| 121 return 0; |
| 122 } |
| 123 if (lowest == max_time) { |
| 124 return 0; |
| 125 } |
| 126 ASSERT(lowest != max_time); |
| 127 ASSERT(lowest > 0); |
| 128 return lowest; |
| 129 } |
| 130 |
| 131 |
| 132 void ProfilerManager::ThreadMain(uword parameters) { |
| 133 ASSERT(initialized_); |
| 134 ASSERT(FLAG_profile); |
| 135 ScopedMonitor lock(monitor_); |
| 136 while (!shutdown_) { |
| 137 int64_t current_time = OS::GetCurrentTimeMicros(); |
| 138 int64_t next_sample = SampleAndRescheduleIsolates(current_time); |
| 139 lock.WaitMicros(next_sample); |
| 140 } |
| 141 } |
| 142 |
| 143 } // namespace dart |
| 144 |
| 145 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |