• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "perf_collect_config.h"
17 
18 #include "cjson_util.h"
19 #include "config_policy_utils.h"
20 #include "hiview_logger.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace UCollectUtil {
25 namespace {
26 DEFINE_LOG_TAG("PerfCollectConfig");
27 const uint8_t HIPERF_PROCESS_COUNTS_FOR_NORMAL = 2;
28 const uint8_t HIPERF_PROCESS_COUNTS_FOR_FACTORY = 8;
29 }
30 
MapPerfCallerToString(PerfCaller caller)31 std::string PerfCollectConfig::MapPerfCallerToString(PerfCaller caller)
32 {
33     switch (caller) {
34         case PerfCaller::EVENTLOGGER:
35             return "EVENTLOGGER";
36         case PerfCaller::XPOWER:
37             return "XPOWER";
38         case PerfCaller::UNIFIED_COLLECTOR:
39             return "UNIFIED_COLLECTOR";
40         case PerfCaller::PERFORMANCE_FACTORY:
41             return "PERFORMANCE_FACTORY";
42         default:
43             return "";
44     }
45 }
46 
GetConfigPath()47 std::string PerfCollectConfig::GetConfigPath()
48 {
49     const std::string collectorConfigPath = "etc/hiview/unified_collection/collector.json";
50     char buf[MAX_PATH_LEN] = {0};
51     char* path = GetOneCfgFile(collectorConfigPath.c_str(), buf, MAX_PATH_LEN);
52     if (path == nullptr || *path == '\0') {
53         return "";
54     }
55     return path;
56 }
57 
GetPerfCount(const std::string & configPath)58 std::map<PerfCaller, uint8_t> PerfCollectConfig::GetPerfCount(const std::string& configPath)
59 {
60     // default config for perf collect concurrency
61     std::map<PerfCaller, uint8_t> perfMaxCountForCaller = {
62         // key : caller, value : max hiperf processes count can be started at the same time
63         {PerfCaller::EVENTLOGGER, HIPERF_PROCESS_COUNTS_FOR_NORMAL},
64         {PerfCaller::XPOWER, HIPERF_PROCESS_COUNTS_FOR_NORMAL},
65         {PerfCaller::UNIFIED_COLLECTOR, HIPERF_PROCESS_COUNTS_FOR_NORMAL},
66         {PerfCaller::PERFORMANCE_FACTORY, HIPERF_PROCESS_COUNTS_FOR_FACTORY},
67     };
68     if (configPath.empty()) {
69         return perfMaxCountForCaller;
70     }
71     auto root = CJsonUtil::ParseJsonRoot(configPath);
72     if (root == nullptr) {
73         HIVIEW_LOGW("parse config failed");
74         return perfMaxCountForCaller;
75     }
76     auto perfConfig = CJsonUtil::GetObjectValue(root, "Perf");
77     if (perfConfig == nullptr) {
78         HIVIEW_LOGW("parse perf config failed");
79         cJSON_Delete(root);
80         return perfMaxCountForCaller;
81     }
82     auto concurrencyConfig = CJsonUtil::GetObjectValue(perfConfig, "ConcurrencyStrategy");
83     if (concurrencyConfig == nullptr) {
84         HIVIEW_LOGW("parse config failed of ConcurrencyStrategy");
85         cJSON_Delete(root);
86         return perfMaxCountForCaller;
87     }
88     for (auto& item : perfMaxCountForCaller) {
89         std::string callerStr = MapPerfCallerToString(item.first);
90         int64_t quota = CJsonUtil::GetIntValue(concurrencyConfig, callerStr, -1);
91         if (quota >= 0) {
92             item.second =static_cast<uint8_t>(quota);
93         }
94     }
95     cJSON_Delete(root);
96     return perfMaxCountForCaller;
97 }
98 
GetAllowMemory(const std::string & configPath)99 int64_t PerfCollectConfig::GetAllowMemory(const std::string& configPath)
100 {
101     const int64_t allowMemory = -1; // perf collect allowed memory, -1 means not limit
102     if (configPath.empty()) {
103         return allowMemory;
104     }
105     auto root = CJsonUtil::ParseJsonRoot(configPath);
106     if (root == nullptr) {
107         HIVIEW_LOGW("parse config failed");
108         return allowMemory;
109     }
110     auto perfConfig = CJsonUtil::GetObjectValue(root, "Perf");
111     if (perfConfig == nullptr) {
112         HIVIEW_LOGW("parse perf config failed");
113         cJSON_Delete(root);
114         return allowMemory;
115     }
116     auto res = CJsonUtil::GetIntValue(perfConfig, "AllowMemory", allowMemory);
117     cJSON_Delete(root);
118     return res;
119 }
120 } // UCollectUtil
121 } // HiViewDFX
122 } // OHOS
123