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 #ifndef BASE_HIVIEWDFX_HIVIEW_PLUGIN_MANAGEMENT_AUDIT_LOG_PARSER_H
16 #define BASE_HIVIEWDFX_HIVIEW_PLUGIN_MANAGEMENT_AUDIT_LOG_PARSER_H
17 #include <list>
18 #include <map>
19 #include <string>
20 #include <vector>
21 namespace OHOS {
22 namespace HiviewDFX {
23 class AuditLogParser {
24 public:
25     struct EventInfo {
26         uint64_t eventSerialId = 0;
27         bool isPipelineEvent = false;
28         std::string sender = "";
29         std::string processor = "";
30         std::string threadNameOrTid = "";
31         std::string digest = "";
32         uint64_t inTime = 0;
33         uint64_t outTime = 0;
34         bool operator<(const EventInfo &obj) const
35         {
36             return (this->eventSerialId < obj.eventSerialId);
37         };
38 
ToStringEventInfo39         std::string ToString()
40         {
41             return "SerialId:" + std::to_string(eventSerialId) + " Sender:" + sender + " Processor:" + processor +
42                    " OnThread:" + threadNameOrTid + " IsPipelineEvent:" + std::to_string(isPipelineEvent) +
43                    " TimeCost:" + std::to_string((outTime - inTime)) + " Digest:" + digest;
44         };
45     };
46 
47     struct PipelineEventInfo {
48         uint64_t eventSerialId = 0;
49         uint64_t createTime = 0;
50         uint64_t destroyTime = 0;
51         std::string creator = "";
52         std::string pipeline = "";
53         std::string digest = "";
54         // only processor, in and out time is valid
55         std::list<EventInfo> processChain;
56         bool operator<(const PipelineEventInfo &obj) const
57         {
58             return (this->eventSerialId < obj.eventSerialId);
59         };
ToStringPipelineEventInfo60         std::string ToString()
61         {
62             std::string ret = "SerialId:" + std::to_string(eventSerialId) + " Creator:" + creator +
63                               " Pipeline:" + pipeline + " TimeCost:" + std::to_string((destroyTime - createTime)) +
64                               " Digest:" + digest;
65             for (auto &processor : processChain) {
66                 ret.append(" @plugin:");
67                 ret.append(processor.processor);
68                 ret.append("@begin:");
69                 ret.append(std::to_string(processor.inTime));
70                 ret.append("@end:");
71                 ret.append(std::to_string(processor.outTime));
72                 ret.append("@thread:");
73                 ret.append(processor.threadNameOrTid);
74             }
75             return ret;
76         };
77 
PipelineEventInfoPipelineEventInfo78         PipelineEventInfo() : eventSerialId(0), createTime(0), destroyTime(0), creator(""), pipeline(""), digest(""){};
PipelineEventInfoPipelineEventInfo79         explicit PipelineEventInfo(const EventInfo &info)
80             : eventSerialId(info.eventSerialId), createTime(info.inTime), creator(info.sender), digest(info.digest){};
81     };
82 
83 public:
AuditLogParser()84     AuditLogParser() : logStartTime_(0), logEndTime_(0), parsed_(false){};
~AuditLogParser()85     ~AuditLogParser(){};
86     // QUEUE_EVENT_IN event format
87     // [timestamp] [eventSerialId] [0] [sender] [processor] [thread] [digest]
88     // QUEUE_EVENT_OUT event format
89     // [timestamp] [eventSerialId] [1]
90     // PIPELINE_EVENT_CREATE event format
91     // [timestamp] [eventSerialId] [2] [creator] [digest]
92     // PIPELINE_EVENT_HANDLE_IN event format
93     // [timestamp] [eventSerialId] [3] [processor]
94     // PIPELINE_EVENT_HANDLE_OUT event format
95     // [timestamp] [eventSerialId] [4] [thread]
96     // PIPELINE_EVENT_DONE event format
97     // [timestamp] [eventSerialId] [5] [pipeline]
98     void StartParse();
99     std::list<std::string> GetThreadSummary(const std::string &name = "");
100     std::list<std::string> GetPluginSummary(const std::string &name = "");
101     std::list<std::string> GetPipelineSummary(const std::string &name = "");
102     // From xxx to xxx
103     std::string GetAuditLogTimeScope();
104 
105 private:
106     void ParseTimeScope(const std::list<std::string> &logList);
107     void ParseEvent(std::string logStr);
108     void ParseNormalAuditEvent(int eventType, const std::vector<std::string> &eventElements);
109     void ParseNormalEnqueueEvent(const std::vector<std::string> &eventElements);
110     void ParseNormalDequeueEvent(const std::vector<std::string> &eventElements);
111     void ParsePipelineAuditEvent(int eventType, const std::vector<std::string> &eventElements);
112     void ParsePipelineCreateEvent(const std::vector<std::string> &eventElements);
113     void ParsePipelineHandleInEvent(const std::vector<std::string> &eventElements);
114     void ParsePipelineHandleOutEvent(const std::vector<std::string> &eventElements);
115     void ParsePipelineDoneEvent(const std::vector<std::string> &eventElements);
116 
117     uint64_t logStartTime_;
118     uint64_t logEndTime_;
119     bool parsed_;
120     std::map<int, std::string> threadNameMap_;
121     std::list<EventInfo> eventList_;
122     std::list<PipelineEventInfo> pipelineEventList_;
123 };
124 } // namespace HiviewDFX
125 } // namespace OHOS
126 #endif // BASE_HIVIEWDFX_HIVIEW_PLUGIN_MANAGEMENT_AUDIT_LOG_PARSER_H