blob: 7ae3c8c4766d8b2f6f630e7df592d4fd363204c8 [file] [log] [blame]
Ben Murdoch61f157c2016-09-16 13:49:30 +01001// Copyright 2016 the V8 project authors. All rights reserved.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdoch61f157c2016-09-16 13:49:30 +01005#ifndef V8_LIBSAMPLER_SAMPLER_H_
6#define V8_LIBSAMPLER_SAMPLER_H_
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007
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 {
Ben Murdoch61f157c2016-09-16 13:49:30 +010014namespace sampler {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015
16// ----------------------------------------------------------------------------
17// Sampler
18//
19// A sampler periodically samples the state of the VM and optionally
20// (if used for profiling) the program counter and stack pointer for
21// the thread that created it.
22
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023class Sampler {
24 public:
Ben Murdoch61f157c2016-09-16 13:49:30 +010025 static const int kMaxFramesCountLog2 = 8;
26 static const unsigned kMaxFramesCount = (1u << kMaxFramesCountLog2) - 1;
27
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028 // Initializes the Sampler support. Called once at VM startup.
29 static void SetUp();
30 static void TearDown();
31
32 // Initialize sampler.
Ben Murdoch61f157c2016-09-16 13:49:30 +010033 explicit Sampler(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 virtual ~Sampler();
35
36 Isolate* isolate() const { return isolate_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037
38 // Performs stack sampling.
Ben Murdoch61f157c2016-09-16 13:49:30 +010039 // Clients should override this method in order to do something on samples,
40 // for example buffer samples in a queue.
41 virtual void SampleStack(const v8::RegisterState& regs) = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042
43 // Start and stop sampler.
44 void Start();
45 void Stop();
46
47 // Whether the sampling thread should use this Sampler for CPU profiling?
48 bool IsProfiling() const {
49 return base::NoBarrier_Load(&profiling_) > 0 &&
50 !base::NoBarrier_Load(&has_processing_thread_);
51 }
52 void IncreaseProfilingDepth();
53 void DecreaseProfilingDepth();
54
55 // Whether the sampler is running (that is, consumes resources).
56 bool IsActive() const { return base::NoBarrier_Load(&active_); }
57
Ben Murdochc5610432016-08-08 18:44:38 +010058 // CpuProfiler collects samples by calling DoSample directly
59 // without calling Start. To keep it working, we register the sampler
60 // with the CpuProfiler.
61 bool IsRegistered() const { return base::NoBarrier_Load(&registered_); }
62
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 void DoSample();
Ben Murdoch61f157c2016-09-16 13:49:30 +010064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 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:
Ben Murdoch61f157c2016-09-16 13:49:30 +010082 // Counts stack samples taken in various VM states.
83 bool is_counting_samples_;
84 unsigned js_sample_count_;
85 unsigned external_sample_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086
87 private:
88 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); }
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_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 base::Atomic32 profiling_;
93 base::Atomic32 has_processing_thread_;
94 base::Atomic32 active_;
Ben Murdochc5610432016-08-08 18:44:38 +010095 base::Atomic32 registered_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 PlatformData* data_; // Platform specific data.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
98};
99
Ben Murdoch61f157c2016-09-16 13:49:30 +0100100} // namespace sampler
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101} // namespace v8
102
Ben Murdoch61f157c2016-09-16 13:49:30 +0100103#endif // V8_LIBSAMPLER_SAMPLER_H_