• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 /*
4  * Copyright (C) 2021 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <chrono>
20 #include <map>
21 
22 #include "ICpuLoadReader.h"
23 #include "IFilesystem.h"
24 #include "ITimeSource.h"
25 #include "Model.h"
26 #include "RealFilesystem.h"
27 #include "TimeSource.h"
28 
29 namespace aidl {
30 namespace google {
31 namespace hardware {
32 namespace power {
33 namespace impl {
34 namespace pixel {
35 
36 struct CpuTime {
37     std::chrono::microseconds idleTime;
38     std::chrono::microseconds totalTime;
39 };
40 
41 // Reads CPU idle stats from /sys/devices/system/cpu/cpuN/cpuidle.
42 class CpuLoadReaderSysDevices : public ICpuLoadReader {
43   public:
CpuLoadReaderSysDevices()44     CpuLoadReaderSysDevices()
45         : mFilesystem(std::make_unique<RealFilesystem>()),
46           mTimeSource(std::make_unique<TimeSource>()) {}
CpuLoadReaderSysDevices(std::unique_ptr<IFilesystem> filesystem,std::unique_ptr<ITimeSource> timeSource)47     CpuLoadReaderSysDevices(std::unique_ptr<IFilesystem> filesystem,
48                             std::unique_ptr<ITimeSource> timeSource)
49         : mFilesystem(std::move(filesystem)), mTimeSource(std::move(timeSource)) {}
50 
51     bool Init() override;
52     bool GetRecentCpuLoads(std::array<double, NUM_CPU_CORES> *cpuCoreIdleTimesPercentage) override;
53     void DumpToStream(std::stringstream &stream) const override;
54 
55   private:
56     const std::unique_ptr<IFilesystem> mFilesystem;
57     const std::unique_ptr<ITimeSource> mTimeSource;
58 
59     std::array<CpuTime, NUM_CPU_CORES> mPreviousCpuTimes;
60     std::vector<std::string> mIdleStateNames;
61 
62     bool ReadCpuTimes(std::array<CpuTime, NUM_CPU_CORES> *result) const;
63     bool ReadIdleStateNames(std::vector<std::string> *result) const;
64 };
65 
66 }  // namespace pixel
67 }  // namespace impl
68 }  // namespace power
69 }  // namespace hardware
70 }  // namespace google
71 }  // namespace aidl
72