1 //===-- xray_buffer_queue.cc -----------------------------------*- 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 instruementation system.
12 // Defines the interface for a buffer queue implementation.
14 //===----------------------------------------------------------------------===//
15 #include "xray_buffer_queue.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "sanitizer_common/sanitizer_posix.h"
23 // no-op on NetBSD (at least), unsupported flag on FreeBSD
24 #define MAP_NORESERVE 0
27 using namespace __xray;
28 using namespace __sanitizer;
30 template <class T> static T *allocRaw(size_t N) {
31 // TODO: Report errors?
32 // We use MAP_NORESERVE on platforms where it's supported to ensure that the
33 // pages we're allocating for XRay never end up in pages that can be swapped
34 // in/out. We're doing this because for FDR mode, we want to ensure that
35 // writes to the buffers stay resident in memory to prevent XRay itself from
36 // causing swapping/thrashing.
38 // In the case when XRay pages cannot be swapped in/out or there's not enough
39 // RAM to back these pages, we're willing to cause a segmentation fault
40 // instead of introducing latency in the measurement. We assume here that
41 // there are enough pages that are swappable in/out outside of the buffers
42 // being used by FDR mode (which are bounded and configurable anyway) to allow
43 // us to keep using always-resident memory.
45 // TODO: Make this configurable?
46 void *A = reinterpret_cast<void *>(
47 internal_mmap(NULL, N * sizeof(T), PROT_WRITE | PROT_READ,
48 MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0));
49 return (A == MAP_FAILED) ? nullptr : reinterpret_cast<T *>(A);
52 template <class T> static void deallocRaw(T *ptr, size_t N) {
53 // TODO: Report errors?
55 internal_munmap(ptr, N);
58 template <class T> static T *initArray(size_t N) {
59 auto A = allocRaw<T>(N);
66 BufferQueue::BufferQueue(size_t B, size_t N, bool &Success)
67 : BufferSize(B), Buffers(initArray<BufferQueue::BufferRep>(N)),
68 BufferCount(N), Finalizing{0}, OwnedBuffers(initArray<void *>(N)),
69 Next(Buffers), First(Buffers), LiveBuffers(0) {
70 if (Buffers == nullptr) {
74 if (OwnedBuffers == nullptr) {
75 // Clean up the buffers we've already allocated.
76 for (auto B = Buffers, E = Buffers + BufferCount; B != E; ++B)
78 deallocRaw(Buffers, N);
83 for (size_t i = 0; i < N; ++i) {
85 void *Tmp = allocRaw<char>(BufferSize);
90 auto *Extents = allocRaw<BufferExtents>(1);
91 if (Extents == nullptr) {
98 Buf.Extents = Extents;
99 OwnedBuffers[i] = Tmp;
104 BufferQueue::ErrorCode BufferQueue::getBuffer(Buffer &Buf) {
105 if (atomic_load(&Finalizing, memory_order_acquire))
106 return ErrorCode::QueueFinalizing;
107 SpinMutexLock Guard(&Mutex);
108 if (LiveBuffers == BufferCount)
109 return ErrorCode::NotEnoughMemory;
117 if (++Next == (Buffers + BufferCount))
120 return ErrorCode::Ok;
123 BufferQueue::ErrorCode BufferQueue::releaseBuffer(Buffer &Buf) {
124 // Blitz through the buffers array to find the buffer.
126 for (auto I = OwnedBuffers, E = OwnedBuffers + BufferCount; I != E; ++I) {
127 if (*I == Buf.Data) {
133 return ErrorCode::UnrecognizedBuffer;
135 SpinMutexLock Guard(&Mutex);
137 // This points to a semantic bug, we really ought to not be releasing more
138 // buffers than we actually get.
139 if (LiveBuffers == 0)
140 return ErrorCode::NotEnoughMemory;
142 // Now that the buffer has been released, we mark it as "used".
148 if (++First == (Buffers + BufferCount))
151 return ErrorCode::Ok;
154 BufferQueue::ErrorCode BufferQueue::finalize() {
155 if (atomic_exchange(&Finalizing, 1, memory_order_acq_rel))
156 return ErrorCode::QueueFinalizing;
157 return ErrorCode::Ok;
160 BufferQueue::~BufferQueue() {
161 for (auto I = Buffers, E = Buffers + BufferCount; I != E; ++I) {
164 deallocRaw(Buf.Data, Buf.Size);
165 deallocRaw(Buf.Extents, 1);
167 for (auto B = Buffers, E = Buffers + BufferCount; B != E; ++B)
169 deallocRaw(Buffers, BufferCount);
170 deallocRaw(OwnedBuffers, BufferCount);