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
3 // found in the LICENSE file.
5 #ifndef V8_PROFILER_SAMPLER_H_
6 #define V8_PROFILER_SAMPLER_H_
8 #include "include/v8.h"
10 #include "src/base/atomicops.h"
11 #include "src/base/platform/time.h"
12 #include "src/frames.h"
13 #include "src/globals.h"
20 // ----------------------------------------------------------------------------
23 // A sampler periodically samples the state of the VM and optionally
24 // (if used for profiling) the program counter and stack pointer for
25 // the thread that created it.
27 // TickSample captures the information collected for each sample.
29 // Internal profiling (with --prof + tools/$OS-tick-processor) wants to
30 // include the runtime function we're calling. Externally exposed tick
31 // samples don't care.
32 enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame };
37 external_callback(NULL),
39 has_external_callback(false),
40 top_frame_type(StackFrame::NONE) {}
41 void Init(Isolate* isolate, const v8::RegisterState& state,
42 RecordCEntryFrame record_c_entry_frame);
43 static void GetStackSample(Isolate* isolate, const v8::RegisterState& state,
44 RecordCEntryFrame record_c_entry_frame,
45 void** frames, size_t frames_limit,
46 v8::SampleInfo* sample_info);
47 StateTag state; // The state of the VM.
48 Address pc; // Instruction pointer.
50 Address tos; // Top stack value (*sp).
51 Address external_callback;
53 static const unsigned kMaxFramesCountLog2 = 8;
54 static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
55 Address stack[kMaxFramesCount]; // Call stack.
56 base::TimeTicks timestamp;
57 unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames.
58 bool has_external_callback : 1;
59 StackFrame::Type top_frame_type : 4;
64 // Initializes the Sampler support. Called once at VM startup.
66 static void TearDown();
68 // Initialize sampler.
69 Sampler(Isolate* isolate, int interval);
72 Isolate* isolate() const { return isolate_; }
73 int interval() const { return interval_; }
75 // Performs stack sampling.
76 void SampleStack(const v8::RegisterState& regs);
78 // Start and stop sampler.
82 // Whether the sampling thread should use this Sampler for CPU profiling?
83 bool IsProfiling() const {
84 return base::NoBarrier_Load(&profiling_) > 0 &&
85 !base::NoBarrier_Load(&has_processing_thread_);
87 void IncreaseProfilingDepth();
88 void DecreaseProfilingDepth();
90 // Whether the sampler is running (that is, consumes resources).
91 bool IsActive() const { return base::NoBarrier_Load(&active_); }
94 // If true next sample must be initiated on the profiler event processor
95 // thread right after latest sample is processed.
96 void SetHasProcessingThread(bool value) {
97 base::NoBarrier_Store(&has_processing_thread_, value);
100 // Used in tests to make sure that stack sampling is performed.
101 unsigned js_and_external_sample_count() const {
102 return js_and_external_sample_count_;
104 void StartCountingSamples() {
105 is_counting_samples_ = true;
106 js_and_external_sample_count_ = 0;
110 PlatformData* platform_data() const { return data_; }
113 // This method is called for each sampling period with the current
115 virtual void Tick(TickSample* sample) = 0;
118 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); }
122 base::Atomic32 profiling_;
123 base::Atomic32 has_processing_thread_;
124 base::Atomic32 active_;
125 PlatformData* data_; // Platform specific data.
126 bool is_counting_samples_;
127 // Counts stack samples taken in JS VM state.
128 unsigned js_and_external_sample_count_;
129 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
133 } } // namespace v8::internal
135 #endif // V8_PROFILER_SAMPLER_H_