2010-03-17 13:15:42 +00:00
|
|
|
// Copyright 2010 the V8 project authors. All rights reserved.
|
2013-03-07 11:12:26 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2010-03-17 13:15:42 +00:00
|
|
|
//
|
2010-05-22 05:27:19 +00:00
|
|
|
// Tests of the circular queue.
|
2010-03-17 13:15:42 +00:00
|
|
|
|
2019-05-24 13:51:59 +00:00
|
|
|
#include "src/init/v8.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
|
2015-09-28 19:34:08 +00:00
|
|
|
#include "src/profiler/circular-queue-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2010-03-17 13:15:42 +00:00
|
|
|
|
|
|
|
using i::SamplingCircularQueue;
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SamplingCircularQueue) {
|
2019-05-27 11:31:49 +00:00
|
|
|
using Record = v8::base::AtomicWord;
|
2013-08-23 08:22:07 +00:00
|
|
|
const int kMaxRecordsInQueue = 4;
|
|
|
|
SamplingCircularQueue<Record, kMaxRecordsInQueue> scq;
|
2010-03-17 13:15:42 +00:00
|
|
|
|
|
|
|
// Check that we are using non-reserved values.
|
|
|
|
// Fill up the first chunk.
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2013-08-23 08:22:07 +00:00
|
|
|
for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
|
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2010-03-17 13:15:42 +00:00
|
|
|
*rec = i;
|
2013-08-23 08:22:07 +00:00
|
|
|
scq.FinishEnqueue();
|
2010-03-17 13:15:42 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 08:22:07 +00:00
|
|
|
// The queue is full, enqueue is not allowed.
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.StartEnqueue());
|
2010-03-17 13:15:42 +00:00
|
|
|
|
2013-08-23 08:22:07 +00:00
|
|
|
// Try to enqueue when the the queue is full. Consumption must be available.
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(scq.Peek());
|
2013-08-23 08:22:07 +00:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!rec);
|
|
|
|
CHECK(scq.Peek());
|
2013-08-23 08:22:07 +00:00
|
|
|
}
|
2010-03-17 13:15:42 +00:00
|
|
|
|
2013-08-23 08:22:07 +00:00
|
|
|
// Consume all records.
|
|
|
|
for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
|
2013-09-05 10:31:18 +00:00
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.Peek());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2010-03-17 13:15:42 +00:00
|
|
|
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
|
2013-09-05 10:31:18 +00:00
|
|
|
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
|
|
|
|
scq.Remove();
|
|
|
|
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
|
2010-03-17 13:15:42 +00:00
|
|
|
}
|
2013-08-23 08:22:07 +00:00
|
|
|
// The queue is empty.
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2013-08-23 08:22:07 +00:00
|
|
|
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2013-08-23 08:22:07 +00:00
|
|
|
for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
|
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2013-08-23 08:22:07 +00:00
|
|
|
*rec = i;
|
|
|
|
scq.FinishEnqueue();
|
|
|
|
}
|
2010-03-17 13:15:42 +00:00
|
|
|
|
2013-08-23 08:22:07 +00:00
|
|
|
// Consume all available kMaxRecordsInQueue / 2 records.
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(scq.Peek());
|
2013-08-23 08:22:07 +00:00
|
|
|
for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
|
2013-09-05 10:31:18 +00:00
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.Peek());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2010-03-17 13:15:42 +00:00
|
|
|
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
|
2013-09-05 10:31:18 +00:00
|
|
|
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
|
|
|
|
scq.Remove();
|
|
|
|
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
|
2010-03-17 13:15:42 +00:00
|
|
|
}
|
2013-08-23 08:22:07 +00:00
|
|
|
|
|
|
|
// The queue is empty.
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2010-03-22 14:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-05-27 11:31:49 +00:00
|
|
|
using Record = v8::base::AtomicWord;
|
|
|
|
using TestSampleQueue = SamplingCircularQueue<Record, 12>;
|
2013-08-23 08:22:07 +00:00
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
class ProducerThread: public v8::base::Thread {
|
2010-03-22 14:23:45 +00:00
|
|
|
public:
|
2014-08-12 13:33:35 +00:00
|
|
|
ProducerThread(TestSampleQueue* scq, int records_per_chunk, Record value,
|
2014-06-30 13:25:46 +00:00
|
|
|
v8::base::Semaphore* finished)
|
2014-08-12 13:33:35 +00:00
|
|
|
: Thread(Options("producer")),
|
2011-03-18 20:35:07 +00:00
|
|
|
scq_(scq),
|
2010-03-22 14:23:45 +00:00
|
|
|
records_per_chunk_(records_per_chunk),
|
|
|
|
value_(value),
|
2014-08-12 13:33:35 +00:00
|
|
|
finished_(finished) {}
|
2010-03-22 14:23:45 +00:00
|
|
|
|
2018-09-14 15:34:02 +00:00
|
|
|
void Run() override {
|
2010-03-22 14:23:45 +00:00
|
|
|
for (Record i = value_; i < value_ + records_per_chunk_; ++i) {
|
2013-08-23 08:22:07 +00:00
|
|
|
Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2010-03-22 14:23:45 +00:00
|
|
|
*rec = i;
|
2013-08-23 08:22:07 +00:00
|
|
|
scq_->FinishEnqueue();
|
2010-03-22 14:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
finished_->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-23 08:22:07 +00:00
|
|
|
TestSampleQueue* scq_;
|
2010-03-22 14:23:45 +00:00
|
|
|
const int records_per_chunk_;
|
|
|
|
Record value_;
|
2014-06-30 13:25:46 +00:00
|
|
|
v8::base::Semaphore* finished_;
|
2010-03-22 14:23:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(SamplingCircularQueueMultithreading) {
|
|
|
|
// 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-08-23 08:22:07 +00:00
|
|
|
TestSampleQueue scq;
|
2014-06-30 13:25:46 +00:00
|
|
|
v8::base::Semaphore semaphore(0);
|
2010-03-22 14:23:45 +00:00
|
|
|
|
2013-09-02 12:26:06 +00:00
|
|
|
ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore);
|
|
|
|
ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore);
|
|
|
|
ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore);
|
2010-03-22 14:23:45 +00:00
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2019-07-29 13:09:02 +00:00
|
|
|
CHECK(producer1.Start());
|
2013-09-02 12:26:06 +00:00
|
|
|
semaphore.Wait();
|
2010-03-22 14:23:45 +00:00
|
|
|
for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) {
|
2013-09-05 10:31:18 +00:00
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.Peek());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2010-03-22 14:23:45 +00:00
|
|
|
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
|
2013-09-05 10:31:18 +00:00
|
|
|
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
|
|
|
|
scq.Remove();
|
|
|
|
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
|
2010-03-22 14:23:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2019-07-29 13:09:02 +00:00
|
|
|
CHECK(producer2.Start());
|
2013-09-02 12:26:06 +00:00
|
|
|
semaphore.Wait();
|
2010-03-22 14:23:45 +00:00
|
|
|
for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) {
|
2013-09-05 10:31:18 +00:00
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.Peek());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2010-03-22 14:23:45 +00:00
|
|
|
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
|
2013-09-05 10:31:18 +00:00
|
|
|
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
|
|
|
|
scq.Remove();
|
|
|
|
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
|
2010-03-22 14:23:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2019-07-29 13:09:02 +00:00
|
|
|
CHECK(producer3.Start());
|
2013-09-02 12:26:06 +00:00
|
|
|
semaphore.Wait();
|
2010-03-22 14:23:45 +00:00
|
|
|
for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) {
|
2013-09-05 10:31:18 +00:00
|
|
|
Record* rec = reinterpret_cast<Record*>(scq.Peek());
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(rec);
|
2010-03-22 14:23:45 +00:00
|
|
|
CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
|
2013-09-05 10:31:18 +00:00
|
|
|
CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
|
|
|
|
scq.Remove();
|
|
|
|
CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
|
2010-03-22 14:23:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK(!scq.Peek());
|
2010-03-17 13:15:42 +00:00
|
|
|
}
|