V8 Project
sampler.h
Go to the documentation of this file.
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.
4 
5 #ifndef V8_SAMPLER_H_
6 #define V8_SAMPLER_H_
7 
8 #include "src/base/atomicops.h"
9 #include "src/frames.h"
10 #include "src/globals.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 class Isolate;
16 
17 // ----------------------------------------------------------------------------
18 // Sampler
19 //
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.
23 
24 struct RegisterState {
26  Address pc; // Instruction pointer.
27  Address sp; // Stack pointer.
28  Address fp; // Frame pointer.
29 };
30 
31 // TickSample captures the information collected for each sample.
32 struct TickSample {
34  : state(OTHER),
35  pc(NULL),
37  frames_count(0),
40  void Init(Isolate* isolate, const RegisterState& state);
41  StateTag state; // The state of the VM.
42  Address pc; // Instruction pointer.
43  union {
44  Address tos; // Top stack value (*sp).
46  };
47  static const unsigned kMaxFramesCountLog2 = 8;
48  static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
49  Address stack[kMaxFramesCount]; // Call stack.
50  base::TimeTicks timestamp;
51  unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames.
54 };
55 
56 class Sampler {
57  public:
58  // Initializes the Sampler support. Called once at VM startup.
59  static void SetUp();
60  static void TearDown();
61 
62  // Initialize sampler.
64  virtual ~Sampler();
65 
66  Isolate* isolate() const { return isolate_; }
67  int interval() const { return interval_; }
68 
69  // Performs stack sampling.
70  void SampleStack(const RegisterState& regs);
71 
72  // Start and stop sampler.
73  void Start();
74  void Stop();
75 
76  // Whether the sampling thread should use this Sampler for CPU profiling?
77  bool IsProfiling() const {
78  return base::NoBarrier_Load(&profiling_) > 0 &&
80  }
83 
84  // Whether the sampler is running (that is, consumes resources).
85  bool IsActive() const { return base::NoBarrier_Load(&active_); }
86 
87  void DoSample();
88  // If true next sample must be initiated on the profiler event processor
89  // thread right after latest sample is processed.
90  void SetHasProcessingThread(bool value) {
92  }
93 
94  // Used in tests to make sure that stack sampling is performed.
95  unsigned js_and_external_sample_count() const {
97  }
99  is_counting_samples_ = true;
101  }
102 
103  class PlatformData;
104  PlatformData* platform_data() const { return data_; }
105 
106  protected:
107  // This method is called for each sampling period with the current
108  // program counter.
109  virtual void Tick(TickSample* sample) = 0;
110 
111  private:
112  void SetActive(bool value) { base::NoBarrier_Store(&active_, value); }
113 
115  const int interval_;
119  PlatformData* data_; // Platform specific data.
121  // Counts stack samples taken in JS VM state.
124 };
125 
126 
127 } } // namespace v8::internal
128 
129 #endif // V8_SAMPLER_H_
A single JavaScript stack frame.
Definition: v8.h:1358
void SetHasProcessingThread(bool value)
Definition: sampler.h:90
unsigned js_and_external_sample_count() const
Definition: sampler.h:95
PlatformData * platform_data() const
Definition: sampler.h:104
Sampler(Isolate *isolate, int interval)
Definition: sampler.cc:637
base::Atomic32 active_
Definition: sampler.h:118
PlatformData * data_
Definition: sampler.h:119
virtual void Tick(TickSample *sample)=0
int interval() const
Definition: sampler.h:67
void SetActive(bool value)
Definition: sampler.h:112
base::Atomic32 has_processing_thread_
Definition: sampler.h:117
bool IsActive() const
Definition: sampler.h:85
void DecreaseProfilingDepth()
Definition: sampler.cc:677
base::Atomic32 profiling_
Definition: sampler.h:116
bool IsProfiling() const
Definition: sampler.h:77
DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler)
void SampleStack(const RegisterState &regs)
Definition: sampler.cc:685
void IncreaseProfilingDepth()
Definition: sampler.cc:669
const int interval_
Definition: sampler.h:115
static void SetUp()
Definition: sampler.cc:621
Isolate * isolate() const
Definition: sampler.h:66
void StartCountingSamples()
Definition: sampler.h:98
static void TearDown()
Definition: sampler.cc:629
Isolate * isolate_
Definition: sampler.h:114
unsigned js_and_external_sample_count_
Definition: sampler.h:122
virtual ~Sampler()
Definition: sampler.cc:649
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
int32_t Atomic32
Definition: atomicops.h:44
void NoBarrier_Store(volatile Atomic8 *ptr, Atomic8 value)
Atomic8 NoBarrier_Load(volatile const Atomic8 *ptr)
TypeImpl< ZoneTypeConfig > Type
byte * Address
Definition: globals.h:101
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
@ NONE
Address external_callback
Definition: sampler.h:45
static const unsigned kMaxFramesCountLog2
Definition: sampler.h:47
StackFrame::Type top_frame_type
Definition: sampler.h:53
base::TimeTicks timestamp
Definition: sampler.h:50
Address stack[kMaxFramesCount]
Definition: sampler.h:49
void Init(Isolate *isolate, const RegisterState &state)
Definition: sampler.cc:579
static const unsigned kMaxFramesCount
Definition: sampler.h:48