• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "common_stdtype.h"
16 
17 namespace SysTuning {
18 namespace TraceStdtype {
MetaData()19 MetaData::MetaData()
20 {
21     columnNames_.resize(METADATA_ITEM_MAX);
22     values_.resize(METADATA_ITEM_MAX);
23     columnNames_[METADATA_ITEM_DATASIZE] = METADATA_ITEM_DATASIZE_COLNAME;
24     columnNames_[METADATA_ITEM_PARSETOOL_NAME] = METADATA_ITEM_PARSETOOL_NAME_COLNAME;
25     columnNames_[METADATA_ITEM_PARSERTOOL_VERSION] = METADATA_ITEM_PARSERTOOL_VERSION_COLNAME;
26     columnNames_[METADATA_ITEM_PARSERTOOL_PUBLISH_DATETIME] = METADATA_ITEM_PARSERTOOL_PUBLISH_DATETIME_COLNAME;
27     columnNames_[METADATA_ITEM_SOURCE_FILENAME] = METADATA_ITEM_SOURCE_FILENAME_COLNAME;
28     columnNames_[METADATA_ITEM_OUTPUT_FILENAME] = METADATA_ITEM_OUTPUT_FILENAME_COLNAME;
29     columnNames_[METADATA_ITEM_PARSERTIME] = METADATA_ITEM_PARSERTIME_COLNAME;
30     columnNames_[METADATA_ITEM_TRACE_DURATION] = METADATA_ITEM_TRACE_DURATION_COLNAME;
31     columnNames_[METADATA_ITEM_SOURCE_DATETYPE] = METADATA_ITEM_SOURCE_DATETYPE_COLNAME;
32     values_[METADATA_ITEM_PARSETOOL_NAME] = "trace_streamer";
33 }
SetTraceType(const std::string & traceType)34 void MetaData::SetTraceType(const std::string &traceType)
35 {
36     values_[METADATA_ITEM_SOURCE_DATETYPE] = traceType;
37 }
SetSourceFileName(const std::string & fileName)38 void MetaData::SetSourceFileName(const std::string &fileName)
39 {
40     MetaData::values_[METADATA_ITEM_SOURCE_FILENAME] = fileName;
41 }
SetOutputFileName(const std::string & fileName)42 void MetaData::SetOutputFileName(const std::string &fileName)
43 {
44     MetaData::values_[METADATA_ITEM_OUTPUT_FILENAME] = fileName;
45 }
SetParserToolVersion(const std::string & version)46 void MetaData::SetParserToolVersion(const std::string &version)
47 {
48     values_[METADATA_ITEM_PARSERTOOL_VERSION] = version;
49 }
SetParserToolPublishDateTime(const std::string & datetime)50 void MetaData::SetParserToolPublishDateTime(const std::string &datetime)
51 {
52     values_[METADATA_ITEM_PARSERTOOL_PUBLISH_DATETIME] = datetime;
53 }
SetTraceDataSize(uint64_t dataSize)54 void MetaData::SetTraceDataSize(uint64_t dataSize)
55 {
56     std::stringstream ss;
57     ss << dataSize;
58     values_[METADATA_ITEM_DATASIZE] = ss.str();
59     // 	Function 'time' may return error. It is not allowed to do anything that might fail inside the constructor.
60     time_t rawtime;
61     tm *timeinfo = nullptr;
62     (void)time(&rawtime);
63     timeinfo = localtime(&rawtime);
64     char buffer[MAX_SIZE_LEN];
65     (void)strftime(buffer, MAX_SIZE_LEN, "%Y-%m-%d %H:%M:%S", timeinfo);
66     values_[METADATA_ITEM_PARSERTIME].append(buffer);
67     // sometimes there will be a extra \n at last
68     values_[METADATA_ITEM_PARSERTIME].pop_back();
69 }
SetTraceDuration(uint64_t dur)70 void MetaData::SetTraceDuration(uint64_t dur)
71 {
72     values_[METADATA_ITEM_TRACE_DURATION] = std::to_string(dur) + " s";
73 }
Value(uint64_t row) const74 const std::string &MetaData::Value(uint64_t row) const
75 {
76     return values_[row];
77 }
Name(uint64_t row) const78 const std::string &MetaData::Name(uint64_t row) const
79 {
80     return columnNames_[row];
81 }
GetStringIndex(std::string_view str)82 DataIndex DataDict::GetStringIndex(std::string_view str)
83 {
84 #ifdef SUPPORTTHREAD
85     std::lock_guard<std::mutex> dictLockGuard(mutex_);
86 #endif
87     auto itor = dataDictInnerMap_.find(str);
88     if (itor != dataDictInnerMap_.end()) {
89         return itor->second;
90     }
91     dataDict_.emplace_back(std::string(str));
92     DataIndex stringIdentity = dataDict_.size() - 1;
93     dataDictInnerMap_.emplace(std::string_view(dataDict_.back()), stringIdentity);
94     return stringIdentity;
95 }
GetStringIndexNoWrite(std::string_view str) const96 DataIndex DataDict::GetStringIndexNoWrite(std::string_view str) const
97 {
98     auto itor = dataDictInnerMap_.find(str);
99     if (itor != dataDictInnerMap_.end()) {
100         return itor->second;
101     }
102     return INVALID_UINT64;
103 }
Finish()104 void DataDict::Finish()
105 {
106     std::string::size_type pos;
107     for (auto i = 0; i < dataDict_.size(); i++) {
108         while ((pos = dataDict_[i].find("\"")) != std::string::npos) {
109             dataDict_[i].replace(pos, 1, "\'");
110         }
111         while (!dataDict_[i].empty() && ((dataDict_[i].back() >= spasciiStart_ && dataDict_[i].back() <= spasciiEnd_) ||
112                                          dataDict_[i].back() == '\r')) {
113             dataDict_[i].pop_back();
114         }
115     }
116 }
117 
DataSourceClockIdData()118 DataSourceClockIdData::DataSourceClockIdData()
119     : dataSource2ClockIdMap_({{DATA_SOURCE_TYPE_TRACE, TS_CLOCK_UNKNOW},
120                               {DATA_SOURCE_TYPE_FFRT, TS_CLOCK_UNKNOW},
121                               {DATA_SOURCE_TYPE_MEM, TS_CLOCK_UNKNOW},
122                               {DATA_SOURCE_TYPE_HILOG, TS_CLOCK_UNKNOW},
123                               {DATA_SOURCE_TYPE_NATIVEHOOK, TS_CLOCK_UNKNOW},
124                               {DATA_SOURCE_TYPE_FPS, TS_CLOCK_UNKNOW},
125                               {DATA_SOURCE_TYPE_NETWORK, TS_CLOCK_UNKNOW},
126                               {DATA_SOURCE_TYPE_DISKIO, TS_CLOCK_UNKNOW},
127                               {DATA_SOURCE_TYPE_CPU, TS_CLOCK_UNKNOW},
128                               {DATA_SOURCE_TYPE_PROCESS, TS_CLOCK_UNKNOW},
129                               {DATA_SOURCE_TYPE_HISYSEVENT, TS_CLOCK_UNKNOW},
130                               {DATA_SOURCE_TYPE_JSMEMORY, TS_CLOCK_UNKNOW}}),
131       dataSource2PluginNameMap_({
132           {DATA_SOURCE_TYPE_TRACE, "ftrace-plugin"},
133           {DATA_SOURCE_TYPE_FFRT, "ffrt-profiler"},
134           {DATA_SOURCE_TYPE_MEM, "memory-plugin"},
135           {DATA_SOURCE_TYPE_HILOG, "hilog-plugin"},
136           {DATA_SOURCE_TYPE_NATIVEHOOK, "nativehook"},
137           {DATA_SOURCE_TYPE_FPS, "hidump-plugin"},
138           {DATA_SOURCE_TYPE_NETWORK, "network-plugin"},
139           {DATA_SOURCE_TYPE_DISKIO, "diskio-plugin"},
140           {DATA_SOURCE_TYPE_CPU, "cpu-plugin"},
141           {DATA_SOURCE_TYPE_PROCESS, "process-plugin"},
142           {DATA_SOURCE_TYPE_HISYSEVENT, "hisysevent-plugin"},
143           {DATA_SOURCE_TYPE_JSMEMORY, "arkts-plugin"},
144       })
145 {
146 }
Finish()147 void DataSourceClockIdData::Finish()
148 {
149     for (auto i = dataSource2ClockIdMap_.begin(); i != dataSource2ClockIdMap_.end(); i++) {
150         if (i->second) { // ignore the empty datasource, for which the clockid is default TS_CLOCK_UNKNOW 0
151             dataSourceNames_.emplace_back(dataSource2PluginNameMap_.at(i->first));
152             clockIds_.emplace_back(i->second);
153         }
154     }
155 }
SetDataSourceClockId(DataSourceType source,uint32_t id)156 void DataSourceClockIdData::SetDataSourceClockId(DataSourceType source, uint32_t id)
157 {
158     dataSource2ClockIdMap_.at(source) = id;
159 }
160 
StatAndInfo()161 StatAndInfo::StatAndInfo()
162 {
163     // sched_switch_received | sched_switch_not_match | sched_switch_not_not_supported etc.
164     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
165         event_[i] = config_.eventNameMap_.at(static_cast<SupportedTraceEventType>(i));
166     }
167     for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
168         stat_[j] = config_.eventErrorDescMap_.at(static_cast<StatType>(j));
169     }
170 
171     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
172         for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
173             statSeverity_[i][j] = config_.eventParserStatSeverityDescMap_.at(static_cast<SupportedTraceEventType>(i))
174                                       .at(static_cast<StatType>(j));
175         }
176     }
177 
178     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
179         for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
180             statSeverityDesc_[i][j] = config_.serverityLevelDescMap_.at(statSeverity_[i][j]);
181         }
182     }
183 
184     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
185         for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
186             statCount_[i][j] = 0;
187         }
188     }
189     clockid2ClockNameMap_ = {
190         {TS_CLOCK_UNKNOW, "unknown"},        {TS_CLOCK_BOOTTIME, "boottime"},
191         {TS_CLOCK_REALTIME, "realtime"},     {TS_CLOCK_REALTIME_COARSE, "realtime_corse"},
192         {TS_MONOTONIC, "monotonic"},         {TS_MONOTONIC_COARSE, "monotonic-coarse"},
193         {TS_MONOTONIC_RAW, "monotonic-raw"},
194     };
195 }
IncreaseStat(SupportedTraceEventType eventType,StatType type)196 void StatAndInfo::IncreaseStat(SupportedTraceEventType eventType, StatType type)
197 {
198 #ifdef SUPPORTTHREAD
199     std::lock_guard<SpinLock> lockGurand(spinlock_);
200 #endif
201     statCount_[eventType][type]++;
202 }
GetValue(SupportedTraceEventType eventType,StatType type) const203 const uint32_t &StatAndInfo::GetValue(SupportedTraceEventType eventType, StatType type) const
204 {
205     return statCount_[eventType][type];
206 }
GetEvent(SupportedTraceEventType eventType) const207 const std::string &StatAndInfo::GetEvent(SupportedTraceEventType eventType) const
208 {
209     return event_[eventType];
210 }
GetStat(StatType type) const211 const std::string &StatAndInfo::GetStat(StatType type) const
212 {
213     return stat_[type];
214 }
GetSeverityDesc(SupportedTraceEventType eventType,StatType type) const215 const std::string &StatAndInfo::GetSeverityDesc(SupportedTraceEventType eventType, StatType type) const
216 {
217     return statSeverityDesc_[eventType][type];
218 }
GetSeverity(SupportedTraceEventType eventType,StatType type) const219 const StatSeverityLevel &StatAndInfo::GetSeverity(SupportedTraceEventType eventType, StatType type) const
220 {
221     return statSeverity_[eventType][type];
222 }
223 
Size() const224 uint64_t SymbolsData::Size() const
225 {
226     return addrs_.size();
227 }
UpdateSymbol(uint64_t addr,DataIndex funcNameDictIndex)228 void SymbolsData::UpdateSymbol(uint64_t addr, DataIndex funcNameDictIndex)
229 {
230     if (symbolsMap_.find(addr) == symbolsMap_.end()) {
231         symbolsMap_.insert(std::make_pair(addr, funcNameDictIndex));
232         addrs_.emplace_back(addr);
233         funcName_.emplace_back(funcNameDictIndex);
234     } else {
235         symbolsMap_.at(addr) = funcNameDictIndex;
236     }
237 }
GetFunc(uint64_t addr) const238 const DataIndex &SymbolsData::GetFunc(uint64_t addr) const
239 {
240     if (symbolsMap_.find(addr) == symbolsMap_.end()) {
241         auto lastAddr = symbolsMap_.lower_bound(addr);
242         if (lastAddr == symbolsMap_.end()) {
243             return INVALID_UINT64;
244         }
245         return lastAddr->second;
246     } else {
247         return symbolsMap_.at(addr);
248     }
249 }
GetConstFuncNames() const250 const std::deque<DataIndex> &SymbolsData::GetConstFuncNames() const
251 {
252     return funcName_;
253 }
GetConstAddrs() const254 const std::deque<uint64_t> &SymbolsData::GetConstAddrs() const
255 {
256     return addrs_;
257 }
258 
UpdateNewDataType(BaseDataType dataType,DataIndex dataDescIndex)259 void DataType::UpdateNewDataType(BaseDataType dataType, DataIndex dataDescIndex)
260 {
261     if (typeToDesc_.count(dataType) == 0) {
262         dataTypes_.emplace_back(dataType);
263         descs_.emplace_back(dataDescIndex);
264         typeToDesc_.insert({dataType, dataDescIndex});
265     }
266 }
DataTypes() const267 const std::deque<BaseDataType> &DataType::DataTypes() const
268 {
269     return dataTypes_;
270 }
DataDesc() const271 const std::deque<DataIndex> &DataType::DataDesc() const
272 {
273     return descs_;
274 }
275 
AppendNewArg(DataIndex nameId,BaseDataType dataType,int64_t value,size_t argSet)276 size_t ArgSet::AppendNewArg(DataIndex nameId, BaseDataType dataType, int64_t value, size_t argSet)
277 {
278     dataTypes_.emplace_back(dataType);
279     argset_.emplace_back(argSet);
280     ids_.emplace_back(id_++);
281     values_.emplace_back(value);
282     names_.emplace_back(nameId);
283     return Size() - 1;
284 }
DataTypes() const285 const std::deque<BaseDataType> &ArgSet::DataTypes() const
286 {
287     return dataTypes_;
288 }
ValuesData() const289 const std::deque<int64_t> &ArgSet::ValuesData() const
290 {
291     return values_;
292 }
ArgsData() const293 const std::deque<uint64_t> &ArgSet::ArgsData() const
294 {
295     return argset_;
296 }
NamesData() const297 const std::deque<DataIndex> &ArgSet::NamesData() const
298 {
299     return names_;
300 }
301 
AppendNewData(std::string traceSource,std::string key,std::string value)302 void TraceConfig::AppendNewData(std::string traceSource, std::string key, std::string value)
303 {
304     traceSource_.emplace_back(traceSource);
305     key_.emplace_back(key);
306     value_.emplace_back(value);
307     ids_.emplace_back(rowCounts_);
308     rowCounts_++;
309 }
TraceSource() const310 const std::deque<std::string> &TraceConfig::TraceSource() const
311 {
312     return traceSource_;
313 }
Key() const314 const std::deque<std::string> &TraceConfig::Key() const
315 {
316     return key_;
317 }
Value() const318 const std::deque<std::string> &TraceConfig::Value() const
319 {
320     return value_;
321 }
322 } // namespace TraceStdtype
323 } // namespace SysTuning
324