1 //===-- xray_buffer_queue.cc -----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a part of XRay, a dynamic runtime instruementation system. 11 // 12 // Defines the interface for a buffer queue implementation. 13 // 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" 19 #include <memory> 20 #include <sys/mman.h> 21 22 #ifndef MAP_NORESERVE 23 // no-op on NetBSD (at least), unsupported flag on FreeBSD 24 #define MAP_NORESERVE 0 25 #endif 26 27 using namespace __xray; 28 using namespace __sanitizer; 29 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. 37 // 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. 44 // 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); 50 } 51 52 template <class T> static void deallocRaw(T *ptr, size_t N) { 53 // TODO: Report errors? 54 if (ptr != nullptr) 55 internal_munmap(ptr, N); 56 } 57 58 template <class T> static T *initArray(size_t N) { 59 auto A = allocRaw<T>(N); 60 if (A != nullptr) 61 while (N > 0) 62 new (A + (--N)) T(); 63 return A; 64 } 65 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) { 71 Success = false; 72 return; 73 } 74 if (OwnedBuffers == nullptr) { 75 // Clean up the buffers we've already allocated. 76 for (auto B = Buffers, E = Buffers + BufferCount; B != E; ++B) 77 B->~BufferRep(); 78 deallocRaw(Buffers, N); 79 Success = false; 80 return; 81 }; 82 83 for (size_t i = 0; i < N; ++i) { 84 auto &T = Buffers[i]; 85 void *Tmp = allocRaw<char>(BufferSize); 86 if (Tmp == nullptr) { 87 Success = false; 88 return; 89 } 90 auto *Extents = allocRaw<BufferExtents>(1); 91 if (Extents == nullptr) { 92 Success = false; 93 return; 94 } 95 auto &Buf = T.Buff; 96 Buf.Data = Tmp; 97 Buf.Size = B; 98 Buf.Extents = Extents; 99 OwnedBuffers[i] = Tmp; 100 } 101 Success = true; 102 } 103 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; 110 111 auto &T = *Next; 112 auto &B = T.Buff; 113 Buf = B; 114 T.Used = true; 115 ++LiveBuffers; 116 117 if (++Next == (Buffers + BufferCount)) 118 Next = Buffers; 119 120 return ErrorCode::Ok; 121 } 122 123 BufferQueue::ErrorCode BufferQueue::releaseBuffer(Buffer &Buf) { 124 // Blitz through the buffers array to find the buffer. 125 bool Found = false; 126 for (auto I = OwnedBuffers, E = OwnedBuffers + BufferCount; I != E; ++I) { 127 if (*I == Buf.Data) { 128 Found = true; 129 break; 130 } 131 } 132 if (!Found) 133 return ErrorCode::UnrecognizedBuffer; 134 135 SpinMutexLock Guard(&Mutex); 136 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; 141 142 // Now that the buffer has been released, we mark it as "used". 143 First->Buff = Buf; 144 First->Used = true; 145 Buf.Data = nullptr; 146 Buf.Size = 0; 147 --LiveBuffers; 148 if (++First == (Buffers + BufferCount)) 149 First = Buffers; 150 151 return ErrorCode::Ok; 152 } 153 154 BufferQueue::ErrorCode BufferQueue::finalize() { 155 if (atomic_exchange(&Finalizing, 1, memory_order_acq_rel)) 156 return ErrorCode::QueueFinalizing; 157 return ErrorCode::Ok; 158 } 159 160 BufferQueue::~BufferQueue() { 161 for (auto I = Buffers, E = Buffers + BufferCount; I != E; ++I) { 162 auto &T = *I; 163 auto &Buf = T.Buff; 164 deallocRaw(Buf.Data, Buf.Size); 165 deallocRaw(Buf.Extents, 1); 166 } 167 for (auto B = Buffers, E = Buffers + BufferCount; B != E; ++B) 168 B->~BufferRep(); 169 deallocRaw(Buffers, BufferCount); 170 deallocRaw(OwnedBuffers, BufferCount); 171 } 172