• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef OHOS_ABILITY_RUNTIME_APPFREEZE_CPU_FREQ_MANAGER_H
16 #define OHOS_ABILITY_RUNTIME_APPFREEZE_CPU_FREQ_MANAGER_H
17 
18 #include <vector>
19 #include <mutex>
20 
21 #include "appfreeze_data.h"
22 #include "cpu_data_processor.h"
23 #include "ffrt.h"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 class AppfreezeCpuFreqManager : public DelayedSingleton<AppfreezeCpuFreqManager>,
29     public std::enable_shared_from_this<AppfreezeCpuFreqManager> {
30         DISALLOW_COPY_AND_MOVE(AppfreezeCpuFreqManager);
31 public:
32     AppfreezeCpuFreqManager();
33     ~AppfreezeCpuFreqManager();
34 
35     static AppfreezeCpuFreqManager &GetInstance();
36     bool InitCpuDataProcessor(const std::string &eventType, int32_t pid, int32_t uid,
37         const std::string &stackPath);
38     std::string WriteCpuInfoToFile(const std::string &eventType, const std::string &bundleName,
39         int32_t uid, int32_t pid, const std::string &eventName);
40 
41 private:
42     void ClearOldCpuData(uint64_t curTime);
43     bool IsSkipTask(uint64_t curTime, const std::string &key, int32_t pid);
44     bool ReadCpuDataByNum(int32_t num, std::vector<CpuFreqData>& parseDatas, TotalTime& totalTime);
45     void ParseCpuData(std::vector<std::vector<CpuFreqData>>& datas, std::vector<TotalTime>& totalTimeLists);
46     std::string GetCpuStr(int code, std::vector<FrequencyPair>& freqPairs, float percentage);
47     bool GetCpuTotalValue(size_t i, std::vector<TotalTime> totalTimeList,
48         std::vector<TotalTime> blockTotalTimeList, TotalTime& totalTime);
49     uint64_t GetProcessCpuTime(int32_t pid);
50     uint64_t GetDeviceRuntime();
51     std::string GetStartTime(uint64_t start);
52     uint64_t GetAppCpuTime(int32_t pid);
53     double GetOptimalCpuTime(int32_t pid);
54     std::string GetStaticInfoHead();
55     std::string GetStaticInfo(int32_t pid, CpuStartTime cpuStartTime);
56     std::string GetCpuInfoContent(const std::vector<std::vector<CpuFreqData>> &handlingHalfCpuData,
57         const std::vector<TotalTime> &totalTimeList);
58     bool IsContainHalfData(const std::string &key, CpuDataProcessor &cpuData, int32_t pid);
59     void WriteDfxLogToFile(const std::string &filePath, const std::string &bundleName);
60 
61     static std::shared_ptr<AppfreezeCpuFreqManager> instance_;
62     static ffrt::mutex freezeInfoMutex_;
63     static int cpuCount_;
64     static std::map<std::string, CpuDataProcessor> cpuInfoMap_;
65 };
66 }  // namespace AppExecFwk
67 }  // namespace OHOS
68 #endif // OHOS_ABILITY_RUNTIME_APPFREEZE_CPU_FREQ_MANAGER_H
69