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/profiler.h" | |
9 #include "vm/signal_handler.h" | |
10 | |
11 namespace dart { | |
12 | |
13 DECLARE_FLAG(bool, profile); | |
14 | |
15 static void CollectSample(IsolateProfilerData* profiler_data, | |
16 uintptr_t pc, | |
17 uintptr_t fp, | |
18 uintptr_t sp, | |
19 uintptr_t stack_lower, | |
20 uintptr_t stack_upper) { | |
21 SampleBuffer* sample_buffer = profiler_data->sample_buffer(); | |
22 Sample* sample = sample_buffer->ReserveSample(); | |
23 ASSERT(sample != NULL); | |
24 sample->timestamp = OS::GetCurrentTimeMicros(); | |
25 // TODO(johnmccutchan): Make real use of vm_tags and runtime_tags. | |
26 // Issue # 14777 | |
27 sample->vm_tags = Sample::kExecuting; | |
28 sample->runtime_tags = 0; | |
29 int64_t cpu_usage; | |
30 Thread::GetThreadCpuUsage(profiler_data->thread_id(), &cpu_usage); | |
31 sample->cpu_usage = profiler_data->set_and_delta_cpu_usage(cpu_usage); | |
32 ProfilerSampleStackWalker stackWalker(sample, stack_lower, stack_upper, | |
33 pc, fp, sp); | |
34 } | |
35 | |
36 | |
37 static void ProfileSignalAction(int signal, siginfo_t* info, void* context_) { | |
38 if (signal != SIGPROF) { | |
39 return; | |
40 } | |
41 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
42 mcontext_t mcontext = context->uc_mcontext; | |
43 Isolate* isolate = Isolate::Current(); | |
44 if (isolate == NULL) { | |
45 return; | |
46 } | |
47 { | |
48 ScopedMutexLock profiler_data_lock(isolate->profiler_data_mutex()); | |
49 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
50 if (profiler_data == NULL) { | |
51 return; | |
52 } | |
53 | |
54 uintptr_t stack_lower; | |
55 uintptr_t stack_upper; | |
56 bool r = isolate->GetStackBounds(&stack_lower, &stack_upper); | |
57 ASSERT(r); | |
58 uintptr_t PC = SignalHandler::GetProgramCounter(mcontext); | |
59 uintptr_t FP = SignalHandler::GetFramePointer(mcontext); | |
60 uintptr_t SP = SignalHandler::GetStackPointer(mcontext); | |
61 | |
62 stack_lower = SignalHandler::GetStackPointer(mcontext); | |
63 int64_t sample_time = OS::GetCurrentTimeMicros(); | |
64 profiler_data->SampledAt(sample_time); | |
65 CollectSample(profiler_data, PC, FP, SP, stack_lower, stack_upper); | |
66 } | |
67 ProfilerManager::ScheduleIsolate(isolate); | |
68 } | |
69 | |
70 | |
71 int64_t ProfilerManager::SampleAndRescheduleIsolates(int64_t current_time) { | |
72 if (isolates_size_ == 0) { | |
73 return 0; | |
74 } | |
75 static const int64_t max_time = 0x7fffffffffffffffLL; | |
76 int64_t lowest = max_time; | |
77 for (intptr_t i = 0; i < isolates_size_; i++) { | |
78 Isolate* isolate = isolates_[i]; | |
79 ScopedMutexLock isolate_lock(isolate->profiler_data_mutex()); | |
80 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
81 ASSERT(profiler_data != NULL); | |
82 if (profiler_data->ShouldSample(current_time)) { | |
83 pthread_kill(profiler_data->thread_id(), SIGPROF); | |
84 RemoveIsolate(i); | |
85 i--; // Remove moves the last element into i, this decrement cancels | |
86 // the increment in the for loop. | |
siva
2013/11/11 03:51:54
Ditto comment.
Cutch
2013/11/18 20:48:54
Done.
| |
87 continue; | |
88 } | |
89 if (profiler_data->CanExpire()) { | |
90 int64_t isolate_time_left = | |
91 profiler_data->TimeUntilExpiration(current_time); | |
92 if (isolate_time_left < 0) { | |
93 continue; | |
94 } | |
95 if (isolate_time_left < lowest) { | |
96 lowest = isolate_time_left; | |
97 } | |
98 } | |
99 } | |
100 if (isolates_size_ == 0) { | |
101 return 0; | |
102 } | |
103 if (lowest == max_time) { | |
104 return 0; | |
105 } | |
106 ASSERT(lowest != max_time); | |
107 ASSERT(lowest > 0); | |
108 return lowest; | |
109 } | |
110 | |
111 | |
112 void ProfilerManager::ThreadMain(uword parameters) { | |
113 ASSERT(initialized_); | |
114 ASSERT(FLAG_profile); | |
115 SignalHandler::Install(ProfileSignalAction); | |
116 ScopedMonitorLock lock(monitor_); | |
117 while (!shutdown_) { | |
118 int64_t current_time = OS::GetCurrentTimeMicros(); | |
119 int64_t next_sample = SampleAndRescheduleIsolates(current_time); | |
120 lock.WaitMicros(next_sample); | |
121 } | |
122 } | |
123 | |
124 | |
125 } // namespace dart | |
126 | |
127 #endif // defined(TARGET_OS_MACOS) | |
OLD | NEW |