cpufreq_monitor_android.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2018 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 BASE_TRACE_EVENT_CPUFREQ_MONITOR_ANDROID_H_
  5. #define BASE_TRACE_EVENT_CPUFREQ_MONITOR_ANDROID_H_
  6. #include "base/atomicops.h"
  7. #include "base/base_export.h"
  8. #include "base/files/scoped_file.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/trace_event/trace_log.h"
  11. namespace base {
  12. class SingleThreadTaskRunner;
  13. namespace trace_event {
  14. // A delegate to isolate CPU frequency monitor functionality mainly for testing.
  15. class BASE_EXPORT CPUFreqMonitorDelegate {
  16. public:
  17. CPUFreqMonitorDelegate();
  18. virtual ~CPUFreqMonitorDelegate() = default;
  19. // Returns a vector of the minimal set of CPU IDs that we need to monitor to
  20. // get CPU frequency information. For CPUs that operate cores in a cluster,
  21. // i.e. modern Qualcomm 8 cores, this is CPU0 and CPU4.
  22. virtual void GetCPUIds(std::vector<unsigned int>* ids) const;
  23. // Reads the kernel_max_cpu file to determine the max CPU ID, i.e. 7 on an
  24. // 8-core CPU.
  25. virtual unsigned int GetKernelMaxCPUs() const;
  26. // Reads the frequency from the CPUs being monitored and records them.
  27. virtual void RecordFrequency(unsigned int cpu_id, unsigned int freq);
  28. // Returns whether or not the tracing category our CPU Frequency counters are
  29. // in is enabled to determine if we should record.
  30. virtual bool IsTraceCategoryEnabled() const;
  31. // Gets the path to CPU frequency related files for a particular CPU ID.
  32. virtual std::string GetScalingCurFreqPathString(unsigned int cpu_id) const;
  33. virtual std::string GetRelatedCPUsPathString(unsigned int cpu_id) const;
  34. // Allows us to delay creating a task runner, necessary because many tests
  35. // don't like us creating one outside of a TaskEnvironment.
  36. virtual scoped_refptr<SingleThreadTaskRunner> CreateTaskRunner();
  37. private:
  38. DISALLOW_COPY_AND_ASSIGN(CPUFreqMonitorDelegate);
  39. };
  40. // A class for monitoring the CPU frequency on unique cores/clusters.
  41. class BASE_EXPORT CPUFreqMonitor : public TraceLog::EnabledStateObserver {
  42. public:
  43. // Overhead of reading one cluster on a Nexus 6P is ~0.1ms per CPU. 50ms seems
  44. // frequent enough to get a general idea of CPU frequency trends.
  45. static const size_t kDefaultCPUFreqSampleIntervalMs = 50;
  46. CPUFreqMonitor();
  47. ~CPUFreqMonitor() override;
  48. static CPUFreqMonitor* GetInstance();
  49. void Start();
  50. void Stop();
  51. // TraceLog::EnabledStateObserver.
  52. void OnTraceLogEnabled() override;
  53. void OnTraceLogDisabled() override;
  54. bool IsEnabledForTesting();
  55. private:
  56. friend class CPUFreqMonitorTest;
  57. CPUFreqMonitor(std::unique_ptr<CPUFreqMonitorDelegate> delegate);
  58. void Sample(std::vector<std::pair<unsigned int, base::ScopedFD>> fds);
  59. // Uses the delegate's CreateTaskRunner function to lazily create a task
  60. // runner so we don't illegally create a task runner on Chrome startup for
  61. // various tests.
  62. const scoped_refptr<SingleThreadTaskRunner>& GetOrCreateTaskRunner();
  63. base::subtle::Atomic32 is_enabled_ = 0;
  64. scoped_refptr<SingleThreadTaskRunner> task_runner_;
  65. std::unique_ptr<CPUFreqMonitorDelegate> delegate_;
  66. base::WeakPtrFactory<CPUFreqMonitor> weak_ptr_factory_{this};
  67. DISALLOW_COPY_AND_ASSIGN(CPUFreqMonitor);
  68. };
  69. } // namespace trace_event
  70. } // namespace base
  71. #endif // BASE_TRACE_EVENT_CPUFREQ_MONITOR_ANDROID_H_