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 27 matching lines...) Expand all Loading... |
38 // previous unread records are overwritten. The queue is designed with | 38 // previous unread records are overwritten. 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 // | 41 // |
42 // IMPORTANT: as a producer never checks for chunks cleanness, it is | 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 | 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. | 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(int record_size_in_bytes, | 48 SamplingCircularQueue(size_t record_size_in_bytes, |
49 int desired_chunk_size_in_bytes, | 49 size_t desired_chunk_size_in_bytes, |
50 int buffer_size_in_chunks); | 50 int buffer_size_in_chunks); |
51 ~SamplingCircularQueue(); | 51 ~SamplingCircularQueue(); |
52 | 52 |
53 // Enqueue returns a pointer to a memory location for storing the next | 53 // Enqueue returns a pointer to a memory location for storing the next |
54 // record. | 54 // record. |
55 INLINE(void* Enqueue()); | 55 INLINE(void* Enqueue()); |
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 void* StartDequeue(); |
63 void FinishDequeue(); | 63 void FinishDequeue(); |
64 // Due to a presence of slipping between the producer and the consumer, | 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 | 65 // the queue must be notified whether producing has been finished in order |
66 // to process remaining records from the buffer. | 66 // to process remaining records from the buffer. |
67 void FlushResidualRecords(); | 67 void FlushResidualRecords(); |
68 | 68 |
69 typedef AtomicWord Cell; | 69 typedef AtomicWord Cell; |
70 // Reserved values for the first cell of a record. | |
71 static const Cell kClear = 0; // Marks clean (processed) chunks. | |
72 static const Cell kEnd = -1; // Marks the end of the buffer. | |
73 | 70 |
74 private: | 71 private: |
| 72 // Reserved values for the chunk marker (first Cell in each chunk). |
| 73 enum { |
| 74 kClear, // Marks clean (processed) chunks. |
| 75 kEnqueueStarted // Marks chunks where enqueue started. |
| 76 }; |
| 77 |
75 struct ProducerPosition { | 78 struct ProducerPosition { |
| 79 Cell* next_chunk_pos; |
76 Cell* enqueue_pos; | 80 Cell* enqueue_pos; |
77 }; | 81 }; |
78 struct ConsumerPosition { | 82 struct ConsumerPosition { |
79 Cell* dequeue_chunk_pos; | 83 Cell* dequeue_chunk_pos; |
80 Cell* dequeue_chunk_poll_pos; | 84 Cell* dequeue_chunk_poll_pos; |
81 Cell* dequeue_pos; | 85 Cell* dequeue_pos; |
82 Cell* dequeue_end_pos; | 86 Cell* dequeue_end_pos; |
83 }; | 87 }; |
84 | 88 |
85 INLINE(void WrapPositionIfNeeded(Cell** pos)); | 89 INLINE(void WrapPositionIfNeeded(Cell** pos)); |
86 | 90 |
87 const int record_size_; | 91 const int record_size_; |
88 const int chunk_size_in_bytes_; | 92 const size_t chunk_size_in_bytes_; |
89 const int chunk_size_; | 93 const int chunk_size_; |
90 const int buffer_size_; | 94 const int buffer_size_; |
91 const int producer_consumer_distance_; | |
92 Cell* buffer_; | 95 Cell* buffer_; |
93 byte* positions_; | 96 byte* positions_; |
94 ProducerPosition* producer_pos_; | 97 ProducerPosition* producer_pos_; |
95 ConsumerPosition* consumer_pos_; | 98 ConsumerPosition* consumer_pos_; |
96 | 99 |
97 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); | 100 DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue); |
98 }; | 101 }; |
99 | 102 |
100 | 103 |
101 } } // namespace v8::internal | 104 } } // namespace v8::internal |
102 | 105 |
103 #endif // V8_CIRCULAR_QUEUE_H_ | 106 #endif // V8_CIRCULAR_QUEUE_H_ |
OLD | NEW |