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 17 matching lines...) Expand all Loading... | |
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 namespace v8 { | 31 namespace v8 { |
32 namespace internal { | 32 namespace internal { |
33 | 33 |
34 | 34 |
35 // Lock-free cache-friendly sampling circular queue for large | 35 // Lock-free cache-friendly sampling circular queue for large |
36 // records. Intended for fast transfer of large records between a | 36 // records. Intended for fast transfer of large records between a |
37 // single producer and a single consumer. If the queue is full, | 37 // single producer and a single consumer. If the queue is full, |
38 // previous unread records are overwritten. The queue is designed with | 38 // StartEnqueue will return NULL. The queue is designed with |
39 // a goal in mind to evade cache lines thrashing by preventing | 39 // a goal in mind to evade cache lines thrashing by preventing |
40 // simultaneous reads and writes to adjanced memory locations. | 40 // simultaneous reads and writes to adjanced memory locations. |
41 // | |
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 { | 41 class SamplingCircularQueue { |
Benedikt Meurer
2013/08/13 09:31:32
While we're already cleaning up this class, let's
yurys
2013/08/13 14:10:29
Done.
| |
46 public: | 42 public: |
47 // Executed on the application thread. | 43 // Executed on the application thread. |
48 SamplingCircularQueue(size_t record_size_in_bytes, | 44 SamplingCircularQueue(size_t record_size_in_bytes, |
49 size_t desired_chunk_size_in_bytes, | 45 unsigned buffer_size_in_records); |
50 unsigned buffer_size_in_chunks); | |
51 ~SamplingCircularQueue(); | 46 ~SamplingCircularQueue(); |
52 | 47 |
53 // Enqueue returns a pointer to a memory location for storing the next | 48 // StartEnqueue returns a pointer to a memory location for storing the next |
54 // record. | 49 // record or NULL if all entries are full at the moment. |
55 INLINE(void* Enqueue()); | 50 INLINE(void* StartEnqueue()); |
51 // Notifies the queue that the producer has complete writing data into the | |
52 // memory returned by StartEnqueue and it can be passed to the consumer. | |
53 INLINE(void FinishEnqueue()); | |
56 | 54 |
57 // Executed on the consumer (analyzer) thread. | 55 // Executed on the consumer (analyzer) thread. |
58 // StartDequeue returns a pointer to a memory location for retrieving | 56 // StartDequeue returns a pointer to a memory location for retrieving |
59 // the next record. After the record had been read by a consumer, | 57 // the next record. After the record had been read by a consumer, |
60 // FinishDequeue must be called. Until that moment, subsequent calls | 58 // FinishDequeue must be called. Until that moment, subsequent calls |
61 // to StartDequeue will return the same pointer. | 59 // to StartDequeue will return the same pointer. |
62 void* StartDequeue(); | 60 void* StartDequeue(); |
63 void FinishDequeue(); | 61 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 | 62 |
69 typedef AtomicWord Cell; | 63 typedef AtomicWord Cell; |
Benedikt Meurer
2013/08/13 09:31:32
Get rid of this typedef, it's just confusing. Also
yurys
2013/08/13 14:10:29
Done.
| |
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 (first Cell in each entry). |
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 ProducerPosition { |
79 Cell* next_chunk_pos; | |
80 Cell* enqueue_pos; | 74 Cell* enqueue_pos; |
81 }; | 75 }; |
82 struct ConsumerPosition { | 76 struct ConsumerPosition { |
83 Cell* dequeue_chunk_pos; | |
84 Cell* dequeue_chunk_poll_pos; | |
85 Cell* dequeue_pos; | 77 Cell* dequeue_pos; |
86 Cell* dequeue_end_pos; | |
87 }; | 78 }; |
88 | 79 |
89 INLINE(void WrapPositionIfNeeded(Cell** pos)); | 80 inline void WrapPositionIfNeeded(Cell** pos); |
90 | 81 |
91 const size_t record_size_; | 82 const size_t entry_size_; |
92 const size_t chunk_size_in_bytes_; | |
93 const size_t chunk_size_; | |
94 const size_t buffer_size_; | 83 const size_t buffer_size_; |
84 Cell* not_aligned_buffer_; | |
95 Cell* buffer_; | 85 Cell* buffer_; |
96 byte* positions_; | 86 byte* positions_; |
97 ProducerPosition* producer_pos_; | 87 ProducerPosition* producer_pos_; |
98 ConsumerPosition* consumer_pos_; | 88 ConsumerPosition* consumer_pos_; |
Benedikt Meurer
2013/08/13 09:31:32
Let's also cleanup this mess. Assuming that the re
yurys
2013/08/13 14:10:29
Done.
| |
99 | 89 |
100 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); | 90 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); |
101 }; | 91 }; |
102 | 92 |
103 | 93 |
104 } } // namespace v8::internal | 94 } } // namespace v8::internal |
105 | 95 |
106 #endif // V8_CIRCULAR_QUEUE_H_ | 96 #endif // V8_CIRCULAR_QUEUE_H_ |
OLD | NEW |