• 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 
16 #ifndef HI_SYS_EVENT_MEASURE_FILTER_H
17 #define HI_SYS_EVENT_MEASURE_FILTER_H
18 
19 #include <map>
20 #include <string_view>
21 #include <tuple>
22 
23 #include "common_types.h"
24 #include "double_map.h"
25 #include "filter_base.h"
26 #include "htrace_plugin_time_parser.h"
27 #include "json.hpp"
28 #include "trace_data_cache.h"
29 #include "trace_streamer_filters.h"
30 #include "triple_map.h"
31 
32 namespace SysTuning {
33 namespace TraceStreamer {
34 
35 using json = nlohmann::json;
36 typedef struct {
37     std::string eventName;
38     uint64_t timeStamp;
39     std::vector<std::string> appName;
40     std::vector<std::string> key;
41     std::vector<json> value;
42 } JsonData;
43 
44 struct JsonMessage {
45     DataIndex domainId = INVALID_DATAINDEX;
46     DataIndex eventNameId = INVALID_DATAINDEX;
47     uint64_t timeStamp = INVALID_UINT64;
48     uint32_t type = INVALID_UINT32;
49     std::string timeZone = "";
50     uint32_t pid = INVALID_UINT32;
51     uint32_t tid = INVALID_UINT32;
52     uint32_t uid = INVALID_UINT32;
53     std::string level = "";
54     std::string tag = "";
55     std::string eventId = "";
56     uint64_t seq = INVALID_UINT64;
57     std::string info = "";
58     json content;
59 };
60 
61 class HiSysEventMeasureFilter : private FilterBase, public HtracePluginTimeParser {
62 public:
63     HiSysEventMeasureFilter(TraceDataCache *dataCache, const TraceStreamerFilters *filter);
64     HiSysEventMeasureFilter(const HiSysEventMeasureFilter &) = delete;
65     HiSysEventMeasureFilter &operator=(const HiSysEventMeasureFilter &) = delete;
66     ~HiSysEventMeasureFilter() override;
67     DataIndex AppendNewValue(uint64_t serial,
68                              uint64_t timeStamp,
69                              DataIndex appNameId,
70                              DataIndex key,
71                              int32_t type,
72                              double numericValue,
73                              DataIndex strValue);
74     void AppendNewValue(std::string msg, std::string processName);
75     void AppendNewValue(int32_t brightnessState,
76                         int32_t btState,
77                         int32_t locationState,
78                         int32_t wifiState,
79                         int32_t streamDefault,
80                         int32_t voiceCall,
81                         int32_t music,
82                         int32_t streamRing,
83                         int32_t media,
84                         int32_t voiceAssistant,
85                         int32_t system,
86                         int32_t alarm,
87                         int32_t notification,
88                         int32_t bluetoolthSco,
89                         int32_t enforcedAudible,
90                         int32_t streamDtmf,
91                         int32_t streamTts,
92                         int32_t accessibility,
93                         int32_t recording,
94                         int32_t streamAll);
95     bool SaveAllHiSysEvent(json jMessage, bool &haveSplitSeg);
96     void Clear();
97     bool FilterAllHiSysEvent(const json &jMessage, uint64_t serial, bool &haveSplitSeg);
98 
99 private:
100     bool JGetData(const json &jMessage,
101                   JsonData &jData,
102                   size_t &maxArraySize,
103                   std::vector<size_t> &noArrayIndex,
104                   std::vector<size_t> &arrayIndex);
105     void NoArrayDataParse(JsonData jData,
106                           std::vector<size_t> noArrayIndex,
107                           DataIndex eventSourceIndex,
108                           uint64_t hiSysEventLineId);
109     void ArrayDataParse(JsonData jData,
110                         std::vector<size_t> arrayIndex,
111                         DataIndex eventSourceIndex,
112                         size_t maxArraySize,
113                         uint64_t hiSysEventLineId);
114     void CommonDataParser(JsonData jData, DataIndex eventSourceIndex, uint64_t hiSysEventLineId);
115     void AppendStringValue(nlohmann::json &value,
116                            uint64_t hiSysEventLineId,
117                            DataIndex eventSourceIndex,
118                            DataIndex keyIndex,
119                            uint64_t timeStamp);
120     void UpdataAllHiSysEvent(const JsonMessage &jsMessage, uint64_t newTimeStamp);
121     void FillJsMessage(const json &jMessage, JsonMessage &jsMessage);
122 
123 private:
124     const uint64_t MSEC_TO_NS = 1000 * 1000;
125     DataIndex GetOrCreateFilterIdInternal(DataIndex appNameId, DataIndex key);
126     DoubleMap<DataIndex, DataIndex, DataIndex> appKey_;
127     std::map<DataIndex, DataIndex> eventSource_;
128     std::vector<std::string> eventsAccordingAppNames_ = {"POWER_IDE_BATTERY",
129                                                          "POWER_IDE_CPU",
130                                                          "POWER_IDE_LOCATION",
131                                                          "POWER_IDE_GPU",
132                                                          "POWER_IDE_DISPLAY",
133                                                          "POWER_IDE_CAMERA",
134                                                          "POWER_IDE_BLUETOOTH",
135                                                          "POWER_IDE_FLASHLIGHT",
136                                                          "POWER_IDE_AUDIO",
137                                                          "POWER_IDE_WIFISCAN",
138                                                          "BRIGHTNESS_NIT",
139                                                          "SIGNAL_LEVEL",
140                                                          "WIFI_EVENT_RECEIVED",
141                                                          "AUDIO_STREAM_CHANGE",
142                                                          "AUDIO_VOLUME_CHANGE",
143                                                          "WIFI_STATE",
144                                                          "BR_SWITCH_STATE",
145                                                          "BLUETOOTH_BR_SWITCH_STATE",
146                                                          "LOCATION_SWITCH_STATE",
147                                                          "ENABLE_SENSOR",
148                                                          "DISABLE_SENSOR",
149                                                          "WORK_REMOVE",
150                                                          "WORK_START",
151                                                          "WORK_STOP",
152                                                          "WORK_ADD",
153                                                          "POWER_RUNNINGLOCK",
154                                                          "GNSS_STATE",
155                                                          "ANOMALY_SCREEN_OFF_ENERGY",
156                                                          "ANOMALY_ALARM_WAKEUP",
157                                                          "ANOMALY_KERNEL_WAKELOCK",
158                                                          "ANOMALY_RUNNINGLOCK",
159                                                          "ANORMALY_APP_ENERGY",
160                                                          "ANOMALY_GNSS_ENERGY",
161                                                          "ANOMALY_CPU_HIGH_FREQUENCY",
162                                                          "ANOMALY_CPU_ENERGY",
163                                                          "ANOMALY_WAKEUP"};
164 };
165 } // namespace TraceStreamer
166 } // namespace SysTuning
167 #endif // HI_SYS_EVENT_MEASURE_FILTER_H
168