1 //===-- xray_buffer_queue.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
12 // Defines the interface for a buffer queue implementation.
14 //===----------------------------------------------------------------------===//
15 #ifndef XRAY_BUFFER_QUEUE_H
16 #define XRAY_BUFFER_QUEUE_H
18 #include "sanitizer_common/sanitizer_atomic.h"
19 #include "sanitizer_common/sanitizer_mutex.h"
21 #include <unordered_set>
26 /// BufferQueue implements a circular queue of fixed sized buffers (much like a
27 /// freelist) but is concerned mostly with making it really quick to initialise,
28 /// finalise, and get/return buffers to the queue. This is one key component of
29 /// the "flight data recorder" (FDR) mode to support ongoing XRay function call
34 void *Buffer = nullptr;
41 // We use a bool to indicate whether the Buffer has been used in this
42 // freelist implementation.
43 std::deque<std::tuple<Buffer, bool>> Buffers;
44 __sanitizer::BlockingMutex Mutex;
45 std::unordered_set<void *> OwnedBuffers;
46 __sanitizer::atomic_uint8_t Finalizing;
49 enum class ErrorCode : unsigned {
57 static const char *getErrorString(ErrorCode E) {
61 case ErrorCode::NotEnoughMemory:
62 return "no available buffers in the queue";
63 case ErrorCode::QueueFinalizing:
64 return "queue already finalizing";
65 case ErrorCode::UnrecognizedBuffer:
66 return "buffer being returned not owned by buffer queue";
67 case ErrorCode::AlreadyFinalized:
68 return "queue already finalized";
70 return "unknown error";
73 /// Initialise a queue of size |N| with buffers of size |B|. We report success
74 /// through |Success|.
75 BufferQueue(size_t B, size_t N, bool &Success);
77 /// Updates |Buf| to contain the pointer to an appropriate buffer. Returns an
78 /// error in case there are no available buffers to return when we will run
79 /// over the upper bound for the total buffers.
82 /// - BufferQueue is not finalising.
85 /// - std::errc::not_enough_memory on exceeding MaxSize.
86 /// - no error when we find a Buffer.
87 /// - std::errc::state_not_recoverable on finalising BufferQueue.
88 ErrorCode getBuffer(Buffer &Buf);
90 /// Updates |Buf| to point to nullptr, with size 0.
94 ErrorCode releaseBuffer(Buffer &Buf);
96 bool finalizing() const {
97 return __sanitizer::atomic_load(&Finalizing,
98 __sanitizer::memory_order_acquire);
101 /// Returns the configured size of the buffers in the buffer queue.
102 size_t ConfiguredBufferSize() const { return BufferSize; }
104 /// Sets the state of the BufferQueue to finalizing, which ensures that:
106 /// - All subsequent attempts to retrieve a Buffer will fail.
107 /// - All releaseBuffer operations will not fail.
109 /// After a call to finalize succeeds, all subsequent calls to finalize will
110 /// fail with std::errc::state_not_recoverable.
111 ErrorCode finalize();
113 /// Applies the provided function F to each Buffer in the queue, only if the
114 /// Buffer is marked 'used' (i.e. has been the result of getBuffer(...) and a
115 /// releaseBuffer(...) operation.
116 template <class F> void apply(F Fn) {
117 __sanitizer::BlockingMutexLock G(&Mutex);
118 for (const auto &T : Buffers) {
124 // Cleans up allocated buffers.
128 } // namespace __xray
130 #endif // XRAY_BUFFER_QUEUE_H