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#ifndef SRC_PRINT_EVENT_PARSER_H
16#define SRC_PRINT_EVENT_PARSER_H
17#include <regex>
18#include <set>
19#include <string_view>
20#include "common_types.h"
21#include "event_parser_base.h"
22#include "measure_filter.h"
23#include "process_filter.h"
24#include "slice_filter.h"
25#include "task_pool_filter.h"
26#include "string_to_numerical.h"
27#include "trace_streamer_config.h"
28namespace SysTuning {
29namespace TraceStreamer {
30class PrintEventParser : private EventParserBase {
31public:
32    PrintEventParser(TraceDataCache *dataCache, const TraceStreamerFilters *filter);
33    bool ParsePrintEvent(const std::string &comm,
34                         uint64_t ts,
35                         uint32_t pid,
36                         std::string_view event,
37                         const BytraceLine &line);
38    void ParseBeginEvent(const std::string &comm,
39                         uint64_t ts,
40                         uint32_t pid,
41                         TracePoint &point,
42                         const BytraceLine &line);
43    void ParseEndEvent(uint64_t ts, uint32_t pid, const TracePoint &point);
44    void ParseStartEvent(const std::string &comm,
45                         uint64_t ts,
46                         uint32_t pid,
47                         const TracePoint &point,
48                         const BytraceLine &line);
49    void ParseFinishEvent(uint64_t ts, uint32_t pid, const TracePoint &point, const BytraceLine &line);
50    void ParseCreateEvent(uint64_t ts, const TracePoint &point);
51    void ParseGEvent(uint64_t ts, uint32_t pid, TracePoint &point);
52    void ParseHEvent(uint64_t ts, const TracePoint &point);
53    void Finish();
54    void SetTraceType(TraceFileType traceType);
55    void SetTraceClockId(BuiltinClocks clock);
56
57private:
58    using FrameFuncCall = std::function<bool(const size_t callStackRow, std::string &args, const BytraceLine &line)>;
59    ParseResult GetTracePoint(std::string_view pointStr, TracePoint &outPoint) const;
60    ParseResult CheckTracePoint(std::string_view pointStr) const;
61    uint32_t GetThreadGroupId(std::string_view pointStr, size_t &length) const;
62    std::string_view GetPointNameForBegin(std::string_view pointStr, size_t tGidlength) const;
63    ParseResult HandlerB(std::string_view pointStr, TracePoint &outPoint, size_t tGidlength) const;
64    bool HandleFrameSliceBeginEvent(DataIndex eventName,
65                                    size_t callStackRow,
66                                    std::string &args,
67                                    const BytraceLine &line);
68    void HandleFrameSliceEndEvent(uint64_t ts, uint64_t pid, uint64_t tid, size_t callStackRow);
69    void HandleFrameQueueEndEvent(uint64_t ts, uint64_t pid, uint64_t tid, size_t callStackRow);
70    bool HandleAnimationBeginEvent(const TracePoint &point, size_t callStackRow, const BytraceLine &line);
71    static ParseResult HandlerE(void);
72    ParseResult HandlerCSF(std::string_view pointStr, TracePoint &outPoint, size_t tGidlength) const;
73    static size_t GetNameLength(std::string_view pointStr, size_t nameIndex);
74    static size_t GetGHNameLength(std::string_view pointStr, size_t nameIndex);
75    ParseResult HandlerGH(std::string_view pointStr, TracePoint &outPoint, size_t tGidlength) const;
76    size_t GetValueLength(std::string_view pointStr, size_t valueIndex) const;
77    bool ReciveVsync(size_t callStackRow, std::string &args, const BytraceLine &line);
78    bool RSReciveOnDoComposition(size_t callStackRow, std::string &args, const BytraceLine &line);
79    bool OnRwTransaction(size_t callStackRow, std::string &args, const BytraceLine &line);
80    bool OnMainThreadProcessCmd(size_t callStackRow, std::string &args, const BytraceLine &line);
81    bool OnFrameQueueStart(uint64_t ts, size_t callStackRow, uint64_t pid);
82    bool OnVsyncEvent(size_t callStackRow, std::string &args, const BytraceLine &line);
83
84private:
85    std::map<DataIndex, FrameFuncCall> eventToFrameFunctionMap_ = {};
86    TraceStreamerConfig config_{};
87    const DataIndex recvievVsync_ = traceDataCache_->GetDataIndex("H:ReceiveVsync");
88    const DataIndex onVsyncEvent_ = traceDataCache_->GetDataIndex("H:OnVsyncEvent");
89    const std::string rsOnDoCompositionStr_ = "H:RSMainThread::DoComposition";
90    DataIndex rsOnDoCompositionEvent_ = INVALID_DATAINDEX;
91    const std::string onFrameQueeuStartEvent_ = "H:M: Frame queued";
92    const std::string onAnimationProcEvent_ = "render_service";     // 并行化前动效过程异步trace打点线程
93    const std::string newOnAnimationProcEvent_ = "RSUniRenderThre"; // 并行化后动效过程异步trace打点线程
94    const DataIndex marshRwTransactionData_ = traceDataCache_->GetDataIndex("H:MarshRSTransactionData");
95    const DataIndex rsMainThreadProcessCmd_ = traceDataCache_->GetDataIndex("H:RSMainThread::ProcessCommandUni");
96    const std::regex recvVsyncPattern_ = std::regex(R"((\w+):\s*(\w+))");
97    const std::regex transFlagPattern_ = std::regex("transactionFlag:\\[(\\d+),(\\d+)\\]");
98    const std::regex mainProcessCmdPattern_ = std::regex("\\[(\\d+),(\\d+)\\]");
99    const std::regex distributeMatcher_ = std::regex(R"(H:\[([a-z0-9]+),([a-z0-9]+),([a-z0-9]+)\]#([CS]?)##(.*))");
100    std::vector<uint64_t> frameCallIds_ = {};
101    std::unordered_map<uint64_t, std::vector<uint64_t>> vsyncSliceMap_ = {};
102    TraceFileType traceType_ = TRACE_FILETYPE_H_TRACE;
103    BuiltinClocks clock_ = TS_CLOCK_BOOTTIME;
104    const uint32_t maxVsyncEventSize_ = 2;
105    // if convert vsync's now and expectEnd
106    bool convertVsyncTs_ = true;
107};
108} // namespace TraceStreamer
109} // namespace SysTuning
110
111#endif // SRC_PRINT_EVENT_PARSER_H
112