blob: a5bcb486af7626b5cbede751bd39eb560db08388 [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Steve Block6ded16b2010-05-10 14:33:55 +010027//
Leon Clarkef7060e22010-06-03 12:02:55 +010028// Tests of the circular queue.
Steve Block6ded16b2010-05-10 14:33:55 +010029
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/v8.h"
31
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032#include "src/profiler/circular-queue-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include "test/cctest/cctest.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034
Steve Block6ded16b2010-05-10 14:33:55 +010035using i::SamplingCircularQueue;
36
37
Steve Block6ded16b2010-05-10 14:33:55 +010038TEST(SamplingCircularQueue) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 typedef v8::base::AtomicWord Record;
40 const int kMaxRecordsInQueue = 4;
41 SamplingCircularQueue<Record, kMaxRecordsInQueue> scq;
Steve Block6ded16b2010-05-10 14:33:55 +010042
43 // Check that we are using non-reserved values.
Steve Block6ded16b2010-05-10 14:33:55 +010044 // Fill up the first chunk.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 CHECK(!scq.Peek());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
47 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048 CHECK(rec);
Steve Block6ded16b2010-05-10 14:33:55 +010049 *rec = i;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 scq.FinishEnqueue();
Steve Block6ded16b2010-05-10 14:33:55 +010051 }
52
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 // The queue is full, enqueue is not allowed.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 CHECK(!scq.StartEnqueue());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055
56 // Try to enqueue when the the queue is full. Consumption must be available.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 CHECK(scq.Peek());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 for (int i = 0; i < 10; ++i) {
59 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 CHECK(!rec);
61 CHECK(scq.Peek());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 }
63
64 // Consume all records.
65 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
66 Record* rec = reinterpret_cast<Record*>(scq.Peek());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 CHECK(rec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
69 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
70 scq.Remove();
71 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
72 }
73 // The queue is empty.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 CHECK(!scq.Peek());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075
76
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 CHECK(!scq.Peek());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
79 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 CHECK(rec);
Steve Block6ded16b2010-05-10 14:33:55 +010081 *rec = i;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 scq.FinishEnqueue();
Steve Block6ded16b2010-05-10 14:33:55 +010083 }
84
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 // Consume all available kMaxRecordsInQueue / 2 records.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086 CHECK(scq.Peek());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
88 Record* rec = reinterpret_cast<Record*>(scq.Peek());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089 CHECK(rec);
Steve Block6ded16b2010-05-10 14:33:55 +010090 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
92 scq.Remove();
93 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
Steve Block6ded16b2010-05-10 14:33:55 +010094 }
Steve Block6ded16b2010-05-10 14:33:55 +010095
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 // The queue is empty.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 CHECK(!scq.Peek());
Steve Block6ded16b2010-05-10 14:33:55 +010098}
99
100
101namespace {
102
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103typedef v8::base::AtomicWord Record;
104typedef SamplingCircularQueue<Record, 12> TestSampleQueue;
Steve Block6ded16b2010-05-10 14:33:55 +0100105
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106class ProducerThread: public v8::base::Thread {
107 public:
108 ProducerThread(TestSampleQueue* scq, int records_per_chunk, Record value,
109 v8::base::Semaphore* finished)
110 : Thread(Options("producer")),
Steve Block44f0eee2011-05-26 01:26:41 +0100111 scq_(scq),
Steve Block6ded16b2010-05-10 14:33:55 +0100112 records_per_chunk_(records_per_chunk),
113 value_(value),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 finished_(finished) {}
Steve Block6ded16b2010-05-10 14:33:55 +0100115
116 virtual void Run() {
117 for (Record i = value_; i < value_ + records_per_chunk_; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 CHECK(rec);
Steve Block6ded16b2010-05-10 14:33:55 +0100120 *rec = i;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 scq_->FinishEnqueue();
Steve Block6ded16b2010-05-10 14:33:55 +0100122 }
123
124 finished_->Signal();
125 }
126
127 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 TestSampleQueue* scq_;
Steve Block6ded16b2010-05-10 14:33:55 +0100129 const int records_per_chunk_;
130 Record value_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 v8::base::Semaphore* finished_;
Steve Block6ded16b2010-05-10 14:33:55 +0100132};
133
134} // namespace
135
136TEST(SamplingCircularQueueMultithreading) {
137 // Emulate multiple VM threads working 'one thread at a time.'
138 // This test enqueues data from different threads. This corresponds
139 // to the case of profiling under Linux, where signal handler that
140 // does sampling is called in the context of different VM threads.
141
Steve Block6ded16b2010-05-10 14:33:55 +0100142 const int kRecordsPerChunk = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143 TestSampleQueue scq;
144 v8::base::Semaphore semaphore(0);
Steve Block6ded16b2010-05-10 14:33:55 +0100145
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore);
147 ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore);
148 ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore);
Steve Block6ded16b2010-05-10 14:33:55 +0100149
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000150 CHECK(!scq.Peek());
Steve Block6ded16b2010-05-10 14:33:55 +0100151 producer1.Start();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 semaphore.Wait();
Steve Block6ded16b2010-05-10 14:33:55 +0100153 for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 Record* rec = reinterpret_cast<Record*>(scq.Peek());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155 CHECK(rec);
Steve Block6ded16b2010-05-10 14:33:55 +0100156 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
158 scq.Remove();
159 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
Steve Block6ded16b2010-05-10 14:33:55 +0100160 }
161
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000162 CHECK(!scq.Peek());
Steve Block6ded16b2010-05-10 14:33:55 +0100163 producer2.Start();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 semaphore.Wait();
Steve Block6ded16b2010-05-10 14:33:55 +0100165 for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 Record* rec = reinterpret_cast<Record*>(scq.Peek());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 CHECK(rec);
Steve Block6ded16b2010-05-10 14:33:55 +0100168 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
170 scq.Remove();
171 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
Steve Block6ded16b2010-05-10 14:33:55 +0100172 }
173
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 CHECK(!scq.Peek());
Steve Block6ded16b2010-05-10 14:33:55 +0100175 producer3.Start();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 semaphore.Wait();
Steve Block6ded16b2010-05-10 14:33:55 +0100177 for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 Record* rec = reinterpret_cast<Record*>(scq.Peek());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179 CHECK(rec);
Steve Block6ded16b2010-05-10 14:33:55 +0100180 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
182 scq.Remove();
183 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
Steve Block6ded16b2010-05-10 14:33:55 +0100184 }
185
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 CHECK(!scq.Peek());
Steve Block6ded16b2010-05-10 14:33:55 +0100187}