QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/test_tools/simulator/simulator.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
| 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 9 | |
| 10 | namespace quic { |
| 11 | namespace simulator { |
| 12 | |
| 13 | Simulator::Simulator() |
| 14 | : random_generator_(nullptr), |
| 15 | alarm_factory_(this, "Default Alarm Manager"), |
| 16 | run_for_should_stop_(false), |
| 17 | enable_random_delays_(false) { |
| 18 | run_for_alarm_.reset( |
| 19 | alarm_factory_.CreateAlarm(new RunForDelegate(&run_for_should_stop_))); |
| 20 | } |
| 21 | |
| 22 | Simulator::~Simulator() { |
| 23 | // Ensure that Actor under run_for_alarm_ is removed before Simulator data |
| 24 | // structures are destructed. |
| 25 | run_for_alarm_.reset(); |
| 26 | } |
| 27 | |
| 28 | Simulator::Clock::Clock() : now_(kStartTime) {} |
| 29 | |
| 30 | QuicTime Simulator::Clock::ApproximateNow() const { |
| 31 | return now_; |
| 32 | } |
| 33 | |
| 34 | QuicTime Simulator::Clock::Now() const { |
| 35 | return now_; |
| 36 | } |
| 37 | |
| 38 | QuicWallTime Simulator::Clock::WallNow() const { |
| 39 | return QuicWallTime::FromUNIXMicroseconds( |
| 40 | (now_ - QuicTime::Zero()).ToMicroseconds()); |
| 41 | } |
| 42 | |
| 43 | void Simulator::AddActor(Actor* actor) { |
| 44 | auto emplace_times_result = |
| 45 | scheduled_times_.insert(std::make_pair(actor, QuicTime::Infinite())); |
| 46 | auto emplace_names_result = actor_names_.insert(actor->name()); |
| 47 | |
| 48 | // Ensure that the object was actually placed into the map. |
| 49 | DCHECK(emplace_times_result.second); |
| 50 | DCHECK(emplace_names_result.second); |
| 51 | } |
| 52 | |
| 53 | void Simulator::RemoveActor(Actor* actor) { |
| 54 | auto scheduled_time_it = scheduled_times_.find(actor); |
| 55 | auto actor_names_it = actor_names_.find(actor->name()); |
| 56 | DCHECK(scheduled_time_it != scheduled_times_.end()); |
| 57 | DCHECK(actor_names_it != actor_names_.end()); |
| 58 | |
| 59 | QuicTime scheduled_time = scheduled_time_it->second; |
| 60 | if (scheduled_time != QuicTime::Infinite()) { |
| 61 | Unschedule(actor); |
| 62 | } |
| 63 | |
| 64 | scheduled_times_.erase(scheduled_time_it); |
| 65 | actor_names_.erase(actor_names_it); |
| 66 | } |
| 67 | |
| 68 | void Simulator::Schedule(Actor* actor, QuicTime new_time) { |
| 69 | auto scheduled_time_it = scheduled_times_.find(actor); |
| 70 | DCHECK(scheduled_time_it != scheduled_times_.end()); |
| 71 | QuicTime scheduled_time = scheduled_time_it->second; |
| 72 | |
| 73 | if (scheduled_time <= new_time) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | if (scheduled_time != QuicTime::Infinite()) { |
| 78 | Unschedule(actor); |
| 79 | } |
| 80 | |
| 81 | scheduled_time_it->second = new_time; |
| 82 | schedule_.insert(std::make_pair(new_time, actor)); |
| 83 | } |
| 84 | |
| 85 | void Simulator::Unschedule(Actor* actor) { |
| 86 | auto scheduled_time_it = scheduled_times_.find(actor); |
| 87 | DCHECK(scheduled_time_it != scheduled_times_.end()); |
| 88 | QuicTime scheduled_time = scheduled_time_it->second; |
| 89 | |
| 90 | DCHECK(scheduled_time != QuicTime::Infinite()); |
| 91 | auto range = schedule_.equal_range(scheduled_time); |
| 92 | for (auto it = range.first; it != range.second; ++it) { |
| 93 | if (it->second == actor) { |
| 94 | schedule_.erase(it); |
| 95 | scheduled_time_it->second = QuicTime::Infinite(); |
| 96 | return; |
| 97 | } |
| 98 | } |
| 99 | DCHECK(false); |
| 100 | } |
| 101 | |
| 102 | const QuicClock* Simulator::GetClock() const { |
| 103 | return &clock_; |
| 104 | } |
| 105 | |
| 106 | QuicRandom* Simulator::GetRandomGenerator() { |
| 107 | if (random_generator_ == nullptr) { |
| 108 | random_generator_ = QuicRandom::GetInstance(); |
| 109 | } |
| 110 | |
| 111 | return random_generator_; |
| 112 | } |
| 113 | |
| 114 | QuicBufferAllocator* Simulator::GetStreamSendBufferAllocator() { |
| 115 | return &buffer_allocator_; |
| 116 | } |
| 117 | |
| 118 | QuicAlarmFactory* Simulator::GetAlarmFactory() { |
| 119 | return &alarm_factory_; |
| 120 | } |
| 121 | |
| 122 | Simulator::RunForDelegate::RunForDelegate(bool* run_for_should_stop) |
| 123 | : run_for_should_stop_(run_for_should_stop) {} |
| 124 | |
| 125 | void Simulator::RunForDelegate::OnAlarm() { |
| 126 | *run_for_should_stop_ = true; |
| 127 | } |
| 128 | |
| 129 | void Simulator::RunFor(QuicTime::Delta time_span) { |
| 130 | DCHECK(!run_for_alarm_->IsSet()); |
| 131 | |
| 132 | // RunFor() ensures that the simulation stops at the exact time specified by |
| 133 | // scheduling an alarm at that point and using that alarm to abort the |
| 134 | // simulation. An alarm is necessary because otherwise it is possible that |
| 135 | // nothing is scheduled at |end_time|, so the simulation will either go |
| 136 | // further than requested or stop before reaching |end_time|. |
| 137 | const QuicTime end_time = clock_.Now() + time_span; |
| 138 | run_for_alarm_->Set(end_time); |
| 139 | run_for_should_stop_ = false; |
| 140 | bool simulation_result = RunUntil([this]() { return run_for_should_stop_; }); |
| 141 | |
| 142 | DCHECK(simulation_result); |
| 143 | DCHECK(clock_.Now() == end_time); |
| 144 | } |
| 145 | |
| 146 | void Simulator::HandleNextScheduledActor() { |
| 147 | const auto current_event_it = schedule_.begin(); |
| 148 | QuicTime event_time = current_event_it->first; |
| 149 | Actor* actor = current_event_it->second; |
| 150 | QUIC_DVLOG(3) << "At t = " << event_time.ToDebuggingValue() << ", calling " |
| 151 | << actor->name(); |
| 152 | |
| 153 | Unschedule(actor); |
| 154 | |
| 155 | if (clock_.Now() > event_time) { |
| 156 | QUIC_BUG << "Error: event registered by [" << actor->name() |
| 157 | << "] requires travelling back in time. Current time: " |
| 158 | << clock_.Now().ToDebuggingValue() |
| 159 | << ", scheduled time: " << event_time.ToDebuggingValue(); |
| 160 | } |
| 161 | clock_.now_ = event_time; |
| 162 | |
| 163 | actor->Act(); |
| 164 | } |
| 165 | |
| 166 | } // namespace simulator |
| 167 | } // namespace quic |