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.
10 #include "v8globals.h"
17 // ----------------------------------------------------------------------------
20 // A sampler periodically samples the state of the VM and optionally
21 // (if used for profiling) the program counter and stack pointer for
22 // the thread that created it.
24 struct RegisterState {
25 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {}
26 Address pc; // Instruction pointer.
27 Address sp; // Stack pointer.
28 Address fp; // Frame pointer.
31 // TickSample captures the information collected for each sample.
36 external_callback(NULL),
38 has_external_callback(false),
39 top_frame_type(StackFrame::NONE) {}
40 void Init(Isolate* isolate, const RegisterState& state);
41 StateTag state; // The state of the VM.
42 Address pc; // Instruction pointer.
44 Address tos; // Top stack value (*sp).
45 Address external_callback;
47 static const int kMaxFramesCount = 64;
48 Address stack[kMaxFramesCount]; // Call stack.
50 int frames_count : 8; // Number of captured frames.
51 bool has_external_callback : 1;
52 StackFrame::Type top_frame_type : 4;
57 // Initializes the Sampler support. Called once at VM startup.
59 static void TearDown();
61 // Initialize sampler.
62 Sampler(Isolate* isolate, int interval);
65 Isolate* isolate() const { return isolate_; }
66 int interval() const { return interval_; }
68 // Performs stack sampling.
69 void SampleStack(const RegisterState& regs);
71 // Start and stop sampler.
75 // Whether the sampling thread should use this Sampler for CPU profiling?
76 bool IsProfiling() const {
77 return NoBarrier_Load(&profiling_) > 0 &&
78 !NoBarrier_Load(&has_processing_thread_);
80 void IncreaseProfilingDepth();
81 void DecreaseProfilingDepth();
83 // Whether the sampler is running (that is, consumes resources).
84 bool IsActive() const { return NoBarrier_Load(&active_); }
87 // If true next sample must be initiated on the profiler event processor
88 // thread right after latest sample is processed.
89 void SetHasProcessingThread(bool value) {
90 NoBarrier_Store(&has_processing_thread_, value);
93 // Used in tests to make sure that stack sampling is performed.
94 unsigned js_and_external_sample_count() const {
95 return js_and_external_sample_count_;
97 void StartCountingSamples() {
98 is_counting_samples_ = true;
99 js_and_external_sample_count_ = 0;
103 PlatformData* platform_data() const { return data_; }
106 // This method is called for each sampling period with the current
108 virtual void Tick(TickSample* sample) = 0;
111 void SetActive(bool value) { NoBarrier_Store(&active_, value); }
116 Atomic32 has_processing_thread_;
118 PlatformData* data_; // Platform specific data.
119 bool is_counting_samples_;
120 // Counts stack samples taken in JS VM state.
121 unsigned js_and_external_sample_count_;
122 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
126 } } // namespace v8::internal
128 #endif // V8_SAMPLER_H_