blob: 3d3a6e997ffb3b891a16093c53db74e7c01075ed [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"
Ben Murdochc5610432016-08-08 18:44:38 +010011#include "src/base/macros.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012
13namespace v8 {
14namespace internal {
15
16class Isolate;
Ben Murdochc5610432016-08-08 18:44:38 +010017struct TickSample;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018
19// ----------------------------------------------------------------------------
20// Sampler
21//
22// A sampler periodically samples the state of the VM and optionally
23// (if used for profiling) the program counter and stack pointer for
24// the thread that created it.
25
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026class Sampler {
27 public:
28 // Initializes the Sampler support. Called once at VM startup.
29 static void SetUp();
30 static void TearDown();
31
32 // Initialize sampler.
33 Sampler(Isolate* isolate, int interval);
34 virtual ~Sampler();
35
36 Isolate* isolate() const { return isolate_; }
37 int interval() const { return interval_; }
38
39 // Performs stack sampling.
40 void SampleStack(const v8::RegisterState& regs);
41
42 // Start and stop sampler.
43 void Start();
44 void Stop();
45
46 // Whether the sampling thread should use this Sampler for CPU profiling?
47 bool IsProfiling() const {
48 return base::NoBarrier_Load(&profiling_) > 0 &&
49 !base::NoBarrier_Load(&has_processing_thread_);
50 }
51 void IncreaseProfilingDepth();
52 void DecreaseProfilingDepth();
53
54 // Whether the sampler is running (that is, consumes resources).
55 bool IsActive() const { return base::NoBarrier_Load(&active_); }
56
Ben Murdochc5610432016-08-08 18:44:38 +010057 // CpuProfiler collects samples by calling DoSample directly
58 // without calling Start. To keep it working, we register the sampler
59 // with the CpuProfiler.
60 bool IsRegistered() const { return base::NoBarrier_Load(&registered_); }
61
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062 void DoSample();
63 // If true next sample must be initiated on the profiler event processor
64 // thread right after latest sample is processed.
65 void SetHasProcessingThread(bool value) {
66 base::NoBarrier_Store(&has_processing_thread_, value);
67 }
68
69 // Used in tests to make sure that stack sampling is performed.
Ben Murdoch097c5b22016-05-18 11:27:45 +010070 unsigned js_sample_count() const { return js_sample_count_; }
71 unsigned external_sample_count() const { return external_sample_count_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 void StartCountingSamples() {
Ben Murdoch097c5b22016-05-18 11:27:45 +010073 js_sample_count_ = 0;
74 external_sample_count_ = 0;
75 is_counting_samples_ = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076 }
77
78 class PlatformData;
79 PlatformData* platform_data() const { return data_; }
80
81 protected:
82 // This method is called for each sampling period with the current
83 // program counter.
84 virtual void Tick(TickSample* sample) = 0;
85
86 private:
87 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); }
88
Ben Murdochc5610432016-08-08 18:44:38 +010089 void SetRegistered(bool value) { base::NoBarrier_Store(&registered_, value); }
90
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091 Isolate* isolate_;
92 const int interval_;
93 base::Atomic32 profiling_;
94 base::Atomic32 has_processing_thread_;
95 base::Atomic32 active_;
Ben Murdochc5610432016-08-08 18:44:38 +010096 base::Atomic32 registered_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 PlatformData* data_; // Platform specific data.
Ben Murdoch097c5b22016-05-18 11:27:45 +010098 // Counts stack samples taken in various VM states.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 bool is_counting_samples_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100100 unsigned js_sample_count_;
101 unsigned external_sample_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
103};
104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000105} // namespace internal
106} // namespace v8
107
108#endif // V8_PROFILER_SAMPLER_H_