nodejs/deps/v8/test/unittests/profiler/circular-queue-unittest.cc

162 lines
4.9 KiB
C++
Raw Permalink Normal View History

// Copyright 2022 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.
2010-03-23 15:19:12 -07:00
// Tests of the circular queue.
#include "src/init/v8.h"
#include "src/profiler/circular-queue-inl.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
2010-03-23 15:19:12 -07:00
using i::SamplingCircularQueue;
using CircularQueueTest = ::testing::Test;
2010-03-23 15:19:12 -07:00
TEST_F(CircularQueueTest, SamplingCircularQueue) {
using Record = v8::base::AtomicWord;
2013-10-22 15:14:25 -07:00
const int kMaxRecordsInQueue = 4;
SamplingCircularQueue<Record, kMaxRecordsInQueue> scq;
2010-03-23 15:19:12 -07:00
// Check that we are using non-reserved values.
// Fill up the first chunk.
CHECK(!scq.Peek());
2013-10-22 15:14:25 -07:00
for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
CHECK(rec);
2010-03-23 15:19:12 -07:00
*rec = i;
2013-10-22 15:14:25 -07:00
scq.FinishEnqueue();
2010-03-23 15:19:12 -07:00
}
2013-10-22 15:14:25 -07:00
// The queue is full, enqueue is not allowed.
CHECK(!scq.StartEnqueue());
2013-10-22 15:14:25 -07:00
// Try to enqueue when the the queue is full. Consumption must be available.
CHECK(scq.Peek());
2013-10-22 15:14:25 -07:00
for (int i = 0; i < 10; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
CHECK(!rec);
CHECK(scq.Peek());
2013-10-22 15:14:25 -07:00
}
// Consume all records.
for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
2013-10-22 15:14:25 -07:00
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
2010-03-23 15:19:12 -07:00
}
2013-10-22 15:14:25 -07:00
// The queue is empty.
CHECK(!scq.Peek());
2010-03-23 15:19:12 -07:00
CHECK(!scq.Peek());
2013-10-22 15:14:25 -07:00
for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
CHECK(rec);
2013-10-22 15:14:25 -07:00
*rec = i;
scq.FinishEnqueue();
2010-03-23 15:19:12 -07:00
}
2013-10-22 15:14:25 -07:00
// Consume all available kMaxRecordsInQueue / 2 records.
CHECK(scq.Peek());
2013-10-22 15:14:25 -07:00
for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
2010-03-23 15:19:12 -07:00
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
2013-10-22 15:14:25 -07:00
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
2010-03-23 15:19:12 -07:00
}
2013-10-22 15:14:25 -07:00
// The queue is empty.
CHECK(!scq.Peek());
2010-03-25 09:53:58 -07:00
}
namespace {
using Record = v8::base::AtomicWord;
using TestSampleQueue = SamplingCircularQueue<Record, 12>;
2013-10-22 15:14:25 -07:00
class ProducerThread : public v8::base::Thread {
2010-03-25 09:53:58 -07:00
public:
ProducerThread(TestSampleQueue* scq, int records_per_chunk, Record value,
v8::base::Semaphore* finished)
: Thread(Options("producer")),
2011-07-08 16:40:11 -07:00
scq_(scq),
2010-03-25 09:53:58 -07:00
records_per_chunk_(records_per_chunk),
value_(value),
finished_(finished) {}
2010-03-25 09:53:58 -07:00
void Run() override {
2010-03-25 09:53:58 -07:00
for (Record i = value_; i < value_ + records_per_chunk_; ++i) {
2013-10-22 15:14:25 -07:00
Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue());
CHECK(rec);
2010-03-25 09:53:58 -07:00
*rec = i;
2013-10-22 15:14:25 -07:00
scq_->FinishEnqueue();
2010-03-25 09:53:58 -07:00
}
finished_->Signal();
}
private:
2013-10-22 15:14:25 -07:00
TestSampleQueue* scq_;
2010-03-25 09:53:58 -07:00
const int records_per_chunk_;
Record value_;
v8::base::Semaphore* finished_;
2010-03-25 09:53:58 -07:00
};
} // namespace
TEST_F(CircularQueueTest, SamplingCircularQueueMultithreading) {
2010-03-25 09:53:58 -07:00
// Emulate multiple VM threads working 'one thread at a time.'
// This test enqueues data from different threads. This corresponds
// to the case of profiling under Linux, where signal handler that
// does sampling is called in the context of different VM threads.
const int kRecordsPerChunk = 4;
2013-10-22 15:14:25 -07:00
TestSampleQueue scq;
v8::base::Semaphore semaphore(0);
2010-03-25 09:53:58 -07:00
2013-10-22 15:14:25 -07:00
ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore);
ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore);
ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore);
2010-03-25 09:53:58 -07:00
CHECK(!scq.Peek());
CHECK(producer1.Start());
2013-10-22 15:14:25 -07:00
semaphore.Wait();
2010-03-25 09:53:58 -07:00
for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) {
2013-10-22 15:14:25 -07:00
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
2010-03-25 09:53:58 -07:00
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
2013-10-22 15:14:25 -07:00
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
2010-03-25 09:53:58 -07:00
}
CHECK(!scq.Peek());
CHECK(producer2.Start());
2013-10-22 15:14:25 -07:00
semaphore.Wait();
2010-03-25 09:53:58 -07:00
for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) {
2013-10-22 15:14:25 -07:00
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
2010-03-25 09:53:58 -07:00
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
2013-10-22 15:14:25 -07:00
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
2010-03-25 09:53:58 -07:00
}
CHECK(!scq.Peek());
CHECK(producer3.Start());
2013-10-22 15:14:25 -07:00
semaphore.Wait();
2010-03-25 09:53:58 -07:00
for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) {
2013-10-22 15:14:25 -07:00
Record* rec = reinterpret_cast<Record*>(scq.Peek());
CHECK(rec);
2010-03-25 09:53:58 -07:00
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
2013-10-22 15:14:25 -07:00
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
scq.Remove();
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
2010-03-25 09:53:58 -07:00
}
CHECK(!scq.Peek());
2010-03-23 15:19:12 -07:00
}