diff options
author | Dean Michael Berris <dberris@google.com> | 2018-10-22 04:53:58 +0000 |
---|---|---|
committer | Dean Michael Berris <dberris@google.com> | 2018-10-22 04:53:58 +0000 |
commit | 788b17ca78e25f7eb079ce7836ce45c1dde3bb67 (patch) | |
tree | b2d4d0db309b675e243c441b58a1fc822f3da7f7 /compiler-rt/lib/xray/tests | |
parent | ebfbf89000f7b698e502dcb8a8a5d8bd2ec2742f (diff) | |
download | bcm5719-llvm-788b17ca78e25f7eb079ce7836ce45c1dde3bb67.tar.gz bcm5719-llvm-788b17ca78e25f7eb079ce7836ce45c1dde3bb67.zip |
[XRay][compiler-rt] Generational Buffer Management
Summary:
This change updates the buffer queue implementation to support using a
generation number to identify the lifetime of buffers. This first part
introduces the notion of the generation number, without changing the way
we handle the buffers yet.
What's missing here is the cleanup of the buffers. Ideally we'll keep
the two most recent generations. We need to ensure that before we do any
writes to the buffers, that we check the generation number(s) first.
Those changes will follow-on from this change.
Depends on D52588.
Reviewers: mboerger, eizan
Subscribers: llvm-commits, jfb
Differential Revision: https://reviews.llvm.org/D52974
llvm-svn: 344881
Diffstat (limited to 'compiler-rt/lib/xray/tests')
-rw-r--r-- | compiler-rt/lib/xray/tests/unit/buffer_queue_test.cc | 116 |
1 files changed, 114 insertions, 2 deletions
diff --git a/compiler-rt/lib/xray/tests/unit/buffer_queue_test.cc b/compiler-rt/lib/xray/tests/unit/buffer_queue_test.cc index c0d4ccb268d..8aa366a20df 100644 --- a/compiler-rt/lib/xray/tests/unit/buffer_queue_test.cc +++ b/compiler-rt/lib/xray/tests/unit/buffer_queue_test.cc @@ -13,7 +13,9 @@ #include "xray_buffer_queue.h" #include "gtest/gtest.h" +#include <atomic> #include <future> +#include <thread> #include <unistd.h> namespace __xray { @@ -55,6 +57,7 @@ TEST(BufferQueueTest, ReleaseUnknown) { BufferQueue::Buffer Buf; Buf.Data = reinterpret_cast<void *>(0xdeadbeef); Buf.Size = kSize; + Buf.Generation = Buffers.generation(); EXPECT_EQ(BufferQueue::ErrorCode::UnrecognizedBuffer, Buffers.releaseBuffer(Buf)); } @@ -70,8 +73,7 @@ TEST(BufferQueueTest, ErrorsWhenFinalising) { BufferQueue::Buffer OtherBuf; ASSERT_EQ(BufferQueue::ErrorCode::QueueFinalizing, Buffers.getBuffer(OtherBuf)); - ASSERT_EQ(BufferQueue::ErrorCode::QueueFinalizing, - Buffers.finalize()); + ASSERT_EQ(BufferQueue::ErrorCode::QueueFinalizing, Buffers.finalize()); ASSERT_EQ(Buffers.releaseBuffer(Buf), BufferQueue::ErrorCode::Ok); } @@ -111,4 +113,114 @@ TEST(BufferQueueTest, Apply) { ASSERT_EQ(Count, 10); } +TEST(BufferQueueTest, GenerationalSupport) { + bool Success = false; + BufferQueue Buffers(kSize, 10, Success); + ASSERT_TRUE(Success); + BufferQueue::Buffer B0; + ASSERT_EQ(Buffers.getBuffer(B0), BufferQueue::ErrorCode::Ok); + ASSERT_EQ(Buffers.finalize(), + BufferQueue::ErrorCode::Ok); // No more new buffers. + + // Re-initialise the queue. + ASSERT_EQ(Buffers.init(kSize, 10), BufferQueue::ErrorCode::Ok); + + BufferQueue::Buffer B1; + ASSERT_EQ(Buffers.getBuffer(B1), BufferQueue::ErrorCode::Ok); + + // Validate that the buffers come from different generations. + ASSERT_NE(B0.Generation, B1.Generation); + + // We stash the current generation, for use later. + auto PrevGen = B1.Generation; + + // At this point, we want to ensure that we can return the buffer from the + // first "generation" would still be accepted in the new generation... + EXPECT_EQ(Buffers.releaseBuffer(B0), BufferQueue::ErrorCode::Ok); + + // ... and that the new buffer is also accepted. + EXPECT_EQ(Buffers.releaseBuffer(B1), BufferQueue::ErrorCode::Ok); + + // A next round will do the same, ensure that we are able to do multiple + // rounds in this case. + ASSERT_EQ(Buffers.finalize(), BufferQueue::ErrorCode::Ok); + ASSERT_EQ(Buffers.init(kSize, 10), BufferQueue::ErrorCode::Ok); + EXPECT_EQ(Buffers.getBuffer(B0), BufferQueue::ErrorCode::Ok); + EXPECT_EQ(Buffers.getBuffer(B1), BufferQueue::ErrorCode::Ok); + + // Here we ensure that the generation is different from the previous + // generation. + EXPECT_NE(B0.Generation, PrevGen); + EXPECT_EQ(B1.Generation, B1.Generation); + ASSERT_EQ(Buffers.finalize(), BufferQueue::ErrorCode::Ok); + EXPECT_EQ(Buffers.releaseBuffer(B0), BufferQueue::ErrorCode::Ok); + EXPECT_EQ(Buffers.releaseBuffer(B1), BufferQueue::ErrorCode::Ok); +} + +TEST(BufferQueueTest, GenerationalSupportAcrossThreads) { + bool Success = false; + BufferQueue Buffers(kSize, 10, Success); + ASSERT_TRUE(Success); + + std::atomic<int> Counter{0}; + + // This function allows us to use thread-local storage to isolate the + // instances of the buffers to be used. It also allows us signal the threads + // of a new generation, and allow those to get new buffers. This is + // representative of how we expect the buffer queue to be used by the XRay + // runtime. + auto Process = [&] { + thread_local BufferQueue::Buffer B; + ASSERT_EQ(Buffers.getBuffer(B), BufferQueue::ErrorCode::Ok); + auto FirstGen = B.Generation; + + // Signal that we've gotten a buffer in the thread. + Counter.fetch_add(1, std::memory_order_acq_rel); + while (!Buffers.finalizing()) { + Buffers.releaseBuffer(B); + Buffers.getBuffer(B); + } + + // Signal that we've exited the get/release buffer loop. + Counter.fetch_sub(1, std::memory_order_acq_rel); + if (B.Data != nullptr) + Buffers.releaseBuffer(B); + + // Spin until we find that the Buffer Queue is no longer finalizing. + while (Buffers.getBuffer(B) != BufferQueue::ErrorCode::Ok) + ; + + // Signal that we've successfully gotten a buffer in the thread. + Counter.fetch_add(1, std::memory_order_acq_rel); + + EXPECT_NE(FirstGen, B.Generation); + EXPECT_EQ(Buffers.releaseBuffer(B), BufferQueue::ErrorCode::Ok); + + // Signal that we've successfully exited. + Counter.fetch_sub(1, std::memory_order_acq_rel); + }; + + // Spawn two threads running Process. + std::thread T0(Process), T1(Process); + + // Spin until we find the counter is up to 2. + while (Counter.load(std::memory_order_acquire) != 2) + ; + + // Then we finalize, then re-initialize immediately. + Buffers.finalize(); + + // Spin until we find the counter is down to 0. + while (Counter.load(std::memory_order_acquire) != 0) + ; + + // Then we re-initialize. + EXPECT_EQ(Buffers.init(kSize, 10), BufferQueue::ErrorCode::Ok); + + T0.join(); + T1.join(); + + ASSERT_EQ(Counter.load(std::memory_order_acquire), 0); +} + } // namespace __xray |