Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: src/libsampler/v8-sampler.h

Issue 1922303002: Create libsampler as V8 sampler library. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2016 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_PROFILER_SAMPLER_H_ 5 #ifndef V8_LIBSAMPLER_SAMPLER_H_
6 #define V8_PROFILER_SAMPLER_H_ 6 #define V8_LIBSAMPLER_SAMPLER_H_
7 7
8 #include "include/v8.h" 8 #include "include/v8.h"
9 9
10 #include "src/base/atomicops.h" 10 #include "src/base/atomicops.h"
11 #include "src/base/platform/time.h" 11 #include "src/base/macros.h"
12 #include "src/frames.h"
13 #include "src/globals.h"
14 12
15 namespace v8 { 13 namespace v8 {
16 namespace internal {
17
18 class Isolate;
19 14
20 // ---------------------------------------------------------------------------- 15 // ----------------------------------------------------------------------------
21 // Sampler 16 // Sampler
22 // 17 //
23 // A sampler periodically samples the state of the VM and optionally 18 // A sampler periodically samples the state of the VM and optionally
24 // (if used for profiling) the program counter and stack pointer for 19 // (if used for profiling) the program counter and stack pointer for
25 // the thread that created it. 20 // the thread that created it.
26 21
27 // TickSample captures the information collected for each sample.
28 struct 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),
37 external_callback_entry(NULL),
38 frames_count(0),
39 has_external_callback(false),
40 update_stats(true),
41 top_frame_type(StackFrame::NONE) {}
42 void Init(Isolate* isolate, const v8::RegisterState& state,
43 RecordCEntryFrame record_c_entry_frame, bool update_stats);
44 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).
52 Address external_callback_entry;
53 };
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;
60 bool update_stats : 1; // Whether the sample should update aggregated stats.
61 StackFrame::Type top_frame_type : 5;
62 };
63
64 class Sampler { 22 class Sampler {
65 public: 23 public:
66 // Initializes the Sampler support. Called once at VM startup. 24 // Initializes the Sampler support. Called once at VM startup.
67 static void SetUp(); 25 static void SetUp();
68 static void TearDown(); 26 static void TearDown();
69 27
70 // Initialize sampler. 28 // Initialize sampler.
71 Sampler(Isolate* isolate, int interval); 29 explicit Sampler(Isolate* isolate);
72 virtual ~Sampler(); 30 virtual ~Sampler();
73 31
74 Isolate* isolate() const { return isolate_; } 32 Isolate* isolate() const { return isolate_; }
75 int interval() const { return interval_; }
76 33
77 // Performs stack sampling. 34 // Performs stack sampling.
78 void SampleStack(const v8::RegisterState& regs); 35 // Everyone else should override this method in order to queue samples,
fmeawad 2016/05/02 23:14:37 "// All Sampler implementations should override th
alph 2016/05/03 18:48:07 Can you then make the method pure virtual and crea
lpy 2016/05/04 00:09:24 Done.
36 // the default method is for testing functionality only.
37 virtual void SampleStack(const v8::RegisterState& regs);
79 38
80 // Start and stop sampler. 39 // Start and stop sampler.
81 void Start(); 40 void Start();
82 void Stop(); 41 void Stop();
83 42
84 // Whether the sampling thread should use this Sampler for CPU profiling? 43 // Whether the sampling thread should use this Sampler for CPU profiling?
85 bool IsProfiling() const { 44 bool IsProfiling() const {
86 return base::NoBarrier_Load(&profiling_) > 0 && 45 return base::NoBarrier_Load(&profiling_) > 0;
87 !base::NoBarrier_Load(&has_processing_thread_);
88 } 46 }
89 void IncreaseProfilingDepth(); 47 void IncreaseProfilingDepth();
90 void DecreaseProfilingDepth(); 48 void DecreaseProfilingDepth();
91 49
92 // Whether the sampler is running (that is, consumes resources). 50 // Whether the sampler is running (that is, consumes resources).
93 bool IsActive() const { return base::NoBarrier_Load(&active_); } 51 bool IsActive() const { return base::NoBarrier_Load(&active_); }
94 52
95 // CpuProfiler collects samples by calling DoSample directly
96 // without calling Start. To keep it working, we register the sampler
97 // with the CpuProfiler.
98 bool IsRegistered() const { return base::NoBarrier_Load(&registered_); }
99
100 void DoSample(); 53 void DoSample();
101 // If true next sample must be initiated on the profiler event processor
102 // thread right after latest sample is processed.
103 void SetHasProcessingThread(bool value) {
104 base::NoBarrier_Store(&has_processing_thread_, value);
105 }
106 54
107 // Used in tests to make sure that stack sampling is performed. 55 // Used in tests to make sure that stack sampling is performed.
108 unsigned js_sample_count() const { return js_sample_count_; } 56 unsigned js_sample_count() const { return js_sample_count_; }
109 unsigned external_sample_count() const { return external_sample_count_; } 57 unsigned external_sample_count() const { return external_sample_count_; }
110 void StartCountingSamples() { 58 void StartCountingSamples() {
111 js_sample_count_ = 0; 59 js_sample_count_ = 0;
112 external_sample_count_ = 0; 60 external_sample_count_ = 0;
113 is_counting_samples_ = true; 61 is_counting_samples_ = true;
114 } 62 }
115 63
116 class PlatformData; 64 class PlatformData;
117 PlatformData* platform_data() const { return data_; } 65 PlatformData* platform_data() const { return data_; }
118 66
119 protected:
120 // This method is called for each sampling period with the current
121 // program counter.
122 virtual void Tick(TickSample* sample) = 0;
123
124 private: 67 private:
125 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); } 68 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); }
126 69
127 void SetRegistered(bool value) { base::NoBarrier_Store(&registered_, value); }
128
129 Isolate* isolate_; 70 Isolate* isolate_;
130 const int interval_;
131 base::Atomic32 profiling_; 71 base::Atomic32 profiling_;
132 base::Atomic32 has_processing_thread_;
133 base::Atomic32 active_; 72 base::Atomic32 active_;
134 base::Atomic32 registered_;
135 PlatformData* data_; // Platform specific data. 73 PlatformData* data_; // Platform specific data.
136 // Counts stack samples taken in various VM states. 74 // Counts stack samples taken in various VM states.
137 bool is_counting_samples_; 75 bool is_counting_samples_;
138 unsigned js_sample_count_; 76 unsigned js_sample_count_;
139 unsigned external_sample_count_; 77 unsigned external_sample_count_;
140 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); 78 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
141 }; 79 };
142 80
143
144 #if defined(USE_SIMULATOR)
145 class SimulatorHelper : AllStatic {
146 public:
147 // Returns true if register values were successfully retrieved
148 // from the simulator, otherwise returns false.
149 static bool FillRegisters(Isolate* isolate, v8::RegisterState* state);
150 };
151 #endif // USE_SIMULATOR
152
153
154 } // namespace internal
155 } // namespace v8 81 } // namespace v8
156 82
157 #endif // V8_PROFILER_SAMPLER_H_ 83 #endif // V8_LIBSAMPLER_SAMPLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698