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