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 DECLARE_FLAG(bool, trace_profiled_isolates); | |
17 | |
18 static void CollectSample(IsolateProfilerData* profiler_data, | |
19 uintptr_t pc, | |
20 uintptr_t fp, | |
21 uintptr_t stack_lower, | |
22 uintptr_t stack_upper) { | |
23 uintptr_t sp = stack_lower; | |
24 ASSERT(profiler_data != NULL); | |
25 SampleBuffer* sample_buffer = profiler_data->sample_buffer(); | |
26 ASSERT(sample_buffer != NULL); | |
27 Sample* sample = sample_buffer->ReserveSample(); | |
28 ASSERT(sample != NULL); | |
29 sample->timestamp = OS::GetCurrentTimeMicros(); | |
30 // TODO(johnmccutchan): Make real use of vm_tags and runtime_tags. | |
31 // Issue # 14777 | |
32 sample->vm_tags = Sample::kExecuting; | |
33 sample->runtime_tags = 0; | |
34 int64_t cpu_usage; | |
35 Thread::GetThreadCpuUsage(profiler_data->thread_id(), &cpu_usage); | |
36 sample->cpu_usage = profiler_data->ComputeDeltaAndSetCpuUsage(cpu_usage); | |
37 ProfilerSampleStackWalker stackWalker(sample, stack_lower, stack_upper, | |
38 pc, fp, sp); | |
39 stackWalker.walk(); | |
40 } | |
41 | |
42 | |
43 static bool GrabRegisters(ThreadId thread, uintptr_t* pc, uintptr_t* fp, | |
44 uintptr_t* sp) { | |
45 CONTEXT context; | |
46 memset(&context, 0, sizeof(context)); | |
47 context.ContextFlags = CONTEXT_FULL; | |
48 if (GetThreadContext(thread, &context) != 0) { | |
49 #if defined(TARGET_ARCH_IA32) | |
50 *pc = static_cast<uintptr_t>(context.Eip); | |
51 *fp = static_cast<uintptr_t>(context.Ebp); | |
52 *sp = static_cast<uintptr_t>(context.Esp); | |
53 #elif defined(TARGET_ARCH_X64) | |
54 *pc = reinterpret_cast<uintptr_t>(context.Rip); | |
55 *fp = reinterpret_cast<uintptr_t>(context.Rbp); | |
56 *sp = reinterpret_cast<uintptr_t>(context.Rsp); | |
57 #else | |
58 UNIMPLEMENTED(); | |
59 #endif | |
60 return true; | |
61 } | |
62 return false; | |
63 } | |
64 | |
65 | |
66 static void SuspendAndSample(Isolate* isolate, | |
67 IsolateProfilerData* profiler_data) { | |
68 ASSERT(GetCurrentThread() != profiler_data->thread_id()); | |
69 DWORD result = SuspendThread(profiler_data->thread_id()); | |
70 if (result == kThreadError) { | |
71 return; | |
72 } | |
73 uintptr_t PC; | |
74 uintptr_t FP; | |
75 uintptr_t stack_lower; | |
76 uintptr_t stack_upper; | |
77 bool r = isolate->GetStackBounds(&stack_lower, &stack_upper); | |
78 if (r) { | |
79 r = GrabRegisters(profiler_data->thread_id(), &PC, &FP, &stack_lower); | |
80 if (r) { | |
81 int64_t sample_time = OS::GetCurrentTimeMicros(); | |
82 profiler_data->SampledAt(sample_time); | |
83 CollectSample(profiler_data, PC, FP, stack_lower, stack_upper); | |
84 } | |
85 } | |
86 | |
87 ResumeThread(profiler_data->thread_id()); | |
88 } | |
89 | |
90 | |
91 static void Reschedule(IsolateProfilerData* profiler_data) { | |
92 profiler_data->Scheduled(OS::GetCurrentTimeMicros(), | |
93 profiler_data->thread_id()); | |
94 } | |
95 | |
96 | |
97 int64_t ProfilerManager::SampleAndRescheduleIsolates(int64_t current_time) { | |
98 if (isolates_size_ == 0) { | |
99 return 0; | |
100 } | |
101 static const int64_t max_time = 0x7fffffffffffffffLL; | |
102 int64_t lowest = max_time; | |
103 for (intptr_t i = 0; i < isolates_size_; i++) { | |
104 Isolate* isolate = isolates_[i]; | |
105 ScopedMutex isolate_lock(isolate->profiler_data_mutex()); | |
106 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
107 if ((profiler_data == NULL) || !profiler_data->CanExpire() || | |
108 (profiler_data->sample_buffer() == NULL)) { | |
109 // Descheduled. | |
110 continue; | |
111 } | |
112 if (profiler_data->ShouldSample(current_time)) { | |
113 SuspendAndSample(isolate, profiler_data); | |
114 Reschedule(profiler_data); | |
115 } | |
116 if (profiler_data->CanExpire()) { | |
117 int64_t isolate_time_left = | |
118 profiler_data->TimeUntilExpiration(current_time); | |
119 if (isolate_time_left < 0) { | |
120 continue; | |
121 } | |
122 if (isolate_time_left < lowest) { | |
123 lowest = isolate_time_left; | |
124 } | |
125 } | |
126 } | |
127 if (isolates_size_ == 0) { | |
128 return 0; | |
129 } | |
130 if (lowest == max_time) { | |
131 return 0; | |
132 } | |
133 ASSERT(lowest != max_time); | |
134 ASSERT(lowest > 0); | |
135 return lowest; | |
136 } | |
137 | |
138 | |
139 void ProfilerManager::ThreadMain(uword parameters) { | |
140 ASSERT(initialized_); | |
141 ASSERT(FLAG_profile); | |
142 if (FLAG_trace_profiled_isolates) { | |
143 OS::Print("ProfilerManager Windows ready.\n"); | |
144 } | |
145 { | |
146 // Signal to main thread we are ready. | |
147 ScopedMonitor startup_lock(start_stop_monitor_); | |
148 thread_running_ = true; | |
149 startup_lock.Notify(); | |
150 } | |
151 ScopedMonitor lock(monitor_); | |
152 while (!shutdown_) { | |
153 int64_t current_time = OS::GetCurrentTimeMicros(); | |
154 int64_t next_sample = SampleAndRescheduleIsolates(current_time); | |
155 lock.WaitMicros(next_sample); | |
156 } | |
157 if (FLAG_trace_profiled_isolates) { | |
158 OS::Print("ProfilerManager Windows exiting.\n"); | |
159 } | |
160 { | |
161 // Signal to main thread we are exiting. | |
162 ScopedMonitor shutdown_lock(start_stop_monitor_); | |
163 thread_running_ = false; | |
164 shutdown_lock.Notify(); | |
165 } | |
166 } | |
167 | |
168 } // namespace dart | |
169 | |
170 #endif // defined(TARGET_OS_WINDOWS) | |
OLD | NEW |