blob: 8e8ef1cfc3e99dda279d165b8905223d5d189a65 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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.
4
5#ifndef V8_PROFILER_SAMPLER_H_
6#define V8_PROFILER_SAMPLER_H_
7
8#include "include/v8.h"
9
10#include "src/base/atomicops.h"
11#include "src/base/platform/time.h"
12#include "src/frames.h"
13#include "src/globals.h"
14
15namespace v8 {
16namespace internal {
17
18class Isolate;
19
20// ----------------------------------------------------------------------------
21// Sampler
22//
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.
26
27// TickSample captures the information collected for each sample.
28struct TickSample {
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 };
33
34 TickSample()
35 : state(OTHER),
36 pc(NULL),
Ben Murdoch097c5b22016-05-18 11:27:45 +010037 external_callback_entry(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 frames_count(0),
39 has_external_callback(false),
Ben Murdoch097c5b22016-05-18 11:27:45 +010040 update_stats(true),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 top_frame_type(StackFrame::NONE) {}
42 void Init(Isolate* isolate, const v8::RegisterState& state,
Ben Murdoch097c5b22016-05-18 11:27:45 +010043 RecordCEntryFrame record_c_entry_frame, bool update_stats);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 static void GetStackSample(Isolate* isolate, const v8::RegisterState& state,
45 RecordCEntryFrame record_c_entry_frame,
46 void** frames, size_t frames_limit,
47 v8::SampleInfo* sample_info);
48 StateTag state; // The state of the VM.
49 Address pc; // Instruction pointer.
50 union {
51 Address tos; // Top stack value (*sp).
Ben Murdoch097c5b22016-05-18 11:27:45 +010052 Address external_callback_entry;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 };
54 static const unsigned kMaxFramesCountLog2 = 8;
55 static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
56 Address stack[kMaxFramesCount]; // Call stack.
57 base::TimeTicks timestamp;
58 unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames.
59 bool has_external_callback : 1;
Ben Murdoch097c5b22016-05-18 11:27:45 +010060 bool update_stats : 1; // Whether the sample should update aggregated stats.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 StackFrame::Type top_frame_type : 4;
62};
63
64class Sampler {
65 public:
66 // Initializes the Sampler support. Called once at VM startup.
67 static void SetUp();
68 static void TearDown();
69
70 // Initialize sampler.
71 Sampler(Isolate* isolate, int interval);
72 virtual ~Sampler();
73
74 Isolate* isolate() const { return isolate_; }
75 int interval() const { return interval_; }
76
77 // Performs stack sampling.
78 void SampleStack(const v8::RegisterState& regs);
79
80 // Start and stop sampler.
81 void Start();
82 void Stop();
83
84 // Whether the sampling thread should use this Sampler for CPU profiling?
85 bool IsProfiling() const {
86 return base::NoBarrier_Load(&profiling_) > 0 &&
87 !base::NoBarrier_Load(&has_processing_thread_);
88 }
89 void IncreaseProfilingDepth();
90 void DecreaseProfilingDepth();
91
92 // Whether the sampler is running (that is, consumes resources).
93 bool IsActive() const { return base::NoBarrier_Load(&active_); }
94
95 void DoSample();
96 // If true next sample must be initiated on the profiler event processor
97 // thread right after latest sample is processed.
98 void SetHasProcessingThread(bool value) {
99 base::NoBarrier_Store(&has_processing_thread_, value);
100 }
101
102 // Used in tests to make sure that stack sampling is performed.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100103 unsigned js_sample_count() const { return js_sample_count_; }
104 unsigned external_sample_count() const { return external_sample_count_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000105 void StartCountingSamples() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100106 js_sample_count_ = 0;
107 external_sample_count_ = 0;
108 is_counting_samples_ = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 }
110
111 class PlatformData;
112 PlatformData* platform_data() const { return data_; }
113
114 protected:
115 // This method is called for each sampling period with the current
116 // program counter.
117 virtual void Tick(TickSample* sample) = 0;
118
119 private:
120 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); }
121
122 Isolate* isolate_;
123 const int interval_;
124 base::Atomic32 profiling_;
125 base::Atomic32 has_processing_thread_;
126 base::Atomic32 active_;
127 PlatformData* data_; // Platform specific data.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100128 // Counts stack samples taken in various VM states.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 bool is_counting_samples_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100130 unsigned js_sample_count_;
131 unsigned external_sample_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
133};
134
135
136} // namespace internal
137} // namespace v8
138
139#endif // V8_PROFILER_SAMPLER_H_