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 COMMON_STDTYPE_H 17 #define COMMON_STDTYPE_H 18 19 #include <mutex> 20 #include <unordered_map> 21 #include <sstream> 22 #include "base_stdtype.h" 23 #include "trace_streamer_config.h" 24 25 namespace SysTuning { 26 namespace TraceStdtype { 27 using namespace SysTuning::TraceCfg; 28 using namespace SysTuning::TraceStreamer; 29 constexpr int32_t MAX_SIZE_LEN = 80; 30 class Thread { 31 public: Thread(uint32_t t)32 explicit Thread(uint32_t t) : tid_(t) {} 33 InternalPid internalPid_ = INVALID_UINT32; 34 uint32_t tid_ = 0; 35 DataIndex nameIndex_ = 0; 36 InternalTime startT_ = 0; 37 InternalTime endT_ = 0; 38 uint32_t switchCount_ = 0; 39 uint32_t sliceSize_ = 0; 40 uint32_t cpuStatesCount_ = 0; 41 }; 42 43 class Process { 44 public: Process(uint32_t p)45 explicit Process(uint32_t p) : pid_(p) {} 46 std::string cmdLine_ = ""; 47 InternalTime startT_ = 0; 48 uint32_t pid_ = 0; 49 uint32_t memSize_ = 0; 50 uint32_t threadSize_ = 0; 51 uint32_t sliceSize_ = 0; 52 uint32_t switchCount_ = 0; 53 int32_t threadCount_ = -1; 54 uint32_t cpuStatesCount_ = 0; 55 }; 56 57 class DataDict { 58 public: Size()59 size_t Size() const 60 { 61 return dataDict_.size(); 62 } 63 DataIndex GetStringIndex(std::string_view str); 64 DataIndex GetStringIndexNoWrite(std::string_view str) const; GetDataFromDict(DataIndex id)65 const std::string &GetDataFromDict(DataIndex id) const 66 { 67 TS_ASSERT(id < dataDict_.size()); 68 return dataDict_[id]; 69 } 70 void Finish(); Clear()71 void Clear() 72 { 73 dataDict_.clear(); 74 } 75 76 public: 77 std::deque<std::string> dataDict_; 78 std::unordered_map<std::string_view, DataIndex> dataDictInnerMap_; 79 80 private: 81 std::hash<std::string_view> hashFun; 82 std::mutex mutex_; 83 const int8_t spasciiStart_ = 0; 84 const int8_t spasciiEnd_ = 32; 85 }; 86 87 class MetaData { 88 public: 89 MetaData(); 90 ~MetaData() = default; 91 void SetTraceType(const std::string &traceType); 92 void SetSourceFileName(const std::string &fileName); 93 void SetOutputFileName(const std::string &fileName); 94 void SetParserToolVersion(const std::string &version); 95 void SetParserToolPublishDateTime(const std::string &datetime); 96 void SetTraceDataSize(uint64_t dataSize); 97 void SetTraceDuration(uint64_t dur); 98 const std::string &Value(uint64_t row) const; 99 const std::string &Name(uint64_t row) const; Clear()100 void Clear() 101 { 102 columnNames_.clear(); 103 values_.clear(); 104 } 105 106 private: 107 const std::string METADATA_ITEM_DATASIZE_COLNAME = "datasize"; 108 const std::string METADATA_ITEM_PARSETOOL_NAME_COLNAME = "parse_tool"; 109 const std::string METADATA_ITEM_PARSERTOOL_VERSION_COLNAME = "tool_version"; 110 const std::string METADATA_ITEM_PARSERTOOL_PUBLISH_DATETIME_COLNAME = "tool_publish_time"; 111 const std::string METADATA_ITEM_SOURCE_FILENAME_COLNAME = "source_name"; 112 const std::string METADATA_ITEM_OUTPUT_FILENAME_COLNAME = "output_name"; 113 const std::string METADATA_ITEM_PARSERTIME_COLNAME = "runtime"; 114 const std::string METADATA_ITEM_TRACE_DURATION_COLNAME = "trace_duration"; 115 const std::string METADATA_ITEM_SOURCE_DATETYPE_COLNAME = "source_type"; 116 std::deque<std::string> columnNames_ = {}; 117 std::deque<std::string> values_ = {}; 118 }; 119 120 class ClockSnapshotData { 121 public: AppendNewSnapshot(uint8_t clockId,uint64_t ts,const std::string & name)122 size_t AppendNewSnapshot(uint8_t clockId, uint64_t ts, const std::string &name) 123 { 124 clockIds_.emplace_back(clockId); 125 ts_.emplace_back(ts); 126 names_.emplace_back(name); 127 return ts_.size(); 128 } Size()129 size_t Size() const 130 { 131 return ts_.size(); 132 } ClockIds()133 const std::deque<uint8_t> &ClockIds() const 134 { 135 return clockIds_; 136 } Ts()137 const std::deque<uint64_t> &Ts() const 138 { 139 return ts_; 140 } Names()141 const std::deque<std::string> &Names() const 142 { 143 return names_; 144 } 145 146 private: 147 std::deque<uint8_t> clockIds_ = {}; 148 std::deque<uint64_t> ts_ = {}; 149 std::deque<std::string> names_ = {}; 150 }; 151 152 class DataSourceClockIdData { 153 public: 154 DataSourceClockIdData(); AppendNewDataSourceClockId(const std::string & dataSoruceName,uint8_t clockId)155 size_t AppendNewDataSourceClockId(const std::string &dataSoruceName, uint8_t clockId) 156 { 157 dataSourceNames_.emplace_back(dataSoruceName); 158 clockIds_.emplace_back(clockId); 159 return dataSourceNames_.size(); 160 } Size()161 size_t Size() const 162 { 163 return dataSourceNames_.size(); 164 } ClockIds()165 const std::deque<uint8_t> &ClockIds() const 166 { 167 return clockIds_; 168 } Names()169 const std::deque<std::string> &Names() const 170 { 171 return dataSourceNames_; 172 } 173 void SetDataSourceClockId(DataSourceType source, uint32_t id); 174 void Finish(); 175 176 private: 177 std::deque<std::string> dataSourceNames_ = {}; 178 std::deque<uint8_t> clockIds_ = {}; 179 std::map<DataSourceType, uint8_t> dataSource2ClockIdMap_ = {}; 180 std::map<DataSourceType, std::string> dataSource2PluginNameMap_ = {}; 181 }; 182 183 class StatAndInfo { 184 public: 185 StatAndInfo(); 186 ~StatAndInfo() = default; 187 void IncreaseStat(SupportedTraceEventType eventType, StatType type); 188 const uint32_t &GetValue(SupportedTraceEventType eventType, StatType type) const; 189 const std::string &GetEvent(SupportedTraceEventType eventType) const; 190 const std::string &GetStat(StatType type) const; 191 const std::string &GetSeverityDesc(SupportedTraceEventType eventType, StatType type) const; 192 const StatSeverityLevel &GetSeverity(SupportedTraceEventType eventType, StatType type) const; 193 std::map<BuiltinClocks, std::string> clockid2ClockNameMap_ = {}; 194 195 private: 196 uint32_t statCount_[TRACE_EVENT_MAX][STAT_EVENT_MAX]; 197 std::string event_[TRACE_EVENT_MAX]; 198 std::string stat_[STAT_EVENT_MAX]; 199 std::string statSeverityDesc_[TRACE_EVENT_MAX][STAT_EVENT_MAX]; 200 StatSeverityLevel statSeverity_[TRACE_EVENT_MAX][STAT_EVENT_MAX]; 201 TraceStreamerConfig config_{}; 202 #ifdef SUPPORTTHREAD 203 SpinLock spinlock_; 204 #endif 205 }; 206 207 class SymbolsData { 208 public: 209 SymbolsData() = default; 210 ~SymbolsData() = default; 211 uint64_t Size() const; 212 void UpdateSymbol(uint64_t addr, DataIndex funcNameDictIndex); 213 const DataIndex &GetFunc(uint64_t addr) const; 214 const std::deque<DataIndex> &GetConstFuncNames() const; 215 const std::deque<uint64_t> &GetConstAddrs() const; Clear()216 void Clear() 217 { 218 addrs_.clear(); 219 funcName_.clear(); 220 symbolsMap_.clear(); 221 } 222 223 private: 224 std::deque<uint64_t> addrs_ = {}; 225 std::deque<DataIndex> funcName_ = {}; 226 std::map<uint64_t, DataIndex> symbolsMap_ = {}; 227 }; 228 229 class DataType { 230 public: 231 void UpdateNewDataType(BaseDataType dataType, DataIndex dataDescIndex); 232 const std::deque<BaseDataType> &DataTypes() const; 233 const std::deque<DataIndex> &DataDesc() const; Size()234 size_t Size() const 235 { 236 return typeToDesc_.size(); 237 } Clear()238 void Clear() 239 { 240 dataTypes_.clear(); 241 descs_.clear(); 242 typeToDesc_.clear(); 243 } 244 245 private: 246 std::deque<BaseDataType> dataTypes_ = {}; 247 std::deque<DataIndex> descs_ = {}; 248 std::unordered_map<BaseDataType, DataIndex> typeToDesc_ = {}; 249 }; 250 251 class ArgSet : public CacheBase, public BatchCacheBase { 252 public: 253 size_t AppendNewArg(DataIndex nameId, BaseDataType dataType, int64_t value, size_t argSet); 254 const std::deque<BaseDataType> &DataTypes() const; 255 const std::deque<int64_t> &ValuesData() const; 256 const std::deque<uint64_t> &ArgsData() const; 257 const std::deque<DataIndex> &NamesData() const; 258 Clear()259 void Clear() override 260 { 261 CacheBase::Clear(); 262 names_.clear(); 263 dataTypes_.clear(); 264 values_.clear(); 265 argset_.clear(); 266 } ClearExportedData()267 void ClearExportedData() override 268 { 269 EraseElements(ids_, names_, dataTypes_, values_, argset_); 270 } 271 272 private: 273 std::deque<uint64_t> names_ = {}; 274 std::deque<BaseDataType> dataTypes_ = {}; 275 std::deque<int64_t> values_ = {}; 276 std::deque<uint64_t> argset_ = {}; 277 }; 278 279 // memory-plugin and hisysevent update this 280 class TraceConfig : public CacheBase { 281 public: 282 void AppendNewData(std::string traceSource, std::string key, std::string value); 283 const std::deque<std::string> &TraceSource() const; 284 const std::deque<std::string> &Key() const; 285 const std::deque<std::string> &Value() const; Clear()286 void Clear() override 287 { 288 CacheBase::Clear(); 289 traceSource_.clear(); 290 key_.clear(); 291 value_.clear(); 292 } 293 294 private: 295 std::deque<std::string> traceSource_ = {}; 296 std::deque<std::string> key_ = {}; 297 std::deque<std::string> value_ = {}; 298 uint32_t rowCounts_ = 0; 299 }; 300 } // namespace TraceStdtype 301 } // namespace SysTuning 302 303 #endif // COMMON_STDTYPE_H 304