pressure_test_support.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2021 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. #include "services/device/compute_pressure/pressure_test_support.h"
  5. #include <ostream>
  6. #include "base/location.h"
  7. #include "base/sequence_checker.h"
  8. #include "base/synchronization/lock.h"
  9. #include "base/threading/scoped_blocking_call.h"
  10. #include "services/device/compute_pressure/pressure_sample.h"
  11. namespace device {
  12. bool operator==(const PressureSample& lhs, const PressureSample& rhs) noexcept {
  13. return lhs.cpu_utilization == rhs.cpu_utilization;
  14. }
  15. std::ostream& operator<<(std::ostream& os, const PressureSample& sample) {
  16. os << "[utilization: " << sample.cpu_utilization << "]";
  17. return os;
  18. }
  19. constexpr PressureSample FakeCpuProbe::kInitialSample;
  20. FakeCpuProbe::FakeCpuProbe() : last_sample_(kInitialSample) {
  21. DETACH_FROM_SEQUENCE(sequence_checker_);
  22. }
  23. FakeCpuProbe::~FakeCpuProbe() {
  24. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  25. }
  26. void FakeCpuProbe::Update() {
  27. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  28. // In DCHECKed builds, the ScopedBlockingCall ensures that Update() is only
  29. // called on sequences where I/O is allowed.
  30. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  31. base::BlockingType::MAY_BLOCK);
  32. }
  33. PressureSample FakeCpuProbe::LastSample() {
  34. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  35. base::AutoLock auto_lock(lock_);
  36. return last_sample_;
  37. }
  38. void FakeCpuProbe::SetLastSample(PressureSample sample) {
  39. base::AutoLock auto_lock(lock_);
  40. last_sample_ = sample;
  41. }
  42. } // namespace device