Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "test/time_controller/simulated_time_controller.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <deque> |
| 14 | #include <list> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 15 | #include <memory> |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <thread> |
| 18 | #include <vector> |
| 19 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 20 | #include "absl/strings/string_view.h" |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 21 | #include "test/time_controller/simulated_process_thread.h" |
| 22 | #include "test/time_controller/simulated_task_queue.h" |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 25 | namespace { |
| 26 | // Helper function to remove from a std container by value. |
| 27 | template <class C> |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 28 | bool RemoveByValue(C* vec, typename C::value_type val) { |
| 29 | auto it = std::find(vec->begin(), vec->end(), val); |
| 30 | if (it == vec->end()) |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 31 | return false; |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 32 | vec->erase(it); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 33 | return true; |
| 34 | } |
| 35 | } // namespace |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 36 | |
| 37 | namespace sim_time_impl { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 38 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 39 | SimulatedTimeControllerImpl::SimulatedTimeControllerImpl(Timestamp start_time) |
| 40 | : thread_id_(rtc::CurrentThreadId()), current_time_(start_time) {} |
| 41 | |
| 42 | SimulatedTimeControllerImpl::~SimulatedTimeControllerImpl() = default; |
| 43 | |
| 44 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> |
| 45 | SimulatedTimeControllerImpl::CreateTaskQueue( |
| 46 | absl::string_view name, |
| 47 | TaskQueueFactory::Priority priority) const { |
| 48 | // TODO(srte): Remove the const cast when the interface is made mutable. |
| 49 | auto mutable_this = const_cast<SimulatedTimeControllerImpl*>(this); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 50 | auto task_queue = std::unique_ptr<SimulatedTaskQueue, TaskQueueDeleter>( |
| 51 | new SimulatedTaskQueue(mutable_this, name)); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 52 | rtc::CritScope lock(&mutable_this->lock_); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 53 | mutable_this->runners_.push_back(task_queue.get()); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 54 | return task_queue; |
| 55 | } |
| 56 | |
| 57 | std::unique_ptr<ProcessThread> SimulatedTimeControllerImpl::CreateProcessThread( |
| 58 | const char* thread_name) { |
| 59 | rtc::CritScope lock(&lock_); |
| 60 | auto process_thread = |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 61 | std::make_unique<SimulatedProcessThread>(this, thread_name); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 62 | runners_.push_back(process_thread.get()); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 63 | return process_thread; |
| 64 | } |
| 65 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 66 | void SimulatedTimeControllerImpl::YieldExecution() { |
| 67 | if (rtc::CurrentThreadId() == thread_id_) { |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 68 | TaskQueueBase* yielding_from = TaskQueueBase::Current(); |
| 69 | // Since we might continue execution on a process thread, we should reset |
| 70 | // the thread local task queue reference. This ensures that thread checkers |
| 71 | // won't think we are executing on the yielding task queue. It also ensure |
| 72 | // that TaskQueueBase::Current() won't return the yielding task queue. |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 73 | TokenTaskQueue::CurrentTaskQueueSetter reset_queue(nullptr); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 74 | // When we yield, we don't want to risk executing further tasks on the |
| 75 | // currently executing task queue. If there's a ready task that also yields, |
| 76 | // it's added to this set as well and only tasks on the remaining task |
| 77 | // queues are executed. |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 78 | auto inserted = yielded_.insert(yielding_from); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 79 | RTC_DCHECK(inserted.second); |
| 80 | RunReadyRunners(); |
| 81 | yielded_.erase(inserted.first); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void SimulatedTimeControllerImpl::RunReadyRunners() { |
Sebastian Jansson | 1b40823 | 2019-04-12 15:39:38 +0200 | [diff] [blame] | 86 | rtc::CritScope lock(&lock_); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 87 | RTC_DCHECK_EQ(rtc::CurrentThreadId(), thread_id_); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 88 | Timestamp current_time = CurrentTime(); |
Sebastian Jansson | 1b40823 | 2019-04-12 15:39:38 +0200 | [diff] [blame] | 89 | // Clearing |ready_runners_| in case this is a recursive call: |
| 90 | // RunReadyRunners -> Run -> Event::Wait -> Yield ->RunReadyRunners |
| 91 | ready_runners_.clear(); |
| 92 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 93 | // We repeat until we have no ready left to handle tasks posted by ready |
| 94 | // runners. |
| 95 | while (true) { |
Sebastian Jansson | 7654081 | 2019-04-11 17:48:30 +0200 | [diff] [blame] | 96 | for (auto* runner : runners_) { |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 97 | if (yielded_.find(runner->GetAsTaskQueue()) == yielded_.end() && |
Sebastian Jansson | 7654081 | 2019-04-11 17:48:30 +0200 | [diff] [blame] | 98 | runner->GetNextRunTime() <= current_time) { |
| 99 | ready_runners_.push_back(runner); |
| 100 | } |
| 101 | } |
| 102 | if (ready_runners_.empty()) |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 103 | break; |
Sebastian Jansson | 7654081 | 2019-04-11 17:48:30 +0200 | [diff] [blame] | 104 | while (!ready_runners_.empty()) { |
| 105 | auto* runner = ready_runners_.front(); |
| 106 | ready_runners_.pop_front(); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 107 | // Note that the RunReady function might indirectly cause a call to |
Sebastian Jansson | 7654081 | 2019-04-11 17:48:30 +0200 | [diff] [blame] | 108 | // Unregister() which will recursively grab |lock_| again to remove items |
| 109 | // from |ready_runners_|. |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 110 | runner->RunReady(current_time); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | Timestamp SimulatedTimeControllerImpl::CurrentTime() const { |
| 116 | rtc::CritScope lock(&time_lock_); |
| 117 | return current_time_; |
| 118 | } |
| 119 | |
| 120 | Timestamp SimulatedTimeControllerImpl::NextRunTime() const { |
| 121 | Timestamp current_time = CurrentTime(); |
| 122 | Timestamp next_time = Timestamp::PlusInfinity(); |
| 123 | rtc::CritScope lock(&lock_); |
| 124 | for (auto* runner : runners_) { |
| 125 | Timestamp next_run_time = runner->GetNextRunTime(); |
| 126 | if (next_run_time <= current_time) |
| 127 | return current_time; |
| 128 | next_time = std::min(next_time, next_run_time); |
| 129 | } |
| 130 | return next_time; |
| 131 | } |
| 132 | |
| 133 | void SimulatedTimeControllerImpl::AdvanceTime(Timestamp target_time) { |
| 134 | rtc::CritScope time_lock(&time_lock_); |
Sebastian Jansson | 5a00016 | 2019-04-12 11:21:32 +0200 | [diff] [blame] | 135 | RTC_DCHECK_GE(target_time, current_time_); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 136 | current_time_ = target_time; |
| 137 | } |
| 138 | |
| 139 | void SimulatedTimeControllerImpl::Unregister(SimulatedSequenceRunner* runner) { |
| 140 | rtc::CritScope lock(&lock_); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 141 | bool removed = RemoveByValue(&runners_, runner); |
Sebastian Jansson | 7b6add3 | 2019-03-29 10:34:26 +0100 | [diff] [blame] | 142 | RTC_CHECK(removed); |
Sebastian Jansson | 53cd9e2 | 2020-01-13 10:33:19 +0100 | [diff] [blame] | 143 | RemoveByValue(&ready_runners_, runner); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 144 | } |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 145 | } // namespace sim_time_impl |
| 146 | |
| 147 | GlobalSimulatedTimeController::GlobalSimulatedTimeController( |
| 148 | Timestamp start_time) |
Sebastian Jansson | 340af97 | 2019-12-04 10:07:48 +0100 | [diff] [blame] | 149 | : sim_clock_(start_time.us()), impl_(start_time), yield_policy_(&impl_) { |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame] | 150 | global_clock_.SetTime(start_time); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | GlobalSimulatedTimeController::~GlobalSimulatedTimeController() = default; |
| 154 | |
| 155 | Clock* GlobalSimulatedTimeController::GetClock() { |
| 156 | return &sim_clock_; |
| 157 | } |
| 158 | |
| 159 | TaskQueueFactory* GlobalSimulatedTimeController::GetTaskQueueFactory() { |
| 160 | return &impl_; |
| 161 | } |
| 162 | |
| 163 | std::unique_ptr<ProcessThread> |
| 164 | GlobalSimulatedTimeController::CreateProcessThread(const char* thread_name) { |
| 165 | return impl_.CreateProcessThread(thread_name); |
| 166 | } |
| 167 | |
Markus Handell | 486cc55 | 2019-12-03 14:37:28 +0100 | [diff] [blame] | 168 | void GlobalSimulatedTimeController::AdvanceTime(TimeDelta duration) { |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 169 | rtc::ScopedYieldPolicy yield_policy(&impl_); |
| 170 | Timestamp current_time = impl_.CurrentTime(); |
| 171 | Timestamp target_time = current_time + duration; |
| 172 | RTC_DCHECK_EQ(current_time.us(), rtc::TimeMicros()); |
| 173 | while (current_time < target_time) { |
| 174 | impl_.RunReadyRunners(); |
| 175 | Timestamp next_time = std::min(impl_.NextRunTime(), target_time); |
| 176 | impl_.AdvanceTime(next_time); |
| 177 | auto delta = next_time - current_time; |
| 178 | current_time = next_time; |
| 179 | sim_clock_.AdvanceTimeMicroseconds(delta.us()); |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame] | 180 | global_clock_.AdvanceTime(delta); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 181 | } |
philipel | d572748 | 2020-01-03 14:43:10 +0100 | [diff] [blame] | 182 | // After time has been simulated up until |target_time| we also need to run |
| 183 | // tasks meant to be executed at |target_time|. |
| 184 | impl_.RunReadyRunners(); |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Sebastian Jansson | 0d617cc | 2019-03-22 15:22:16 +0100 | [diff] [blame] | 187 | } // namespace webrtc |