v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sampler.h
Go to the documentation of this file.
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_SAMPLER_H_
29 #define V8_SAMPLER_H_
30 
31 #include "atomicops.h"
32 #include "frames.h"
33 #include "v8globals.h"
34 
35 namespace v8 {
36 namespace internal {
37 
38 class Isolate;
39 
40 // ----------------------------------------------------------------------------
41 // Sampler
42 //
43 // A sampler periodically samples the state of the VM and optionally
44 // (if used for profiling) the program counter and stack pointer for
45 // the thread that created it.
46 
47 struct RegisterState {
49  Address pc; // Instruction pointer.
50  Address sp; // Stack pointer.
51  Address fp; // Frame pointer.
52 };
53 
54 // TickSample captures the information collected for each sample.
55 struct TickSample {
57  : state(OTHER),
58  pc(NULL),
60  frames_count(0),
63  void Init(Isolate* isolate, const RegisterState& state);
64  StateTag state; // The state of the VM.
65  Address pc; // Instruction pointer.
66  union {
67  Address tos; // Top stack value (*sp).
69  };
70  static const int kMaxFramesCount = 64;
71  Address stack[kMaxFramesCount]; // Call stack.
72  int frames_count : 8; // Number of captured frames.
75 };
76 
77 class Sampler {
78  public:
79  // Initializes the Sampler support. Called once at VM startup.
80  static void SetUp();
81  static void TearDown();
82 
83  // Initialize sampler.
85  virtual ~Sampler();
86 
87  Isolate* isolate() const { return isolate_; }
88  int interval() const { return interval_; }
89 
90  // Performs stack sampling.
91  void SampleStack(const RegisterState& regs);
92 
93  // Start and stop sampler.
94  void Start();
95  void Stop();
96 
97  // Whether the sampling thread should use this Sampler for CPU profiling?
98  bool IsProfiling() const {
99  return NoBarrier_Load(&profiling_) > 0 &&
100  !NoBarrier_Load(&has_processing_thread_);
101  }
102  void IncreaseProfilingDepth();
103  void DecreaseProfilingDepth();
104 
105  // Whether the sampler is running (that is, consumes resources).
106  bool IsActive() const { return NoBarrier_Load(&active_); }
107 
108  void DoSample();
109  // If true next sample must be initiated on the profiler event processor
110  // thread right after latest sample is processed.
111  void SetHasProcessingThread(bool value) {
112  NoBarrier_Store(&has_processing_thread_, value);
113  }
114 
115  // Used in tests to make sure that stack sampling is performed.
116  unsigned js_and_external_sample_count() const {
117  return js_and_external_sample_count_;
118  }
120  is_counting_samples_ = true;
121  js_and_external_sample_count_ = 0;
122  }
123 
124  class PlatformData;
125  PlatformData* platform_data() const { return data_; }
126 
127  protected:
128  // This method is called for each sampling period with the current
129  // program counter.
130  virtual void Tick(TickSample* sample) = 0;
131 
132  private:
133  void SetActive(bool value) { NoBarrier_Store(&active_, value); }
134 
135  Isolate* isolate_;
136  const int interval_;
137  Atomic32 profiling_;
138  Atomic32 has_processing_thread_;
139  Atomic32 active_;
140  PlatformData* data_; // Platform specific data.
141  bool is_counting_samples_;
142  // Counts stack samples taken in JS VM state.
143  unsigned js_and_external_sample_count_;
145 };
146 
147 
148 } } // namespace v8::internal
149 
150 #endif // V8_SAMPLER_H_
byte * Address
Definition: globals.h:186
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
unsigned js_and_external_sample_count() const
Definition: sampler.h:116
static const int kMaxFramesCount
Definition: sampler.h:70
bool IsActive() const
Definition: sampler.h:106
TickSample * sample
Address external_callback
Definition: sampler.h:68
void DecreaseProfilingDepth()
Definition: sampler.cc:686
TypeImpl< ZoneTypeConfig > Type
int interval() const
Definition: sampler.h:88
void NoBarrier_Store(volatile Atomic32 *ptr, Atomic32 value)
HANDLE HANDLE LPSTACKFRAME64 StackFrame
virtual ~Sampler()
Definition: sampler.cc:658
void IncreaseProfilingDepth()
Definition: sampler.cc:678
bool IsProfiling() const
Definition: sampler.h:98
void StartCountingSamples()
Definition: sampler.h:119
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
Definition: globals.h:370
StackFrame::Type top_frame_type
Definition: sampler.h:74
Address stack[kMaxFramesCount]
Definition: sampler.h:71
static void TearDown()
Definition: sampler.cc:638
void SampleStack(const RegisterState &regs)
Definition: sampler.cc:694
Atomic32 NoBarrier_Load(volatile const Atomic32 *ptr)
void SetHasProcessingThread(bool value)
Definition: sampler.h:111
static void SetUp()
Definition: sampler.cc:630
void Init(Isolate *isolate, const RegisterState &state)
Definition: sampler.cc:589
int32_t Atomic32
Definition: atomicops.h:66
Sampler(Isolate *isolate, int interval)
Definition: sampler.cc:646
virtual void Tick(TickSample *sample)=0
PlatformData * platform_data() const
Definition: sampler.h:125
Isolate * isolate() const
Definition: sampler.h:87