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