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 31 enum 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 41 namespace SysTuning { 42 namespace TraceStreamer { 43 const std::string TRACE_MEM = "trace_mem"; 44 const std::string TRACE_MEM_TOP_TEN = "trace_mem_top10"; 45 const std::string TRACE_MEM_UNAGG = "trace_mem_unagg"; 46 const std::string TRACE_TASK_NAMES = "trace_task_names"; 47 const std::string TRACE_STATS = "trace_stats"; 48 const std::string TRACE_METADATA = "trace_metadata"; 49 const std::string SYS_CALLS = "sys_calls"; 50 const std::string PROCESS_METRICES = "process_metrics:{"; 51 const std::string PROCESS_NAME = "process_name:"; 52 const std::string OVERALL_COUNTERS = "overall_counters:{"; 53 const std::string ANON_RSS = "anon_rss:{"; 54 const std::string MIN = "min:"; 55 const std::string MAX = "max:"; 56 const std::string AVG = "avg:"; 57 const std::string PROCESS_VALUES = "process_value:{"; 58 const std::string TS = "ts:"; 59 const std::string OOM_SCORE = "oom_score:"; 60 const std::string VALUE = "value:"; 61 const std::string FILE_RSS = "file_rss:{"; 62 const std::string SWAP = "swap:{"; 63 const std::string ANON_AND_SWAP = "anon_and_swap:{"; 64 const std::string PROCESS = "process:{"; 65 const std::string PID = "pid:"; 66 const std::string THREAD_NAME = "thread_name:"; 67 const std::string STAT = "stat:{"; 68 const std::string NAME = "name:"; 69 const std::string COUNT = "count:"; 70 const std::string SOURCE = "source:"; 71 const std::string SEVERITY = "severity:"; 72 const std::string FUNCTION = "function:{"; 73 const std::string FUNCTION_NAME = "function_name:"; 74 const std::string DUR_MAX = "dur_max:"; 75 const std::string DUR_MIN = "dur_min:"; 76 const std::string DUR_AVG = "dur_avg:"; 77 using json = nlohmann::json; 78 class Metrics { 79 public: 80 Metrics(); ~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); GetMetricsMap()85 const auto GetMetricsMap() 86 { 87 return initMetricsMap_; 88 } 89 90 private: 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