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 #include "hi_sysevent_measure_filter.h"
17 #include "clock_filter_ex.h"
18 #include "filter_filter.h"
19 #include "stat_filter.h"
20 #include "system_event_measure_filter.h"
21 #include "ts_common.h"
22
23 namespace SysTuning {
24 namespace TraceStreamer {
HiSysEventMeasureFilter(TraceDataCache * dataCache,const TraceStreamerFilters * filter)25 HiSysEventMeasureFilter::HiSysEventMeasureFilter(TraceDataCache *dataCache, const TraceStreamerFilters *filter)
26 : FilterBase(dataCache, filter), appKey_(INVALID_UINT64)
27 {
28 }
29
~HiSysEventMeasureFilter()30 HiSysEventMeasureFilter::~HiSysEventMeasureFilter() {}
31
AppendNewValue(uint64_t serial,uint64_t timeStamp,DataIndex appNameId,DataIndex key,int32_t type,double numericValue,DataIndex strValue)32 DataIndex HiSysEventMeasureFilter::AppendNewValue(uint64_t serial,
33 uint64_t timeStamp,
34 DataIndex appNameId,
35 DataIndex key,
36 int32_t type,
37 double numericValue,
38 DataIndex strValue)
39 {
40 uint64_t appKeyId = GetOrCreateFilterIdInternal(appNameId, key);
41 HiSysEventMeasureDataRow hiSysEventMeasureDataRow = {
42 serial, timeStamp, static_cast<uint32_t>(appNameId), static_cast<uint32_t>(appKeyId), type,
43 numericValue, strValue};
44 traceDataCache_->GetHiSysEventMeasureData()->AppendData(hiSysEventMeasureDataRow);
45 return appNameId;
46 }
AppendNewValue(std::string msg,std::string processName)47 void HiSysEventMeasureFilter::AppendNewValue(std::string msg, std::string processName)
48 {
49 traceDataCache_->GetTraceConfigData()->AppendNewData("hisys_event", msg, processName);
50 return;
51 }
AppendNewValue(int32_t brightnessState,int32_t btState,int32_t locationState,int32_t wifiState,int32_t streamDefault,int32_t voiceCall,int32_t music,int32_t streamRing,int32_t media,int32_t voiceAssistant,int32_t system,int32_t alarm,int32_t notification,int32_t bluetoolthSco,int32_t enforcedAudible,int32_t streamDtmf,int32_t streamTts,int32_t accessibility,int32_t recording,int32_t streamAll)52 void HiSysEventMeasureFilter::AppendNewValue(int32_t brightnessState,
53 int32_t btState,
54 int32_t locationState,
55 int32_t wifiState,
56 int32_t streamDefault,
57 int32_t voiceCall,
58 int32_t music,
59 int32_t streamRing,
60 int32_t media,
61 int32_t voiceAssistant,
62 int32_t system,
63 int32_t alarm,
64 int32_t notification,
65 int32_t bluetoolthSco,
66 int32_t enforcedAudible,
67 int32_t streamDtmf,
68 int32_t streamTts,
69 int32_t accessibility,
70 int32_t recording,
71 int32_t streamAll)
72 {
73 HiSysEventDeviceStateDataRow hiSysEventDeviceStateDataRow = {
74 brightnessState, btState, locationState, wifiState, streamDefault, voiceCall, music,
75 streamRing, media, voiceAssistant, system, alarm, notification, bluetoolthSco,
76 enforcedAudible, streamDtmf, streamTts, accessibility, recording, streamAll};
77 traceDataCache_->GetHiSysEventDeviceStateData()->AppendNewData(hiSysEventDeviceStateDataRow);
78 return;
79 }
FilterAllHiSysEvent(const json & jMessage,uint64_t serial,bool & haveSplitSeg)80 bool HiSysEventMeasureFilter::FilterAllHiSysEvent(const json &jMessage, uint64_t serial, bool &haveSplitSeg)
81 {
82 TS_CHECK_TRUE_RET(SaveAllHiSysEvent(jMessage, haveSplitSeg), false);
83 size_t maxArraySize = 0;
84 JsonData jData;
85 std::vector<size_t> noArrayIndex = {};
86 std::vector<size_t> arrayIndex = {};
87 if (!JGetData(jMessage, jData, maxArraySize, noArrayIndex, arrayIndex)) {
88 return false;
89 }
90 DataIndex eventSourceIndex = traceDataCache_->GetDataIndex(jData.eventName);
91 if (maxArraySize) {
92 NoArrayDataParse(jData, noArrayIndex, eventSourceIndex, serial);
93 ArrayDataParse(jData, arrayIndex, eventSourceIndex, maxArraySize, serial);
94 } else {
95 CommonDataParser(jData, eventSourceIndex, serial);
96 }
97 return true;
98 }
FillJsMessage(const json & jMessage,JsonMessage & jsMessage)99 void HiSysEventMeasureFilter::FillJsMessage(const json &jMessage, JsonMessage &jsMessage)
100 {
101 for (auto item = jMessage.begin(); item != jMessage.end(); item++) {
102 if (item.key() == "domain_") {
103 std::string domainName = item.value();
104 jsMessage.domainId = traceDataCache_->GetDataIndex(domainName.c_str());
105 } else if (item.key() == "name_") {
106 std::string eventName = item.value();
107 jsMessage.eventNameId = traceDataCache_->GetDataIndex(eventName.c_str());
108 } else if (item.key() == "type_") {
109 jsMessage.type = item.value();
110 } else if (item.key() == "time_") {
111 jsMessage.timeStamp = item.value();
112 jsMessage.timeStamp *= MSEC_TO_NS;
113 } else if (item.key() == "tz_") {
114 jsMessage.timeZone = item.value();
115 } else if (item.key() == "pid_") {
116 jsMessage.pid = item.value();
117 } else if (item.key() == "tid_") {
118 jsMessage.tid = item.value();
119 } else if (item.key() == "uid_") {
120 jsMessage.uid = item.value();
121 } else if (item.key() == "id_") {
122 jsMessage.eventId = item.value();
123 } else if (item.key() == "info_") {
124 jsMessage.info = item.value();
125 } else if (item.key() == "tag_") {
126 jsMessage.tag = item.value();
127 } else if (item.key() == "level_") {
128 jsMessage.level = item.value();
129 } else if (item.key() == "seq_") {
130 jsMessage.seq = item.value();
131 } else {
132 jsMessage.content[item.key()] = item.value();
133 }
134 }
135 }
SaveAllHiSysEvent(json jMessage,bool & haveSplitSeg)136 bool HiSysEventMeasureFilter::SaveAllHiSysEvent(json jMessage, bool &haveSplitSeg)
137 {
138 JsonMessage jsMessage;
139 FillJsMessage(jMessage, jsMessage);
140 auto newTimeStamp = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, jsMessage.timeStamp);
141 UpdatePluginTimeRange(TS_CLOCK_BOOTTIME, jsMessage.timeStamp, newTimeStamp);
142 if (traceDataCache_->isSplitFile_) {
143 if (newTimeStamp >= traceDataCache_->SplitFileMinTime() &&
144 newTimeStamp <= traceDataCache_->SplitFileMaxTime()) {
145 haveSplitSeg = true;
146 }
147 return false;
148 }
149 UpdataAllHiSysEvent(jsMessage, newTimeStamp);
150 return true;
151 }
UpdataAllHiSysEvent(const JsonMessage & jsMessage,uint64_t newTimeStamp)152 void HiSysEventMeasureFilter::UpdataAllHiSysEvent(const JsonMessage &jsMessage, uint64_t newTimeStamp)
153 {
154 HiSysEventAllEventDataRow hiSysEventAllEventDataRow = {
155 jsMessage.domainId, jsMessage.eventNameId, newTimeStamp, jsMessage.type, jsMessage.timeZone,
156 jsMessage.pid, jsMessage.tid, jsMessage.uid, jsMessage.level, jsMessage.tag,
157 jsMessage.eventId, jsMessage.seq, jsMessage.info, jsMessage.content.dump()};
158 traceDataCache_->GetHiSysEventAllEventData()->AppendHiSysEventData(hiSysEventAllEventDataRow);
159 }
JGetData(const json & jMessage,JsonData & jData,size_t & maxArraySize,std::vector<size_t> & noArrayIndex,std::vector<size_t> & arrayIndex)160 bool HiSysEventMeasureFilter::JGetData(const json &jMessage,
161 JsonData &jData,
162 size_t &maxArraySize,
163 std::vector<size_t> &noArrayIndex,
164 std::vector<size_t> &arrayIndex)
165 {
166 streamFilters_->statFilter_->IncreaseStat(TRACE_HISYSEVENT, STAT_EVENT_RECEIVED);
167 for (auto subItem = jMessage.begin(); subItem != jMessage.end(); subItem++) {
168 if (subItem.key() == "name_") {
169 jData.eventName = subItem.value();
170 if (find(eventsAccordingAppNames_.begin(), eventsAccordingAppNames_.end(), jData.eventName) ==
171 eventsAccordingAppNames_.end()) {
172 streamFilters_->statFilter_->IncreaseStat(TRACE_HISYSEVENT, STAT_EVENT_NOTMATCH);
173 return false;
174 }
175 continue;
176 }
177 if (subItem.key() == "time_") {
178 jData.timeStamp = subItem.value();
179 continue;
180 }
181 if (subItem.key() == "tag_" && subItem.value() != "PowerStats") {
182 TS_LOGW("energy data without PowerStats tag_ would be invalid");
183 return false;
184 }
185 if (subItem.key() == "APPNAME") {
186 jData.appName.assign(subItem.value().begin(), subItem.value().end());
187 }
188 if (subItem.value().is_array()) {
189 maxArraySize = std::max(maxArraySize, subItem.value().size());
190 arrayIndex.push_back(jData.key.size());
191 } else {
192 noArrayIndex.push_back(jData.key.size());
193 }
194 jData.key.push_back(subItem.key());
195 jData.value.push_back(subItem.value());
196 }
197 jData.timeStamp = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, jData.timeStamp * MSEC_TO_NS);
198 return true;
199 }
NoArrayDataParse(JsonData jData,std::vector<size_t> noArrayIndex,DataIndex eventSourceIndex,uint64_t hiSysEventLineId)200 void HiSysEventMeasureFilter::NoArrayDataParse(JsonData jData,
201 std::vector<size_t> noArrayIndex,
202 DataIndex eventSourceIndex,
203 uint64_t hiSysEventLineId)
204 {
205 for (auto itor = noArrayIndex.begin(); itor != noArrayIndex.end(); itor++) {
206 auto value = jData.value[*itor];
207 auto key = jData.key[*itor];
208 DataIndex keyIndex = traceDataCache_->GetDataIndex(key);
209 AppendStringValue(value, hiSysEventLineId, eventSourceIndex, keyIndex, jData.timeStamp);
210 }
211 }
ArrayDataParse(JsonData jData,std::vector<size_t> arrayIndex,DataIndex eventSourceIndex,size_t maxArraySize,uint64_t hiSysEventLineId)212 void HiSysEventMeasureFilter::ArrayDataParse(JsonData jData,
213 std::vector<size_t> arrayIndex,
214 DataIndex eventSourceIndex,
215 size_t maxArraySize,
216 uint64_t hiSysEventLineId)
217 {
218 for (int32_t i = 0; i < maxArraySize; i++) {
219 for (auto itor = arrayIndex.begin(); itor != arrayIndex.end(); itor++) {
220 auto value = jData.value[*itor][i];
221 std::string key = jData.key[*itor];
222 DataIndex keyIndex = traceDataCache_->GetDataIndex(key);
223 if (value.is_number()) {
224 double valueIndex = value;
225 streamFilters_->hiSysEventMeasureFilter_->AppendNewValue(hiSysEventLineId, jData.timeStamp,
226 eventSourceIndex, keyIndex, 0, valueIndex, 0);
227 } else if (value.is_string()) {
228 std::string strValue = value;
229 DataIndex valueIndex = traceDataCache_->GetDataIndex(strValue);
230 streamFilters_->hiSysEventMeasureFilter_->AppendNewValue(hiSysEventLineId, jData.timeStamp,
231 eventSourceIndex, keyIndex, 1, 0, valueIndex);
232 }
233 }
234 }
235 }
AppendStringValue(nlohmann::json & value,uint64_t hiSysEventLineId,DataIndex eventSourceIndex,DataIndex keyIndex,uint64_t timeStamp)236 void HiSysEventMeasureFilter::AppendStringValue(nlohmann::json &value,
237 uint64_t hiSysEventLineId,
238 DataIndex eventSourceIndex,
239 DataIndex keyIndex,
240 uint64_t timeStamp)
241 {
242 if (value.is_string()) {
243 std::string strValue = value;
244 DataIndex valueIndex = traceDataCache_->GetDataIndex(strValue);
245 streamFilters_->hiSysEventMeasureFilter_->AppendNewValue(hiSysEventLineId, timeStamp, eventSourceIndex,
246 keyIndex, 1, 0, valueIndex);
247 } else {
248 double valueIndex = value;
249 streamFilters_->hiSysEventMeasureFilter_->AppendNewValue(hiSysEventLineId, timeStamp, eventSourceIndex,
250 keyIndex, 0, valueIndex, 0);
251 }
252 }
CommonDataParser(JsonData jData,DataIndex eventSourceIndex,uint64_t hiSysEventLineId)253 void HiSysEventMeasureFilter::CommonDataParser(JsonData jData, DataIndex eventSourceIndex, uint64_t hiSysEventLineId)
254 {
255 for (int32_t i = 0; i < jData.key.size(); i++) {
256 std::string key = jData.key[i];
257 auto value = jData.value[i];
258 DataIndex keyIndex = traceDataCache_->GetDataIndex(key);
259 AppendStringValue(value, hiSysEventLineId, eventSourceIndex, keyIndex, jData.timeStamp);
260 }
261 }
GetOrCreateFilterIdInternal(DataIndex appNameId,DataIndex key)262 DataIndex HiSysEventMeasureFilter::GetOrCreateFilterIdInternal(DataIndex appNameId, DataIndex key)
263 {
264 uint64_t appKeyId = appKey_.Find(appNameId, key);
265 if (appKeyId == INVALID_DATAINDEX) {
266 appKeyId = traceDataCache_->GetHiSysEventSubkeysData()->AppendSysEventSubkey(appNameId, key);
267 appKey_.Insert(appNameId, key, appKeyId);
268 }
269 return appKeyId;
270 }
271
Clear()272 void HiSysEventMeasureFilter::Clear()
273 {
274 appKey_.Clear();
275 eventSource_.clear();
276 }
277 } // namespace TraceStreamer
278 } // namespace SysTuning
279