| 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 "allocation.h" |
| 32 #include "v8globals.h" |
| 33 |
| 31 namespace v8 { | 34 namespace v8 { |
| 32 namespace internal { | 35 namespace internal { |
| 33 | 36 |
| 34 | 37 |
| 35 // Lock-free cache-friendly sampling circular queue for large | 38 // Lock-free cache-friendly sampling circular queue for large |
| 36 // records. Intended for fast transfer of large records between a | 39 // records. Intended for fast transfer of large records between a |
| 37 // single producer and a single consumer. If the queue is full, | 40 // single producer and a single consumer. If the queue is full, |
| 38 // previous unread records are overwritten. The queue is designed with | 41 // StartEnqueue will return NULL. The queue is designed with |
| 39 // a goal in mind to evade cache lines thrashing by preventing | 42 // a goal in mind to evade cache lines thrashing by preventing |
| 40 // simultaneous reads and writes to adjanced memory locations. | 43 // simultaneous reads and writes to adjanced memory locations. |
| 41 // | 44 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 { | 45 class SamplingCircularQueue { |
| 46 public: | 46 public: |
| 47 // Executed on the application thread. | 47 // Executed on the application thread. |
| 48 SamplingCircularQueue(size_t record_size_in_bytes, | 48 SamplingCircularQueue(); |
| 49 size_t desired_chunk_size_in_bytes, | |
| 50 unsigned buffer_size_in_chunks); | |
| 51 ~SamplingCircularQueue(); | 49 ~SamplingCircularQueue(); |
| 52 | 50 |
| 53 // Enqueue returns a pointer to a memory location for storing the next | 51 // StartEnqueue returns a pointer to a memory location for storing the next |
| 54 // record. | 52 // record or NULL if all entries are full at the moment. |
| 55 INLINE(void* Enqueue()); | 53 T* StartEnqueue(); |
| 54 // Notifies the queue that the producer has complete writing data into the |
| 55 // memory returned by StartEnqueue and it can be passed to the consumer. |
| 56 void FinishEnqueue(); |
| 56 | 57 |
| 57 // Executed on the consumer (analyzer) thread. | 58 // Executed on the consumer (analyzer) thread. |
| 58 // StartDequeue returns a pointer to a memory location for retrieving | 59 // StartDequeue returns a pointer to a memory location for retrieving |
| 59 // the next record. After the record had been read by a consumer, | 60 // the next record. After the record had been read by a consumer, |
| 60 // FinishDequeue must be called. Until that moment, subsequent calls | 61 // FinishDequeue must be called. Until that moment, subsequent calls |
| 61 // to StartDequeue will return the same pointer. | 62 // to StartDequeue will return the same pointer. |
| 62 void* StartDequeue(); | 63 T* StartDequeue(); |
| 63 void FinishDequeue(); | 64 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 | 65 |
| 71 private: | 66 private: |
| 72 // Reserved values for the chunk marker (first Cell in each chunk). | 67 // Reserved values for the entry marker. |
| 73 enum { | 68 enum { |
| 74 kClear, // Marks clean (processed) chunks. | 69 kEmpty, // Marks clean (processed) entries. |
| 75 kEnqueueStarted // Marks chunks where enqueue started. | 70 kFull // Marks entries already filled by the producer but not yet |
| 71 // completely processed by the consumer. |
| 76 }; | 72 }; |
| 77 | 73 |
| 78 struct ProducerPosition { | 74 struct ALIGN_AT(PROCESSOR_CACHE_LINE_SIZE) Entry { |
| 79 Cell* next_chunk_pos; | 75 Entry() : marker(kEmpty) {} |
| 80 Cell* enqueue_pos; | 76 T record; |
| 81 }; | 77 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 }; | 78 }; |
| 88 | 79 |
| 89 INLINE(void WrapPositionIfNeeded(Cell** pos)); | 80 Entry* Next(Entry* entry); |
| 90 | 81 |
| 91 const size_t record_size_; | 82 Entry buffer_[Length]; |
| 92 const size_t chunk_size_in_bytes_; | 83 Entry* enqueue_pos_ ALIGN_AT(PROCESSOR_CACHE_LINE_SIZE); |
| 93 const size_t chunk_size_; | 84 Entry* dequeue_pos_ ALIGN_AT(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 | 85 |
| 100 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); | 86 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); |
| 101 }; | 87 }; |
| 102 | 88 |
| 103 | 89 |
| 104 } } // namespace v8::internal | 90 } } // namespace v8::internal |
| 105 | 91 |
| 106 #endif // V8_CIRCULAR_QUEUE_H_ | 92 #endif // V8_CIRCULAR_QUEUE_H_ |
| OLD | NEW |