Hash :
24a3d30d
Author :
Date :
2024-08-28T14:33:49
Possibly fix FixedQueue.ConcurrentPushPopWithResize flakiness Queue may be empty when `enqueueThreadFinished` become true. Bug: b/302739073 Change-Id: Idb636e3f87c1217520a9e68a69e749f5bcac4d0f Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5823039 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Igor Nazarov <i.nazarov@samsung.com> Reviewed-by: Charlie Lao <cclao@google.com>
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CircularBuffer_unittest:
// Tests of the CircularBuffer class
//
#include <gtest/gtest.h>
#include "common/FixedQueue.h"
#include <chrono>
#include <thread>
namespace angle
{
// Make sure the various constructors compile and do basic checks
TEST(FixedQueue, Constructors)
{
FixedQueue<int> q(5);
EXPECT_EQ(0u, q.size());
EXPECT_EQ(true, q.empty());
}
// Make sure the destructor destroys all elements.
TEST(FixedQueue, Destructor)
{
struct s
{
s() : counter(nullptr) {}
s(int *c) : counter(c) {}
~s()
{
if (counter)
{
++*counter;
}
}
s(const s &) = default;
s &operator=(const s &) = default;
int *counter;
};
int destructorCount = 0;
{
FixedQueue<s> q(11);
q.push(s(&destructorCount));
// Destructor called once for the temporary above.
EXPECT_EQ(1, destructorCount);
}
// Destructor should be called one more time for the element we pushed.
EXPECT_EQ(2, destructorCount);
}
// Make sure the pop destroys the element.
TEST(FixedQueue, Pop)
{
struct s
{
s() : counter(nullptr) {}
s(int *c) : counter(c) {}
~s()
{
if (counter)
{
++*counter;
}
}
s(const s &) = default;
s &operator=(const s &s)
{
// increment if we are overwriting the custom initialized object
if (counter)
{
++*counter;
}
counter = s.counter;
return *this;
}
int *counter;
};
int destructorCount = 0;
FixedQueue<s> q(11);
q.push(s(&destructorCount));
// Destructor called once for the temporary above.
EXPECT_EQ(1, destructorCount);
q.pop();
// Copy assignment should be called for the element we popped.
EXPECT_EQ(2, destructorCount);
}
// Test circulating behavior.
TEST(FixedQueue, WrapAround)
{
FixedQueue<int> q(7);
for (int i = 0; i < 7; ++i)
{
q.push(i);
}
EXPECT_EQ(0, q.front());
q.pop();
// This should wrap around
q.push(7);
for (int i = 0; i < 7; ++i)
{
EXPECT_EQ(i + 1, q.front());
q.pop();
}
}
// Test concurrent push and pop behavior.
TEST(FixedQueue, ConcurrentPushPop)
{
FixedQueue<uint64_t> q(7);
double timeOut = 1.0;
uint64_t kMaxLoop = 1000000ull;
std::atomic<bool> enqueueThreadFinished;
enqueueThreadFinished = false;
std::atomic<bool> dequeueThreadFinished;
dequeueThreadFinished = false;
std::thread enqueueThread = std::thread([&]() {
std::time_t t1 = std::time(nullptr);
uint64_t value = 0;
do
{
while (q.full() && !dequeueThreadFinished)
{
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
if (dequeueThreadFinished)
{
break;
}
q.push(value);
value++;
} while (difftime(std::time(nullptr), t1) < timeOut && value < kMaxLoop);
ASSERT(difftime(std::time(nullptr), t1) >= timeOut || value >= kMaxLoop);
enqueueThreadFinished = true;
});
std::thread dequeueThread = std::thread([&]() {
std::time_t t1 = std::time(nullptr);
uint64_t expectedValue = 0;
do
{
while (q.empty() && !enqueueThreadFinished)
{
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
EXPECT_EQ(expectedValue, q.front());
// test pop
q.pop();
expectedValue++;
} while (difftime(std::time(nullptr), t1) < timeOut && expectedValue < kMaxLoop);
ASSERT(difftime(std::time(nullptr), t1) >= timeOut || expectedValue >= kMaxLoop);
dequeueThreadFinished = true;
});
enqueueThread.join();
dequeueThread.join();
}
// Test concurrent push and pop behavior. When queue is full, instead of wait, it will try to
// increase capacity. At dequeue thread, it will also try to shrink the queue capacity when size
// fall under half of the capacity.
TEST(FixedQueue, ConcurrentPushPopWithResize)
{
static constexpr size_t kInitialQueueCapacity = 64;
static constexpr size_t kMaxQueueCapacity = 64 * 1024;
FixedQueue<uint64_t> q(kInitialQueueCapacity);
double timeOut = 1.0;
uint64_t kMaxLoop = 1000000ull;
std::atomic<bool> enqueueThreadFinished(false);
std::atomic<bool> dequeueThreadFinished(false);
std::mutex enqueueMutex;
std::mutex dequeueMutex;
std::thread enqueueThread = std::thread([&]() {
std::time_t t1 = std::time(nullptr);
uint64_t value = 0;
do
{
std::unique_lock<std::mutex> enqueueLock(enqueueMutex);
if (q.full())
{
// Take both lock to ensure no one will access while we try to double the
// storage. Note that under a well balanced system, this should happen infrequently.
std::unique_lock<std::mutex> dequeueLock(dequeueMutex);
// Check again to see if queue is still full after taking the dequeueMutex.
size_t newCapacity = q.capacity() * 2;
if (q.full() && newCapacity < kMaxQueueCapacity)
{
// Double the storage size while we took the lock
q.updateCapacity(newCapacity);
}
}
// If queue is still full, lets wait for dequeue thread to make some progress
while (q.full() && !dequeueThreadFinished)
{
enqueueLock.unlock();
std::this_thread::sleep_for(std::chrono::microseconds(1));
enqueueLock.lock();
}
if (dequeueThreadFinished)
{
break;
}
q.push(value);
value++;
} while (difftime(std::time(nullptr), t1) < timeOut && value < kMaxLoop &&
!dequeueThreadFinished);
enqueueThreadFinished = true;
});
std::thread dequeueThread = std::thread([&]() {
std::time_t t1 = std::time(nullptr);
uint64_t expectedValue = 0;
do
{
std::unique_lock<std::mutex> dequeueLock(dequeueMutex);
if (q.size() < q.capacity() / 10 && q.capacity() > kInitialQueueCapacity)
{
// Shrink the storage if we only used less than 10% of storage. We must take both
// lock to ensure no one is accessing it when we update storage. And the lock must
// take in the same order as other thread to avoid deadlock.
dequeueLock.unlock();
std::unique_lock<std::mutex> enqueueLock(enqueueMutex);
dequeueLock.lock();
// Figure out what the new capacity should be
size_t newCapacity = q.capacity() / 2;
while (q.size() < newCapacity)
{
newCapacity /= 2;
}
newCapacity *= 2;
newCapacity = std::max(newCapacity, kInitialQueueCapacity);
q.updateCapacity(newCapacity);
}
while (q.empty() && !enqueueThreadFinished)
{
dequeueLock.unlock();
std::this_thread::sleep_for(std::chrono::microseconds(1));
dequeueLock.lock();
}
if (enqueueThreadFinished)
{
break;
}
ASSERT(expectedValue == q.front());
// test pop
q.pop();
expectedValue++;
} while (difftime(std::time(nullptr), t1) < timeOut && expectedValue < kMaxLoop &&
!enqueueThreadFinished);
dequeueThreadFinished = true;
});
enqueueThread.join();
dequeueThread.join();
}
// Test clearing the queue
TEST(FixedQueue, Clear)
{
FixedQueue<int> q(5);
for (int i = 0; i < 5; ++i)
{
q.push(i);
}
q.clear();
EXPECT_EQ(0u, q.size());
EXPECT_EQ(true, q.empty());
}
} // namespace angle