1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 METRICS_H
17#define METRICS_H
18#include <functional>
19#include <map>
20#include <string>
21#include <vector>
22#include "json.hpp"
23#include "memAggStrategy.h"
24#include "memStrategy.h"
25#include "metaDataStrategy.h"
26#include "sysCallStrategy.h"
27#include "traceStateStrategy.h"
28#include "traceTaskStrategy.h"
29#include "ts_common.h"
30
31enum METRICS_INDEX {
32    METRICS_TRACE_MEM,
33    METRICS_TRACE_MEM_TOP_TEN,
34    METRICS_TRACE_MEM_UNAGG,
35    METRICS_TRACE_TASK_NAMES,
36    METRICS_TRACE_STATS,
37    METRICS_TRACE_METADATA,
38    METRICS_SYS_CALLS,
39};
40
41namespace SysTuning {
42namespace TraceStreamer {
43const std::string TRACE_MEM = "trace_mem";
44const std::string TRACE_MEM_TOP_TEN = "trace_mem_top10";
45const std::string TRACE_MEM_UNAGG = "trace_mem_unagg";
46const std::string TRACE_TASK_NAMES = "trace_task_names";
47const std::string TRACE_STATS = "trace_stats";
48const std::string TRACE_METADATA = "trace_metadata";
49const std::string SYS_CALLS = "sys_calls";
50const std::string PROCESS_METRICES = "process_metrics:{";
51const std::string PROCESS_NAME = "process_name:";
52const std::string OVERALL_COUNTERS = "overall_counters:{";
53const std::string ANON_RSS = "anon_rss:{";
54const std::string MIN = "min:";
55const std::string MAX = "max:";
56const std::string AVG = "avg:";
57const std::string PROCESS_VALUES = "process_value:{";
58const std::string TS = "ts:";
59const std::string OOM_SCORE = "oom_score:";
60const std::string VALUE = "value:";
61const std::string FILE_RSS = "file_rss:{";
62const std::string SWAP = "swap:{";
63const std::string ANON_AND_SWAP = "anon_and_swap:{";
64const std::string PROCESS = "process:{";
65const std::string PID = "pid:";
66const std::string THREAD_NAME = "thread_name:";
67const std::string STAT = "stat:{";
68const std::string NAME = "name:";
69const std::string COUNT = "count:";
70const std::string SOURCE = "source:";
71const std::string SEVERITY = "severity:";
72const std::string FUNCTION = "function:{";
73const std::string FUNCTION_NAME = "function_name:";
74const std::string DUR_MAX = "dur_max:";
75const std::string DUR_MIN = "dur_min:";
76const std::string DUR_AVG = "dur_avg:";
77using json = nlohmann::json;
78class Metrics {
79public:
80    Metrics();
81    ~Metrics() {}
82    using ResultCallBack = std::function<void(const std::string /* json result */, int32_t)>;
83    void ParserJson(const std::string &metrics, std::string &result);
84    void PrintMetricsResult(uint32_t metricsIndex, ResultCallBack callback);
85    const auto GetMetricsMap()
86    {
87        return initMetricsMap_;
88    }
89
90private:
91    using FuncCall = std::function<void(const std::string &result)>;
92    std::map<std::string, FuncCall> metricsFunction_ = {};
93    void InitMemoryStrategy(const std::string &result);
94    void InitMemoryUnAggStrategy(const std::string &result);
95    void InitMemoryTaskNameStrategy(const std::string &result);
96    void InitTraceStatsStrategy(const std::string &result);
97    void InitTraceMetaDataStrategy(const std::string &result);
98    void InitSysCallStrategy(const std::string &result);
99    std::string JsonFormat(std::string json);
100    std::string GetLevelSpace(int level);
101    void UpdataRepeateValueByTraceMem(std::string &repeateValue, std::string &metricsName);
102    void UpdataRepeateValueByTopTen(std::string &repeateValue, std::string &metricsName);
103    void UpdataRepeateValueByMemUnagg(std::string &repeateValue, std::string &metricsName);
104    void UpdataRepeateValueByTaskNames(std::string &repeateValue, std::string &metricsName);
105    void UpdataRepeateValueByStats(std::string &repeateValue, std::string &metricsName);
106    void UpdataRepeateValueByMetadata(std::string &repeateValue, std::string &metricsName);
107    void UpdataRepeateValueBySysCalls(std::string &repeateValue, std::string &metricsName);
108    std::vector<ProcessMetricsItems> memStrategy_ = {};
109    std::vector<ProcessValuesItem> memAggStrategy_ = {};
110    std::vector<TaskProcessItem> taskNameStrategy_ = {};
111    std::vector<StatItem> statStrategy_ = {};
112    std::vector<TraceMetadataItem> metaDataStrategy_ = {};
113    std::vector<FunctionItem> sysCallStrategy_ = {};
114    std::map<int, std::string> initMetricsMap_ = {};
115};
116} // namespace TraceStreamer
117} // namespace SysTuning
118
119#endif // METRICS_H