• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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     auto itor = dataDictInnerMap_.find(str);
85     if (itor != dataDictInnerMap_.end()) {
86         return itor->second;
87     }
88     mutex_.lock();
89     dataDict_.emplace_back(std::string(str));
90     DataIndex stringIdentity = dataDict_.size() - 1;
91     dataDictInnerMap_.emplace(std::string_view(dataDict_.back()), stringIdentity);
92     mutex_.unlock();
93     return stringIdentity;
94 }
GetStringIndexNoWrite(std::string_view str) const95 DataIndex DataDict::GetStringIndexNoWrite(std::string_view str) const
96 {
97     auto itor = dataDictInnerMap_.find(str);
98     if (itor != dataDictInnerMap_.end()) {
99         return itor->second;
100     }
101     return INVALID_UINT64;
102 }
Finish()103 void DataDict::Finish()
104 {
105     std::string::size_type pos;
106     for (auto i = 0; i < dataDict_.size(); i++) {
107         while ((pos = dataDict_[i].find("\"")) != std::string::npos) {
108             dataDict_[i].replace(pos, 1, "\'");
109         }
110         while (!dataDict_[i].empty() && ((dataDict_[i].back() >= SPASCII_START && dataDict_[i].back() <= SPASCII_END) ||
111                                          dataDict_[i].back() == '\r')) {
112             dataDict_[i].pop_back();
113         }
114     }
115 }
116 
DataSourceClockIdData()117 DataSourceClockIdData::DataSourceClockIdData()
118     : dataSource2ClockIdMap_({{DATA_SOURCE_TYPE_TRACE, TS_CLOCK_UNKNOW},
119                               {DATA_SOURCE_TYPE_MEM, TS_CLOCK_UNKNOW},
120                               {DATA_SOURCE_TYPE_HILOG, TS_CLOCK_UNKNOW},
121                               {DATA_SOURCE_TYPE_NATIVEHOOK, TS_CLOCK_UNKNOW},
122                               {DATA_SOURCE_TYPE_FPS, TS_CLOCK_UNKNOW},
123                               {DATA_SOURCE_TYPE_NETWORK, TS_CLOCK_UNKNOW},
124                               {DATA_SOURCE_TYPE_DISKIO, TS_CLOCK_UNKNOW},
125                               {DATA_SOURCE_TYPE_CPU, TS_CLOCK_UNKNOW},
126                               {DATA_SOURCE_TYPE_PROCESS, TS_CLOCK_UNKNOW},
127                               {DATA_SOURCE_TYPE_HISYSEVENT, TS_CLOCK_UNKNOW},
128                               {DATA_SOURCE_TYPE_JSMEMORY, TS_CLOCK_UNKNOW}}),
129       dataSource2PluginNameMap_({
130           {DATA_SOURCE_TYPE_TRACE, "ftrace-plugin"},
131           {DATA_SOURCE_TYPE_MEM, "memory-plugin"},
132           {DATA_SOURCE_TYPE_HILOG, "hilog-plugin"},
133           {DATA_SOURCE_TYPE_NATIVEHOOK, "nativehook"},
134           {DATA_SOURCE_TYPE_FPS, "hidump-plugin"},
135           {DATA_SOURCE_TYPE_NETWORK, "network-plugin"},
136           {DATA_SOURCE_TYPE_DISKIO, "diskio-plugin"},
137           {DATA_SOURCE_TYPE_CPU, "cpu-plugin"},
138           {DATA_SOURCE_TYPE_PROCESS, "process-plugin"},
139           {DATA_SOURCE_TYPE_HISYSEVENT, "hisysevent-plugin"},
140           {DATA_SOURCE_TYPE_JSMEMORY, "arkts-plugin"},
141       })
142 {
143 }
Finish()144 void DataSourceClockIdData::Finish()
145 {
146     for (auto i = dataSource2ClockIdMap_.begin(); i != dataSource2ClockIdMap_.end(); i++) {
147         if (i->second) { // ignore the empty datasource, for which the clockid is default TS_CLOCK_UNKNOW 0
148             dataSourceNames_.emplace_back(dataSource2PluginNameMap_.at(i->first));
149             clockIds_.emplace_back(i->second);
150         }
151     }
152 }
SetDataSourceClockId(DataSourceType source,uint32_t id)153 void DataSourceClockIdData::SetDataSourceClockId(DataSourceType source, uint32_t id)
154 {
155     dataSource2ClockIdMap_.at(source) = id;
156 }
157 
StatAndInfo()158 StatAndInfo::StatAndInfo()
159 {
160     // sched_switch_received | sched_switch_not_match | sched_switch_not_not_supported etc.
161     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
162         event_[i] = config_.eventNameMap_.at(static_cast<SupportedTraceEventType>(i));
163     }
164     for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
165         stat_[j] = config_.eventErrorDescMap_.at(static_cast<StatType>(j));
166     }
167 
168     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
169         for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
170             statSeverity_[i][j] = config_.eventParserStatSeverityDescMap_.at(static_cast<SupportedTraceEventType>(i))
171                                       .at(static_cast<StatType>(j));
172         }
173     }
174 
175     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
176         for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
177             statSeverityDesc_[i][j] = config_.serverityLevelDescMap_.at(statSeverity_[i][j]);
178         }
179     }
180 
181     for (int32_t i = TRACE_EVENT_START; i < TRACE_EVENT_MAX; i++) {
182         for (int32_t j = STAT_EVENT_START; j < STAT_EVENT_MAX; j++) {
183             statCount_[i][j] = 0;
184         }
185     }
186     clockid2ClockNameMap_ = {
187         {TS_CLOCK_UNKNOW, "unknown"},        {TS_CLOCK_BOOTTIME, "boottime"},
188         {TS_CLOCK_REALTIME, "realtime"},     {TS_CLOCK_REALTIME_COARSE, "realtime_corse"},
189         {TS_MONOTONIC, "monotonic"},         {TS_MONOTONIC_COARSE, "monotonic-coarse"},
190         {TS_MONOTONIC_RAW, "monotonic-raw"},
191     };
192 }
IncreaseStat(SupportedTraceEventType eventType,StatType type)193 void StatAndInfo::IncreaseStat(SupportedTraceEventType eventType, StatType type)
194 {
195 #ifdef SUPPORTTHREAD
196     std::lock_guard<SpinLock> lockGurand(spinlock_);
197 #endif
198     statCount_[eventType][type]++;
199 }
GetValue(SupportedTraceEventType eventType,StatType type) const200 const uint32_t& StatAndInfo::GetValue(SupportedTraceEventType eventType, StatType type) const
201 {
202     return statCount_[eventType][type];
203 }
GetEvent(SupportedTraceEventType eventType) const204 const std::string& StatAndInfo::GetEvent(SupportedTraceEventType eventType) const
205 {
206     return event_[eventType];
207 }
GetStat(StatType type) const208 const std::string& StatAndInfo::GetStat(StatType type) const
209 {
210     return stat_[type];
211 }
GetSeverityDesc(SupportedTraceEventType eventType,StatType type) const212 const std::string& StatAndInfo::GetSeverityDesc(SupportedTraceEventType eventType, StatType type) const
213 {
214     return statSeverityDesc_[eventType][type];
215 }
GetSeverity(SupportedTraceEventType eventType,StatType type) const216 const StatSeverityLevel& StatAndInfo::GetSeverity(SupportedTraceEventType eventType, StatType type) const
217 {
218     return statSeverity_[eventType][type];
219 }
220 
Size() const221 uint64_t SymbolsData::Size() const
222 {
223     return addrs_.size();
224 }
UpdateSymbol(uint64_t addr,DataIndex funcNameDictIndex)225 void SymbolsData::UpdateSymbol(uint64_t addr, DataIndex funcNameDictIndex)
226 {
227     if (symbolsMap_.find(addr) == symbolsMap_.end()) {
228         symbolsMap_.insert(std::make_pair(addr, funcNameDictIndex));
229         addrs_.emplace_back(addr);
230         funcName_.emplace_back(funcNameDictIndex);
231     } else {
232         symbolsMap_.at(addr) = funcNameDictIndex;
233     }
234 }
GetFunc(uint64_t addr) const235 const DataIndex& SymbolsData::GetFunc(uint64_t addr) const
236 {
237     if (symbolsMap_.find(addr) == symbolsMap_.end()) {
238         auto lastAddr = symbolsMap_.lower_bound(addr);
239         if (lastAddr == symbolsMap_.end()) {
240             return INVALID_UINT64;
241         }
242         return lastAddr->second;
243     } else {
244         return symbolsMap_.at(addr);
245     }
246 }
GetConstFuncNames() const247 const std::deque<DataIndex>& SymbolsData::GetConstFuncNames() const
248 {
249     return funcName_;
250 }
GetConstAddrs() const251 const std::deque<uint64_t>& SymbolsData::GetConstAddrs() const
252 {
253     return addrs_;
254 }
255 
UpdateNewDataType(BaseDataType dataType,DataIndex dataDescIndex)256 void DataType::UpdateNewDataType(BaseDataType dataType, DataIndex dataDescIndex)
257 {
258     if (typeToDesc_.count(dataType) == 0) {
259         dataTypes_.emplace_back(dataType);
260         descs_.emplace_back(dataDescIndex);
261         typeToDesc_.insert({dataType, dataDescIndex});
262     }
263 }
DataTypes() const264 const std::deque<BaseDataType>& DataType::DataTypes() const
265 {
266     return dataTypes_;
267 }
DataDesc() const268 const std::deque<DataIndex>& DataType::DataDesc() const
269 {
270     return descs_;
271 }
272 
AppendNewArg(DataIndex nameId,BaseDataType dataType,int64_t value,size_t argSet)273 size_t ArgSet::AppendNewArg(DataIndex nameId, BaseDataType dataType, int64_t value, size_t argSet)
274 {
275     dataTypes_.emplace_back(dataType);
276     argset_.emplace_back(argSet);
277     ids_.emplace_back(id_++);
278     values_.emplace_back(value);
279     names_.emplace_back(nameId);
280     return Size() - 1;
281 }
DataTypes() const282 const std::deque<BaseDataType>& ArgSet::DataTypes() const
283 {
284     return dataTypes_;
285 }
ValuesData() const286 const std::deque<int64_t>& ArgSet::ValuesData() const
287 {
288     return values_;
289 }
ArgsData() const290 const std::deque<uint64_t>& ArgSet::ArgsData() const
291 {
292     return argset_;
293 }
NamesData() const294 const std::deque<DataIndex>& ArgSet::NamesData() const
295 {
296     return names_;
297 }
298 
AppendNewData(std::string traceSource,std::string key,std::string value)299 void TraceConfig::AppendNewData(std::string traceSource, std::string key, std::string value)
300 {
301     traceSource_.emplace_back(traceSource);
302     key_.emplace_back(key);
303     value_.emplace_back(value);
304     ids_.emplace_back(rowCounts_);
305     rowCounts_++;
306 }
TraceSource() const307 const std::deque<std::string>& TraceConfig::TraceSource() const
308 {
309     return traceSource_;
310 }
Key() const311 const std::deque<std::string>& TraceConfig::Key() const
312 {
313     return key_;
314 }
Value() const315 const std::deque<std::string>& TraceConfig::Value() const
316 {
317     return value_;
318 }
319 } // namespace TraceStdtype
320 } // namespace SysTuning
321