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