• 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 
16 #ifndef HTRACE_PARSER_H
17 #define HTRACE_PARSER_H
18 #include <cstdint>
19 #include <limits>
20 #include <map>
21 #include <stdexcept>
22 #include <string>
23 #include <thread>
24 #include "common_types.h"
25 #include "htrace_clock_detail_parser.h"
26 #include "htrace_cpu_detail_parser.h"
27 #include "htrace_hilog_parser.h"
28 #include "htrace_mem_parser.h"
29 #include "htrace_symbols_detail_parser.h"
30 #include "log.h"
31 #include "parser_base.h"
32 #include "trace_data/trace_data_cache.h"
33 #include "trace_streamer_filters.h"
34 
35 namespace SysTuning {
36 namespace TraceStreamer {
37 class HtraceParser : public ParserBase {
38 public:
39     HtraceParser(TraceDataCache* dataCache, const TraceStreamerFilters* filters);
40     ~HtraceParser();
41     void ParseTraceDataSegment(std::unique_ptr<uint8_t[]> bufferStr, size_t size) override;
42     void WaitForParserEnd();
43 private:
44     void ParseTraceDataItem(const std::string& buffer) override;
45 private:
46     void ParseMemory(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg);
47     void ParseHilog(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg);
48     void ParseFtrace(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg);
49     void ParseThread();
50     int GetNextSegment();
51     void FilterThread();
52     enum ErrorCode {
53         ERROR_CODE_EXIT = -2,
54         ERROR_CODE_NODATA = -1
55     };
56     bool hasGotSegLength_ = false;
57     bool hasGotHeader = false;
58     uint32_t nextLength_ = 0;
59     const size_t PACKET_SEG_LENGTH = 4;
60     const size_t PACKET_HEADER_LENGTH = 1024;
61     std::unique_ptr<HtraceCpuDetailParser> htraceCpuDetailParser_;
62     std::unique_ptr<HtraceSymbolsDetailParser> htraceSymbolsDetailParser_;
63     std::unique_ptr<HtraceMemParser> htraceMemParser_;
64     std::unique_ptr<HtraceClockDetailParser> htraceClockDetailParser_;
65     std::unique_ptr<HtraceHiLogParser> htraceHiLogParser_;
66     std::atomic<bool> filterThreadStarted_{false};
67     const int MAX_SEG_ARRAY_SIZE = 10000;
68     std::unique_ptr<HtraceDataSegment[]> dataSegArray;
69     int rawDataHead_ = 0;
70     bool toExit_ = false;
71     bool exited_ = false;
72     int filterHead_ = 0;
73     int parseHead_ = 0;
74     const int sleepDur_ = 100;
75     bool parseThreadStarted_ = false;
76     const int maxThread_ = 4; // 4 is the best on ubuntu 113MB/s, max 138MB/s, 6 is best on mac m1 21MB/s,
77     int parserThreadCount_ = 0;
78     std::mutex dataSegMux_;
79 };
80 } // namespace TraceStreamer
81 } // namespace SysTuning
82 
83 #endif // HTRACE_PARSER_H_
84