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

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

Issue 2322233004: Landing Recent QUIC changes until Sun Sep 4 03:41:00 (Closed)
Patch Set: Remove simulation files from the build. 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 // 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 "base/memory/ptr_util.h"
6 #include "net/quic/core/congestion_control/simulation/simulator.h"
7 #include "net/quic/core/crypto/quic_random.h"
8
9 namespace net {
10 namespace simulation {
11
12 Simulator::Simulator()
13 : random_generator_(nullptr),
14 alarm_factory_(this, "Default Alarm Manager") {}
15
16 Simulator::~Simulator() {}
17
18 Simulator::Clock::Clock() : now_(kStartTime) {}
19
20 QuicTime Simulator::Clock::ApproximateNow() const {
21 return now_;
22 }
23
24 QuicTime Simulator::Clock::Now() const {
25 return now_;
26 }
27
28 QuicWallTime Simulator::Clock::WallNow() const {
29 return QuicWallTime::FromUNIXMicroseconds(
30 (now_ - QuicTime::Zero()).ToMicroseconds());
31 }
32
33 void Simulator::AddActor(Actor* actor) {
34 auto emplace_times_result =
35 scheduled_times_.insert(std::make_pair(actor, QuicTime::Infinite()));
36 auto emplace_names_result = actor_names_.insert(actor->name());
37
38 // Ensure that the object was actually placed into the map.
39 DCHECK(emplace_times_result.second);
40 DCHECK(emplace_names_result.second);
41 }
42
43 void Simulator::Schedule(Actor* actor, QuicTime new_time) {
44 auto scheduled_time_it = scheduled_times_.find(actor);
45 DCHECK(scheduled_time_it != scheduled_times_.end());
46 QuicTime scheduled_time = scheduled_time_it->second;
47
48 if (scheduled_time <= new_time) {
49 return;
50 }
51
52 if (scheduled_time != QuicTime::Infinite()) {
53 Unschedule(actor);
54 }
55
56 scheduled_time_it->second = new_time;
57 schedule_.insert(std::make_pair(new_time, actor));
58 }
59
60 void Simulator::Unschedule(Actor* actor) {
61 auto scheduled_time_it = scheduled_times_.find(actor);
62 DCHECK(scheduled_time_it != scheduled_times_.end());
63 QuicTime scheduled_time = scheduled_time_it->second;
64
65 DCHECK(scheduled_time != QuicTime::Infinite());
66 auto range = schedule_.equal_range(scheduled_time);
67 for (auto it = range.first; it != range.second; ++it) {
68 if (it->second == actor) {
69 schedule_.erase(it);
70 scheduled_time_it->second = QuicTime::Infinite();
71 return;
72 }
73 }
74 DCHECK(false);
75 }
76
77 const QuicClock* Simulator::GetClock() const {
78 return &clock_;
79 }
80
81 QuicRandom* Simulator::GetRandomGenerator() {
82 if (random_generator_ == nullptr) {
83 random_generator_ = QuicRandom::GetInstance();
84 }
85
86 return random_generator_;
87 }
88
89 QuicBufferAllocator* Simulator::GetBufferAllocator() {
90 return &buffer_allocator_;
91 }
92
93 QuicAlarmFactory* Simulator::GetAlarmFactory() {
94 return &alarm_factory_;
95 }
96
97 void Simulator::HandleNextScheduledActor() {
98 const auto current_event_it = schedule_.begin();
99 QuicTime event_time = current_event_it->first;
100 Actor* actor = current_event_it->second;
101 DVLOG(3) << "At t = " << event_time.ToDebuggingValue() << ", calling "
102 << actor->name();
103
104 Unschedule(actor);
105
106 if (clock_.Now() > event_time) {
107 QUIC_BUG << "Error: event registered by [" << actor->name()
108 << "] requires travelling back in time. Current time: "
109 << clock_.Now().ToDebuggingValue()
110 << ", scheduled time: " << event_time.ToDebuggingValue();
111 }
112 clock_.now_ = event_time;
113
114 actor->Act();
115 }
116
117 } // namespace simulation
118 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/core/congestion_control/simulation/simulator.h ('k') | net/quic/core/congestion_control/simulation/simulator_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698