Line data Source code
1 : // Copyright 2010 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_PROFILER_CIRCULAR_QUEUE_H_
6 : #define V8_PROFILER_CIRCULAR_QUEUE_H_
7 :
8 : #include "src/base/atomicops.h"
9 : #include "src/globals.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 : // Lock-free cache-friendly sampling circular queue for large
15 : // records. Intended for fast transfer of large records between a
16 : // single producer and a single consumer. If the queue is full,
17 : // StartEnqueue will return nullptr. The queue is designed with
18 : // a goal in mind to evade cache lines thrashing by preventing
19 : // simultaneous reads and writes to adjanced memory locations.
20 : template<typename T, unsigned Length>
21 817 : class SamplingCircularQueue {
22 : public:
23 : // Executed on the application thread.
24 : SamplingCircularQueue();
25 : ~SamplingCircularQueue();
26 :
27 : // StartEnqueue returns a pointer to a memory location for storing the next
28 : // record or nullptr if all entries are full at the moment.
29 : T* StartEnqueue();
30 : // Notifies the queue that the producer has complete writing data into the
31 : // memory returned by StartEnqueue and it can be passed to the consumer.
32 : void FinishEnqueue();
33 :
34 : // Executed on the consumer (analyzer) thread.
35 : // Retrieves, but does not remove, the head of this queue, returning nullptr
36 : // if this queue is empty. After the record had been read by a consumer,
37 : // Remove must be called.
38 : T* Peek();
39 : void Remove();
40 :
41 : private:
42 : // Reserved values for the entry marker.
43 : enum {
44 : kEmpty, // Marks clean (processed) entries.
45 : kFull // Marks entries already filled by the producer but not yet
46 : // completely processed by the consumer.
47 : };
48 :
49 : struct alignas(PROCESSOR_CACHE_LINE_SIZE) Entry {
50 203892 : Entry() : marker(kEmpty) {}
51 : T record;
52 : base::Atomic32 marker;
53 : };
54 :
55 : Entry* Next(Entry* entry);
56 :
57 : Entry buffer_[Length];
58 : alignas(PROCESSOR_CACHE_LINE_SIZE) Entry* enqueue_pos_;
59 : alignas(PROCESSOR_CACHE_LINE_SIZE) Entry* dequeue_pos_;
60 :
61 : DISALLOW_COPY_AND_ASSIGN(SamplingCircularQueue);
62 : };
63 :
64 :
65 : } // namespace internal
66 : } // namespace v8
67 :
68 : #endif // V8_PROFILER_CIRCULAR_QUEUE_H_
|