|
| 1 | +/* |
| 2 | +Copyright 2022 The Photon Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <atomic> |
| 18 | +#include <thread> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include <photon/common/alog.h> |
| 22 | +#include <photon/common/lockfree_queue.h> |
| 23 | +#include <photon/photon.h> |
| 24 | +#include <photon/thread/thread.h> |
| 25 | + |
| 26 | +#include "../../../test/gtest.h" |
| 27 | + |
| 28 | +// Regression test for the RingChannel notification refactor. |
| 29 | +// |
| 30 | +// Failure mode being guarded: |
| 31 | +// When N producers fire a burst, the previous design issued one |
| 32 | +// `queue_sem.signal(1)` per push every time the consumer happened to be in |
| 33 | +// its `idler` window. The semaphore counter accumulated linearly with the |
| 34 | +// burst size, so after the burst the consumer would loop forever between |
| 35 | +// 1ms busy-yield and a `wait()` that returned immediately, burning a full |
| 36 | +// core for as long as the accumulated counter took to drain. |
| 37 | +// |
| 38 | +// Verification strategy (deterministic, no wall-clock assumptions): |
| 39 | +// - Drive a real `RingChannel` directly, with `kProducers` std::thread |
| 40 | +// producers and a single std::thread consumer that owns its own photon |
| 41 | +// vCPU. |
| 42 | +// - Synchronize using a producer-side `received` counter and a sentinel |
| 43 | +// value to terminate the consumer. All "wait" operations are |
| 44 | +// event-driven (`std::thread::join`, atomic counter spin), never |
| 45 | +// wall-clock sleeps. |
| 46 | +// - After the consumer thread has fully joined, all RingChannel state is |
| 47 | +// quiescent and visible to the test thread. Assert the *invariant* |
| 48 | +// `notification_pending() <= idler_peak`. With a single consumer, |
| 49 | +// `idler_peak == 1`, so `pending` must remain ≤1 regardless of burst |
| 50 | +// size. Without the fix, `pending` (== `queue_sem.m_count`) would carry |
| 51 | +// leftover tokens proportional to how often the consumer dipped into |
| 52 | +// the idler window during the burst. |
| 53 | +TEST(ring_channel, burst_drain_no_signal_accumulation) { |
| 54 | + using QT = LockfreeMPMCRingQueue<int, 4096>; |
| 55 | + photon::common::RingChannel<QT> ch(1024, 1024); |
| 56 | + |
| 57 | + constexpr int kProducers = 4; |
| 58 | + constexpr int kPerProducer = 25000; |
| 59 | + constexpr int kBurst = kProducers * kPerProducer; |
| 60 | + constexpr int kSentinel = 0; // sentinel value to terminate consumer |
| 61 | + |
| 62 | + std::atomic<int> received{0}; |
| 63 | + |
| 64 | + // Consumer owns its own photon vCPU, so it can be scheduled |
| 65 | + // independently of the producers and of this test thread. |
| 66 | + std::thread consumer([&] { |
| 67 | + photon::init(photon::INIT_EVENT_DEFAULT, photon::INIT_IO_NONE); |
| 68 | + DEFER(photon::fini()); |
| 69 | + for (;;) { |
| 70 | + int x = ch.recv(); |
| 71 | + if (x == kSentinel) break; |
| 72 | + received.fetch_add(1, std::memory_order_acq_rel); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + std::vector<std::thread> producers; |
| 77 | + producers.reserve(kProducers); |
| 78 | + for (int p = 0; p < kProducers; ++p) { |
| 79 | + producers.emplace_back([&] { |
| 80 | + for (int i = 0; i < kPerProducer; ++i) { |
| 81 | + // Producers are plain std::threads; ThreadPause yields the |
| 82 | + // OS thread on the (extremely unlikely) full-queue spin. |
| 83 | + ch.template send<ThreadPause>(/*non-zero*/ 1); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + for (auto& t : producers) t.join(); |
| 88 | + |
| 89 | + // Deterministic synchronization point: spin until the consumer has |
| 90 | + // drained exactly the burst. No wall-clock sleeps. |
| 91 | + while (received.load(std::memory_order_acquire) < kBurst) { |
| 92 | + std::this_thread::yield(); |
| 93 | + } |
| 94 | + |
| 95 | + // Terminate consumer cleanly, then join. After join() returns, no other |
| 96 | + // thread can touch `ch`, so we can sample its state safely. |
| 97 | + ch.template send<ThreadPause>(kSentinel); |
| 98 | + consumer.join(); |
| 99 | + |
| 100 | + auto pending = ch.notification_pending(); |
| 101 | + LOG_INFO("after burst: received=`, notification_pending=`", |
| 102 | + received.load(), pending); |
| 103 | + |
| 104 | + // Strong invariant under the fix: in-flight wake-up tokens are capped |
| 105 | + // by the historical peak of `idler`. With a single consumer that peak |
| 106 | + // is 1, so `pending` must remain ≤1 regardless of burst size. Without |
| 107 | + // the fix `pending` would scale with kBurst. |
| 108 | + EXPECT_LE(pending, 1u); |
| 109 | + EXPECT_EQ(kBurst, received.load()); |
| 110 | +} |
0 commit comments