1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CPU_TIME_READER 17 #define CPU_TIME_READER 18 19 #include <map> 20 #include <vector> 21 22 namespace OHOS { 23 namespace PowerMgr { 24 class CpuTimeReader { 25 public: 26 CpuTimeReader() = default; 27 ~CpuTimeReader() = default; 28 bool Init(); 29 int64_t GetUidCpuActiveTimeMs(int32_t uid); 30 int64_t GetUidCpuClusterTimeMs(int32_t uid, uint32_t cluster); 31 int64_t GetUidCpuFreqTimeMs(int32_t uid, uint32_t cluster, uint32_t speed); 32 bool UpdateCpuTime(); 33 std::vector<int64_t> GetUidCpuTimeMs(int32_t uid); 34 void DumpInfo(std::string& result, int32_t uid); 35 36 private: 37 uint32_t wakelockCounts_ = 0; 38 std::map<int32_t, int64_t> activeTimeMap_; 39 std::map<int32_t, std::vector<int64_t>> clusterTimeMap_; 40 std::map<int32_t, std::map<uint32_t, std::vector<int64_t>>> freqTimeMap_; 41 std::map<int32_t, std::vector<int64_t>> uidTimeMap_; 42 std::map<int32_t, int64_t> lastActiveTimeMap_; 43 std::map<int32_t, std::vector<int64_t>> lastClusterTimeMap_; 44 std::map<int32_t, std::map<uint32_t, std::vector<int64_t>>> lastFreqTimeMap_; 45 std::map<int32_t, std::vector<int64_t>> lastUidTimeMap_; 46 std::map<uint16_t, uint16_t> clustersMap_; 47 bool ReadUidCpuActiveTime(); 48 bool ReadUidCpuActiveTimeImpl(std::string& line, int32_t uid); 49 bool ReadUidCpuClusterTime(); 50 void ReadPolicy(std::vector<uint16_t>& clusters, std::string& line); 51 bool ReadClusterTimeIncrement(std::vector<int64_t>& clusterTime, std::vector<int64_t>& increments, int32_t uid, 52 std::vector<uint16_t>& clusters, std::string& timeLine); 53 bool ReadUidCpuFreqTime(); 54 bool ReadFreqTimeIncrement(std::map<uint32_t, std::vector<int64_t>>& speedTime, 55 std::map<uint32_t, std::vector<int64_t>>& increments, int32_t uid, std::vector<std::string>& splitedTime); 56 bool ProcessFreqTime(std::map<uint32_t, std::vector<int64_t>>& map, std::map<uint32_t, 57 std::vector<int64_t>>& increments, std::map<uint32_t, std::vector<int64_t>>& speedTime, int32_t index, 58 int32_t uid); 59 void DistributeFreqTime(std::map<uint32_t, std::vector<int64_t>>& uidIncrements, 60 std::map<uint32_t, std::vector<int64_t>>& increments); 61 void AddFreqTimeToUid(std::map<uint32_t, std::vector<int64_t>>& uidIncrements, int32_t uid); 62 bool ReadUidCpuTime(); 63 bool ReadUidTimeIncrement(std::vector<int64_t>& clusterTime, std::vector<int64_t>& uidIncrements, int32_t uid, 64 std::string& timeLine); 65 void Split(std::string &origin, char delimiter, std::vector<std::string> &splited); 66 }; 67 } // namespace PowerMgr 68 } // namespace OHOS 69 #endif // CPU_TIME_READER