Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/circular-queue.h

Issue 22849002: Rewrite SamplingCircularQueue (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Deleted circular-queue.cc Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 STATIC_ASSERT(kProcessorCacheLineSize == 64);
Benedikt Meurer 2013/08/13 14:29:42 We should probably turn that kProcessorCacheLineSi
yurys 2013/08/13 14:52:37 Done.
79 Cell* next_chunk_pos; 75 struct ALIGN_AT(64) Entry {
80 Cell* enqueue_pos; 76 Entry() : marker(kEmpty) {}
81 }; 77 T record;
82 struct ConsumerPosition { 78 Atomic32 marker ALIGN_AT(sizeof(Atomic32));
Benedikt Meurer 2013/08/13 14:29:42 The ALIGN_AT here is redundant, as the compiler wi
yurys 2013/08/13 14:52:37 Done. But why are they always aligned?
yurys 2013/08/13 15:28:53 Ok, I see, it should be just naturally aligned by
83 Cell* dequeue_chunk_pos;
84 Cell* dequeue_chunk_poll_pos;
85 Cell* dequeue_pos;
86 Cell* dequeue_end_pos;
87 }; 79 };
88 80
89 INLINE(void WrapPositionIfNeeded(Cell** pos)); 81 Entry* Next(Entry* entry);
90 82
91 const size_t record_size_; 83 Entry buffer_[Length];
92 const size_t chunk_size_in_bytes_; 84 Entry* enqueue_pos_ ALIGN_AT(64);
93 const size_t chunk_size_; 85 Entry* dequeue_pos_ ALIGN_AT(64);
94 const size_t buffer_size_;
95 Cell* buffer_;
96 byte* positions_;
97 ProducerPosition* producer_pos_;
98 ConsumerPosition* consumer_pos_;
99 86
100 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); 87 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue);
101 }; 88 };
102 89
103 90
104 } } // namespace v8::internal 91 } } // namespace v8::internal
105 92
106 #endif // V8_CIRCULAR_QUEUE_H_ 93 #endif // V8_CIRCULAR_QUEUE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698