• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 
36 private:
37     void InitNumOfCpuCores();
38     void InitCpuDmipses();
39     void InitMaxCpuLoadUnit();
40     bool IsSMTEnabled();
41     uint64_t GetMaxStCpuLoad();
42     uint64_t GetMaxStCpuLoadWithSMT();
43 
44 private:
45     uint32_t numOfCpuCores_ = 0;
46     uint64_t maxCpuLoadUnit_ = 0;
47     std::vector<uint32_t> cpuDmipses_;
48 };
49 } // namespace UCollectUtil
50 } // namespace HiviewDFX
51 } // namespace OHOS
52 #endif // HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_CALCULATOR_CPU_CALCULATOR_H
53