BpfRingBuf.h - implement isEmpty()
Test: TreeHugger, atest BpfRingbufTest Signed-off-by: Maciej Żenczykowski <maze@google.com> Change-Id: I23020869ab665c029c3219b1b39c3749be6e6992
This commit is contained in:
@@ -72,12 +72,15 @@ class BpfRingbufTest : public ::testing::Test {
|
|||||||
|
|
||||||
auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
|
auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
|
||||||
ASSERT_RESULT_OK(result);
|
ASSERT_RESULT_OK(result);
|
||||||
|
EXPECT_TRUE(result.value()->isEmpty());
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
RunProgram();
|
RunProgram();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EXPECT_FALSE(result.value()->isEmpty());
|
||||||
EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(n));
|
EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(n));
|
||||||
|
EXPECT_TRUE(result.value()->isEmpty());
|
||||||
EXPECT_EQ(output, TEST_RINGBUF_MAGIC_NUM);
|
EXPECT_EQ(output, TEST_RINGBUF_MAGIC_NUM);
|
||||||
EXPECT_EQ(run_count, n);
|
EXPECT_EQ(run_count, n);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ class BpfRingbufBase {
|
|||||||
mProducerPos = nullptr;
|
mProducerPos = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isEmpty(void);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Non-initializing constructor, used by Create.
|
// Non-initializing constructor, used by Create.
|
||||||
BpfRingbufBase(size_t value_size) : mValueSize(value_size) {}
|
BpfRingbufBase(size_t value_size) : mValueSize(value_size) {}
|
||||||
@@ -197,6 +199,13 @@ inline base::Result<void> BpfRingbufBase::Init(const char* path) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool BpfRingbufBase::isEmpty(void) {
|
||||||
|
uint32_t prod_pos = mProducerPos->load(std::memory_order_acquire);
|
||||||
|
// Only userspace writes to mConsumerPos, so no need to use std::memory_order_acquire
|
||||||
|
uint64_t cons_pos = mConsumerPos->load(std::memory_order_relaxed);
|
||||||
|
return (cons_pos & 0xFFFFFFFF) == prod_pos;
|
||||||
|
}
|
||||||
|
|
||||||
inline base::Result<int> BpfRingbufBase::ConsumeAll(
|
inline base::Result<int> BpfRingbufBase::ConsumeAll(
|
||||||
const std::function<void(const void*)>& callback) {
|
const std::function<void(const void*)>& callback) {
|
||||||
int64_t count = 0;
|
int64_t count = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user