core_times.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2022 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. #ifndef SERVICES_DEVICE_COMPUTE_PRESSURE_CORE_TIMES_H_
  5. #define SERVICES_DEVICE_COMPUTE_PRESSURE_CORE_TIMES_H_
  6. #include <stdint.h>
  7. #include <initializer_list>
  8. #include "base/gtest_prod_util.h"
  9. namespace device {
  10. // CPU core utilization statistics.
  11. //
  12. // Linux:
  13. // Quantities are expressed in "user hertz", which is a Linux kernel
  14. // configuration knob (USER_HZ). Typical values range between 1/100 seconds
  15. // and 1/1000 seconds. The denominator can be obtained from
  16. // sysconf(_SC_CLK_TCK).
  17. //
  18. // Mac:
  19. // Quantities are expressed in "CPU Ticks", which is an arbitrary unit of time
  20. // recording how many intervals of time elapsed, typically 1/100 of a second.
  21. class CoreTimes {
  22. public:
  23. CoreTimes() = default;
  24. ~CoreTimes() = default;
  25. // Normal processes executing in user mode.
  26. uint64_t user() const { return times_[0]; }
  27. // Niced processes executing in user mode.
  28. uint64_t nice() const { return times_[1]; }
  29. // Processes executing in kernel mode.
  30. uint64_t system() const { return times_[2]; }
  31. // Twiddling thumbs.
  32. uint64_t idle() const { return times_[3]; }
  33. // Waiting for I/O to complete. Unreliable.
  34. uint64_t iowait() const { return times_[4]; }
  35. // Servicing interrupts.
  36. uint64_t irq() const { return times_[5]; }
  37. // Servicing softirqs.
  38. uint64_t softirq() const { return times_[6]; }
  39. // Involuntary wait.
  40. uint64_t steal() const { return times_[7]; }
  41. // Running a normal guest.
  42. uint64_t guest() const { return times_[8]; }
  43. // Running a niced guest.
  44. uint64_t guest_nice() const { return times_[9]; }
  45. // Setters.
  46. //
  47. // Ensure that the reported core usage times are monotonically increasing.
  48. // We assume that by any decrease is a temporary blip.
  49. void set_user(uint64_t time);
  50. void set_nice(uint64_t time);
  51. void set_system(uint64_t time);
  52. void set_idle(uint64_t time);
  53. void set_iowait(uint64_t time);
  54. void set_irq(uint64_t time);
  55. void set_softirq(uint64_t time);
  56. void set_steal(uint64_t time);
  57. void set_guest(uint64_t time);
  58. void set_guest_nice(uint64_t time);
  59. // Computes a CPU's utilization over the time between two stat snapshots.
  60. //
  61. // Returns a value between 0.0 and 1.0 on success, and -1.0 when given
  62. // invalid data, such as a `baseline` that does not represent a stat
  63. // snapshot collected before `this` snapshot.
  64. double TimeUtilization(const CoreTimes& baseline) const;
  65. private:
  66. FRIEND_TEST_ALL_PREFIXES(CoreTimesTest, TimeUtilization_Balanced);
  67. FRIEND_TEST_ALL_PREFIXES(CoreTimesTest, TimeUtilization_EmptyRange);
  68. FRIEND_TEST_ALL_PREFIXES(CoreTimesTest, TimeUtilization_NegativeRange);
  69. FRIEND_TEST_ALL_PREFIXES(CoreTimesTest, TimeUtilization_Computation);
  70. // Used by CoreTimesTest.
  71. CoreTimes(const std::initializer_list<uint64_t>& times);
  72. uint64_t times_[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  73. };
  74. } // namespace device
  75. #endif // SERVICES_DEVICE_COMPUTE_PRESSURE_CORE_TIMES_H_