OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_PROFILER_H_ | 5 #ifndef VM_PROFILER_H_ |
6 #define VM_PROFILER_H_ | 6 #define VM_PROFILER_H_ |
7 | 7 |
8 #include "platform/hashmap.h" | |
9 #include "platform/thread.h" | |
10 #include "vm/allocation.h" | 8 #include "vm/allocation.h" |
11 #include "vm/code_observers.h" | 9 #include "vm/code_observers.h" |
12 #include "vm/globals.h" | 10 #include "vm/globals.h" |
| 11 #include "vm/thread.h" |
| 12 #include "vm/thread_interrupter.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 | 15 |
16 // Forward declarations. | 16 // Forward declarations. |
| 17 class JSONArray; |
17 class JSONStream; | 18 class JSONStream; |
| 19 struct Sample; |
18 | 20 |
19 // Profiler manager. | 21 // Profiler |
20 class ProfilerManager : public AllStatic { | 22 class Profiler : public AllStatic { |
21 public: | 23 public: |
22 static void InitOnce(); | 24 static void InitOnce(); |
23 static void Shutdown(); | 25 static void Shutdown(); |
24 | 26 |
25 static void SetupIsolateForProfiling(Isolate* isolate); | 27 static void InitProfilingForIsolate(Isolate* isolate, |
26 static void ShutdownIsolateForProfiling(Isolate* isolate); | 28 bool shared_buffer = false); |
27 static void ScheduleIsolate(Isolate* isolate, bool inside_signal = false); | 29 static void ShutdownProfilingForIsolate(Isolate* isolate); |
28 static void DescheduleIsolate(Isolate* isolate); | 30 |
| 31 static void BeginExecution(Isolate* isolate); |
| 32 static void EndExecution(Isolate* isolate); |
29 | 33 |
30 static void PrintToJSONStream(Isolate* isolate, JSONStream* stream); | 34 static void PrintToJSONStream(Isolate* isolate, JSONStream* stream); |
31 | 35 |
32 static void WriteTracing(Isolate* isolate, const char* name, Dart_Port port); | 36 static void WriteTracing(Isolate* isolate); |
| 37 |
| 38 static SampleBuffer* sample_buffer() { |
| 39 return sample_buffer_; |
| 40 } |
33 | 41 |
34 private: | 42 private: |
35 static const intptr_t kMaxProfiledIsolates = 4096; | |
36 static bool initialized_; | 43 static bool initialized_; |
37 static bool shutdown_; | |
38 static bool thread_running_; | |
39 static Monitor* monitor_; | 44 static Monitor* monitor_; |
40 static Monitor* start_stop_monitor_; | |
41 | 45 |
42 static Isolate** isolates_; | 46 static void WriteTracingSample(Isolate* isolate, intptr_t pid, |
43 static intptr_t isolates_capacity_; | 47 Sample* sample, JSONArray& events); |
44 static intptr_t isolates_size_; | |
45 | 48 |
46 static void ScheduleIsolateHelper(Isolate* isolate); | 49 static void RecordTickInterruptCallback(const InterruptedThreadState& state, |
47 static void ResizeIsolates(intptr_t new_capacity); | 50 void* data); |
48 static void AddIsolate(Isolate* isolate); | |
49 static intptr_t FindIsolate(Isolate* isolate); | |
50 static void RemoveIsolate(intptr_t i); | |
51 | 51 |
52 // Returns the microseconds until the next live timer fires. | 52 static void RecordSampleInterruptCallback(const InterruptedThreadState& state, |
53 static int64_t SampleAndRescheduleIsolates(int64_t current_time); | 53 void* data); |
54 static void FreeIsolateProfilingData(Isolate* isolate); | 54 |
55 static void ThreadMain(uword parameters); | 55 static SampleBuffer* sample_buffer_; |
56 }; | 56 }; |
57 | 57 |
58 | 58 |
59 class IsolateProfilerData { | 59 class IsolateProfilerData { |
60 public: | 60 public: |
61 static const int64_t kDescheduledCpuUsage = -1; | 61 IsolateProfilerData(SampleBuffer* sample_buffer, bool own_sample_buffer); |
62 static const int64_t kNoExpirationTime = -2; | |
63 | |
64 IsolateProfilerData(Isolate* isolate, SampleBuffer* sample_buffer); | |
65 ~IsolateProfilerData(); | 62 ~IsolateProfilerData(); |
66 | 63 |
67 int64_t sample_interval_micros() const { return sample_interval_micros_; } | |
68 | |
69 void set_sample_interval_micros(int64_t sample_interval) { | |
70 sample_interval_micros_ = sample_interval; | |
71 } | |
72 | |
73 bool CanExpire() const { | |
74 return timer_expiration_micros_ != kNoExpirationTime; | |
75 } | |
76 | |
77 bool ShouldSample(int64_t current_time) const { | |
78 return CanExpire() && TimeUntilExpiration(current_time) <= 0; | |
79 } | |
80 | |
81 int64_t TimeUntilExpiration(int64_t current_time_micros) const { | |
82 ASSERT(CanExpire()); | |
83 return timer_expiration_micros_ - current_time_micros; | |
84 } | |
85 | |
86 void set_cpu_usage(int64_t cpu_usage) { | |
87 cpu_usage_ = cpu_usage; | |
88 } | |
89 | |
90 void SampledAt(int64_t current_time); | |
91 | |
92 void Scheduled(int64_t current_time, ThreadId thread); | |
93 | |
94 void Descheduled(); | |
95 | |
96 int64_t cpu_usage() const { return cpu_usage_; } | |
97 | |
98 int64_t ComputeDeltaAndSetCpuUsage(int64_t cpu_usage) { | |
99 int64_t delta = 0; | |
100 if (cpu_usage_ != kDescheduledCpuUsage) { | |
101 // Only compute the real delta if we are being sampled regularly. | |
102 delta = cpu_usage - cpu_usage_; | |
103 } | |
104 set_cpu_usage(cpu_usage); | |
105 return delta; | |
106 } | |
107 | |
108 ThreadId thread_id() const { return thread_id_; } | |
109 | |
110 Isolate* isolate() const { return isolate_; } | |
111 | |
112 SampleBuffer* sample_buffer() const { return sample_buffer_; } | 64 SampleBuffer* sample_buffer() const { return sample_buffer_; } |
113 | 65 |
114 void set_sample_buffer(SampleBuffer* sample_buffer) { | 66 void set_sample_buffer(SampleBuffer* sample_buffer) { |
115 sample_buffer_ = sample_buffer; | 67 sample_buffer_ = sample_buffer; |
116 } | 68 } |
117 | 69 |
118 private: | 70 private: |
119 int64_t last_sampled_micros_; | |
120 int64_t timer_expiration_micros_; | |
121 int64_t sample_interval_micros_; | |
122 int64_t cpu_usage_; | |
123 ThreadId thread_id_; | |
124 Isolate* isolate_; | |
125 SampleBuffer* sample_buffer_; | 71 SampleBuffer* sample_buffer_; |
| 72 bool own_sample_buffer_; |
126 DISALLOW_COPY_AND_ASSIGN(IsolateProfilerData); | 73 DISALLOW_COPY_AND_ASSIGN(IsolateProfilerData); |
127 }; | 74 }; |
128 | 75 |
129 | 76 |
130 // Profile sample. | 77 // Profile sample. |
131 struct Sample { | 78 struct Sample { |
132 static const char* kLookupSymbol; | 79 static const char* kLookupSymbol; |
133 static const char* kNoSymbol; | 80 static const char* kNoSymbol; |
134 static const intptr_t kNumStackFrames = 4; | 81 static const char* kNoFrame; |
135 enum SampleState { | 82 static const intptr_t kNumStackFrames = 6; |
136 kIdle = 0, | 83 enum SampleType { |
137 kExecuting = 1, | 84 kIsolateStart, |
138 kNumSampleStates | 85 kIsolateStop, |
| 86 kIsolateSample, |
139 }; | 87 }; |
140 int64_t timestamp; | 88 int64_t timestamp; |
141 int64_t cpu_usage; | 89 ThreadId tid; |
| 90 Isolate* isolate; |
142 uintptr_t pcs[kNumStackFrames]; | 91 uintptr_t pcs[kNumStackFrames]; |
| 92 SampleType type; |
143 uint16_t vm_tags; | 93 uint16_t vm_tags; |
144 uint16_t runtime_tags; | 94 uint16_t runtime_tags; |
145 Sample(); | 95 |
| 96 void Init(SampleType type, Isolate* isolate, int64_t timestamp, ThreadId tid); |
146 }; | 97 }; |
147 | 98 |
148 | 99 |
149 // Ring buffer of samples. One per isolate. | 100 // Ring buffer of samples. |
150 class SampleBuffer { | 101 class SampleBuffer { |
151 public: | 102 public: |
152 static const intptr_t kDefaultBufferCapacity = 120000; // 2 minutes @ 1000hz. | 103 static const intptr_t kDefaultBufferCapacity = 120000; // 2 minutes @ 1000hz. |
153 | 104 |
154 explicit SampleBuffer(intptr_t capacity = kDefaultBufferCapacity); | 105 explicit SampleBuffer(intptr_t capacity = kDefaultBufferCapacity); |
155 ~SampleBuffer(); | 106 ~SampleBuffer(); |
156 | 107 |
157 intptr_t capacity() const { return capacity_; } | 108 intptr_t capacity() const { return capacity_; } |
158 | 109 |
159 Sample* ReserveSample(); | 110 Sample* ReserveSample(); |
160 | 111 |
161 Sample* FirstSample() const; | 112 Sample* GetSample(intptr_t i) const { |
162 Sample* NextSample(Sample* sample) const; | 113 ASSERT(i >= 0); |
163 Sample* LastSample() const; | 114 ASSERT(i < capacity_); |
| 115 return &samples_[i]; |
| 116 } |
| 117 |
164 private: | 118 private: |
165 Sample* samples_; | 119 Sample* samples_; |
166 intptr_t capacity_; | 120 intptr_t capacity_; |
167 intptr_t start_; | 121 uintptr_t cursor_; |
168 intptr_t end_; | |
169 | 122 |
170 intptr_t WrapIncrement(intptr_t i) const; | |
171 DISALLOW_COPY_AND_ASSIGN(SampleBuffer); | 123 DISALLOW_COPY_AND_ASSIGN(SampleBuffer); |
172 }; | 124 }; |
173 | 125 |
174 | 126 |
175 class ProfilerSampleStackWalker : public ValueObject { | 127 class ProfilerSampleStackWalker : public ValueObject { |
176 public: | 128 public: |
177 ProfilerSampleStackWalker(Sample* sample, | 129 ProfilerSampleStackWalker(Sample* sample, |
178 uintptr_t stack_lower, | 130 uintptr_t stack_lower, |
179 uintptr_t stack_upper, | 131 uintptr_t stack_upper, |
180 uintptr_t pc, | 132 uintptr_t pc, |
(...skipping 16 matching lines...) Expand all Loading... |
197 const uintptr_t original_pc_; | 149 const uintptr_t original_pc_; |
198 const uintptr_t original_fp_; | 150 const uintptr_t original_fp_; |
199 const uintptr_t original_sp_; | 151 const uintptr_t original_sp_; |
200 uintptr_t lower_bound_; | 152 uintptr_t lower_bound_; |
201 }; | 153 }; |
202 | 154 |
203 | 155 |
204 } // namespace dart | 156 } // namespace dart |
205 | 157 |
206 #endif // VM_PROFILER_H_ | 158 #endif // VM_PROFILER_H_ |
OLD | NEW |