• 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 <vector>
19 #include <system_ability.h>
20 #include "system_ability_status_change_stub.h"
21 #include "util/dump_cpu_info_util.h"
22 #include "delayed_sp_singleton.h"
23 #include "dump_common_utils.h"
24 #include "dump_broker_cpu_stub.h"
25 #include "dump_event_handler.h"
26 #include "dump_cpu_data.h"
27 #include "common.h"
28 #ifdef HIDUMPER_BATTERY_ENABLE
29 #include "common_event_subscriber.h"
30 #endif
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 class DumpCpuData;
35 class DumpManagerCpuService final : public SystemAbility, public DumpBrokerCpuStub {
36     DECLARE_SYSTEM_ABILITY(DumpManagerCpuService)
37 public:
38     DumpManagerCpuService();
39     ~DumpManagerCpuService();
40 public:
41     virtual void OnStart() override;
42 public:
43     // Used for dump request
44     int32_t Request(DumpCpuData &dumpCpuData) override;
45     int32_t DumpCpuUsageData();
46     void InitParam(DumpCpuData &dumpCpuData);
47     void ResetParam();
48     bool SendImmediateEvent();
49     void StartService();
50 public:
51     std::shared_ptr<DumpEventHandler> GetHandler();
52 private:
53     friend DumpDelayedSpSingleton<DumpManagerCpuService>;
54 private:
55     bool Init();
56     void EventHandlerInit();
57     DumpStatus ReadLoadAvgInfo(const std::string& filePath, std::string& info);
58     void CreateDumpTimeString(const std::string& startTime, const std::string& endTime,
59         std::string& timeStr);
60     void AddStrLineToDumpInfo(const std::string& strLine);
61     void CreateCPUStatString(std::string& str);
62     std::shared_ptr<ProcInfo> GetOldProc(const std::string& pid);
63     void DumpProcInfo();
64     static bool SortProcInfo(std::shared_ptr<ProcInfo> &left, std::shared_ptr<ProcInfo> &right);
65     bool SubscribeAppStateEvent();
66     bool SubscribeCommonEvent();
67     bool GetProcCPUInfo();
68 private:
69     using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
70     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_{nullptr};
71     std::shared_ptr<DumpEventHandler> handler_{nullptr};
72     bool started_{false};
73     bool registered_{false};
74     std::string startTime_;
75     std::string endTime_;
76     std::shared_ptr<CPUInfo> curCPUInfo_{nullptr};
77     std::shared_ptr<CPUInfo> oldCPUInfo_{nullptr};
78     std::shared_ptr<ProcInfo> curSpecProc_{nullptr};
79     std::shared_ptr<ProcInfo> oldSpecProc_{nullptr};
80     std::vector<std::shared_ptr<ProcInfo>> curProcs_;
81     std::vector<std::shared_ptr<ProcInfo>> oldProcs_;
82     int cpuUsagePid_{-1};
83     StringMatrix dumpCPUDatas_{nullptr};
84 
85 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
86 public:
SystemAbilityStatusChangeListener()87     explicit SystemAbilityStatusChangeListener() {};
88     ~SystemAbilityStatusChangeListener() override = default;
89 
90     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
91     virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
92 };
93     sptr<SystemAbilityStatusChangeListener> sysAbilityListener_{nullptr};
94 };
95 } // namespace HiviewDFX
96 } // namespace OHOS
97 #endif // HIDUMPER_SERVICES_CPU_MANAGER_SERVICE_H
98