• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 HIDUMPER_SERVICES_CPU_MANAGER_SERVICE_H
16 #define HIDUMPER_SERVICES_CPU_MANAGER_SERVICE_H
17 #include <map>
18 #include <mutex>
19 #include <vector>
20 #include <system_ability.h>
21 #include "system_ability_status_change_stub.h"
22 #include "delayed_sp_singleton.h"
23 #include "dump_common_utils.h"
24 #include "dump_cpu_data.h"
25 #include "common.h"
26 #include "hidumper_cpu_service_stub.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 struct CPUInfo {
31     double userUsage; // user space usage
32     double niceUsage; // adjust process priority cpu usage
33     double systemUsage; // kernel space cpu usage
34     double idleUsage; // idle cpu usage
35     double ioWaitUsage; // io wait cpu usage
36     double irqUsage; // hard interrupt cpu usage
37     double softIrqUsage; // soft interrupt cpu usage
38 };
39 
40 struct ProcInfo {
41     double userSpaceUsage;
42     double sysSpaceUsage;
43     double totalUsage;
44     std::string pid;
45     std::string comm;
46     std::string minflt;
47     std::string majflt;
48 };
49 class DumpCpuData;
50 class DumpManagerCpuService final : public SystemAbility, public HidumperCpuServiceStub {
51     DECLARE_SYSTEM_ABILITY(DumpManagerCpuService)
52 public:
53     DumpManagerCpuService();
54     ~DumpManagerCpuService();
55 public:
56     virtual void OnStart() override;
57 public:
58     // Used for dump request
59     int32_t Request(DumpCpuData &dumpCpuData) override;
60     int32_t GetCpuUsageByPid(int32_t pid, double &cpuUsage) override;
61     int32_t DumpCpuUsageData();
62     void InitParam(DumpCpuData &dumpCpuData);
63     void ResetParam();
64     bool GetSingleProcInfo(int pid, std::shared_ptr<ProcInfo> &specProc);
65     void StartService();
66 private:
67     friend DumpDelayedSpSingleton<DumpManagerCpuService>;
68 private:
69     DumpStatus ReadLoadAvgInfo(std::string& info);
70     void CreateDumpTimeString(const std::string& startTime, const std::string& endTime,
71         std::string& timeStr);
72     void AddStrLineToDumpInfo(const std::string& strLine);
73     void CreateCPUStatString(std::string& str);
74     void DumpProcInfo();
75     static bool SortProcInfo(std::shared_ptr<ProcInfo> &left, std::shared_ptr<ProcInfo> &right);
76     bool SubscribeAppStateEvent();
77     bool SubscribeCommonEvent();
78 
79     bool GetSysCPUInfo(std::shared_ptr<CPUInfo> &cpuInfo);
80     bool GetAllProcInfo(std::vector<std::shared_ptr<ProcInfo>> &procInfos);
81     bool GetDateAndTime(uint64_t timeStamp, std::string& dateTime);
82     bool HasDumpPermission() const;
83 private:
84     using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
85     bool started_{false};
86     bool registered_{false};
87     uint64_t startTime_{0};
88     uint64_t endTime_{0};
89     std::mutex mutex_;
90     std::shared_ptr<CPUInfo> curCPUInfo_{nullptr};
91     std::shared_ptr<ProcInfo> curSpecProc_{nullptr};
92     std::vector<std::shared_ptr<ProcInfo>> curProcs_;
93     int cpuUsagePid_{-1};
94     StringMatrix dumpCPUDatas_{nullptr};
95 };
96 } // namespace HiviewDFX
97 } // namespace OHOS
98 #endif // HIDUMPER_SERVICES_CPU_MANAGER_SERVICE_H
99