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_MACOS) | |
7 | |
8 #include "vm/isolate.h" | |
9 #include "vm/json_stream.h" | |
10 #include "vm/profiler.h" | |
11 #include "vm/signal_handler.h" | |
12 | |
13 namespace dart { | |
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 sp, | |
22 uintptr_t stack_lower, | |
23 uintptr_t stack_upper) { | |
24 SampleBuffer* sample_buffer = profiler_data->sample_buffer(); | |
25 Sample* sample = sample_buffer->ReserveSample(); | |
26 ASSERT(sample != NULL); | |
27 sample->timestamp = OS::GetCurrentTimeMicros(); | |
28 // TODO(johnmccutchan): Make real use of vm_tags and runtime_tags. | |
29 // Issue # 14777 | |
30 sample->vm_tags = Sample::kExecuting; | |
31 sample->runtime_tags = 0; | |
32 int64_t cpu_usage = 0; | |
33 Thread::GetThreadCpuUsage(profiler_data->thread_id(), &cpu_usage); | |
34 sample->cpu_usage = profiler_data->ComputeDeltaAndSetCpuUsage(cpu_usage); | |
35 ProfilerSampleStackWalker stackWalker(sample, stack_lower, stack_upper, | |
36 pc, fp, sp); | |
37 stackWalker.walk(); | |
38 } | |
39 | |
40 | |
41 static void ProfileSignalAction(int signal, siginfo_t* info, void* context_) { | |
42 if (signal != SIGPROF) { | |
43 return; | |
44 } | |
45 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
46 mcontext_t mcontext = context->uc_mcontext; | |
47 Isolate* isolate = Isolate::Current(); | |
48 if (isolate == NULL) { | |
49 return; | |
50 } | |
51 // Thread owns no profiler locks at this point. | |
52 { | |
53 // Thread owns isolate profiler data mutex. | |
54 ScopedMutex profiler_data_lock(isolate->profiler_data_mutex()); | |
55 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
56 if ((profiler_data == NULL) || !profiler_data->CanExpire() || | |
57 (profiler_data->sample_buffer() == NULL)) { | |
58 // Descheduled. | |
59 return; | |
60 } | |
61 if (profiler_data->thread_id() == Thread::GetCurrentThreadId()) { | |
62 // Still scheduled on this thread. | |
63 uintptr_t stack_lower = 0; | |
64 uintptr_t stack_upper = 0; | |
65 isolate->GetStackBounds(&stack_lower, &stack_upper); | |
66 uintptr_t PC = SignalHandler::GetProgramCounter(mcontext); | |
67 uintptr_t FP = SignalHandler::GetFramePointer(mcontext); | |
68 uintptr_t SP = SignalHandler::GetStackPointer(mcontext); | |
69 int64_t sample_time = OS::GetCurrentTimeMicros(); | |
70 profiler_data->SampledAt(sample_time); | |
71 CollectSample(profiler_data, PC, FP, SP, stack_lower, stack_upper); | |
72 } | |
73 } | |
74 // Thread owns no profiler locks at this point. | |
75 // This call will acquire both ProfilerManager::monitor and the | |
76 // isolate's profiler data mutex. | |
77 ProfilerManager::ScheduleIsolate(isolate, true); | |
78 } | |
79 | |
80 | |
81 int64_t ProfilerManager::SampleAndRescheduleIsolates(int64_t current_time) { | |
82 if (isolates_size_ == 0) { | |
83 return 0; | |
84 } | |
85 static const int64_t max_time = 0x7fffffffffffffffLL; | |
86 int64_t lowest = max_time; | |
87 intptr_t i = 0; | |
88 while (i < isolates_size_) { | |
89 Isolate* isolate = isolates_[i]; | |
90 ScopedMutex isolate_lock(isolate->profiler_data_mutex()); | |
91 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
92 if (profiler_data == NULL) { | |
93 // Isolate has been shutdown for profiling. | |
94 RemoveIsolate(i); | |
95 // Remove moves the last element into i, do not increment i. | |
96 continue; | |
97 } | |
98 ASSERT(profiler_data != NULL); | |
99 if (profiler_data->ShouldSample(current_time)) { | |
100 pthread_kill(profiler_data->thread_id(), SIGPROF); | |
101 RemoveIsolate(i); | |
102 // Remove moves the last element into i, do not increment i. | |
103 continue; | |
104 } | |
105 if (profiler_data->CanExpire()) { | |
106 int64_t isolate_time_left = | |
107 profiler_data->TimeUntilExpiration(current_time); | |
108 if (isolate_time_left < 0) { | |
109 continue; | |
110 } | |
111 if (isolate_time_left < lowest) { | |
112 lowest = isolate_time_left; | |
113 } | |
114 } | |
115 i++; | |
116 } | |
117 if (isolates_size_ == 0) { | |
118 return 0; | |
119 } | |
120 if (lowest == max_time) { | |
121 return 0; | |
122 } | |
123 ASSERT(lowest != max_time); | |
124 ASSERT(lowest > 0); | |
125 return lowest; | |
126 } | |
127 | |
128 | |
129 void ProfilerManager::ThreadMain(uword parameters) { | |
130 ASSERT(initialized_); | |
131 ASSERT(FLAG_profile); | |
132 SignalHandler::Install(ProfileSignalAction); | |
133 if (FLAG_trace_profiled_isolates) { | |
134 OS::Print("ProfilerManager MacOS ready.\n"); | |
135 } | |
136 { | |
137 // Signal to main thread we are ready. | |
138 ScopedMonitor startup_lock(start_stop_monitor_); | |
139 thread_running_ = true; | |
140 startup_lock.Notify(); | |
141 } | |
142 ScopedMonitor lock(monitor_); | |
143 while (!shutdown_) { | |
144 int64_t current_time = OS::GetCurrentTimeMicros(); | |
145 int64_t next_sample = SampleAndRescheduleIsolates(current_time); | |
146 lock.WaitMicros(next_sample); | |
147 } | |
148 if (FLAG_trace_profiled_isolates) { | |
149 OS::Print("ProfilerManager MacOS exiting.\n"); | |
150 } | |
151 { | |
152 // Signal to main thread we are exiting. | |
153 ScopedMonitor shutdown_lock(start_stop_monitor_); | |
154 thread_running_ = false; | |
155 shutdown_lock.Notify(); | |
156 } | |
157 } | |
158 | |
159 | |
160 } // namespace dart | |
161 | |
162 #endif // defined(TARGET_OS_MACOS) | |
OLD | NEW |