2016-05-26 02:13:58 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-07-08 06:44:46 +00:00
|
|
|
#include "src/libsampler/sampler.h"
|
2016-05-26 02:13:58 +00:00
|
|
|
|
|
|
|
#include "src/base/platform/platform.h"
|
2016-09-01 12:01:33 +00:00
|
|
|
#include "src/base/platform/time.h"
|
2016-05-26 02:13:58 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
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 {
|
2019-01-21 09:59:04 +00:00
|
|
|
while (sampler_->IsActive()) {
|
2016-05-26 02:13:58 +00:00
|
|
|
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 {
|
2016-08-10 17:12:05 +00:00
|
|
|
void* frames[kMaxFramesCount];
|
2016-05-26 02:13:58 +00:00
|
|
|
SampleInfo sample_info;
|
2016-08-10 17:12:05 +00:00
|
|
|
isolate()->GetStackSample(regs, frames, kMaxFramesCount, &sample_info);
|
2016-05-26 02:13:58 +00:00
|
|
|
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:
|
2018-09-13 10:07:40 +00:00
|
|
|
TestApiCallbacks() = default;
|
2016-05-26 02:13:58 +00:00
|
|
|
|
|
|
|
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) {
|
2019-01-21 09:58:44 +00:00
|
|
|
TestSampler sampler(env->GetIsolate());
|
|
|
|
TestSamplingThread thread(&sampler);
|
|
|
|
sampler.Start();
|
|
|
|
sampler.StartCountingSamples();
|
|
|
|
thread.StartSynchronously();
|
2016-05-26 02:13:58 +00:00
|
|
|
do {
|
|
|
|
function->Call(env, env->Global(), argc, argv).ToLocalChecked();
|
2019-01-21 09:58:44 +00:00
|
|
|
} while (sampler.js_sample_count() < min_js_samples ||
|
|
|
|
sampler.external_sample_count() < min_external_samples);
|
|
|
|
sampler.Stop();
|
|
|
|
thread.Join();
|
2016-05-26 02:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // 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) {
|
2020-11-27 16:06:57 +00:00
|
|
|
return env->Global()
|
|
|
|
->Get(env, v8_str(name))
|
|
|
|
.ToLocalChecked()
|
|
|
|
.As<v8::Function>();
|
2016-05-26 02:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(LibSamplerCollectSample) {
|
|
|
|
LocalContext env;
|
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
|
|
|
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(v8_str("foo"), &TestApiCallbacks::Getter,
|
|
|
|
&TestApiCallbacks::Setter, data);
|
|
|
|
v8::Local<v8::Function> func =
|
|
|
|
func_template->GetFunction(env.local()).ToLocalChecked();
|
|
|
|
v8::Local<v8::Object> instance =
|
|
|
|
func->NewInstance(env.local()).ToLocalChecked();
|
|
|
|
env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
|
|
|
|
|
|
|
|
CompileRun(sampler_test_source);
|
|
|
|
v8::Local<v8::Function> function = GetFunction(env.local(), "start");
|
|
|
|
|
|
|
|
int32_t repeat_count = 100;
|
|
|
|
v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
|
|
|
|
RunSampler(env.local(), function, args, arraysize(args), 100, 100);
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:58:10 +00:00
|
|
|
#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); }
|
2019-02-13 04:34:19 +00:00
|
|
|
void set_should_record_sample() { SetShouldRecordSample(); }
|
2019-01-21 08:58:10 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int sample_count_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(SamplerManager_AddRemoveSampler) {
|
|
|
|
LocalContext env;
|
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
|
|
|
|
|
|
|
SamplerManager* manager = SamplerManager::instance();
|
|
|
|
CountingSampler sampler1(isolate);
|
2019-01-21 09:59:04 +00:00
|
|
|
sampler1.set_active(true);
|
2019-02-13 04:34:19 +00:00
|
|
|
sampler1.set_should_record_sample();
|
2019-01-21 08:58:10 +00:00
|
|
|
CHECK_EQ(0, sampler1.sample_count());
|
|
|
|
|
|
|
|
manager->AddSampler(&sampler1);
|
|
|
|
|
|
|
|
RegisterState state;
|
|
|
|
manager->DoSample(state);
|
|
|
|
CHECK_EQ(1, sampler1.sample_count());
|
|
|
|
|
|
|
|
sampler1.set_active(true);
|
2019-02-13 04:34:19 +00:00
|
|
|
sampler1.set_should_record_sample();
|
2019-01-21 08:58:10 +00:00
|
|
|
manager->RemoveSampler(&sampler1);
|
|
|
|
sampler1.set_active(false);
|
|
|
|
|
|
|
|
manager->DoSample(state);
|
|
|
|
CHECK_EQ(1, sampler1.sample_count());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SamplerManager_DoesNotReAdd) {
|
|
|
|
LocalContext env;
|
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
|
|
|
|
|
|
|
// Add the same sampler twice, but check we only get one sample for it.
|
|
|
|
SamplerManager* manager = SamplerManager::instance();
|
|
|
|
CountingSampler sampler1(isolate);
|
2019-01-21 09:59:04 +00:00
|
|
|
sampler1.set_active(true);
|
2019-02-13 04:34:19 +00:00
|
|
|
sampler1.set_should_record_sample();
|
2019-01-21 08:58:10 +00:00
|
|
|
manager->AddSampler(&sampler1);
|
|
|
|
manager->AddSampler(&sampler1);
|
|
|
|
|
|
|
|
RegisterState state;
|
|
|
|
manager->DoSample(state);
|
|
|
|
CHECK_EQ(1, sampler1.sample_count());
|
2019-01-21 09:59:04 +00:00
|
|
|
sampler1.set_active(false);
|
2019-01-21 08:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(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(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
|
|
|
|
|
2016-05-26 02:13:58 +00:00
|
|
|
} // namespace sampler
|
|
|
|
} // namespace v8
|