1// Copyright 2016 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// Tests of sampler functionalities.
5
6#include "src/libsampler/v8-sampler.h"
7
8#include "src/base/platform/platform.h"
9#include "test/cctest/cctest.h"
10
11
12namespace v8 {
13namespace sampler {
14
15namespace {
16
17class TestSamplingThread : public base::Thread {
18 public:
19  static const int kSamplerThreadStackSize = 64 * 1024;
20
21  explicit TestSamplingThread(Sampler* sampler)
22      : Thread(base::Thread::Options("TestSamplingThread",
23                                     kSamplerThreadStackSize)),
24        sampler_(sampler) {}
25
26  // Implement Thread::Run().
27  void Run() override {
28    while (sampler_->IsProfiling()) {
29      sampler_->DoSample();
30      base::OS::Sleep(base::TimeDelta::FromMilliseconds(1));
31    }
32  }
33
34 private:
35  Sampler* sampler_;
36};
37
38
39class TestSampler : public Sampler {
40 public:
41  explicit TestSampler(Isolate* isolate) : Sampler(isolate) {}
42
43  void SampleStack(const v8::RegisterState& regs) override {
44    void* frames[Sampler::kMaxFramesCount];
45    SampleInfo sample_info;
46    isolate()->GetStackSample(regs, reinterpret_cast<void**>(frames),
47                              Sampler::kMaxFramesCount, &sample_info);
48    if (is_counting_samples_) {
49      if (sample_info.vm_state == JS) ++js_sample_count_;
50      if (sample_info.vm_state == EXTERNAL) ++external_sample_count_;
51    }
52  }
53};
54
55
56class TestApiCallbacks {
57 public:
58  TestApiCallbacks() {}
59
60  static void Getter(v8::Local<v8::String> name,
61                     const v8::PropertyCallbackInfo<v8::Value>& info) {
62  }
63
64  static void Setter(v8::Local<v8::String> name,
65                     v8::Local<v8::Value> value,
66                     const v8::PropertyCallbackInfo<void>& info) {
67  }
68};
69
70
71static void RunSampler(v8::Local<v8::Context> env,
72                       v8::Local<v8::Function> function,
73                       v8::Local<v8::Value> argv[], int argc,
74                       unsigned min_js_samples = 0,
75                       unsigned min_external_samples = 0) {
76  Sampler::SetUp();
77  TestSampler* sampler = new TestSampler(env->GetIsolate());
78  TestSamplingThread* thread = new TestSamplingThread(sampler);
79  sampler->IncreaseProfilingDepth();
80  sampler->Start();
81  sampler->StartCountingSamples();
82  thread->StartSynchronously();
83  do {
84    function->Call(env, env->Global(), argc, argv).ToLocalChecked();
85  } while (sampler->js_sample_count() < min_js_samples ||
86           sampler->external_sample_count() < min_external_samples);
87  sampler->Stop();
88  sampler->DecreaseProfilingDepth();
89  thread->Join();
90  delete thread;
91  delete sampler;
92  Sampler::TearDown();
93}
94
95}  // namespace
96
97static const char* sampler_test_source = "function start(count) {\n"
98"  for (var i = 0; i < count; i++) {\n"
99"    var o = instance.foo;\n"
100"    instance.foo = o + 1;\n"
101"  }\n"
102"}\n";
103
104static v8::Local<v8::Function> GetFunction(v8::Local<v8::Context> env,
105                                           const char* name) {
106  return v8::Local<v8::Function>::Cast(
107      env->Global()->Get(env, v8_str(name)).ToLocalChecked());
108}
109
110
111TEST(LibSamplerCollectSample) {
112  LocalContext env;
113  v8::Isolate* isolate = env->GetIsolate();
114  v8::HandleScope scope(isolate);
115
116  v8::Local<v8::FunctionTemplate> func_template =
117      v8::FunctionTemplate::New(isolate);
118  v8::Local<v8::ObjectTemplate> instance_template =
119      func_template->InstanceTemplate();
120
121  TestApiCallbacks accessors;
122  v8::Local<v8::External> data =
123      v8::External::New(isolate, &accessors);
124  instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
125                                 &TestApiCallbacks::Setter, data);
126  v8::Local<v8::Function> func =
127      func_template->GetFunction(env.local()).ToLocalChecked();
128  v8::Local<v8::Object> instance =
129      func->NewInstance(env.local()).ToLocalChecked();
130  env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
131
132  CompileRun(sampler_test_source);
133  v8::Local<v8::Function> function = GetFunction(env.local(), "start");
134
135  int32_t repeat_count = 100;
136  v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
137  RunSampler(env.local(), function, args, arraysize(args), 100, 100);
138}
139
140}  // namespace sampler
141}  // namespace v8
142