sampler.h 4.12 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_SAMPLER_H_
#define V8_SAMPLER_H_

8 9
#include "include/v8.h"

10
#include "src/base/atomicops.h"
11 12
#include "src/frames.h"
#include "src/globals.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

namespace v8 {
namespace internal {

class Isolate;

// ----------------------------------------------------------------------------
// Sampler
//
// A sampler periodically samples the state of the VM and optionally
// (if used for profiling) the program counter and stack pointer for
// the thread that created it.

// TickSample captures the information collected for each sample.
struct TickSample {
28 29 30 31 32
  // Internal profiling (with --prof + tools/$OS-tick-processor) wants to
  // include the runtime function we're calling. Externally exposed tick
  // samples don't care.
  enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame };

33 34 35 36
  TickSample()
      : state(OTHER),
        pc(NULL),
        external_callback(NULL),
37
        frames_count(0),
38 39
        has_external_callback(false),
        top_frame_type(StackFrame::NONE) {}
40 41
  void Init(Isolate* isolate, const v8::RegisterState& state,
            RecordCEntryFrame record_c_entry_frame);
42
  static void GetStackSample(Isolate* isolate, const v8::RegisterState& state,
43
                             RecordCEntryFrame record_c_entry_frame,
44 45
                             void** frames, size_t frames_limit,
                             v8::SampleInfo* sample_info);
46 47
  StateTag state;  // The state of the VM.
  Address pc;      // Instruction pointer.
48 49 50 51
  union {
    Address tos;   // Top stack value (*sp).
    Address external_callback;
  };
52 53
  static const unsigned kMaxFramesCountLog2 = 8;
  static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
54
  Address stack[kMaxFramesCount];  // Call stack.
55
  base::TimeTicks timestamp;
56
  unsigned frames_count : kMaxFramesCountLog2;  // Number of captured frames.
57
  bool has_external_callback : 1;
58
  StackFrame::Type top_frame_type : 4;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
};

class Sampler {
 public:
  // Initializes the Sampler support. Called once at VM startup.
  static void SetUp();
  static void TearDown();

  // Initialize sampler.
  Sampler(Isolate* isolate, int interval);
  virtual ~Sampler();

  Isolate* isolate() const { return isolate_; }
  int interval() const { return interval_; }

  // Performs stack sampling.
75
  void SampleStack(const v8::RegisterState& regs);
76 77 78 79 80

  // Start and stop sampler.
  void Start();
  void Stop();

81 82
  // Whether the sampling thread should use this Sampler for CPU profiling?
  bool IsProfiling() const {
83 84
    return base::NoBarrier_Load(&profiling_) > 0 &&
        !base::NoBarrier_Load(&has_processing_thread_);
85
  }
86 87
  void IncreaseProfilingDepth();
  void DecreaseProfilingDepth();
88 89

  // Whether the sampler is running (that is, consumes resources).
90
  bool IsActive() const { return base::NoBarrier_Load(&active_); }
91

92
  void DoSample();
93 94 95
  // If true next sample must be initiated on the profiler event processor
  // thread right after latest sample is processed.
  void SetHasProcessingThread(bool value) {
96
    base::NoBarrier_Store(&has_processing_thread_, value);
97 98
  }

99
  // Used in tests to make sure that stack sampling is performed.
100 101 102 103 104 105 106
  unsigned js_and_external_sample_count() const {
    return js_and_external_sample_count_;
  }
  void StartCountingSamples() {
      is_counting_samples_ = true;
      js_and_external_sample_count_ = 0;
  }
107 108 109 110

  class PlatformData;
  PlatformData* platform_data() const { return data_; }

111 112 113 114 115
 protected:
  // This method is called for each sampling period with the current
  // program counter.
  virtual void Tick(TickSample* sample) = 0;

116
 private:
117
  void SetActive(bool value) { base::NoBarrier_Store(&active_, value); }
118 119 120

  Isolate* isolate_;
  const int interval_;
121 122 123
  base::Atomic32 profiling_;
  base::Atomic32 has_processing_thread_;
  base::Atomic32 active_;
124
  PlatformData* data_;  // Platform specific data.
125 126 127
  bool is_counting_samples_;
  // Counts stack samples taken in JS VM state.
  unsigned js_and_external_sample_count_;
128 129 130 131 132 133 134
  DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
};


} }  // namespace v8::internal

#endif  // V8_SAMPLER_H_