test-circular-queue.cc 6.14 KB
Newer Older
1
// Copyright 2010 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 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.
27
//
28
// Tests of the circular queue.
29

30
#include "src/v8.h"
31

32
#include "src/profiler/circular-queue-inl.h"
33
#include "test/cctest/cctest.h"
34 35 36 37 38

using i::SamplingCircularQueue;


TEST(SamplingCircularQueue) {
39
  typedef v8::base::AtomicWord Record;
40 41
  const int kMaxRecordsInQueue = 4;
  SamplingCircularQueue<Record, kMaxRecordsInQueue> scq;
42 43 44

  // Check that we are using non-reserved values.
  // Fill up the first chunk.
45
  CHECK(!scq.Peek());
46 47
  for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
    Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
48
    CHECK(rec);
49
    *rec = i;
50
    scq.FinishEnqueue();
51 52
  }

53
  // The queue is full, enqueue is not allowed.
54
  CHECK(!scq.StartEnqueue());
55

56
  // Try to enqueue when the the queue is full. Consumption must be available.
57
  CHECK(scq.Peek());
58 59
  for (int i = 0; i < 10; ++i) {
    Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
60 61
    CHECK(!rec);
    CHECK(scq.Peek());
62
  }
63

64 65
  // Consume all records.
  for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) {
66
    Record* rec = reinterpret_cast<Record*>(scq.Peek());
67
    CHECK(rec);
68
    CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
69 70 71
    CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
    scq.Remove();
    CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
72
  }
73
  // The queue is empty.
74
  CHECK(!scq.Peek());
75 76


77
  CHECK(!scq.Peek());
78 79
  for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
    Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue());
80
    CHECK(rec);
81 82 83
    *rec = i;
    scq.FinishEnqueue();
  }
84

85
  // Consume all available kMaxRecordsInQueue / 2 records.
86
  CHECK(scq.Peek());
87
  for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) {
88
    Record* rec = reinterpret_cast<Record*>(scq.Peek());
89
    CHECK(rec);
90
    CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
91 92 93
    CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
    scq.Remove();
    CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
94
  }
95 96

  // The queue is empty.
97
  CHECK(!scq.Peek());
98 99 100 101 102
}


namespace {

103
typedef v8::base::AtomicWord Record;
104 105
typedef SamplingCircularQueue<Record, 12> TestSampleQueue;

106
class 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")),
111
        scq_(scq),
112 113
        records_per_chunk_(records_per_chunk),
        value_(value),
114
        finished_(finished) {}
115 116 117

  virtual void Run() {
    for (Record i = value_; i < value_ + records_per_chunk_; ++i) {
118
      Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue());
119
      CHECK(rec);
120
      *rec = i;
121
      scq_->FinishEnqueue();
122 123 124 125 126 127
    }

    finished_->Signal();
  }

 private:
128
  TestSampleQueue* scq_;
129 130
  const int records_per_chunk_;
  Record value_;
131
  v8::base::Semaphore* finished_;
132 133 134 135 136 137 138 139 140 141 142
};

}  // 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;
143
  TestSampleQueue scq;
144
  v8::base::Semaphore semaphore(0);
145

146 147 148
  ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore);
  ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore);
  ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore);
149

150
  CHECK(!scq.Peek());
151
  producer1.Start();
152
  semaphore.Wait();
153
  for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) {
154
    Record* rec = reinterpret_cast<Record*>(scq.Peek());
155
    CHECK(rec);
156
    CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
157 158 159
    CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
    scq.Remove();
    CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
160 161
  }

162
  CHECK(!scq.Peek());
163
  producer2.Start();
164
  semaphore.Wait();
165
  for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) {
166
    Record* rec = reinterpret_cast<Record*>(scq.Peek());
167
    CHECK(rec);
168
    CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
169 170 171
    CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
    scq.Remove();
    CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
172 173
  }

174
  CHECK(!scq.Peek());
175
  producer3.Start();
176
  semaphore.Wait();
177
  for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) {
178
    Record* rec = reinterpret_cast<Record*>(scq.Peek());
179
    CHECK(rec);
180
    CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec));
181 182 183
    CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek()));
    scq.Remove();
    CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek()));
184 185
  }

186
  CHECK(!scq.Peek());
187
}