summaryrefslogtreecommitdiff
path: root/lib/xray/xray_buffer_queue.cc
blob: 61a0add0eadbb5bd2d0365ebe609588a2bc64dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===-- 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 "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_libc.h"

#include <algorithm>
#include <cstdlib>
#include <tuple>

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(Buffers.get()), LiveBuffers(0) {
  for (size_t i = 0; i < N; ++i) {
    auto &T = Buffers[i];
    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;
  }
  Success = true;
}

BufferQueue::ErrorCode BufferQueue::getBuffer(Buffer &Buf) {
  if (__sanitizer::atomic_load(&Finalizing, __sanitizer::memory_order_acquire))
    return ErrorCode::QueueFinalizing;
  __sanitizer::SpinMutexLock Guard(&Mutex);
  if (LiveBuffers == BufferCount)
    return ErrorCode::NotEnoughMemory;

  auto &T = *Next;
  auto &B = std::get<0>(T);
  Buf = B;
  ++LiveBuffers;

  if (++Next == (Buffers.get() + BufferCount))
    Next = Buffers.get();

  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; }))
    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 (LiveBuffers == 0)
    return ErrorCode::NotEnoughMemory;

  // Now that the buffer has been released, we mark it as "used".
  *First = std::make_tuple(Buf, true);
  Buf.Buffer = nullptr;
  Buf.Size = 0;
  --LiveBuffers;
  if (++First == (Buffers.get() + BufferCount))
    First = Buffers.get();

  return ErrorCode::Ok;
}

BufferQueue::ErrorCode BufferQueue::finalize() {
  if (__sanitizer::atomic_exchange(&Finalizing, 1,
                                   __sanitizer::memory_order_acq_rel))
    return ErrorCode::QueueFinalizing;
  return ErrorCode::Ok;
}

BufferQueue::~BufferQueue() {
  for (auto I = Buffers.get(), E = Buffers.get() + BufferCount; I != E; ++I) {
    auto &T = *I;
    auto &Buf = std::get<0>(T);
    free(Buf.Buffer);
  }
}