summaryrefslogtreecommitdiff
path: root/lib/xray
diff options
context:
space:
mode:
authorDean Michael Berris <dberris@google.com>2017-10-03 11:40:54 +0000
committerDean Michael Berris <dberris@google.com>2017-10-03 11:40:54 +0000
commit8f30a9c5f0810060699ae500140af885e3378b92 (patch)
tree9c1a9d22d3d44f918da99621716f9085026b27e6 /lib/xray
parentebffae6c671e3c5c914395212eea2ce3b5269312 (diff)
Revert "[XRay][compiler-rt] Use a hand-written circular buffer in BufferQueue"
This reverts r314766 (rL314766). Unit tests fail in multiple bots. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@314786 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/xray')
-rw-r--r--lib/xray/xray_buffer_queue.cc49
-rw-r--r--lib/xray/xray_buffer_queue.h23
2 files changed, 21 insertions, 51 deletions
diff --git a/lib/xray/xray_buffer_queue.cc b/lib/xray/xray_buffer_queue.cc
index 99175a025..7ba755ac3 100644
--- a/lib/xray/xray_buffer_queue.cc
+++ b/lib/xray/xray_buffer_queue.cc
@@ -16,7 +16,6 @@
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_libc.h"
-#include <algorithm>
#include <cstdlib>
#include <tuple>
@@ -24,21 +23,18 @@ using namespace __xray;
using namespace __sanitizer;
BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success)
- : BufferSize(B), Buffers(new std::tuple<Buffer, bool>[N]()),
- BufferCount(N), Finalizing{0}, OwnedBuffers(new void *[N]()),
- Next(Buffers.get()), First(nullptr) {
- for (size_t i = 0; i < N; ++i) {
- auto &T = Buffers[i];
+ : BufferSize(B), Buffers(N), Mutex(), OwnedBuffers(), Finalizing{0} {
+ for (auto &T : Buffers) {
void *Tmp = malloc(BufferSize);
if (Tmp == nullptr) {
Success = false;
return;
}
+
auto &Buf = std::get<0>(T);
- std::get<1>(T) = false;
Buf.Buffer = Tmp;
Buf.Size = B;
- OwnedBuffers[i] = Tmp;
+ OwnedBuffers.emplace(Tmp);
}
Success = true;
}
@@ -46,43 +42,27 @@ BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success)
BufferQueue::ErrorCode BufferQueue::getBuffer(Buffer &Buf) {
if (__sanitizer::atomic_load(&Finalizing, __sanitizer::memory_order_acquire))
return ErrorCode::QueueFinalizing;
- __sanitizer::SpinMutexLock Guard(&Mutex);
-
- if (Next == First)
+ __sanitizer::BlockingMutexLock Guard(&Mutex);
+ if (Buffers.empty())
return ErrorCode::NotEnoughMemory;
-
- auto &T = *Next;
+ auto &T = Buffers.front();
auto &B = std::get<0>(T);
Buf = B;
-
- if (First == nullptr)
- First = Next;
- ++Next;
- if (Next == (Buffers.get() + BufferCount))
- Next = Buffers.get();
-
+ B.Buffer = nullptr;
+ B.Size = 0;
+ Buffers.pop_front();
return ErrorCode::Ok;
}
BufferQueue::ErrorCode BufferQueue::releaseBuffer(Buffer &Buf) {
- // Blitz through the buffers array to find the buffer.
- if (std::none_of(OwnedBuffers.get(), OwnedBuffers.get() + BufferCount,
- [&Buf](void *P) { return P == Buf.Buffer; }))
+ if (OwnedBuffers.count(Buf.Buffer) == 0)
return ErrorCode::UnrecognizedBuffer;
- __sanitizer::SpinMutexLock Guard(&Mutex);
-
- // This points to a semantic bug, we really ought to not be releasing more
- // buffers than we actually get.
- if (First == nullptr || First == Next)
- return ErrorCode::NotEnoughMemory;
+ __sanitizer::BlockingMutexLock Guard(&Mutex);
// Now that the buffer has been released, we mark it as "used".
- *First = std::make_tuple(Buf, true);
+ Buffers.emplace(Buffers.end(), Buf, true /* used */);
Buf.Buffer = nullptr;
Buf.Size = 0;
- ++First;
- if (First == (Buffers.get() + BufferCount))
- First = Buffers.get();
return ErrorCode::Ok;
}
@@ -94,8 +74,7 @@ BufferQueue::ErrorCode BufferQueue::finalize() {
}
BufferQueue::~BufferQueue() {
- for (auto I = Buffers.get(), E = Buffers.get() + BufferCount; I != E; ++I) {
- auto &T = *I;
+ for (auto &T : Buffers) {
auto &Buf = std::get<0>(T);
free(Buf.Buffer);
}
diff --git a/lib/xray/xray_buffer_queue.h b/lib/xray/xray_buffer_queue.h
index 1115b4722..bd382a26c 100644
--- a/lib/xray/xray_buffer_queue.h
+++ b/lib/xray/xray_buffer_queue.h
@@ -17,8 +17,8 @@
#include "sanitizer_common/sanitizer_atomic.h"
#include "sanitizer_common/sanitizer_mutex.h"
-#include <cstdint>
-#include <memory>
+#include <deque>
+#include <unordered_set>
#include <utility>
namespace __xray {
@@ -36,23 +36,15 @@ public:
};
private:
- // Size of each individual Buffer.
size_t BufferSize;
// We use a bool to indicate whether the Buffer has been used in this
// freelist implementation.
- std::unique_ptr<std::tuple<Buffer, bool>[]> Buffers;
- size_t BufferCount;
-
- __sanitizer::SpinMutex Mutex;
+ std::deque<std::tuple<Buffer, bool>> Buffers;
+ __sanitizer::BlockingMutex Mutex;
+ std::unordered_set<void *> OwnedBuffers;
__sanitizer::atomic_uint8_t Finalizing;
- // Sorted buffer pointers, making it quick to find buffers that we own.
- std::unique_ptr<void *[]> OwnedBuffers;
-
- std::tuple<Buffer, bool> *Next;
- std::tuple<Buffer, bool> *First;
-
public:
enum class ErrorCode : unsigned {
Ok,
@@ -125,9 +117,8 @@ public:
/// Buffer is marked 'used' (i.e. has been the result of getBuffer(...) and a
/// releaseBuffer(...) operation).
template <class F> void apply(F Fn) {
- __sanitizer::SpinMutexLock G(&Mutex);
- for (auto I = Buffers.get(), E = Buffers.get() + BufferCount; I != E; ++I) {
- const auto &T = *I;
+ __sanitizer::BlockingMutexLock G(&Mutex);
+ for (const auto &T : Buffers) {
if (std::get<1>(T))
Fn(std::get<0>(T));
}