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 OHOS_HIVIEWDFX_HITRACE_DYNAMIC_BUFFER_H
17 #define OHOS_HIVIEWDFX_HITRACE_DYNAMIC_BUFFER_H
18 
19 #include <vector>
20 #include <string>
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace Hitrace {
25 
26 struct TraceStatsInfo {
27     double oldTs = 0;
28     double nowTs = 0;
29     int bytes = 0;
30     int averageTrace = 0;
31     double freq = 0;
32 };
33 
34 class DynamicBuffer {
35 public:
36 
37     /**
38      * traceRootPath, eg: /sys/kernel/debug/tracing/;
39      * cpuNums, Number of CPU cores.
40     */
DynamicBuffer(std::string & traceRootPath,int cpuNums)41     DynamicBuffer(std::string& traceRootPath, int cpuNums)
42         : traceRootPath(traceRootPath), cpuNums(cpuNums) {}
43 
44     /**
45      * Evaluate the set values of each buffer based on the load balancing algorithm,
46      * and the results are stored.
47     */
48     void CalculateBufferSize(std::vector<int>& result);
49 
50 private:
51     bool GetPerCpuStatsInfo(const size_t cpuIndex, TraceStatsInfo& traceStats);
52     void UpdateTraceLoad();
53 
54     std::vector<TraceStatsInfo> allTraceStats;
55     std::string traceRootPath;
56     int cpuNums = 0;
57     double totalCpusLoad = 0.0;
58     int totalAverage = 0;
59     int maxAverage = 0;
60 };
61 
62 
63 } // Hitrace
64 } // HiviewDFX
65 } // OHOS
66 
67 #endif // OHOS_HIVIEWDFX_HITRACE_DYNAMIC_BUFFER_H