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 #ifndef VM_PROFILER_H_ | |
6 #define VM_PROFILER_H_ | |
7 | |
8 #include "platform/hashmap.h" | |
9 #include "platform/thread.h" | |
10 #include "vm/allocation.h" | |
11 #include "vm/code_observers.h" | |
12 #include "vm/globals.h" | |
13 | |
14 namespace dart { | |
15 | |
16 class JSONStream; | |
siva
2013/11/11 03:51:54
Add comment:
// Forward declarations.
Cutch
2013/11/18 20:48:54
Done.
| |
17 | |
18 // Profiler manager. | |
19 class ProfilerManager : public AllStatic { | |
20 public: | |
21 static void InitOnce(); | |
22 static void Shutdown(); | |
23 | |
24 static void SetupIsolateForProfiling(Isolate* isolate); | |
25 static void ShutdownIsolateForProfiling(Isolate* isolate); | |
26 static void ScheduleIsolate(Isolate* isolate); | |
27 static void DescheduleIsolate(Isolate* isolate); | |
28 | |
29 static void PrintToJSONStream(Isolate* isolate, JSONStream* stream); | |
30 | |
31 static void WriteTracing(Isolate* isolate, const char* name, Dart_Port port); | |
32 | |
33 private: | |
34 static bool initialized_; | |
35 static bool shutdown_; | |
36 static Monitor* monitor_; | |
37 | |
38 static Isolate** isolates_; | |
39 static intptr_t isolates_capacity_; | |
40 static intptr_t isolates_size_; | |
41 | |
42 static void ResizeIsolates(intptr_t new_capacity); | |
43 static void AddIsolate(Isolate* isolate); | |
44 static intptr_t FindIsolate(Isolate* isolate); | |
45 static void RemoveIsolate(intptr_t i); | |
46 | |
47 // Returns the microseconds until the next live timer fires. | |
48 static int64_t SampleAndRescheduleIsolates(int64_t current_time); | |
49 static void FreeIsolateProfilingData(Isolate* isolate); | |
50 static void ThreadMain(uword parameters); | |
51 }; | |
52 | |
53 | |
54 class IsolateProfilerData { | |
55 public: | |
56 static const int64_t kDescheduledCpuUsage = -1; | |
57 static const int64_t kNoExpirationTime = -2; | |
58 | |
59 IsolateProfilerData(Isolate* isolate, SampleBuffer* sample_buffer); | |
60 ~IsolateProfilerData(); | |
61 | |
62 int64_t sample_interval() const { return sample_interval_micros_; } | |
siva
2013/11/11 03:51:54
normally we have been naming the setters and acces
Cutch
2013/11/18 20:48:54
Done.
| |
63 | |
64 void set_sample_interval(int64_t sample_interval) { | |
65 sample_interval_micros_ = sample_interval; | |
66 } | |
67 | |
68 bool CanExpire() { | |
siva
2013/11/11 03:51:54
const?
Cutch
2013/11/18 20:48:54
Done.
| |
69 return timer_expiration_micros_ != kNoExpirationTime; | |
70 } | |
71 | |
72 bool ShouldSample(int64_t current_time) { | |
siva
2013/11/11 03:51:54
const
Cutch
2013/11/18 20:48:54
Done.
| |
73 return CanExpire() && TimeUntilExpiration(current_time) <= 0; | |
74 } | |
75 | |
76 int64_t TimeUntilExpiration(int64_t current_time_micros) { | |
siva
2013/11/11 03:51:54
const, maybe there are others too in these new cla
Cutch
2013/11/18 20:48:54
Done.
| |
77 ASSERT(CanExpire()); | |
78 return timer_expiration_micros_ - current_time_micros; | |
79 } | |
80 | |
81 void set_cpu_usage(int64_t cpu_usage) { | |
82 cpu_usage_ = cpu_usage; | |
83 } | |
84 | |
85 void SampledAt(int64_t current_time); | |
86 | |
87 void Scheduled(int64_t current_time, ThreadId thread); | |
88 | |
89 void Descheduled(); | |
90 | |
91 int64_t cpu_usage() const { return cpu_usage_; } | |
92 | |
93 int64_t set_and_delta_cpu_usage(int64_t cpu_usage) { | |
siva
2013/11/11 03:51:54
ComputeDeltaAndSetCpuUsage(int64_t cpu_usage) ?
Cutch
2013/11/18 20:48:54
Done.
| |
94 int64_t delta = 0; | |
95 if (cpu_usage_ != kDescheduledCpuUsage) { | |
96 // Only compute the real delta if we are being sampled regularly. | |
97 delta = cpu_usage - cpu_usage_; | |
98 } | |
99 set_cpu_usage(cpu_usage); | |
100 return delta; | |
101 } | |
102 | |
103 ThreadId thread_id() const { return thread_id_; } | |
104 | |
105 Isolate* isolate() const { return isolate_; } | |
106 | |
107 SampleBuffer* sample_buffer() const { return sample_buffer_; } | |
108 | |
109 private: | |
110 int64_t last_sampled_micros_; | |
111 int64_t timer_expiration_micros_; | |
112 int64_t sample_interval_micros_; | |
113 int64_t cpu_usage_; | |
114 ThreadId thread_id_; | |
115 Isolate* isolate_; | |
116 SampleBuffer* sample_buffer_; | |
117 DISALLOW_COPY_AND_ASSIGN(IsolateProfilerData); | |
118 }; | |
119 | |
120 | |
121 // Profile sample. | |
122 struct Sample { | |
123 static const char* kLookupSymbol; | |
124 static const char* kNoSymbol; | |
125 static const intptr_t kNumStackFrames = 4; | |
126 enum SampleState { | |
127 kIdle = 0, | |
128 kExecuting = 1, | |
129 kNumSampleStates | |
130 }; | |
131 int64_t timestamp; | |
132 int64_t cpu_usage; | |
133 uintptr_t pcs[kNumStackFrames]; | |
134 uint16_t vm_tags; | |
135 uint16_t runtime_tags; | |
136 Sample(); | |
137 }; | |
138 | |
139 | |
140 // Ring buffer of samples. One per isolate. | |
141 class SampleBuffer { | |
142 public: | |
143 static const intptr_t kDefaultBufferCapacity = 1000000; | |
144 | |
145 explicit SampleBuffer(intptr_t capacity = kDefaultBufferCapacity); | |
146 ~SampleBuffer(); | |
147 | |
148 intptr_t capacity() const { return capacity_; } | |
149 | |
150 Sample* ReserveSample(); | |
151 | |
152 Sample* FirstSample() const; | |
153 Sample* NextSample(Sample* sample) const; | |
154 Sample* LastSample() const; | |
155 private: | |
156 Sample* samples_; | |
157 intptr_t capacity_; | |
158 intptr_t start_; | |
159 intptr_t end_; | |
160 | |
161 intptr_t WrapIncrement(intptr_t i) const; | |
162 DISALLOW_COPY_AND_ASSIGN(SampleBuffer); | |
163 }; | |
164 | |
165 | |
166 class ProfilerSampleStackWalker : public ValueObject { | |
167 public: | |
168 ProfilerSampleStackWalker(Sample* sample, | |
169 uintptr_t stack_lower, | |
170 uintptr_t stack_upper, | |
171 uintptr_t pc, | |
172 uintptr_t fp, | |
173 uintptr_t sp); | |
174 | |
175 int walk(); | |
176 | |
177 private: | |
178 uword* CallerPC(uword* fp); | |
179 uword* CallerFP(uword* fp); | |
180 | |
181 bool ValidInstructionPointer(uword* pc); | |
182 | |
183 bool ValidFramePointer(uword* fp); | |
184 | |
185 Sample* sample_; | |
186 const uintptr_t stack_lower_; | |
187 const uintptr_t stack_upper_; | |
188 const uintptr_t original_pc_; | |
189 const uintptr_t original_fp_; | |
190 const uintptr_t original_sp_; | |
191 }; | |
192 | |
193 | |
194 } // namespace dart | |
195 | |
196 #endif // VM_PROFILER_H_ | |
OLD | NEW |