| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 using i::SamplingCircularQueue; | 35 using i::SamplingCircularQueue; |
| 36 | 36 |
| 37 | 37 |
| 38 TEST(SamplingCircularQueue) { | 38 TEST(SamplingCircularQueue) { |
| 39 typedef v8::base::AtomicWord Record; | 39 typedef v8::base::AtomicWord Record; |
| 40 const int kMaxRecordsInQueue = 4; | 40 const int kMaxRecordsInQueue = 4; |
| 41 SamplingCircularQueue<Record, kMaxRecordsInQueue> scq; | 41 SamplingCircularQueue<Record, kMaxRecordsInQueue> scq; |
| 42 | 42 |
| 43 // Check that we are using non-reserved values. | 43 // Check that we are using non-reserved values. |
| 44 // Fill up the first chunk. | 44 // Fill up the first chunk. |
| 45 CHECK_EQ(NULL, scq.Peek()); | 45 CHECK(!scq.Peek()); |
| 46 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { | 46 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { |
| 47 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); | 47 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); |
| 48 CHECK_NE(NULL, rec); | 48 CHECK(rec); |
| 49 *rec = i; | 49 *rec = i; |
| 50 scq.FinishEnqueue(); | 50 scq.FinishEnqueue(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // The queue is full, enqueue is not allowed. | 53 // The queue is full, enqueue is not allowed. |
| 54 CHECK_EQ(NULL, scq.StartEnqueue()); | 54 CHECK(!scq.StartEnqueue()); |
| 55 | 55 |
| 56 // Try to enqueue when the the queue is full. Consumption must be available. | 56 // Try to enqueue when the the queue is full. Consumption must be available. |
| 57 CHECK_NE(NULL, scq.Peek()); | 57 CHECK(scq.Peek()); |
| 58 for (int i = 0; i < 10; ++i) { | 58 for (int i = 0; i < 10; ++i) { |
| 59 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); | 59 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); |
| 60 CHECK_EQ(NULL, rec); | 60 CHECK(!rec); |
| 61 CHECK_NE(NULL, scq.Peek()); | 61 CHECK(scq.Peek()); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Consume all records. | 64 // Consume all records. |
| 65 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { | 65 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { |
| 66 Record* rec = reinterpret_cast<Record*>(scq.Peek()); | 66 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
| 67 CHECK_NE(NULL, rec); | 67 CHECK(rec); |
| 68 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 68 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
| 69 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); | 69 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 70 scq.Remove(); | 70 scq.Remove(); |
| 71 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); | 71 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 72 } | 72 } |
| 73 // The queue is empty. | 73 // The queue is empty. |
| 74 CHECK_EQ(NULL, scq.Peek()); | 74 CHECK(!scq.Peek()); |
| 75 | 75 |
| 76 | 76 |
| 77 CHECK_EQ(NULL, scq.Peek()); | 77 CHECK(!scq.Peek()); |
| 78 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { | 78 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { |
| 79 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); | 79 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); |
| 80 CHECK_NE(NULL, rec); | 80 CHECK(rec); |
| 81 *rec = i; | 81 *rec = i; |
| 82 scq.FinishEnqueue(); | 82 scq.FinishEnqueue(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Consume all available kMaxRecordsInQueue / 2 records. | 85 // Consume all available kMaxRecordsInQueue / 2 records. |
| 86 CHECK_NE(NULL, scq.Peek()); | 86 CHECK(scq.Peek()); |
| 87 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { | 87 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { |
| 88 Record* rec = reinterpret_cast<Record*>(scq.Peek()); | 88 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
| 89 CHECK_NE(NULL, rec); | 89 CHECK(rec); |
| 90 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 90 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
| 91 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); | 91 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 92 scq.Remove(); | 92 scq.Remove(); |
| 93 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); | 93 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 94 } | 94 } |
| 95 | 95 |
| 96 // The queue is empty. | 96 // The queue is empty. |
| 97 CHECK_EQ(NULL, scq.Peek()); | 97 CHECK(!scq.Peek()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 | 100 |
| 101 namespace { | 101 namespace { |
| 102 | 102 |
| 103 typedef v8::base::AtomicWord Record; | 103 typedef v8::base::AtomicWord Record; |
| 104 typedef SamplingCircularQueue<Record, 12> TestSampleQueue; | 104 typedef SamplingCircularQueue<Record, 12> TestSampleQueue; |
| 105 | 105 |
| 106 class ProducerThread: public v8::base::Thread { | 106 class ProducerThread: public v8::base::Thread { |
| 107 public: | 107 public: |
| 108 ProducerThread(TestSampleQueue* scq, int records_per_chunk, Record value, | 108 ProducerThread(TestSampleQueue* scq, int records_per_chunk, Record value, |
| 109 v8::base::Semaphore* finished) | 109 v8::base::Semaphore* finished) |
| 110 : Thread(Options("producer")), | 110 : Thread(Options("producer")), |
| 111 scq_(scq), | 111 scq_(scq), |
| 112 records_per_chunk_(records_per_chunk), | 112 records_per_chunk_(records_per_chunk), |
| 113 value_(value), | 113 value_(value), |
| 114 finished_(finished) {} | 114 finished_(finished) {} |
| 115 | 115 |
| 116 virtual void Run() { | 116 virtual void Run() { |
| 117 for (Record i = value_; i < value_ + records_per_chunk_; ++i) { | 117 for (Record i = value_; i < value_ + records_per_chunk_; ++i) { |
| 118 Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue()); | 118 Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue()); |
| 119 CHECK_NE(NULL, rec); | 119 CHECK(rec); |
| 120 *rec = i; | 120 *rec = i; |
| 121 scq_->FinishEnqueue(); | 121 scq_->FinishEnqueue(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 finished_->Signal(); | 124 finished_->Signal(); |
| 125 } | 125 } |
| 126 | 126 |
| 127 private: | 127 private: |
| 128 TestSampleQueue* scq_; | 128 TestSampleQueue* scq_; |
| 129 const int records_per_chunk_; | 129 const int records_per_chunk_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 140 // does sampling is called in the context of different VM threads. | 140 // does sampling is called in the context of different VM threads. |
| 141 | 141 |
| 142 const int kRecordsPerChunk = 4; | 142 const int kRecordsPerChunk = 4; |
| 143 TestSampleQueue scq; | 143 TestSampleQueue scq; |
| 144 v8::base::Semaphore semaphore(0); | 144 v8::base::Semaphore semaphore(0); |
| 145 | 145 |
| 146 ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore); | 146 ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore); |
| 147 ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore); | 147 ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore); |
| 148 ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore); | 148 ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore); |
| 149 | 149 |
| 150 CHECK_EQ(NULL, scq.Peek()); | 150 CHECK(!scq.Peek()); |
| 151 producer1.Start(); | 151 producer1.Start(); |
| 152 semaphore.Wait(); | 152 semaphore.Wait(); |
| 153 for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) { | 153 for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) { |
| 154 Record* rec = reinterpret_cast<Record*>(scq.Peek()); | 154 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
| 155 CHECK_NE(NULL, rec); | 155 CHECK(rec); |
| 156 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 156 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
| 157 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); | 157 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 158 scq.Remove(); | 158 scq.Remove(); |
| 159 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); | 159 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 160 } | 160 } |
| 161 | 161 |
| 162 CHECK_EQ(NULL, scq.Peek()); | 162 CHECK(!scq.Peek()); |
| 163 producer2.Start(); | 163 producer2.Start(); |
| 164 semaphore.Wait(); | 164 semaphore.Wait(); |
| 165 for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) { | 165 for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) { |
| 166 Record* rec = reinterpret_cast<Record*>(scq.Peek()); | 166 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
| 167 CHECK_NE(NULL, rec); | 167 CHECK(rec); |
| 168 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 168 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
| 169 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); | 169 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 170 scq.Remove(); | 170 scq.Remove(); |
| 171 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); | 171 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 172 } | 172 } |
| 173 | 173 |
| 174 CHECK_EQ(NULL, scq.Peek()); | 174 CHECK(!scq.Peek()); |
| 175 producer3.Start(); | 175 producer3.Start(); |
| 176 semaphore.Wait(); | 176 semaphore.Wait(); |
| 177 for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) { | 177 for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) { |
| 178 Record* rec = reinterpret_cast<Record*>(scq.Peek()); | 178 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
| 179 CHECK_NE(NULL, rec); | 179 CHECK(rec); |
| 180 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 180 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
| 181 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); | 181 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 182 scq.Remove(); | 182 scq.Remove(); |
| 183 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); | 183 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 184 } | 184 } |
| 185 | 185 |
| 186 CHECK_EQ(NULL, scq.Peek()); | 186 CHECK(!scq.Peek()); |
| 187 } | 187 } |
| OLD | NEW |