Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: runtime/vm/profiler_android.cc

Issue 25909002: Sampling profiler (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/profiler.cc ('k') | runtime/vm/profiler_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
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 }
28
29
30 static void ProfileSignalAction(int signal, siginfo_t* info, void* context_) {
31 if (signal != SIGPROF) {
32 return;
33 }
34 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_);
35 mcontext_t mcontext = context->uc_mcontext;
36 Isolate* isolate = Isolate::Current();
37 if (isolate == NULL) {
38 return;
39 }
40 // Thread owns no profiler locks at this point.
41 {
42 // Thread owns isolate profiler data mutex.
43 ScopedMutex profiler_data_lock(isolate->profiler_data_mutex());
44 IsolateProfilerData* profiler_data = isolate->profiler_data();
45 if (profiler_data == NULL) {
46 return;
47 }
48 uintptr_t stack_lower = 0;
49 uintptr_t stack_upper = 0;
50 isolate->GetStackBounds(&stack_lower, &stack_upper);
51 uintptr_t PC = SignalHandler::GetProgramCounter(mcontext);
52 uintptr_t FP = SignalHandler::GetFramePointer(mcontext);
53 uintptr_t SP = SignalHandler::GetStackPointer(mcontext);
54 stack_lower = SignalHandler::GetStackPointer(mcontext);
55 int64_t sample_time = OS::GetCurrentTimeMicros();
56 profiler_data->SampledAt(sample_time);
57 CollectSample(profiler_data, PC, FP, SP, stack_lower, stack_upper);
58 }
59 // Thread owns no profiler locks at this point.
60 // This call will acquire both ProfilerManager::monitor and the
61 // isolate's profiler data mutex.
62 ProfilerManager::ScheduleIsolate(isolate);
63 }
64
65
66 int64_t ProfilerManager::SampleAndRescheduleIsolates(int64_t current_time) {
67 if (isolates_size_ == 0) {
68 return 0;
69 }
70 static const int64_t max_time = 0x7fffffffffffffffLL;
71 int64_t lowest = max_time;
72 intptr_t i = 0;
73 while (i < isolates_size_) {
74 Isolate* isolate = isolates_[i];
75 ScopedMutex isolate_lock(isolate->profiler_data_mutex());
76 IsolateProfilerData* profiler_data = isolate->profiler_data();
77 ASSERT(profiler_data != NULL);
78 if (profiler_data->ShouldSample(current_time)) {
79 pthread_kill(profiler_data->thread_id(), SIGPROF);
80 RemoveIsolate(i);
81 // Remove moves the last element into i, do not increment i.
82 continue;
83 }
84 if (profiler_data->CanExpire()) {
85 int64_t isolate_time_left =
86 profiler_data->TimeUntilExpiration(current_time);
87 if (isolate_time_left < 0) {
88 continue;
89 }
90 if (isolate_time_left < lowest) {
91 lowest = isolate_time_left;
92 }
93 }
94 i++;
95 }
96 if (isolates_size_ == 0) {
97 return 0;
98 }
99 if (lowest == max_time) {
100 return 0;
101 }
102 ASSERT(lowest != max_time);
103 ASSERT(lowest > 0);
104 return lowest;
105 }
106
107
108 void ProfilerManager::ThreadMain(uword parameters) {
109 ASSERT(initialized_);
110 ASSERT(FLAG_profile);
111 SignalHandler::Install(ProfileSignalAction);
112 ScopedMonitor lock(monitor_);
113 while (!shutdown_) {
114 int64_t current_time = OS::GetCurrentTimeMicros();
115 int64_t next_sample = SampleAndRescheduleIsolates(current_time);
116 lock.WaitMicros(next_sample);
117 }
118 }
119
120
121 } // namespace dart
122
123 #endif // defined(TARGET_OS_ANDROID)
OLDNEW
« no previous file with comments | « runtime/vm/profiler.cc ('k') | runtime/vm/profiler_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698