• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 TRACE_JSON_PARSER_H
17 #define TRACE_JSON_PARSER_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace Hitrace {
26 enum TraceType {
27     USER = 0,
28     KERNEL
29 };
30 
31 struct TraceTag {
32     std::string description;
33     uint64_t tag;
34     TraceType type;
35     std::vector<std::string> enablePath;
36     std::vector<std::string> formatPath;
37 };
38 
39 enum TraceJsonInfo : uint8_t {
40     TRACE_SNAPSHOT_BUFSZ = 1,
41     TRACE_TAG_BASE_INFO = 1 << 1,
42     TRACE_TAG_ENABLE_INFO = 1 << 2,
43     TRACE_TAG_FORMAT_INFO = 1 << 3,
44     TRACE_TAG_GROUP_INFO = 1 << 4,
45     TRACE_SNAPSHOT_FILE_AGE = 1 << 5,
46 };
47 
48 enum ParsePolicy : uint8_t {
49     PARSE_NONE = 0,
50     PARSE_TRACE_BUFSZ_INFO = TRACE_SNAPSHOT_BUFSZ,
51     PARSE_TRACE_BASE_INFO = TRACE_TAG_BASE_INFO,
52     PARSE_TRACE_ENABLE_INFO = TRACE_TAG_BASE_INFO | TRACE_TAG_ENABLE_INFO,
53     PARSE_TRACE_FORMAT_INFO = TRACE_TAG_BASE_INFO | TRACE_TAG_FORMAT_INFO,
54     PARSE_TRACE_GROUP_INFO = PARSE_TRACE_ENABLE_INFO | PARSE_TRACE_FORMAT_INFO | TRACE_TAG_GROUP_INFO,
55     PARSE_ALL_INFO = PARSE_TRACE_GROUP_INFO | TRACE_SNAPSHOT_BUFSZ,
56 };
57 
58 class TraceJsonParser {
59 public:
60     TraceJsonParser() = default;
61     bool ParseTraceJson(const uint8_t policy);
GetAllTagInfos()62     std::map<std::string, TraceTag>& GetAllTagInfos() { return traceTagInfos_; }
GetTagGroups()63     std::map<std::string, std::vector<std::string>>& GetTagGroups() { return tagGroups_; }
GetBaseFmtPath()64     std::vector<std::string> GetBaseFmtPath() { return baseTraceFormats_; }
GetSnapShotBufSzKb()65     int GetSnapShotBufSzKb() { return snapshotBufSzKb_; }
GetSnapShotFileAge()66     bool GetSnapShotFileAge() { return snapshotFileAge_; }
67 
68 private:
69     uint8_t parserState_ = PARSE_NONE;
70     std::map<std::string, TraceTag> traceTagInfos_ = {};
71     std::map<std::string, std::vector<std::string>> tagGroups_ = {};
72     std::vector<std::string> baseTraceFormats_ = {};
73     int snapshotBufSzKb_ = 0;
74     bool snapshotFileAge_ = true;
75 };
76 } // namespace HiTrace
77 } // namespace HiviewDFX
78 } // namespace OHOS
79 #endif // TRACE_JSON_PARSER_H