• 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 #ifndef HTRACE_JS_MEMORY_PARSER_H
16 #define HTRACE_JS_MEMORY_PARSER_H
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 #include "common_types.h"
21 #include "common_types.pb.h"
22 #include "event_parser_base.h"
23 #include "pbreader_file_header.h"
24 #include "pbreader_js_cpu_profiler_parser.h"
25 #include "htrace_plugin_time_parser.h"
26 #include "js_heap_result.pb.h"
27 #include "json.hpp"
28 #include "trace_streamer_config.h"
29 #include "trace_streamer_filters.h"
30 using json = nlohmann::json;
31 
32 namespace SysTuning {
33 namespace TraceStreamer {
34 struct SnapShotData {
35     uint64_t startTime;
36     uint64_t endTime;
37     std::string snapshotData;
38 };
39 class PbreaderJSMemoryParser : public EventParserBase, public HtracePluginTimeParser {
40 public:
41     PbreaderJSMemoryParser(TraceDataCache *dataCache, const TraceStreamerFilters *ctx);
42     ~PbreaderJSMemoryParser();
43     void ParseJSMemoryConfig(ProtoReader::BytesView tracePacket);
44     void Parse(ProtoReader::BytesView tracePacket,
45                uint64_t ts,
46                uint64_t startTime,
47                uint64_t endTime,
48                ProfilerPluginDataHeader profilerPluginData);
49     void EnableSaveFile(bool enable);
50     void Finish();
GetArkTsSplitFileData()51     auto GetArkTsSplitFileData()
52     {
53         return profilerArktsData_;
54     }
GetArkTsSize()55     auto GetArkTsSize()
56     {
57         return profilerArktsData_.size();
58     }
ClearArkTsSplitFileData()59     void ClearArkTsSplitFileData()
60     {
61         jsMemorySplitFileData_ = "";
62         cpuProfilerSplitFileData_ = "";
63         arkTsSplitFileDataResult_ = "";
64         profilerArktsData_ = "";
65     }
66 
67 private:
68     void ParseTimeLine(ProfilerPluginDataHeader &profilerPluginData, const std::string &jsonString);
69     void ParseSnapshot(ProtoReader::BytesView &tracePacket,
70                        ProfilerPluginDataHeader &profilerPluginData,
71                        const std::string &jsonString,
72                        uint64_t &ts);
73     void ParseSnapshotOrTimeLineEnd(const std::string &result,
74                                     ProtoReader::BytesView &tracePacket,
75                                     ProfilerPluginDataHeader &profilerPluginData,
76                                     uint64_t ts);
77     void ParseJsCpuProfiler(const std::string &result, ProfilerPluginDataHeader &profilerPluginData, uint64_t ts);
78     void ParserJSSnapInfo(int32_t fileId, const json &jMessage);
79     void ParseNodes(int32_t fileId, const json &jMessage, uint64_t endTime, bool isSplitFile);
80     void ParseEdges(int32_t fileId, const json &jMessage);
81     void ParseLocation(int32_t fileId, const json &jMessage);
82     void ParseSample(int32_t fileId, const json &jMessage, uint64_t startTime, uint64_t endTime, bool isSplitFile);
83     void ParseString(int32_t fileId, const json &jMessage);
84     void ParseTraceFuncInfo(int32_t fileId, const json &jMessage);
85     void ParseTraceNode(int32_t fileId, const json &jMessage);
86     void ParserSnapInfo(int32_t fileId, const std::string &key, const std::vector<std::vector<std::string>> &types);
87     void SerializeToString(const ProfilerPluginDataHeader &profilerPluginData, uint64_t startTime, uint64_t endTime);
88     void SerializeSnapshotData(ProfilerPluginData &profilerPluginDataResult, ArkTSResult &jsHeapResult);
89     void SerializeTimelineData(uint64_t startTime,
90                                uint64_t endTime,
91                                ProfilerPluginData &profilerPluginDataResult,
92                                ArkTSResult &jsHeapResult);
93     void SerializeCpuProfilerData(uint64_t startTime,
94                                   uint64_t endTime,
95                                   ProfilerPluginData &profilerPluginDataResult,
96                                   ArkTSResult &jsHeapResult);
97     struct timespec TimeToTimespec(uint64_t timeMs);
98     int32_t type_ = 0;
99     const std::string snapshotEnd_ = "{\"id\":1,\"result\":{}}";
100     const std::string timeLineEnd_ = "{\"id\":2,\"result\":{}}";
101     const std::string jsCpuProfilerStart_ = "{\"id\":3,\"result\":{}}";
102     uint64_t startTime_ = std::numeric_limits<uint64_t>::max();
103     bool isFirst_ = true;
104     bool cpuTimeFirst_ = true;
105     std::string jsMemoryString_ = "";
106     int32_t fileId_ = 0;
107     int32_t jsFileId_ = 0;
108     std::list<int32_t> fileIds_ = {};
109     uint64_t selfSizeCount_ = 0;
110     bool enableFileSave_ = false;
111     const std::string tmpJsMemoryTimelineData_ = "ts_tmp.jsmemory_timeline.heapsnapshot";
112     const std::string tmpJsMemorySnapshotData_ = "ts_tmp.jsmemory_snapshot";
113     const std::string jsSnapshotFileTail = ".heapsnapshot";
114     const std::string tmpJsCpuProfilerData_ = "Profile";
115     const std::string jsCpuProFiler = ".cpuprofile";
116     std::unique_ptr<HtraceJsCpuProfilerParser> jsCpuProfilerParser_;
117     std::string jsMemorySplitFileData_ = "";
118     std::string cpuProfilerSplitFileData_ = "";
119     std::string arkTsSplitFileDataResult_ = "";
120     std::string profilerArktsData_ = "";
121     json updatedJson_;
122     uint32_t nodeFileId_ = INVALID_UINT32;
123     uint32_t nodeCount_ = 0;
124     bool hasCpuProfiler_ = false;
125     SnapShotData snapShotData_ = {};
126     bool curTypeIsCpuProfile_ = false;
127 };
128 } // namespace TraceStreamer
129 } // namespace SysTuning
130 
131 #endif // HTRACE_JS_MEMORY_PARSER_H
132