| OLD | NEW |
| (Empty) |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | |
| 2 // 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. | |
| 27 | |
| 28 #ifndef V8_CIRCULAR_QUEUE_H_ | |
| 29 #define V8_CIRCULAR_QUEUE_H_ | |
| 30 | |
| 31 namespace v8 { | |
| 32 namespace internal { | |
| 33 | |
| 34 | |
| 35 // Lock-based blocking circular queue for small records. Intended for | |
| 36 // transfer of small records between a single producer and a single | |
| 37 // consumer. Blocks on enqueue operation if the queue is full. | |
| 38 template<typename Record> | |
| 39 class CircularQueue { | |
| 40 public: | |
| 41 inline explicit CircularQueue(int desired_buffer_size_in_bytes); | |
| 42 inline ~CircularQueue(); | |
| 43 | |
| 44 INLINE(void Dequeue(Record* rec)); | |
| 45 INLINE(void Enqueue(const Record& rec)); | |
| 46 INLINE(bool IsEmpty()) { return enqueue_pos_ == dequeue_pos_; } | |
| 47 | |
| 48 private: | |
| 49 INLINE(Record* Next(Record* curr)); | |
| 50 | |
| 51 Record* buffer_; | |
| 52 Record* const buffer_end_; | |
| 53 Semaphore* enqueue_semaphore_; | |
| 54 Record* enqueue_pos_; | |
| 55 Record* dequeue_pos_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(CircularQueue); | |
| 58 }; | |
| 59 | |
| 60 | |
| 61 // Lock-free cache-friendly sampling circular queue for large | |
| 62 // records. Intended for fast transfer of large records between a | |
| 63 // single producer and a single consumer. If the queue is full, | |
| 64 // previous unread records are overwritten. The queue is designed with | |
| 65 // a goal in mind to evade cache lines thrashing by preventing | |
| 66 // simultaneous reads and writes to adjanced memory locations. | |
| 67 // | |
| 68 // IMPORTANT: as a producer never checks for chunks cleanness, it is | |
| 69 // possible that it can catch up and overwrite a chunk that a consumer | |
| 70 // is currently reading, resulting in a corrupt record being read. | |
| 71 class SamplingCircularQueue { | |
| 72 public: | |
| 73 // Executed on the application thread. | |
| 74 SamplingCircularQueue(int record_size_in_bytes, | |
| 75 int desired_chunk_size_in_bytes, | |
| 76 int buffer_size_in_chunks); | |
| 77 ~SamplingCircularQueue(); | |
| 78 | |
| 79 // Executed on the producer (sampler) or application thread. | |
| 80 void SetUpProducer(); | |
| 81 // Enqueue returns a pointer to a memory location for storing the next | |
| 82 // record. | |
| 83 INLINE(void* Enqueue()); | |
| 84 void TearDownProducer(); | |
| 85 | |
| 86 // Executed on the consumer (analyzer) thread. | |
| 87 void SetUpConsumer(); | |
| 88 // StartDequeue returns a pointer to a memory location for retrieving | |
| 89 // the next record. After the record had been read by a consumer, | |
| 90 // FinishDequeue must be called. Until that moment, subsequent calls | |
| 91 // to StartDequeue will return the same pointer. | |
| 92 void* StartDequeue(); | |
| 93 void FinishDequeue(); | |
| 94 // Due to a presence of slipping between the producer and the consumer, | |
| 95 // the queue must be notified whether producing has been finished in order | |
| 96 // to process remaining records from the buffer. | |
| 97 void FlushResidualRecords(); | |
| 98 void TearDownConsumer(); | |
| 99 | |
| 100 typedef AtomicWord Cell; | |
| 101 // Reserved values for the first cell of a record. | |
| 102 static const Cell kClear = 0; // Marks clean (processed) chunks. | |
| 103 static const Cell kEnd = -1; // Marks the end of the buffer. | |
| 104 | |
| 105 private: | |
| 106 struct ConsumerPosition { | |
| 107 Cell* dequeue_chunk_pos; | |
| 108 Cell* dequeue_chunk_poll_pos; | |
| 109 Cell* dequeue_pos; | |
| 110 Cell* dequeue_end_pos; | |
| 111 }; | |
| 112 | |
| 113 INLINE(void WrapPositionIfNeeded(Cell** pos)); | |
| 114 | |
| 115 const int record_size_; | |
| 116 const int chunk_size_in_bytes_; | |
| 117 const int chunk_size_; | |
| 118 const int buffer_size_; | |
| 119 const int producer_consumer_distance_; | |
| 120 Cell* buffer_; | |
| 121 // Store producer and consumer data in TLS to avoid modifying the | |
| 122 // same CPU cache line from two threads simultaneously. | |
| 123 Thread::LocalStorageKey consumer_key_; | |
| 124 Thread::LocalStorageKey producer_key_; | |
| 125 }; | |
| 126 | |
| 127 | |
| 128 } } // namespace v8::internal | |
| 129 | |
| 130 #endif // V8_CIRCULAR_QUEUE_H_ | |
| OLD | NEW |