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_ANDROID) | |
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 ProfileSignalAction(int signal, siginfo_t* info, void* context_) { | |
19 if (signal != SIGPROF) { | |
20 return; | |
21 } | |
22 Isolate* isolate = Isolate::Current(); | |
23 if (isolate == NULL) { | |
24 return; | |
25 } | |
26 // Thread owns no profiler locks at this point. | |
27 { | |
28 // Thread owns isolate profiler data mutex. | |
29 ScopedMutex profiler_data_lock(isolate->profiler_data_mutex()); | |
30 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
31 if ((profiler_data == NULL) || !profiler_data->CanExpire() || | |
32 (profiler_data->sample_buffer() == NULL)) { | |
33 // Descheduled. | |
34 return; | |
35 } | |
36 if (profiler_data->thread_id() == Thread::GetCurrentThreadId()) { | |
37 // Still scheduled on this thread. | |
38 // TODO(johnmccutchan): Perform sample on Android. | |
39 } | |
40 } | |
41 // Thread owns no profiler locks at this point. | |
42 // This call will acquire both ProfilerManager::monitor and the | |
43 // isolate's profiler data mutex. | |
44 ProfilerManager::ScheduleIsolate(isolate, true); | |
45 } | |
46 | |
47 | |
48 int64_t ProfilerManager::SampleAndRescheduleIsolates(int64_t current_time) { | |
49 if (isolates_size_ == 0) { | |
50 return 0; | |
51 } | |
52 static const int64_t max_time = 0x7fffffffffffffffLL; | |
53 int64_t lowest = max_time; | |
54 intptr_t i = 0; | |
55 while (i < isolates_size_) { | |
56 Isolate* isolate = isolates_[i]; | |
57 ScopedMutex isolate_lock(isolate->profiler_data_mutex()); | |
58 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
59 if (profiler_data == NULL) { | |
60 // Isolate has been shutdown for profiling. | |
61 RemoveIsolate(i); | |
62 // Remove moves the last element into i, do not increment i. | |
63 continue; | |
64 } | |
65 ASSERT(profiler_data != NULL); | |
66 if (profiler_data->ShouldSample(current_time)) { | |
67 pthread_kill(profiler_data->thread_id(), SIGPROF); | |
68 RemoveIsolate(i); | |
69 // Remove moves the last element into i, do not increment i. | |
70 continue; | |
71 } | |
72 if (profiler_data->CanExpire()) { | |
73 int64_t isolate_time_left = | |
74 profiler_data->TimeUntilExpiration(current_time); | |
75 if (isolate_time_left < 0) { | |
76 continue; | |
77 } | |
78 if (isolate_time_left < lowest) { | |
79 lowest = isolate_time_left; | |
80 } | |
81 } | |
82 i++; | |
83 } | |
84 if (isolates_size_ == 0) { | |
85 return 0; | |
86 } | |
87 if (lowest == max_time) { | |
88 return 0; | |
89 } | |
90 ASSERT(lowest != max_time); | |
91 ASSERT(lowest > 0); | |
92 return lowest; | |
93 } | |
94 | |
95 | |
96 void ProfilerManager::ThreadMain(uword parameters) { | |
97 ASSERT(initialized_); | |
98 ASSERT(FLAG_profile); | |
99 SignalHandler::Install(ProfileSignalAction); | |
100 if (FLAG_trace_profiled_isolates) { | |
101 OS::Print("ProfilerManager Android ready.\n"); | |
102 } | |
103 { | |
104 // Signal to main thread we are ready. | |
105 ScopedMonitor startup_lock(start_stop_monitor_); | |
106 thread_running_ = true; | |
107 startup_lock.Notify(); | |
108 } | |
109 ScopedMonitor lock(monitor_); | |
110 while (!shutdown_) { | |
111 int64_t current_time = OS::GetCurrentTimeMicros(); | |
112 int64_t next_sample = SampleAndRescheduleIsolates(current_time); | |
113 lock.WaitMicros(next_sample); | |
114 } | |
115 if (FLAG_trace_profiled_isolates) { | |
116 OS::Print("ProfilerManager Android exiting.\n"); | |
117 } | |
118 { | |
119 // Signal to main thread we are exiting. | |
120 ScopedMonitor shutdown_lock(start_stop_monitor_); | |
121 thread_running_ = false; | |
122 shutdown_lock.Notify(); | |
123 } | |
124 } | |
125 | |
126 | |
127 } // namespace dart | |
128 | |
129 #endif // defined(TARGET_OS_ANDROID) | |
OLD | NEW |