| 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 10 matching lines...) Expand all Loading... |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef V8_CIRCULAR_QUEUE_H_ | 28 #ifndef V8_CIRCULAR_QUEUE_H_ |
| 29 #define V8_CIRCULAR_QUEUE_H_ | 29 #define V8_CIRCULAR_QUEUE_H_ |
| 30 | 30 |
| 31 #include "v8globals.h" |
| 32 |
| 31 namespace v8 { | 33 namespace v8 { |
| 32 namespace internal { | 34 namespace internal { |
| 33 | 35 |
| 34 | 36 |
| 35 // Lock-free cache-friendly sampling circular queue for large | 37 // Lock-free cache-friendly sampling circular queue for large |
| 36 // records. Intended for fast transfer of large records between a | 38 // records. Intended for fast transfer of large records between a |
| 37 // single producer and a single consumer. If the queue is full, | 39 // single producer and a single consumer. If the queue is full, |
| 38 // previous unread records are overwritten. The queue is designed with | 40 // StartEnqueue will return NULL. The queue is designed with |
| 39 // a goal in mind to evade cache lines thrashing by preventing | 41 // a goal in mind to evade cache lines thrashing by preventing |
| 40 // simultaneous reads and writes to adjanced memory locations. | 42 // simultaneous reads and writes to adjanced memory locations. |
| 41 // | 43 template<typename T, unsigned Length> |
| 42 // IMPORTANT: as a producer never checks for chunks cleanness, it is | |
| 43 // possible that it can catch up and overwrite a chunk that a consumer | |
| 44 // is currently reading, resulting in a corrupt record being read. | |
| 45 class SamplingCircularQueue { | 44 class SamplingCircularQueue { |
| 46 public: | 45 public: |
| 47 // Executed on the application thread. | 46 // Executed on the application thread. |
| 48 SamplingCircularQueue(size_t record_size_in_bytes, | 47 SamplingCircularQueue(); |
| 49 size_t desired_chunk_size_in_bytes, | |
| 50 unsigned buffer_size_in_chunks); | |
| 51 ~SamplingCircularQueue(); | 48 ~SamplingCircularQueue(); |
| 52 | 49 |
| 53 // Enqueue returns a pointer to a memory location for storing the next | 50 // StartEnqueue returns a pointer to a memory location for storing the next |
| 54 // record. | 51 // record or NULL if all entries are full at the moment. |
| 55 INLINE(void* Enqueue()); | 52 T* StartEnqueue(); |
| 53 // Notifies the queue that the producer has complete writing data into the |
| 54 // memory returned by StartEnqueue and it can be passed to the consumer. |
| 55 void FinishEnqueue(); |
| 56 | 56 |
| 57 // Executed on the consumer (analyzer) thread. | 57 // Executed on the consumer (analyzer) thread. |
| 58 // StartDequeue returns a pointer to a memory location for retrieving | 58 // StartDequeue returns a pointer to a memory location for retrieving |
| 59 // the next record. After the record had been read by a consumer, | 59 // the next record. After the record had been read by a consumer, |
| 60 // FinishDequeue must be called. Until that moment, subsequent calls | 60 // FinishDequeue must be called. Until that moment, subsequent calls |
| 61 // to StartDequeue will return the same pointer. | 61 // to StartDequeue will return the same pointer. |
| 62 void* StartDequeue(); | 62 T* StartDequeue(); |
| 63 void FinishDequeue(); | 63 void FinishDequeue(); |
| 64 // Due to a presence of slipping between the producer and the consumer, | |
| 65 // the queue must be notified whether producing has been finished in order | |
| 66 // to process remaining records from the buffer. | |
| 67 void FlushResidualRecords(); | |
| 68 | |
| 69 typedef AtomicWord Cell; | |
| 70 | 64 |
| 71 private: | 65 private: |
| 72 // Reserved values for the chunk marker (first Cell in each chunk). | 66 // Reserved values for the entry marker. |
| 73 enum { | 67 enum { |
| 74 kClear, // Marks clean (processed) chunks. | 68 kEmpty, // Marks clean (processed) entries. |
| 75 kEnqueueStarted // Marks chunks where enqueue started. | 69 kFull // Marks entries already filled by the producer but not yet |
| 70 // completely processed by the consumer. |
| 76 }; | 71 }; |
| 77 | 72 |
| 78 struct ProducerPosition { | 73 struct V8_ALIGNAS(PROCESSOR_CACHE_LINE_SIZE) Entry { |
| 79 Cell* next_chunk_pos; | 74 Entry() : marker(kEmpty) {} |
| 80 Cell* enqueue_pos; | 75 T record; |
| 81 }; | 76 Atomic32 marker; |
| 82 struct ConsumerPosition { | |
| 83 Cell* dequeue_chunk_pos; | |
| 84 Cell* dequeue_chunk_poll_pos; | |
| 85 Cell* dequeue_pos; | |
| 86 Cell* dequeue_end_pos; | |
| 87 }; | 77 }; |
| 88 | 78 |
| 89 INLINE(void WrapPositionIfNeeded(Cell** pos)); | 79 Entry* Next(Entry* entry); |
| 90 | 80 |
| 91 const size_t record_size_; | 81 Entry buffer_[Length]; |
| 92 const size_t chunk_size_in_bytes_; | 82 Entry* enqueue_pos_ V8_ALIGNAS(PROCESSOR_CACHE_LINE_SIZE); |
| 93 const size_t chunk_size_; | 83 Entry* dequeue_pos_ V8_ALIGNAS(PROCESSOR_CACHE_LINE_SIZE); |
| 94 const size_t buffer_size_; | |
| 95 Cell* buffer_; | |
| 96 byte* positions_; | |
| 97 ProducerPosition* producer_pos_; | |
| 98 ConsumerPosition* consumer_pos_; | |
| 99 | 84 |
| 100 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); | 85 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); |
| 101 }; | 86 }; |
| 102 | 87 |
| 103 | 88 |
| 104 } } // namespace v8::internal | 89 } } // namespace v8::internal |
| 105 | 90 |
| 106 #endif // V8_CIRCULAR_QUEUE_H_ | 91 #endif // V8_CIRCULAR_QUEUE_H_ |
| OLD | NEW |