Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "services/resource_coordinator/coordination_unit/coordination_unit_impl .h" | 5 #include "services/resource_coordinator/coordination_unit/coordination_unit_impl .h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 13 #include "services/resource_coordinator/coordination_unit/coordination_unit_grap h_observer.h" | |
| 13 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" | 14 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" |
| 14 | 15 |
| 15 namespace resource_coordinator { | 16 namespace resource_coordinator { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 using CUIDMap = std::unordered_map<CoordinationUnitID, CoordinationUnitImpl*>; | 20 using CUIDMap = std::unordered_map<CoordinationUnitID, CoordinationUnitImpl*>; |
| 20 | 21 |
| 21 CUIDMap& g_cu_map() { | 22 CUIDMap& g_cu_map() { |
| 22 static CUIDMap* instance = new CUIDMap(); | 23 static CUIDMap* instance = new CUIDMap(); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 if (AddChild(child)) { | 151 if (AddChild(child)) { |
| 151 child->AddParent(this); | 152 child->AddParent(this); |
| 152 } | 153 } |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { | 157 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { |
| 157 // We don't recalculate the policy here as policies are only dependent | 158 // We don't recalculate the policy here as policies are only dependent |
| 158 // on the current CU or its parents, not its children. In other words, | 159 // on the current CU or its parents, not its children. In other words, |
| 159 // policies only bubble down. | 160 // policies only bubble down. |
| 160 return children_.count(child) ? false : children_.insert(child).second; | 161 bool success = |
| 162 children_.count(child) ? false : children_.insert(child).second; | |
| 163 | |
| 164 if (success) { | |
| 165 for (auto* observer : | |
| 166 on_child_added_event_observer_registry_.GetObserversForFilter( | |
| 167 child->id().type)) { | |
| 168 observer->OnChildAddedEvent(this, child); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 return success; | |
| 161 } | 173 } |
| 162 | 174 |
| 163 void CoordinationUnitImpl::RemoveChild(const CoordinationUnitID& child_id) { | 175 void CoordinationUnitImpl::RemoveChild(const CoordinationUnitID& child_id) { |
| 164 auto child_iter = g_cu_map().find(child_id); | 176 auto child_iter = g_cu_map().find(child_id); |
| 165 if (child_iter == g_cu_map().end()) { | 177 if (child_iter == g_cu_map().end()) { |
| 166 return; | 178 return; |
| 167 } | 179 } |
| 168 | 180 |
| 169 CoordinationUnitImpl* child = child_iter->second; | 181 CoordinationUnitImpl* child = child_iter->second; |
| 170 if (!HasChild(child)) { | 182 if (!HasChild(child)) { |
| 171 return; | 183 return; |
| 172 } | 184 } |
| 173 | 185 |
| 174 DCHECK(child->id_ == child_id); | 186 DCHECK(child->id_ == child_id); |
| 175 DCHECK(child != this); | 187 DCHECK(child != this); |
| 176 | 188 |
| 177 if (RemoveChild(child)) { | 189 if (RemoveChild(child)) { |
| 178 child->RemoveParent(this); | 190 child->RemoveParent(this); |
| 179 } | 191 } |
| 180 } | 192 } |
| 181 | 193 |
| 182 bool CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { | 194 bool CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { |
| 183 size_t children_removed = children_.erase(child); | 195 size_t children_removed = children_.erase(child); |
| 184 return children_removed > 0; | 196 bool success = children_removed > 0; |
| 197 | |
| 198 if (success) { | |
| 199 for (auto* observer : | |
| 200 on_child_removed_event_observer_registry_.GetObserversForFilter( | |
| 201 child->id().type)) { | |
| 202 observer->OnChildRemovedEvent(this, child); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 return success; | |
| 185 } | 207 } |
| 186 | 208 |
| 187 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { | 209 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { |
| 188 DCHECK_EQ(0u, parents_.count(parent)); | 210 DCHECK_EQ(0u, parents_.count(parent)); |
| 189 parents_.insert(parent); | 211 parents_.insert(parent); |
| 190 | 212 |
| 213 for (auto* observer : | |
| 214 on_parent_added_event_observer_registry_.GetObserversForFilter( | |
| 215 parent->id().type)) { | |
| 216 observer->OnParentAddedEvent(this, parent); | |
| 217 } | |
| 218 | |
| 191 RecalcCoordinationPolicy(); | 219 RecalcCoordinationPolicy(); |
| 192 } | 220 } |
| 193 | 221 |
| 194 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { | 222 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { |
| 195 size_t parents_removed = parents_.erase(parent); | 223 size_t parents_removed = parents_.erase(parent); |
| 196 DCHECK_EQ(1u, parents_removed); | 224 DCHECK_EQ(1u, parents_removed); |
| 197 | 225 |
| 226 // TODO(matthalp, oysteine) should this go before or | |
| 227 // after RecalcCoordinationPolicy? | |
|
Zhen Wang
2017/06/19 22:54:25
Is this undecided or you intend to ask Oystein for
oystein (OOO til 10th of July)
2017/06/20 18:39:23
Recalc should happen after, though TBH I think we'
matthalp
2017/06/20 19:02:52
RecalcCoordinationPolicy does not do anything righ
matthalp
2017/06/20 19:02:52
Sounds good.
| |
| 228 for (auto* observer : | |
| 229 on_parent_removed_event_observer_registry_.GetObserversForFilter( | |
| 230 parent->id().type)) { | |
| 231 observer->OnParentRemovedEvent(this, parent); | |
| 232 } | |
| 233 | |
| 198 RecalcCoordinationPolicy(); | 234 RecalcCoordinationPolicy(); |
| 199 } | 235 } |
| 200 | 236 |
| 201 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { | 237 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { |
| 202 for (CoordinationUnitImpl* parent : parents_) { | 238 for (CoordinationUnitImpl* parent : parents_) { |
| 203 if (parent == unit || parent->HasParent(unit)) { | 239 if (parent == unit || parent->HasParent(unit)) { |
| 204 return true; | 240 return true; |
| 205 } | 241 } |
| 206 } | 242 } |
| 207 | 243 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 231 | 267 |
| 232 void CoordinationUnitImpl::UnregisterCoordinationPolicyCallback() { | 268 void CoordinationUnitImpl::UnregisterCoordinationPolicyCallback() { |
| 233 policy_callback_.reset(); | 269 policy_callback_.reset(); |
| 234 current_policy_.reset(); | 270 current_policy_.reset(); |
| 235 } | 271 } |
| 236 | 272 |
| 237 double CoordinationUnitImpl::GetCPUUsageForTesting() { | 273 double CoordinationUnitImpl::GetCPUUsageForTesting() { |
| 238 return kCPUUsageUnmeasuredForTesting; | 274 return kCPUUsageUnmeasuredForTesting; |
| 239 } | 275 } |
| 240 | 276 |
| 241 base::Value CoordinationUnitImpl::GetProperty(mojom::PropertyType property) { | 277 base::Value CoordinationUnitImpl::GetProperty( |
| 278 mojom::PropertyType property) const { | |
| 242 auto value_it = property_store_.find(property); | 279 auto value_it = property_store_.find(property); |
| 243 | 280 |
| 244 return value_it != property_store_.end() ? value_it->second : base::Value(); | 281 return value_it != property_store_.end() ? value_it->second : base::Value(); |
| 245 } | 282 } |
| 246 | 283 |
| 247 void CoordinationUnitImpl::ClearProperty(mojom::PropertyType property) { | 284 void CoordinationUnitImpl::ClearProperty(mojom::PropertyType property) { |
| 248 property_store_.erase(property); | 285 property_store_.erase(property); |
| 249 } | 286 } |
| 250 | 287 |
| 251 void CoordinationUnitImpl::SetProperty(mojom::PropertyPtr property) { | 288 void CoordinationUnitImpl::SetProperty(mojom::PropertyPtr property) { |
| 252 SetProperty(property->property, *property->value); | 289 SetProperty(property->property, *property->value); |
| 253 } | 290 } |
| 254 | 291 |
| 255 void CoordinationUnitImpl::SetProperty(mojom::PropertyType property, | 292 void CoordinationUnitImpl::SetProperty(mojom::PropertyType property, |
| 256 base::Value value) { | 293 base::Value value) { |
| 257 // setting a property with an empty value is effectively clearing the | 294 // setting a property with an empty value is effectively clearing the |
| 258 // value from storage | 295 // value from storage |
| 259 if (value.IsType(base::Value::Type::NONE)) { | 296 if (value.IsType(base::Value::Type::NONE)) { |
| 260 ClearProperty(property); | 297 ClearProperty(property); |
| 261 return; | 298 return; |
| 262 } | 299 } |
| 263 | 300 |
| 264 property_store_[property] = value; | 301 property_store_[property] = value; |
| 302 | |
| 303 for (auto* observer : | |
| 304 on_property_changed_event_observer_registry_.GetObserversForFilter( | |
| 305 property)) { | |
| 306 observer->OnPropertyChangedEvent(this, property); | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 void CoordinationUnitImpl::WillBeDestroyed() { | |
| 311 for (auto* observer : on_will_be_destroyed_event_observer_registry_ | |
| 312 .GetObserversWithoutAFilter()) { | |
| 313 observer->OnWillBeDestroyedEvent(this); | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 void CoordinationUnitImpl::ObserveOnChildAddedEvent( | |
| 318 CoordinationUnitGraphObserver* observer, | |
| 319 CoordinationUnitType child_filter) { | |
| 320 on_child_added_event_observer_registry_.AddObserver(observer, child_filter); | |
| 321 } | |
| 322 | |
| 323 void CoordinationUnitImpl::ObserveOnChildAddedEvent( | |
| 324 CoordinationUnitGraphObserver* observer) { | |
| 325 ObserveOnChildAddedEvent( | |
| 326 observer, | |
| 327 CoordinationUnitGraphObserverRegistry<CoordinationUnitType>::kNoFilter); | |
| 328 } | |
| 329 | |
| 330 void CoordinationUnitImpl::ObserveOnParentAddedEvent( | |
| 331 CoordinationUnitGraphObserver* observer, | |
| 332 CoordinationUnitType parent_filter) { | |
| 333 on_parent_added_event_observer_registry_.AddObserver(observer, parent_filter); | |
| 334 } | |
| 335 | |
| 336 void CoordinationUnitImpl::ObserveOnParentAddedEvent( | |
| 337 CoordinationUnitGraphObserver* observer) { | |
| 338 ObserveOnParentAddedEvent( | |
| 339 observer, | |
| 340 CoordinationUnitGraphObserverRegistry<CoordinationUnitType>::kNoFilter); | |
| 341 } | |
| 342 | |
| 343 void CoordinationUnitImpl::ObserveOnPropertyChangedEvent( | |
| 344 CoordinationUnitGraphObserver* observer, | |
| 345 mojom::PropertyType property_filter) { | |
| 346 on_property_changed_event_observer_registry_.AddObserver(observer, | |
| 347 property_filter); | |
| 348 } | |
| 349 | |
| 350 void CoordinationUnitImpl::ObserveOnPropertyChangedEvent( | |
| 351 CoordinationUnitGraphObserver* observer) { | |
| 352 ObserveOnPropertyChangedEvent( | |
| 353 observer, | |
| 354 CoordinationUnitGraphObserverRegistry<mojom::PropertyType>::kNoFilter); | |
| 355 } | |
| 356 | |
| 357 void CoordinationUnitImpl::ObserveOnChildRemovedEvent( | |
| 358 CoordinationUnitGraphObserver* observer, | |
| 359 CoordinationUnitType child_filter) { | |
| 360 on_child_removed_event_observer_registry_.AddObserver(observer, child_filter); | |
| 361 } | |
| 362 | |
| 363 void CoordinationUnitImpl::ObserveOnChildRemovedEvent( | |
| 364 CoordinationUnitGraphObserver* observer) { | |
| 365 ObserveOnChildRemovedEvent( | |
| 366 observer, | |
| 367 CoordinationUnitGraphObserverRegistry<CoordinationUnitType>::kNoFilter); | |
| 368 } | |
| 369 | |
| 370 void CoordinationUnitImpl::ObserveOnParentRemovedEvent( | |
| 371 CoordinationUnitGraphObserver* observer, | |
| 372 CoordinationUnitType parent_filter) { | |
| 373 on_parent_removed_event_observer_registry_.AddObserver(observer, | |
| 374 parent_filter); | |
| 375 } | |
| 376 | |
| 377 void CoordinationUnitImpl::ObserveOnParentRemovedEvent( | |
| 378 CoordinationUnitGraphObserver* observer) { | |
| 379 ObserveOnParentRemovedEvent( | |
| 380 observer, | |
| 381 CoordinationUnitGraphObserverRegistry<CoordinationUnitType>::kNoFilter); | |
| 382 } | |
| 383 | |
| 384 void CoordinationUnitImpl::ObserveOnWillBeDestroyedEvent( | |
| 385 CoordinationUnitGraphObserver* observer) { | |
| 386 on_will_be_destroyed_event_observer_registry_.AddObserver(observer); | |
| 265 } | 387 } |
| 266 | 388 |
| 267 } // namespace resource_coordinator | 389 } // namespace resource_coordinator |
| OLD | NEW |