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 
16 #ifndef HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_CPU_COLLECTOR_IMPL_H
17 #define HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_CPU_COLLECTOR_IMPL_H
18 
19 #include <mutex>
20 
21 #include "collect_device_client.h"
22 #include "cpu_calculator.h"
23 #include "cpu_collector.h"
24 #include "cpu_util.h"
25 #include "process_state_info_collector.h"
26 #include "sys_cpu_usage_collector.h"
27 #include "thread_state_info_collector.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace UCollectUtil {
32 
33 class CpuCollectorImpl : public CpuCollector {
34 public:
35     CpuCollectorImpl();
36     virtual ~CpuCollectorImpl() = default;
37 
38 public:
39     virtual CollectResult<SysCpuLoad> CollectSysCpuLoad() override;
40     virtual CollectResult<SysCpuUsage> CollectSysCpuUsage(bool isNeedUpdate = false) override;
41     virtual CollectResult<double> GetSysCpuUsage() override;
42     virtual CollectResult<ProcessCpuStatInfo> CollectProcessCpuStatInfo(int32_t pid,
43         bool isNeedUpdate = false) override;
44     virtual CollectResult<std::vector<CpuFreq>> CollectCpuFrequency() override;
45     virtual CollectResult<std::vector<ProcessCpuStatInfo>> CollectProcessCpuStatInfos(
46         bool isNeedUpdate = false) override;
47     virtual std::shared_ptr<ThreadCpuCollector> CreateThreadCollector(int pid) override;
48 private:
49     bool InitDeviceClient();
50 
51 private:
52     std::shared_ptr<CollectDeviceClient> deviceClient_ = nullptr;
53     std::shared_ptr<ProcessStatInfoCollector> statInfoCollector_ = nullptr;
54     std::shared_ptr<SysCpuUsageCollector> usageCollector_ = nullptr;
55     std::shared_ptr<CpuCalculator> cpuCalculator_ = nullptr;
56 };
57 } // namespace UCollectUtil
58 } // namespace HiviewDFX
59 } // namespace OHOS
60 #endif // HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_CPU_COLLECTOR_IMPL_H