OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_SAMPLER_H_ | 5 #ifndef V8_SAMPLER_H_ |
6 #define V8_SAMPLER_H_ | 6 #define V8_SAMPLER_H_ |
7 | 7 |
| 8 #include "include/v8.h" |
| 9 |
8 #include "src/base/atomicops.h" | 10 #include "src/base/atomicops.h" |
9 #include "src/frames.h" | 11 #include "src/frames.h" |
10 #include "src/globals.h" | 12 #include "src/globals.h" |
11 | 13 |
12 namespace v8 { | 14 namespace v8 { |
13 namespace internal { | 15 namespace internal { |
14 | 16 |
15 class Isolate; | 17 class Isolate; |
16 | 18 |
| 19 class ScopedSemaphore { |
| 20 public: |
| 21 explicit ScopedSemaphore(base::Semaphore* sem) {semaphore_ = sem;} |
| 22 ~ScopedSemaphore() {semaphore_->Signal();} |
| 23 private: |
| 24 base::Semaphore* semaphore_; |
| 25 }; |
| 26 |
| 27 |
17 // ---------------------------------------------------------------------------- | 28 // ---------------------------------------------------------------------------- |
18 // Sampler | 29 // Sampler |
19 // | 30 // |
20 // A sampler periodically samples the state of the VM and optionally | 31 // A sampler periodically samples the state of the VM and optionally |
21 // (if used for profiling) the program counter and stack pointer for | 32 // (if used for profiling) the program counter and stack pointer for |
22 // the thread that created it. | 33 // the thread that created it. |
23 | 34 |
24 struct RegisterState { | 35 struct RegisterState { |
25 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {} | 36 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {} |
26 Address pc; // Instruction pointer. | 37 Address pc; // Instruction pointer. |
27 Address sp; // Stack pointer. | 38 Address sp; // Stack pointer. |
28 Address fp; // Frame pointer. | 39 Address fp; // Frame pointer. |
29 }; | 40 }; |
30 | 41 |
31 // TickSample captures the information collected for each sample. | 42 // TickSample captures the information collected for each sample. |
32 struct TickSample { | 43 struct TickSample { |
33 TickSample() | 44 TickSample() |
34 : state(OTHER), | 45 : state(OTHER), |
35 pc(NULL), | 46 pc(NULL), |
36 external_callback(NULL), | 47 external_callback(NULL), |
37 frames_count(0), | |
38 has_external_callback(false), | 48 has_external_callback(false), |
39 top_frame_type(StackFrame::NONE) {} | 49 top_frame_type(StackFrame::NONE) {} |
40 void Init(Isolate* isolate, const RegisterState& state); | 50 void Init(Isolate* isolate, const RegisterState& state); |
41 StateTag state; // The state of the VM. | 51 enum { kMaxFramesCount = 255u }; |
42 Address pc; // Instruction pointer. | 52 |
| 53 void* stack[kMaxFramesCount]; // Call stack. |
| 54 size_t frames_count; // Number of captured frames. |
| 55 StateTag state; // The state of the VM. |
| 56 Address pc; // Instruction pointer. |
43 union { | 57 union { |
44 Address tos; // Top stack value (*sp). | 58 Address tos; // Top stack value (*sp). |
45 Address external_callback; | 59 Address external_callback; |
46 }; | 60 }; |
47 static const unsigned kMaxFramesCountLog2 = 8; | |
48 static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1; | |
49 Address stack[kMaxFramesCount]; // Call stack. | |
50 base::TimeTicks timestamp; | 61 base::TimeTicks timestamp; |
51 unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames. | |
52 bool has_external_callback : 1; | 62 bool has_external_callback : 1; |
53 StackFrame::Type top_frame_type : 4; | 63 StackFrame::Type top_frame_type : 4; |
54 }; | 64 }; |
55 | 65 |
56 class Sampler { | 66 class Sampler { |
57 public: | 67 public: |
58 // Initializes the Sampler support. Called once at VM startup. | 68 // Initializes the Sampler support. Called once at VM startup. |
59 static void SetUp(); | 69 static void SetUp(); |
60 static void TearDown(); | 70 static void TearDown(); |
61 | 71 |
(...skipping 16 matching lines...) Expand all Loading... |
78 return base::NoBarrier_Load(&profiling_) > 0 && | 88 return base::NoBarrier_Load(&profiling_) > 0 && |
79 !base::NoBarrier_Load(&has_processing_thread_); | 89 !base::NoBarrier_Load(&has_processing_thread_); |
80 } | 90 } |
81 void IncreaseProfilingDepth(); | 91 void IncreaseProfilingDepth(); |
82 void DecreaseProfilingDepth(); | 92 void DecreaseProfilingDepth(); |
83 | 93 |
84 // Whether the sampler is running (that is, consumes resources). | 94 // Whether the sampler is running (that is, consumes resources). |
85 bool IsActive() const { return base::NoBarrier_Load(&active_); } | 95 bool IsActive() const { return base::NoBarrier_Load(&active_); } |
86 | 96 |
87 void DoSample(); | 97 void DoSample(); |
| 98 void GetSample(v8::Sample* sample); |
88 // If true next sample must be initiated on the profiler event processor | 99 // If true next sample must be initiated on the profiler event processor |
89 // thread right after latest sample is processed. | 100 // thread right after latest sample is processed. |
90 void SetHasProcessingThread(bool value) { | 101 void SetHasProcessingThread(bool value) { |
91 base::NoBarrier_Store(&has_processing_thread_, value); | 102 base::NoBarrier_Store(&has_processing_thread_, value); |
92 } | 103 } |
93 | 104 |
94 // Used in tests to make sure that stack sampling is performed. | 105 // Used in tests to make sure that stack sampling is performed. |
95 unsigned js_and_external_sample_count() const { | 106 unsigned js_and_external_sample_count() const { |
96 return js_and_external_sample_count_; | 107 return js_and_external_sample_count_; |
97 } | 108 } |
(...skipping 15 matching lines...) Expand all Loading... |
113 | 124 |
114 Isolate* isolate_; | 125 Isolate* isolate_; |
115 const int interval_; | 126 const int interval_; |
116 base::Atomic32 profiling_; | 127 base::Atomic32 profiling_; |
117 base::Atomic32 has_processing_thread_; | 128 base::Atomic32 has_processing_thread_; |
118 base::Atomic32 active_; | 129 base::Atomic32 active_; |
119 PlatformData* data_; // Platform specific data. | 130 PlatformData* data_; // Platform specific data. |
120 bool is_counting_samples_; | 131 bool is_counting_samples_; |
121 // Counts stack samples taken in JS VM state. | 132 // Counts stack samples taken in JS VM state. |
122 unsigned js_and_external_sample_count_; | 133 unsigned js_and_external_sample_count_; |
| 134 void* GetSampleHelper(); |
123 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); | 135 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
124 }; | 136 }; |
125 | 137 |
126 | 138 |
127 } } // namespace v8::internal | 139 } } // namespace v8::internal |
128 | 140 |
129 #endif // V8_SAMPLER_H_ | 141 #endif // V8_SAMPLER_H_ |
OLD | NEW |