v8/test/unittests/libsampler/sampler-unittest.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

218 lines
6.2 KiB
C++
Raw Normal View History

// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests of sampler functionalities.
#include "src/libsampler/sampler.h"
Reland "[include] Split out v8.h" This is a reland of d1b27019d3bf86360ea838c317f8505fac6d3a7e Fixes include: Adding missing file to bazel build Forward-declaring classing before friend-classing them to fix win/gcc Add missing v8-isolate.h include for vtune builds Original change's description: > [include] Split out v8.h > > This moves every single class/function out of include/v8.h into a > separate header in include/, which v8.h then includes so that > externally nothing appears to have changed. > > Every include of v8.h from inside v8 has been changed to a more > fine-grained include. > > Previously inline functions defined at the bottom of v8.h would call > private non-inline functions in the V8 class. Since that class is now > in v8-initialization.h and is rarely included (as that would create > dependency cycles), this is not possible and so those methods have been > moved out of the V8 class into the namespace v8::api_internal. > > None of the previous files in include/ now #include v8.h, which means > if embedders were relying on this transitive dependency then it will > give compile failures. > > v8-inspector.h does depend on v8-scripts.h for the time being to ensure > that Chrome continue to compile but that change will be reverted once > those transitive #includes in chrome are changed to include it directly. > > Full design: > https://docs.google.com/document/d/1rTD--I8hCAr-Rho1WTumZzFKaDpEp0IJ8ejZtk4nJdA/edit?usp=sharing > > Bug: v8:11965 > Change-Id: I53b84b29581632710edc80eb11f819c2097a2877 > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3097448 > Reviewed-by: Yang Guo <yangguo@chromium.org> > Reviewed-by: Camillo Bruni <cbruni@chromium.org> > Reviewed-by: Jakob Kummerow <jkummerow@chromium.org> > Reviewed-by: Leszek Swirski <leszeks@chromium.org> > Reviewed-by: Michael Lippautz <mlippautz@chromium.org> > Commit-Queue: Dan Elphick <delphick@chromium.org> > Cr-Commit-Position: refs/heads/main@{#76424} Cq-Include-Trybots: luci.v8.try:v8_linux_vtunejit Bug: v8:11965 Change-Id: I99f5d3a73bf8fe25b650adfaf9567dc4e44a09e6 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3113629 Reviewed-by: Leszek Swirski <leszeks@chromium.org> Reviewed-by: Camillo Bruni <cbruni@chromium.org> Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org> Reviewed-by: Simon Zünd <szuend@chromium.org> Commit-Queue: Dan Elphick <delphick@chromium.org> Cr-Commit-Position: refs/heads/main@{#76460}
2021-08-23 13:01:06 +00:00
#include "include/v8-external.h"
#include "include/v8-function.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
using SamplerTest = TestWithContext;
namespace sampler {
namespace {
class TestSamplingThread : public base::Thread {
public:
static const int kSamplerThreadStackSize = 64 * 1024;
explicit TestSamplingThread(Sampler* sampler)
: Thread(base::Thread::Options("TestSamplingThread",
kSamplerThreadStackSize)),
sampler_(sampler) {}
// Implement Thread::Run().
void Run() override {
while (sampler_->IsActive()) {
sampler_->DoSample();
base::OS::Sleep(base::TimeDelta::FromMilliseconds(1));
}
}
private:
Sampler* sampler_;
};
class TestSampler : public Sampler {
public:
explicit TestSampler(Isolate* isolate) : Sampler(isolate) {}
void SampleStack(const v8::RegisterState& regs) override {
void* frames[kMaxFramesCount];
SampleInfo sample_info;
isolate()->GetStackSample(regs, frames, kMaxFramesCount, &sample_info);
if (is_counting_samples_) {
if (sample_info.vm_state == JS) ++js_sample_count_;
if (sample_info.vm_state == EXTERNAL) ++external_sample_count_;
}
}
};
class TestApiCallbacks {
public:
TestApiCallbacks() = default;
static void Getter(v8::Local<v8::String> name,
const v8::PropertyCallbackInfo<v8::Value>& info) {}
static void Setter(v8::Local<v8::String> name, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) {}
};
static void RunSampler(v8::Local<v8::Context> env,
v8::Local<v8::Function> function,
v8::Local<v8::Value> argv[], int argc,
unsigned min_js_samples = 0,
unsigned min_external_samples = 0) {
TestSampler sampler(env->GetIsolate());
TestSamplingThread thread(&sampler);
sampler.Start();
sampler.StartCountingSamples();
thread.StartSynchronously();
do {
function->Call(env, env->Global(), argc, argv).ToLocalChecked();
} while (sampler.js_sample_count() < min_js_samples ||
sampler.external_sample_count() < min_external_samples);
sampler.Stop();
thread.Join();
}
} // namespace
static const char* sampler_test_source =
"function start(count) {\n"
" for (var i = 0; i < count; i++) {\n"
" var o = instance.foo;\n"
" instance.foo = o + 1;\n"
" }\n"
"}\n";
static v8::Local<v8::Function> GetFunction(v8::Local<v8::Context> env,
const char* name) {
return env->Global()
->Get(env, String::NewFromUtf8(env->GetIsolate(), name).ToLocalChecked())
.ToLocalChecked()
.As<v8::Function>();
}
TEST_F(SamplerTest, LibSamplerCollectSample) {
v8::HandleScope scope(isolate());
v8::Local<v8::FunctionTemplate> func_template =
v8::FunctionTemplate::New(isolate());
v8::Local<v8::ObjectTemplate> instance_template =
func_template->InstanceTemplate();
TestApiCallbacks accessors;
v8::Local<v8::External> data = v8::External::New(isolate(), &accessors);
instance_template->SetAccessor(NewString("foo"), &TestApiCallbacks::Getter,
&TestApiCallbacks::Setter, data);
v8::Local<v8::Function> func =
func_template->GetFunction(context()).ToLocalChecked();
v8::Local<v8::Object> instance =
func->NewInstance(context()).ToLocalChecked();
context()
->Global()
->Set(context(), NewString("instance"), instance)
.FromJust();
RunJS(sampler_test_source);
v8::Local<v8::Function> function = GetFunction(context(), "start");
int32_t repeat_count = 100;
v8::Local<v8::Value> args[] = {v8::Integer::New(isolate(), repeat_count)};
RunSampler(context(), function, args, arraysize(args), 100, 100);
}
#ifdef USE_SIGNALS
class CountingSampler : public Sampler {
public:
explicit CountingSampler(Isolate* isolate) : Sampler(isolate) {}
void SampleStack(const v8::RegisterState& regs) override { sample_count_++; }
int sample_count() { return sample_count_; }
void set_active(bool active) { SetActive(active); }
void set_should_record_sample() { SetShouldRecordSample(); }
private:
int sample_count_ = 0;
};
TEST_F(SamplerTest, SamplerManager_AddRemoveSampler) {
SamplerManager* manager = SamplerManager::instance();
CountingSampler sampler1(isolate());
sampler1.set_active(true);
sampler1.set_should_record_sample();
CHECK_EQ(0, sampler1.sample_count());
manager->AddSampler(&sampler1);
RegisterState state;
manager->DoSample(state);
CHECK_EQ(1, sampler1.sample_count());
sampler1.set_active(true);
sampler1.set_should_record_sample();
manager->RemoveSampler(&sampler1);
sampler1.set_active(false);
manager->DoSample(state);
CHECK_EQ(1, sampler1.sample_count());
}
TEST_F(SamplerTest, SamplerManager_DoesNotReAdd) {
// Add the same sampler twice, but check we only get one sample for it.
SamplerManager* manager = SamplerManager::instance();
CountingSampler sampler1(isolate());
sampler1.set_active(true);
sampler1.set_should_record_sample();
manager->AddSampler(&sampler1);
manager->AddSampler(&sampler1);
RegisterState state;
manager->DoSample(state);
CHECK_EQ(1, sampler1.sample_count());
sampler1.set_active(false);
}
TEST_F(SamplerTest, AtomicGuard_GetNonBlockingSuccess) {
std::atomic_bool atomic{false};
{
AtomicGuard guard(&atomic, false);
CHECK(guard.is_success());
AtomicGuard guard2(&atomic, false);
CHECK(!guard2.is_success());
}
AtomicGuard guard(&atomic, false);
CHECK(guard.is_success());
}
TEST_F(SamplerTest, AtomicGuard_GetBlockingSuccess) {
std::atomic_bool atomic{false};
{
AtomicGuard guard(&atomic);
CHECK(guard.is_success());
AtomicGuard guard2(&atomic, false);
CHECK(!guard2.is_success());
}
AtomicGuard guard(&atomic);
CHECK(guard.is_success());
}
#endif // USE_SIGNALS
} // namespace sampler
} // namespace v8