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_CALCULATOR_CPU_CALCULATOR_H
17 #define HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_CALCULATOR_CPU_CALCULATOR_H
18 
19 #include <vector>
20 #include <unordered_map>
21 
22 #include "cpu.h"
23 #include "cpu_util.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace UCollectUtil {
28 class CpuCalculator {
29 public:
30     CpuCalculator();
31     ~CpuCalculator() = default;
32     double CalculateCpuLoad(uint64_t currCpuLoad, uint64_t lastCpuLoad, uint64_t statPeriod);
33     double CalculateCpuUsage(uint64_t currCpuUsage, uint64_t lastCpuUsage, uint64_t statPeriod);
34     CpuUsageInfo CalculateSysCpuUsageInfo(const CpuTimeInfo& currCpuTimeInfo, const CpuTimeInfo& lastCpuTimeInfo);
35     std::vector<uint32_t> GetCpuDmipses();
36 
37 private:
38     void InitNumOfCpuCores();
39     void InitCpuDmipses();
40     void InitMaxCpuLoadUnit();
41     bool IsSMTEnabled();
42     uint64_t GetMaxStCpuLoad();
43     uint64_t GetMaxStCpuLoadWithSMT();
44 
45 private:
46     uint32_t numOfCpuCores_ = 0;
47     uint64_t maxCpuLoadUnit_ = 0;
48     std::vector<uint32_t> cpuDmipses_;
49 };
50 } // namespace UCollectUtil
51 } // namespace HiviewDFX
52 } // namespace OHOS
53 #endif // HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_CALCULATOR_CPU_CALCULATOR_H
54