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