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