1 // Copyright 2016 The Chromium Authors
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/spdy/http2_priority_dependencies.h"
6 #include "base/trace_event/memory_usage_estimator.h"
7
8 namespace net {
9
10 Http2PriorityDependencies::Http2PriorityDependencies() = default;
11
12 Http2PriorityDependencies::~Http2PriorityDependencies() = default;
13
OnStreamCreation(spdy::SpdyStreamId id,spdy::SpdyPriority priority,spdy::SpdyStreamId * parent_stream_id,int * weight,bool * exclusive)14 void Http2PriorityDependencies::OnStreamCreation(
15 spdy::SpdyStreamId id,
16 spdy::SpdyPriority priority,
17 spdy::SpdyStreamId* parent_stream_id,
18 int* weight,
19 bool* exclusive) {
20 if (entry_by_stream_id_.find(id) != entry_by_stream_id_.end())
21 return;
22
23 *parent_stream_id = 0;
24 *exclusive = true;
25 // Since the generated dependency graph is a single linked list, the value
26 // of weight should not actually matter, and perhaps the default weight of 16
27 // from the HTTP/2 spec would be reasonable. However, there are some servers
28 // which currently interpret the weight field like an old SPDY priority value.
29 // As long as those servers need to be supported, weight should be set to
30 // a value those servers will interpret correctly.
31 *weight = spdy::Spdy3PriorityToHttp2Weight(priority);
32
33 // Dependent on the lowest-priority stream that has a priority >= |priority|.
34 IdList::iterator parent;
35 if (PriorityLowerBound(priority, &parent)) {
36 *parent_stream_id = parent->first;
37 }
38
39 id_priority_lists_[priority].emplace_back(id, priority);
40 auto it = id_priority_lists_[priority].end();
41 --it;
42 entry_by_stream_id_[id] = it;
43 }
44
PriorityLowerBound(spdy::SpdyPriority priority,IdList::iterator * bound)45 bool Http2PriorityDependencies::PriorityLowerBound(spdy::SpdyPriority priority,
46 IdList::iterator* bound) {
47 for (int i = priority; i >= spdy::kV3HighestPriority; --i) {
48 if (!id_priority_lists_[i].empty()) {
49 *bound = id_priority_lists_[i].end();
50 --(*bound);
51 return true;
52 }
53 }
54 return false;
55 }
56
ParentOfStream(spdy::SpdyStreamId id,IdList::iterator * parent)57 bool Http2PriorityDependencies::ParentOfStream(spdy::SpdyStreamId id,
58 IdList::iterator* parent) {
59 auto entry = entry_by_stream_id_.find(id);
60 DCHECK(entry != entry_by_stream_id_.end());
61
62 spdy::SpdyPriority priority = entry->second->second;
63 auto curr = entry->second;
64 if (curr != id_priority_lists_[priority].begin()) {
65 *parent = curr;
66 --(*parent);
67 return true;
68 }
69
70 // |id| is at the head of its priority list, so its parent is the last
71 // entry of the next-highest priority band.
72 if (priority == spdy::kV3HighestPriority) {
73 return false;
74 }
75 return PriorityLowerBound(priority - 1, parent);
76 }
77
ChildOfStream(spdy::SpdyStreamId id,IdList::iterator * child)78 bool Http2PriorityDependencies::ChildOfStream(spdy::SpdyStreamId id,
79 IdList::iterator* child) {
80 auto entry = entry_by_stream_id_.find(id);
81 DCHECK(entry != entry_by_stream_id_.end());
82
83 spdy::SpdyPriority priority = entry->second->second;
84 *child = entry->second;
85 ++(*child);
86 if (*child != id_priority_lists_[priority].end()) {
87 return true;
88 }
89
90 // |id| is at the end of its priority list, so its child is the stream
91 // at the front of the next-lowest priority band.
92 for (int i = priority + 1; i <= spdy::kV3LowestPriority; ++i) {
93 if (!id_priority_lists_[i].empty()) {
94 *child = id_priority_lists_[i].begin();
95 return true;
96 }
97 }
98
99 return false;
100 }
101
102 std::vector<Http2PriorityDependencies::DependencyUpdate>
OnStreamUpdate(spdy::SpdyStreamId id,spdy::SpdyPriority new_priority)103 Http2PriorityDependencies::OnStreamUpdate(spdy::SpdyStreamId id,
104 spdy::SpdyPriority new_priority) {
105 std::vector<DependencyUpdate> result;
106 result.reserve(2);
107
108 auto curr_entry = entry_by_stream_id_.find(id);
109 if (curr_entry == entry_by_stream_id_.end()) {
110 return result;
111 }
112
113 spdy::SpdyPriority old_priority = curr_entry->second->second;
114 if (old_priority == new_priority) {
115 return result;
116 }
117
118 IdList::iterator old_parent;
119 bool old_has_parent = ParentOfStream(id, &old_parent);
120
121 IdList::iterator new_parent;
122 bool new_has_parent = PriorityLowerBound(new_priority, &new_parent);
123
124 // If we move |id| from MEDIUM to LOW, where HIGH = {other_id}, MEDIUM = {id},
125 // and LOW = {}, then PriorityLowerBound(new_priority) is |id|. In this corner
126 // case, |id| does not change parents.
127 if (new_has_parent && new_parent->first == id) {
128 new_has_parent = old_has_parent;
129 new_parent = old_parent;
130 }
131
132 // If the parent has changed, we generate dependency updates.
133 if ((old_has_parent != new_has_parent) ||
134 (old_has_parent && old_parent->first != new_parent->first)) {
135 // If |id| has a child, then that child moves to be dependent on
136 // |old_parent|.
137 IdList::iterator old_child;
138 if (ChildOfStream(id, &old_child)) {
139 int weight = spdy::Spdy3PriorityToHttp2Weight(old_child->second);
140 if (old_has_parent) {
141 result.push_back({old_child->first, old_parent->first, weight, true});
142 } else {
143 result.push_back({old_child->first, 0, weight, true});
144 }
145 }
146
147 int weight = spdy::Spdy3PriorityToHttp2Weight(new_priority);
148 // |id| moves to be dependent on |new_parent|.
149 if (new_has_parent) {
150 result.push_back({id, new_parent->first, weight, true});
151 } else {
152 result.push_back({id, 0, weight, true});
153 }
154 }
155
156 // Move to the new priority.
157 auto old = entry_by_stream_id_.find(id);
158 id_priority_lists_[old->second->second].erase(old->second);
159 id_priority_lists_[new_priority].emplace_back(id, new_priority);
160 auto it = id_priority_lists_[new_priority].end();
161 --it;
162 entry_by_stream_id_[id] = it;
163
164 return result;
165 }
166
OnStreamDestruction(spdy::SpdyStreamId id)167 void Http2PriorityDependencies::OnStreamDestruction(spdy::SpdyStreamId id) {
168 auto emit = entry_by_stream_id_.find(id);
169 if (emit == entry_by_stream_id_.end())
170 return;
171
172 auto it = emit->second;
173 id_priority_lists_[it->second].erase(it);
174 entry_by_stream_id_.erase(emit);
175 }
176
177 } // namespace net
178