• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 #include "dynamic_buffer.h"
17 
18 #include <fstream>
19 #include <cmath>
20 
21 #include "common_define.h"
22 #include "common_utils.h"
23 #include "hilog/log.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace Hitrace {
28 #ifdef LOG_DOMAIN
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD002D33
31 #endif
32 #ifdef LOG_TAG
33 #undef LOG_TAG
34 #define LOG_TAG "HitraceDynamicBuf"
35 #endif
36 namespace {
37 constexpr int EXPANSION_SIZE = 1024 * 6; // 6M
38 constexpr int LOW_THRESHOLD = 400 * 1024; // 400kb
39 constexpr int BASE_SIZE = 12 * 1024; // 12M
40 constexpr int EXPONENT = 2;
41 constexpr int PAGE_KB = 4;
42 } // namespace
43 
GetPerCpuStatsInfo(const size_t cpuIndex,TraceStatsInfo & traceStats)44 bool DynamicBuffer::GetPerCpuStatsInfo(const size_t cpuIndex, TraceStatsInfo& traceStats)
45 {
46     std::string statsPath = traceRootPath + "per_cpu/cpu" + std::to_string(cpuIndex) + "/stats";
47     std::string standardizedPath = CanonicalizeSpecPath(statsPath.c_str());
48     std::ifstream inFile;
49     inFile.open(standardizedPath.c_str(), std::ios::in);
50     if (!inFile.is_open()) {
51         return false;
52     }
53 
54     std::string line;
55     const size_t oldTsPos = 17;
56     const size_t nowTsPos = 8;
57     const size_t bytesPos = 7;
58     while (std::getline(inFile, line)) {
59         if ((line.find("oldest event ts: ")) != std::string::npos) {
60             if (!OHOS::HiviewDFX::Hitrace::StringToDouble(line.substr(oldTsPos).c_str(), traceStats.oldTs)) {
61                 inFile.close();
62                 return false;
63             }
64         }
65         if ((line.find("now ts: ")) != std::string::npos) {
66             if (!OHOS::HiviewDFX::Hitrace::StringToDouble(line.substr(nowTsPos).c_str(), traceStats.nowTs)) {
67                 inFile.close();
68                 return false;
69             }
70         }
71         if ((line.find("bytes: ")) != std::string::npos) {
72             if (!OHOS::HiviewDFX::Hitrace::StringToInt(line.substr(bytesPos).c_str(), traceStats.bytes)) {
73                 inFile.close();
74                 return false;
75             }
76         }
77     }
78     inFile.close();
79     return true;
80 }
81 
UpdateTraceLoad()82 void DynamicBuffer::UpdateTraceLoad()
83 {
84     if (!allTraceStats.empty()) {
85         allTraceStats.clear();
86     }
87     totalCpusLoad = 0.0;
88     totalAverage = 0;
89     maxAverage = 0;
90     for (int i = 0; i < cpuNums; i++) {
91         TraceStatsInfo traceStats = {};
92         if (!GetPerCpuStatsInfo(i, traceStats)) {
93             HILOG_ERROR(LOG_CORE, "GetPerCpuStatsInfo failed.");
94             return;
95         }
96         int duration = floor(traceStats.nowTs - traceStats.oldTs);
97         if (duration == 0) {
98             HILOG_ERROR(LOG_CORE, "nowTs:%{public}lf, oldTs:%{public}lf", traceStats.nowTs, traceStats.oldTs);
99             return;
100         }
101         traceStats.averageTrace = traceStats.bytes / duration;
102         totalAverage += traceStats.averageTrace;
103         if (maxAverage < traceStats.averageTrace) {
104             maxAverage = traceStats.averageTrace;
105         }
106         traceStats.freq = pow(traceStats.averageTrace, EXPONENT);
107         totalCpusLoad += traceStats.freq;
108         allTraceStats.push_back(traceStats);
109     }
110 }
111 
CalculateBufferSize(std::vector<int> & result)112 void DynamicBuffer::CalculateBufferSize(std::vector<int>& result)
113 {
114     UpdateTraceLoad();
115     if (static_cast<int>(allTraceStats.size()) != cpuNums) {
116         return;
117     }
118     HILOG_DEBUG(LOG_CORE, "hitrace: average = %{public}d.", totalAverage / cpuNums);
119 
120     int totalBonus = 0;
121     if (maxAverage > LOW_THRESHOLD) {
122         totalBonus = EXPANSION_SIZE * cpuNums;
123     }
124 
125     for (int i = 0; i < cpuNums; i++) {
126         int newSize = BASE_SIZE + floor((allTraceStats[i].freq / totalCpusLoad) * totalBonus);
127         newSize = newSize / PAGE_KB * PAGE_KB;
128         result.push_back(newSize);
129     }
130 }
131 
132 } // namespace Hitrace
133 } // namespace HiviewDFX
134 } // namespace OHOS