Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: net/quic/core/congestion_control/simulation/simulator.cc

Issue 2323963002: Implement an event-based simulator for QuicConnection (Closed)
Patch Set: Adding missing files. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include "net/quic/core/congestion_control/simulation/simulator.h"
2 #include "net/quic/core/crypto/quic_random.h"
3 #include "util/gtl/ptr_util.h"
4
5 namespace net {
6 namespace simulation {
7
8 Simulator::Simulator()
9 : random_generator_(nullptr),
10 alarm_factory_(this, "Default Alarm Manager") {}
11
12 Simulator::Clock::Clock() : now_(kStartTime) {}
13
14 QuicTime Simulator::Clock::ApproximateNow() const {
15 return now_;
16 }
17
18 QuicTime Simulator::Clock::Now() const {
19 return now_;
20 }
21
22 QuicWallTime Simulator::Clock::WallNow() const {
23 return QuicWallTime::FromUNIXMicroseconds(
24 (now_ - QuicTime::Zero()).ToMicroseconds());
25 }
26
27 void Simulator::AddActor(Actor* actor) {
28 auto emplace_times_result =
29 scheduled_times_.emplace(actor, QuicTime::Infinite());
30 auto emplace_names_result = actor_names_.emplace(actor->name());
31
32 // Ensure that the object was actually placed into the map.
33 DCHECK(emplace_times_result.second);
34 DCHECK(emplace_names_result.second);
35 }
36
37 void Simulator::Schedule(Actor* actor, QuicTime new_time) {
38 auto scheduled_time_it = scheduled_times_.find(actor);
39 DCHECK(scheduled_time_it != scheduled_times_.end());
40 QuicTime scheduled_time = scheduled_time_it->second;
41
42 if (scheduled_time <= new_time) {
43 return;
44 }
45
46 if (scheduled_time != QuicTime::Infinite()) {
47 Unschedule(actor);
48 }
49
50 scheduled_time_it->second = new_time;
51 schedule_.insert(std::make_pair(new_time, actor));
52 }
53
54 void Simulator::Unschedule(Actor* actor) {
55 auto scheduled_time_it = scheduled_times_.find(actor);
56 DCHECK(scheduled_time_it != scheduled_times_.end());
57 QuicTime scheduled_time = scheduled_time_it->second;
58
59 DCHECK(scheduled_time != QuicTime::Infinite());
60 auto range = schedule_.equal_range(scheduled_time);
61 for (auto it = range.first; it != range.second; ++it) {
62 if (it->second == actor) {
63 schedule_.erase(it);
64 scheduled_time_it->second = QuicTime::Infinite();
65 return;
66 }
67 }
68 DCHECK(false);
69 }
70
71 const QuicClock* Simulator::GetClock() const {
72 return &clock_;
73 }
74
75 QuicRandom* Simulator::GetRandomGenerator() {
76 if (random_generator_ == nullptr) {
77 random_generator_ = QuicRandom::GetInstance();
78 }
79
80 return random_generator_;
81 }
82
83 QuicBufferAllocator* Simulator::GetBufferAllocator() {
84 return &buffer_allocator_;
85 }
86
87 QuicAlarmFactory* Simulator::GetAlarmFactory() {
88 return &alarm_factory_;
89 }
90
91 void Simulator::HandleNextScheduledActor() {
92 const auto current_event_it = schedule_.begin();
93 QuicTime event_time = current_event_it->first;
94 Actor* actor = current_event_it->second;
95 DVLOG(3) << "At t = " << event_time.ToDebuggingValue() << ", calling "
96 << actor->name();
97
98 Unschedule(actor);
99
100 if (clock_.Now() > event_time) {
101 QUIC_BUG << "Error: event registered by [" << actor->name()
102 << "] requires travelling back in time. Current time: "
103 << clock_.Now().ToDebuggingValue()
104 << ", scheduled time: " << event_time.ToDebuggingValue();
105 }
106 clock_.now_ = event_time;
107
108 actor->Act();
109 }
110
111 } // namespace simulation
112 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698