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 23 matching lines...) Expand all Loading... |
34 using i::SamplingCircularQueue; | 34 using i::SamplingCircularQueue; |
35 | 35 |
36 | 36 |
37 TEST(SamplingCircularQueue) { | 37 TEST(SamplingCircularQueue) { |
38 typedef i::AtomicWord Record; | 38 typedef i::AtomicWord Record; |
39 const int kMaxRecordsInQueue = 4; | 39 const int kMaxRecordsInQueue = 4; |
40 SamplingCircularQueue<Record, kMaxRecordsInQueue> scq; | 40 SamplingCircularQueue<Record, kMaxRecordsInQueue> scq; |
41 | 41 |
42 // Check that we are using non-reserved values. | 42 // Check that we are using non-reserved values. |
43 // Fill up the first chunk. | 43 // Fill up the first chunk. |
44 CHECK_EQ(NULL, scq.StartDequeue()); | 44 CHECK_EQ(NULL, scq.Peek()); |
45 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { | 45 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { |
46 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); | 46 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); |
47 CHECK_NE(NULL, rec); | 47 CHECK_NE(NULL, rec); |
48 *rec = i; | 48 *rec = i; |
49 scq.FinishEnqueue(); | 49 scq.FinishEnqueue(); |
50 } | 50 } |
51 | 51 |
52 // The queue is full, enqueue is not allowed. | 52 // The queue is full, enqueue is not allowed. |
53 CHECK_EQ(NULL, scq.StartEnqueue()); | 53 CHECK_EQ(NULL, scq.StartEnqueue()); |
54 | 54 |
55 // Try to enqueue when the the queue is full. Consumption must be available. | 55 // Try to enqueue when the the queue is full. Consumption must be available. |
56 CHECK_NE(NULL, scq.StartDequeue()); | 56 CHECK_NE(NULL, scq.Peek()); |
57 for (int i = 0; i < 10; ++i) { | 57 for (int i = 0; i < 10; ++i) { |
58 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); | 58 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); |
59 CHECK_EQ(NULL, rec); | 59 CHECK_EQ(NULL, rec); |
60 CHECK_NE(NULL, scq.StartDequeue()); | 60 CHECK_NE(NULL, scq.Peek()); |
61 } | 61 } |
62 | 62 |
63 // Consume all records. | 63 // Consume all records. |
64 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { | 64 for (Record i = 1; i < 1 + kMaxRecordsInQueue; ++i) { |
65 Record* rec = reinterpret_cast<Record*>(scq.StartDequeue()); | 65 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
66 CHECK_NE(NULL, rec); | 66 CHECK_NE(NULL, rec); |
67 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 67 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
68 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 68 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
69 scq.FinishDequeue(); | 69 scq.Remove(); |
70 CHECK_NE(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 70 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
71 } | 71 } |
72 // The queue is empty. | 72 // The queue is empty. |
73 CHECK_EQ(NULL, scq.StartDequeue()); | 73 CHECK_EQ(NULL, scq.Peek()); |
74 | 74 |
75 | 75 |
76 CHECK_EQ(NULL, scq.StartDequeue()); | 76 CHECK_EQ(NULL, scq.Peek()); |
77 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { | 77 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { |
78 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); | 78 Record* rec = reinterpret_cast<Record*>(scq.StartEnqueue()); |
79 CHECK_NE(NULL, rec); | 79 CHECK_NE(NULL, rec); |
80 *rec = i; | 80 *rec = i; |
81 scq.FinishEnqueue(); | 81 scq.FinishEnqueue(); |
82 } | 82 } |
83 | 83 |
84 // Consume all available kMaxRecordsInQueue / 2 records. | 84 // Consume all available kMaxRecordsInQueue / 2 records. |
85 CHECK_NE(NULL, scq.StartDequeue()); | 85 CHECK_NE(NULL, scq.Peek()); |
86 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { | 86 for (Record i = 0; i < kMaxRecordsInQueue / 2; ++i) { |
87 Record* rec = reinterpret_cast<Record*>(scq.StartDequeue()); | 87 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
88 CHECK_NE(NULL, rec); | 88 CHECK_NE(NULL, rec); |
89 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 89 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
90 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 90 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
91 scq.FinishDequeue(); | 91 scq.Remove(); |
92 CHECK_NE(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 92 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
93 } | 93 } |
94 | 94 |
95 // The queue is empty. | 95 // The queue is empty. |
96 CHECK_EQ(NULL, scq.StartDequeue()); | 96 CHECK_EQ(NULL, scq.Peek()); |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 namespace { | 100 namespace { |
101 | 101 |
102 typedef i::AtomicWord Record; | 102 typedef i::AtomicWord Record; |
103 typedef SamplingCircularQueue<Record, 12> TestSampleQueue; | 103 typedef SamplingCircularQueue<Record, 12> TestSampleQueue; |
104 | 104 |
105 class ProducerThread: public i::Thread { | 105 class ProducerThread: public i::Thread { |
106 public: | 106 public: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // does sampling is called in the context of different VM threads. | 141 // does sampling is called in the context of different VM threads. |
142 | 142 |
143 const int kRecordsPerChunk = 4; | 143 const int kRecordsPerChunk = 4; |
144 TestSampleQueue scq; | 144 TestSampleQueue scq; |
145 i::Semaphore semaphore(0); | 145 i::Semaphore semaphore(0); |
146 | 146 |
147 ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore); | 147 ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore); |
148 ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore); | 148 ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore); |
149 ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore); | 149 ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore); |
150 | 150 |
151 CHECK_EQ(NULL, scq.StartDequeue()); | 151 CHECK_EQ(NULL, scq.Peek()); |
152 producer1.Start(); | 152 producer1.Start(); |
153 semaphore.Wait(); | 153 semaphore.Wait(); |
154 for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) { | 154 for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) { |
155 Record* rec = reinterpret_cast<Record*>(scq.StartDequeue()); | 155 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
156 CHECK_NE(NULL, rec); | 156 CHECK_NE(NULL, rec); |
157 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 157 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
158 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 158 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
159 scq.FinishDequeue(); | 159 scq.Remove(); |
160 CHECK_NE(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 160 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
161 } | 161 } |
162 | 162 |
163 CHECK_EQ(NULL, scq.StartDequeue()); | 163 CHECK_EQ(NULL, scq.Peek()); |
164 producer2.Start(); | 164 producer2.Start(); |
165 semaphore.Wait(); | 165 semaphore.Wait(); |
166 for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) { | 166 for (Record i = 10; i < 10 + kRecordsPerChunk; ++i) { |
167 Record* rec = reinterpret_cast<Record*>(scq.StartDequeue()); | 167 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
168 CHECK_NE(NULL, rec); | 168 CHECK_NE(NULL, rec); |
169 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 169 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
170 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 170 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
171 scq.FinishDequeue(); | 171 scq.Remove(); |
172 CHECK_NE(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 172 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
173 } | 173 } |
174 | 174 |
175 CHECK_EQ(NULL, scq.StartDequeue()); | 175 CHECK_EQ(NULL, scq.Peek()); |
176 producer3.Start(); | 176 producer3.Start(); |
177 semaphore.Wait(); | 177 semaphore.Wait(); |
178 for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) { | 178 for (Record i = 20; i < 20 + kRecordsPerChunk; ++i) { |
179 Record* rec = reinterpret_cast<Record*>(scq.StartDequeue()); | 179 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
180 CHECK_NE(NULL, rec); | 180 CHECK_NE(NULL, rec); |
181 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 181 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
182 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 182 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
183 scq.FinishDequeue(); | 183 scq.Remove(); |
184 CHECK_NE(rec, reinterpret_cast<Record*>(scq.StartDequeue())); | 184 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
185 } | 185 } |
186 | 186 |
187 CHECK_EQ(NULL, scq.StartDequeue()); | 187 CHECK_EQ(NULL, scq.Peek()); |
188 } | 188 } |
OLD | NEW |