diff options
| author | Dean Michael Berris <dberris@google.com> | 2016-12-06 06:24:08 +0000 |
|---|---|---|
| committer | Dean Michael Berris <dberris@google.com> | 2016-12-06 06:24:08 +0000 |
| commit | abe04e3295450afd1b9e03a1b91cf87ee3725780 (patch) | |
| tree | 898d4263d7e5b839ce2eb1559092b12231dc53a5 /compiler-rt/lib/xray/xray_buffer_queue.cc | |
| parent | 8b058aec1d9499015e4a3fd2e5a503273a5d384b (diff) | |
| download | bcm5719-llvm-abe04e3295450afd1b9e03a1b91cf87ee3725780.tar.gz bcm5719-llvm-abe04e3295450afd1b9e03a1b91cf87ee3725780.zip | |
[XRay][compiler-rt] XRay Buffer Queue
This implements a simple buffer queue to manage a pre-allocated queue of
fixed-sized buffers to hold XRay records. We need this to support
Flight Data Recorder (FDR) mode. We also implement this as a sub-library
first to allow for development before actually using it in an
implementation.
Some important properties of the buffer queue:
- Thread-safe enqueueing/dequeueing of fixed-size buffers.
- Pre-allocation of buffers at construction.
This is a re-roll of the previous attempt to submit, because it caused
failures in arm and aarch64.
Reviewers: majnemer, echristo, rSerge
Subscribers: tberghammer, danalbert, srhines, modocache, mehdi_amini, mgorny, llvm-commits
Differential Revision: https://reviews.llvm.org/D26232
llvm-svn: 288775
Diffstat (limited to 'compiler-rt/lib/xray/xray_buffer_queue.cc')
| -rw-r--r-- | compiler-rt/lib/xray/xray_buffer_queue.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/compiler-rt/lib/xray/xray_buffer_queue.cc b/compiler-rt/lib/xray/xray_buffer_queue.cc new file mode 100644 index 00000000000..17878eb85de --- /dev/null +++ b/compiler-rt/lib/xray/xray_buffer_queue.cc @@ -0,0 +1,65 @@ +//===-- xray_buffer_queue.cc -----------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file is a part of XRay, a dynamic runtime instruementation system. +// +// Defines the interface for a buffer queue implementation. +// +//===----------------------------------------------------------------------===// +#include "xray_buffer_queue.h" +#include <cassert> +#include <cstdlib> + +using namespace __xray; + +BufferQueue::BufferQueue(std::size_t B, std::size_t N) + : BufferSize(B), Buffers(N) { + for (auto &Buf : Buffers) { + void *Tmp = malloc(BufferSize); + Buf.Buffer = Tmp; + Buf.Size = B; + if (Tmp != 0) + OwnedBuffers.insert(Tmp); + } +} + +std::error_code BufferQueue::getBuffer(Buffer &Buf) { + if (Finalizing.load(std::memory_order_acquire)) + return std::make_error_code(std::errc::state_not_recoverable); + std::lock_guard<std::mutex> Guard(Mutex); + if (Buffers.empty()) + return std::make_error_code(std::errc::not_enough_memory); + Buf = Buffers.front(); + Buffers.pop_front(); + return {}; +} + +std::error_code BufferQueue::releaseBuffer(Buffer &Buf) { + if (OwnedBuffers.count(Buf.Buffer) == 0) + return std::make_error_code(std::errc::argument_out_of_domain); + std::lock_guard<std::mutex> Guard(Mutex); + Buffers.push_back(Buf); + Buf.Buffer = nullptr; + Buf.Size = BufferSize; + return {}; +} + +std::error_code BufferQueue::finalize() { + if (Finalizing.exchange(true, std::memory_order_acq_rel)) + return std::make_error_code(std::errc::state_not_recoverable); + return {}; +} + +BufferQueue::~BufferQueue() { + for (auto &Buf : Buffers) { + free(Buf.Buffer); + Buf.Buffer = nullptr; + Buf.Size = 0; + } +} |

