1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ci#include "metrics.h"
17fb726d48Sopenharmony_ci#include <regex>
18fb726d48Sopenharmony_ci#include "string_help.h"
19fb726d48Sopenharmony_ci
20fb726d48Sopenharmony_cinamespace SysTuning {
21fb726d48Sopenharmony_cinamespace TraceStreamer {
22fb726d48Sopenharmony_ciconstexpr uint32_t EXTRA_CHAR = 4;
23fb726d48Sopenharmony_ciconstexpr uint32_t SEND_FINISH = 1;
24fb726d48Sopenharmony_ciconstexpr uint32_t FUNCTION_ITEM_DUR_MIN = 1;
25fb726d48Sopenharmony_ciconstexpr uint32_t FUNCTION_ITEM_DUR_MAX = 2;
26fb726d48Sopenharmony_ciconstexpr uint32_t FUNCTION_ITEM_DUR_AVG = 3;
27fb726d48Sopenharmony_ciconstexpr uint32_t FUNCTION_ITEM_FUNCTION_NAME = 4;
28fb726d48Sopenharmony_ciMetrics ::Metrics()
29fb726d48Sopenharmony_ci{
30fb726d48Sopenharmony_ci    metricsFunction_ = {
31fb726d48Sopenharmony_ci        {TRACE_MEM, std::bind(&Metrics::InitMemoryStrategy, this, std::placeholders::_1)},
32fb726d48Sopenharmony_ci        {TRACE_MEM_TOP_TEN, std::bind(&Metrics::InitMemoryStrategy, this, std::placeholders::_1)},
33fb726d48Sopenharmony_ci        {TRACE_MEM_UNAGG, std::bind(&Metrics::InitMemoryUnAggStrategy, this, std::placeholders::_1)},
34fb726d48Sopenharmony_ci        {TRACE_TASK_NAMES, std::bind(&Metrics::InitMemoryTaskNameStrategy, this, std::placeholders::_1)},
35fb726d48Sopenharmony_ci        {TRACE_STATS, std::bind(&Metrics::InitTraceStatsStrategy, this, std::placeholders::_1)},
36fb726d48Sopenharmony_ci        {TRACE_METADATA, std::bind(&Metrics::InitTraceMetaDataStrategy, this, std::placeholders::_1)},
37fb726d48Sopenharmony_ci        {SYS_CALLS, std::bind(&Metrics::InitSysCallStrategy, this, std::placeholders::_1)}};
38fb726d48Sopenharmony_ci    initMetricsMap_ = {
39fb726d48Sopenharmony_ci        {METRICS_TRACE_MEM, TRACE_MEM},
40fb726d48Sopenharmony_ci        {METRICS_TRACE_MEM_TOP_TEN, TRACE_MEM_TOP_TEN},
41fb726d48Sopenharmony_ci        {METRICS_TRACE_MEM_UNAGG, TRACE_MEM_UNAGG},
42fb726d48Sopenharmony_ci        {METRICS_TRACE_TASK_NAMES, TRACE_TASK_NAMES},
43fb726d48Sopenharmony_ci        {METRICS_TRACE_STATS, TRACE_STATS},
44fb726d48Sopenharmony_ci        {METRICS_TRACE_METADATA, TRACE_METADATA},
45fb726d48Sopenharmony_ci        {METRICS_SYS_CALLS, SYS_CALLS},
46fb726d48Sopenharmony_ci    };
47fb726d48Sopenharmony_ci}
48fb726d48Sopenharmony_ci
49fb726d48Sopenharmony_civoid Metrics::ParserJson(const std::string &metrics, std::string &result)
50fb726d48Sopenharmony_ci{
51fb726d48Sopenharmony_ci    result = result.substr(EXTRA_CHAR, result.size());
52fb726d48Sopenharmony_ci    auto it = metricsFunction_.find(metrics);
53fb726d48Sopenharmony_ci    if (it == metricsFunction_.end()) {
54fb726d48Sopenharmony_ci        TS_LOGE("Not support metrics!");
55fb726d48Sopenharmony_ci        return;
56fb726d48Sopenharmony_ci    }
57fb726d48Sopenharmony_ci    it->second(result);
58fb726d48Sopenharmony_ci}
59fb726d48Sopenharmony_ci
60fb726d48Sopenharmony_civoid Metrics::InitMemoryStrategy(const std::string &result)
61fb726d48Sopenharmony_ci{
62fb726d48Sopenharmony_ci    json jMessage = json::parse(result);
63fb726d48Sopenharmony_ci    const uint32_t typeInfoItemMax = 0;
64fb726d48Sopenharmony_ci    const uint32_t typeInfoItemMin = 1;
65fb726d48Sopenharmony_ci    const uint32_t typeInfoItemAvg = 2;
66fb726d48Sopenharmony_ci    const uint32_t processMetricesItemsName = 4;
67fb726d48Sopenharmony_ci    for (int i = 0; i < jMessage.at("values").size(); i++) {
68fb726d48Sopenharmony_ci        TypeInfoItem typeInfoItem;
69fb726d48Sopenharmony_ci        typeInfoItem.max = jMessage.at("values")[i].at(typeInfoItemMax);
70fb726d48Sopenharmony_ci        typeInfoItem.min = jMessage.at("values")[i].at(typeInfoItemMin);
71fb726d48Sopenharmony_ci        typeInfoItem.avg = jMessage.at("values")[i].at(typeInfoItemAvg);
72fb726d48Sopenharmony_ci        ProcessMetricsItems processMetricsItems;
73fb726d48Sopenharmony_ci        processMetricsItems.overallCounters = typeInfoItem;
74fb726d48Sopenharmony_ci        processMetricsItems.processName = jMessage.at("values")[i].at(processMetricesItemsName);
75fb726d48Sopenharmony_ci        memStrategy_.emplace_back(std::move(processMetricsItems));
76fb726d48Sopenharmony_ci    }
77fb726d48Sopenharmony_ci    return;
78fb726d48Sopenharmony_ci}
79fb726d48Sopenharmony_civoid Metrics::InitMemoryUnAggStrategy(const std::string &result)
80fb726d48Sopenharmony_ci{
81fb726d48Sopenharmony_ci    json jMessage = json::parse(result);
82fb726d48Sopenharmony_ci    const uint32_t processValuesItemName = 0;
83fb726d48Sopenharmony_ci    const uint32_t namesIndex = 1;
84fb726d48Sopenharmony_ci    const uint32_t valuesIndex = 2;
85fb726d48Sopenharmony_ci    const uint32_t timesIndex = 3;
86fb726d48Sopenharmony_ci    for (int i = 0; i < jMessage.at("values").size(); i++) {
87fb726d48Sopenharmony_ci        ProcessValuesItem processValuesItem;
88fb726d48Sopenharmony_ci        if (jMessage.at("values")[i].at(0).is_null()) {
89fb726d48Sopenharmony_ci            processValuesItem.processName = "";
90fb726d48Sopenharmony_ci        } else {
91fb726d48Sopenharmony_ci            processValuesItem.processName = jMessage.at("values")[i].at(processValuesItemName);
92fb726d48Sopenharmony_ci        }
93fb726d48Sopenharmony_ci        auto names = base::SplitStringToVec(jMessage.at("values")[i].at(namesIndex), ",");
94fb726d48Sopenharmony_ci        auto values = base::SplitStringToVec(jMessage.at("values")[i].at(valuesIndex), ",");
95fb726d48Sopenharmony_ci        auto times = base::SplitStringToVec(jMessage.at("values")[i].at(timesIndex), ",");
96fb726d48Sopenharmony_ci        auto oomScoreValue = 0;
97fb726d48Sopenharmony_ci        for (auto index = 0; index < names.size(); index++) {
98fb726d48Sopenharmony_ci            if (names[index] == "oom_score_adj") {
99fb726d48Sopenharmony_ci                oomScoreValue = atoi(values.at(index).c_str());
100fb726d48Sopenharmony_ci            }
101fb726d48Sopenharmony_ci            TypeItem typeItem;
102fb726d48Sopenharmony_ci            typeItem.ts = atoll(times.at(index).c_str());
103fb726d48Sopenharmony_ci            typeItem.oomScore = oomScoreValue;
104fb726d48Sopenharmony_ci            typeItem.value = atoi(values.at(index).c_str());
105fb726d48Sopenharmony_ci            if (names.at(index) == "mem.rss.anon") {
106fb726d48Sopenharmony_ci                processValuesItem.anonRss = typeItem;
107fb726d48Sopenharmony_ci            } else if (names.at(index) == "mem.swap") {
108fb726d48Sopenharmony_ci                processValuesItem.swap = typeItem;
109fb726d48Sopenharmony_ci            } else if (names.at(index) == "mem.rss.file") {
110fb726d48Sopenharmony_ci                processValuesItem.fileRss = typeItem;
111fb726d48Sopenharmony_ci            } else if (names.at(index) == "oom_score_adj") {
112fb726d48Sopenharmony_ci                processValuesItem.anonAndSwap = typeItem;
113fb726d48Sopenharmony_ci            }
114fb726d48Sopenharmony_ci        }
115fb726d48Sopenharmony_ci        memAggStrategy_.emplace_back(processValuesItem);
116fb726d48Sopenharmony_ci    }
117fb726d48Sopenharmony_ci    return;
118fb726d48Sopenharmony_ci}
119fb726d48Sopenharmony_civoid Metrics::InitMemoryTaskNameStrategy(const std::string &result)
120fb726d48Sopenharmony_ci{
121fb726d48Sopenharmony_ci    json jMessage = json::parse(result);
122fb726d48Sopenharmony_ci    const uint32_t jmessageValueSizeOne = 1;
123fb726d48Sopenharmony_ci    const uint32_t jmessageValueSizeTwo = 2;
124fb726d48Sopenharmony_ci    const uint32_t jmessageValueSizeThree = 3;
125fb726d48Sopenharmony_ci    for (int i = 0; i < jMessage.at("values").size(); i++) {
126fb726d48Sopenharmony_ci        TaskProcessItem taskProcessItem;
127fb726d48Sopenharmony_ci        taskProcessItem.pid = jMessage.at("values")[i].at(jmessageValueSizeOne);
128fb726d48Sopenharmony_ci        if (jMessage.at("values")[i].at(jmessageValueSizeTwo).is_null()) {
129fb726d48Sopenharmony_ci            taskProcessItem.processName = "";
130fb726d48Sopenharmony_ci        } else {
131fb726d48Sopenharmony_ci            taskProcessItem.processName = jMessage.at("values")[i].at(jmessageValueSizeTwo);
132fb726d48Sopenharmony_ci        }
133fb726d48Sopenharmony_ci        if (!jMessage.at("values")[i].at(jmessageValueSizeThree).is_null()) {
134fb726d48Sopenharmony_ci            taskProcessItem.threadName =
135fb726d48Sopenharmony_ci                base::SplitStringToVec(jMessage.at("values")[i].at(jmessageValueSizeThree), ",");
136fb726d48Sopenharmony_ci        }
137fb726d48Sopenharmony_ci        taskNameStrategy_.emplace_back(taskProcessItem);
138fb726d48Sopenharmony_ci    }
139fb726d48Sopenharmony_ci    return;
140fb726d48Sopenharmony_ci}
141fb726d48Sopenharmony_civoid Metrics::InitTraceStatsStrategy(const std::string &result)
142fb726d48Sopenharmony_ci{
143fb726d48Sopenharmony_ci    json jMessage = json::parse(result);
144fb726d48Sopenharmony_ci    const uint32_t statItemName = 0;
145fb726d48Sopenharmony_ci    const uint32_t statItemCount = 2;
146fb726d48Sopenharmony_ci    const uint32_t statItemSource = 3;
147fb726d48Sopenharmony_ci    const uint32_t statItemSeverity = 4;
148fb726d48Sopenharmony_ci    for (int i = 0; i < jMessage.at("values").size(); i++) {
149fb726d48Sopenharmony_ci        StatItem statItem;
150fb726d48Sopenharmony_ci        statItem.name = jMessage.at("values")[i].at(statItemName);
151fb726d48Sopenharmony_ci        statItem.count = jMessage.at("values")[i].at(statItemCount);
152fb726d48Sopenharmony_ci        statItem.source = jMessage.at("values")[i].at(statItemSource);
153fb726d48Sopenharmony_ci        statItem.severity = jMessage.at("values")[i].at(statItemSeverity);
154fb726d48Sopenharmony_ci        statStrategy_.emplace_back(statItem);
155fb726d48Sopenharmony_ci    }
156fb726d48Sopenharmony_ci    return;
157fb726d48Sopenharmony_ci}
158fb726d48Sopenharmony_civoid Metrics::InitTraceMetaDataStrategy(const std::string &result)
159fb726d48Sopenharmony_ci{
160fb726d48Sopenharmony_ci    json jMessage = json::parse(result);
161fb726d48Sopenharmony_ci    const uint32_t traceMetaDataItemName = 0;
162fb726d48Sopenharmony_ci    const uint32_t traceMetaDataItemValue = 1;
163fb726d48Sopenharmony_ci    for (int i = 0; i < jMessage.at("values").size(); i++) {
164fb726d48Sopenharmony_ci        TraceMetadataItem traceMetadataItem;
165fb726d48Sopenharmony_ci        traceMetadataItem.name = jMessage.at("values")[i].at(traceMetaDataItemName);
166fb726d48Sopenharmony_ci        traceMetadataItem.value = jMessage.at("values")[i].at(traceMetaDataItemValue);
167fb726d48Sopenharmony_ci        metaDataStrategy_.emplace_back(traceMetadataItem);
168fb726d48Sopenharmony_ci    }
169fb726d48Sopenharmony_ci    return;
170fb726d48Sopenharmony_ci}
171fb726d48Sopenharmony_civoid Metrics::InitSysCallStrategy(const std::string &result)
172fb726d48Sopenharmony_ci{
173fb726d48Sopenharmony_ci    json jMessage = json::parse(result);
174fb726d48Sopenharmony_ci    for (int i = 0; i < jMessage.at("values").size(); i++) {
175fb726d48Sopenharmony_ci        FunctionItem functionItem;
176fb726d48Sopenharmony_ci        functionItem.functionName = jMessage.at("values")[i].at(FUNCTION_ITEM_FUNCTION_NAME);
177fb726d48Sopenharmony_ci        if (!jMessage.at("values")[i].at(FUNCTION_ITEM_DUR_MAX).is_null()) {
178fb726d48Sopenharmony_ci            functionItem.durMax = jMessage.at("values")[i].at(FUNCTION_ITEM_DUR_MAX);
179fb726d48Sopenharmony_ci        }
180fb726d48Sopenharmony_ci        if (!jMessage.at("values")[i].at(FUNCTION_ITEM_DUR_MIN).is_null()) {
181fb726d48Sopenharmony_ci            functionItem.durMin = jMessage.at("values")[i].at(FUNCTION_ITEM_DUR_MIN);
182fb726d48Sopenharmony_ci        }
183fb726d48Sopenharmony_ci        if (!jMessage.at("values")[i].at(FUNCTION_ITEM_DUR_AVG).is_null()) {
184fb726d48Sopenharmony_ci            functionItem.durAvg = jMessage.at("values")[i].at(FUNCTION_ITEM_DUR_AVG);
185fb726d48Sopenharmony_ci        }
186fb726d48Sopenharmony_ci        sysCallStrategy_.emplace_back(functionItem);
187fb726d48Sopenharmony_ci    }
188fb726d48Sopenharmony_ci    return;
189fb726d48Sopenharmony_ci}
190fb726d48Sopenharmony_civoid Metrics::PrintMetricsResult(uint32_t metricsIndex, ResultCallBack callback)
191fb726d48Sopenharmony_ci{
192fb726d48Sopenharmony_ci    std::string res = "\r\n";
193fb726d48Sopenharmony_ci    std::string metricsName = "";
194fb726d48Sopenharmony_ci    std::string repeateValue = "";
195fb726d48Sopenharmony_ci    switch (metricsIndex) {
196fb726d48Sopenharmony_ci        case METRICS_TRACE_MEM:
197fb726d48Sopenharmony_ci            UpdataRepeateValueByTraceMem(repeateValue, metricsName);
198fb726d48Sopenharmony_ci            break;
199fb726d48Sopenharmony_ci        case METRICS_TRACE_MEM_TOP_TEN:
200fb726d48Sopenharmony_ci            UpdataRepeateValueByTopTen(repeateValue, metricsName);
201fb726d48Sopenharmony_ci            break;
202fb726d48Sopenharmony_ci        case METRICS_TRACE_MEM_UNAGG:
203fb726d48Sopenharmony_ci            UpdataRepeateValueByMemUnagg(repeateValue, metricsName);
204fb726d48Sopenharmony_ci            break;
205fb726d48Sopenharmony_ci        case METRICS_TRACE_TASK_NAMES:
206fb726d48Sopenharmony_ci            UpdataRepeateValueByTaskNames(repeateValue, metricsName);
207fb726d48Sopenharmony_ci            break;
208fb726d48Sopenharmony_ci        case METRICS_TRACE_STATS:
209fb726d48Sopenharmony_ci            UpdataRepeateValueByStats(repeateValue, metricsName);
210fb726d48Sopenharmony_ci            break;
211fb726d48Sopenharmony_ci        case METRICS_TRACE_METADATA:
212fb726d48Sopenharmony_ci            UpdataRepeateValueByMetadata(repeateValue, metricsName);
213fb726d48Sopenharmony_ci            break;
214fb726d48Sopenharmony_ci        case METRICS_SYS_CALLS:
215fb726d48Sopenharmony_ci            UpdataRepeateValueBySysCalls(repeateValue, metricsName);
216fb726d48Sopenharmony_ci            break;
217fb726d48Sopenharmony_ci        default:
218fb726d48Sopenharmony_ci            break;
219fb726d48Sopenharmony_ci    }
220fb726d48Sopenharmony_ci    if (repeateValue != "") {
221fb726d48Sopenharmony_ci        repeateValue.pop_back();
222fb726d48Sopenharmony_ci    }
223fb726d48Sopenharmony_ci    res += metricsName + ": {" + repeateValue + "}";
224fb726d48Sopenharmony_ci    res = JsonFormat(res) + "\r\n";
225fb726d48Sopenharmony_ci    std::regex strRegex(",");
226fb726d48Sopenharmony_ci    auto str = std::regex_replace(res, strRegex, "");
227fb726d48Sopenharmony_ci#ifndef IS_WASM
228fb726d48Sopenharmony_ci    printf("%s", str.c_str());
229fb726d48Sopenharmony_ci#else
230fb726d48Sopenharmony_ci    callback(str, SEND_FINISH);
231fb726d48Sopenharmony_ci#endif
232fb726d48Sopenharmony_ci    return;
233fb726d48Sopenharmony_ci}
234fb726d48Sopenharmony_civoid Metrics::UpdataRepeateValueByTraceMem(std::string &repeateValue, std::string &metricsName)
235fb726d48Sopenharmony_ci{
236fb726d48Sopenharmony_ci    metricsName = TRACE_MEM;
237fb726d48Sopenharmony_ci    for (auto item : memStrategy_) {
238fb726d48Sopenharmony_ci        repeateValue += PROCESS_METRICES + PROCESS_NAME + "\"" + item.processName + "\"," + OVERALL_COUNTERS +
239fb726d48Sopenharmony_ci                        ANON_RSS + MIN + std::to_string(item.overallCounters.min) + "," + MAX +
240fb726d48Sopenharmony_ci                        std::to_string(item.overallCounters.max) + "," + AVG +
241fb726d48Sopenharmony_ci                        std::to_string(item.overallCounters.avg) + "}}},";
242fb726d48Sopenharmony_ci    }
243fb726d48Sopenharmony_ci}
244fb726d48Sopenharmony_civoid Metrics::UpdataRepeateValueByTopTen(std::string &repeateValue, std::string &metricsName)
245fb726d48Sopenharmony_ci{
246fb726d48Sopenharmony_ci    metricsName = TRACE_MEM_TOP_TEN;
247fb726d48Sopenharmony_ci    for (auto item : memStrategy_) {
248fb726d48Sopenharmony_ci        repeateValue += PROCESS_METRICES + PROCESS_NAME + "\"" + item.processName + "\"," + OVERALL_COUNTERS +
249fb726d48Sopenharmony_ci                        ANON_RSS + MIN + std::to_string(item.overallCounters.min) + "," + MAX +
250fb726d48Sopenharmony_ci                        std::to_string(item.overallCounters.max) + "," + AVG +
251fb726d48Sopenharmony_ci                        std::to_string(item.overallCounters.avg) + "}}},";
252fb726d48Sopenharmony_ci    }
253fb726d48Sopenharmony_ci}
254fb726d48Sopenharmony_civoid Metrics::UpdataRepeateValueByMemUnagg(std::string &repeateValue, std::string &metricsName)
255fb726d48Sopenharmony_ci{
256fb726d48Sopenharmony_ci    metricsName = TRACE_MEM_UNAGG;
257fb726d48Sopenharmony_ci    for (auto item : memAggStrategy_) {
258fb726d48Sopenharmony_ci        repeateValue += PROCESS_VALUES + PROCESS_NAME + "\"" + item.processName + "\"," + ANON_RSS + TS +
259fb726d48Sopenharmony_ci                        std::to_string(item.anonRss.ts) + "," + OOM_SCORE + std::to_string(item.anonRss.oomScore) +
260fb726d48Sopenharmony_ci                        "," + VALUE + std::to_string(item.anonRss.value) + "}," + FILE_RSS + TS +
261fb726d48Sopenharmony_ci                        std::to_string(item.fileRss.ts) + "," + OOM_SCORE + std::to_string(item.fileRss.oomScore) +
262fb726d48Sopenharmony_ci                        "," + VALUE + std::to_string(item.fileRss.value) + "}," + SWAP + TS +
263fb726d48Sopenharmony_ci                        std::to_string(item.swap.ts) + "," + OOM_SCORE + std::to_string(item.swap.oomScore) + "," +
264fb726d48Sopenharmony_ci                        VALUE + std::to_string(item.swap.value) + "}},";
265fb726d48Sopenharmony_ci    }
266fb726d48Sopenharmony_ci}
267fb726d48Sopenharmony_civoid Metrics::UpdataRepeateValueByTaskNames(std::string &repeateValue, std::string &metricsName)
268fb726d48Sopenharmony_ci{
269fb726d48Sopenharmony_ci    metricsName = TRACE_TASK_NAMES;
270fb726d48Sopenharmony_ci    for (auto item : taskNameStrategy_) {
271fb726d48Sopenharmony_ci        repeateValue += PROCESS + PID + std::to_string(item.pid) + "," + PROCESS_NAME + "\"" + item.processName + "\",";
272fb726d48Sopenharmony_ci        for (auto threadItem : item.threadName) {
273fb726d48Sopenharmony_ci            repeateValue += THREAD_NAME + "\"" + threadItem + "\",";
274fb726d48Sopenharmony_ci        }
275fb726d48Sopenharmony_ci        repeateValue.pop_back();
276fb726d48Sopenharmony_ci        repeateValue += "},";
277fb726d48Sopenharmony_ci    }
278fb726d48Sopenharmony_ci}
279fb726d48Sopenharmony_civoid Metrics::UpdataRepeateValueByStats(std::string &repeateValue, std::string &metricsName)
280fb726d48Sopenharmony_ci{
281fb726d48Sopenharmony_ci    metricsName = TRACE_STATS;
282fb726d48Sopenharmony_ci    for (auto item : statStrategy_) {
283fb726d48Sopenharmony_ci        repeateValue += STAT + NAME + "\"" + item.name + "\"," + COUNT + std::to_string(item.count) + "," + SOURCE +
284fb726d48Sopenharmony_ci                        "\"" + item.source + "\"," + SEVERITY + "\"" + item.severity + "\"" + "},";
285fb726d48Sopenharmony_ci    }
286fb726d48Sopenharmony_ci}
287fb726d48Sopenharmony_civoid Metrics::UpdataRepeateValueByMetadata(std::string &repeateValue, std::string &metricsName)
288fb726d48Sopenharmony_ci{
289fb726d48Sopenharmony_ci    metricsName = TRACE_METADATA;
290fb726d48Sopenharmony_ci    for (auto item : metaDataStrategy_) {
291fb726d48Sopenharmony_ci        repeateValue +=
292fb726d48Sopenharmony_ci            TRACE_METADATA + ":{" + NAME + "\"" + item.name + "\"," + VALUE + "\"" + item.value + "\"" + "},";
293fb726d48Sopenharmony_ci    }
294fb726d48Sopenharmony_ci}
295fb726d48Sopenharmony_civoid Metrics::UpdataRepeateValueBySysCalls(std::string &repeateValue, std::string &metricsName)
296fb726d48Sopenharmony_ci{
297fb726d48Sopenharmony_ci    metricsName = SYS_CALLS;
298fb726d48Sopenharmony_ci    for (auto item : sysCallStrategy_) {
299fb726d48Sopenharmony_ci        repeateValue += FUNCTION + FUNCTION_NAME + "\"" + item.functionName + "\"," + DUR_MAX +
300fb726d48Sopenharmony_ci                        std::to_string(item.durMax) + "," + DUR_MIN + std::to_string(item.durMin) + "," + DUR_AVG +
301fb726d48Sopenharmony_ci                        std::to_string(item.durAvg) + "},";
302fb726d48Sopenharmony_ci    }
303fb726d48Sopenharmony_ci}
304fb726d48Sopenharmony_cistd::string Metrics::GetLevelSpace(int level)
305fb726d48Sopenharmony_ci{
306fb726d48Sopenharmony_ci    std::string levelStr = "";
307fb726d48Sopenharmony_ci    for (int i = 0; i < level; i++) {
308fb726d48Sopenharmony_ci        levelStr += "    ";
309fb726d48Sopenharmony_ci    }
310fb726d48Sopenharmony_ci    return levelStr;
311fb726d48Sopenharmony_ci}
312fb726d48Sopenharmony_cistd::string Metrics::JsonFormat(std::string json)
313fb726d48Sopenharmony_ci{
314fb726d48Sopenharmony_ci    std::string result = "";
315fb726d48Sopenharmony_ci    int level = 0;
316fb726d48Sopenharmony_ci    for (std::string::size_type index = 0; index < json.size(); index++) {
317fb726d48Sopenharmony_ci        char value = json[index];
318fb726d48Sopenharmony_ci        if (level > 0 && json[json.size() - 1] == '\n') {
319fb726d48Sopenharmony_ci            result += GetLevelSpace(level);
320fb726d48Sopenharmony_ci        }
321fb726d48Sopenharmony_ci        switch (value) {
322fb726d48Sopenharmony_ci            case '{':
323fb726d48Sopenharmony_ci            case '[':
324fb726d48Sopenharmony_ci                result = result + value + "\n";
325fb726d48Sopenharmony_ci                level++;
326fb726d48Sopenharmony_ci                result += GetLevelSpace(level);
327fb726d48Sopenharmony_ci                break;
328fb726d48Sopenharmony_ci            case ',':
329fb726d48Sopenharmony_ci                result = result + value + "\n";
330fb726d48Sopenharmony_ci                result += GetLevelSpace(level);
331fb726d48Sopenharmony_ci                break;
332fb726d48Sopenharmony_ci            case '}':
333fb726d48Sopenharmony_ci            case ']':
334fb726d48Sopenharmony_ci                result += "\n";
335fb726d48Sopenharmony_ci                level--;
336fb726d48Sopenharmony_ci                result += GetLevelSpace(level);
337fb726d48Sopenharmony_ci                result += value;
338fb726d48Sopenharmony_ci                break;
339fb726d48Sopenharmony_ci            default:
340fb726d48Sopenharmony_ci                result += value;
341fb726d48Sopenharmony_ci                break;
342fb726d48Sopenharmony_ci        }
343fb726d48Sopenharmony_ci    }
344fb726d48Sopenharmony_ci    return result;
345fb726d48Sopenharmony_ci}
346fb726d48Sopenharmony_ci} // namespace TraceStreamer
347fb726d48Sopenharmony_ci} // namespace SysTuning
348