Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Eric Seckler | d8b5208 | 2019-10-17 15:58:38 +0100 | [diff] [blame] | 17 | #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_JSON_JSON_TRACE_TOKENIZER_H_ |
| 18 | #define SRC_TRACE_PROCESSOR_IMPORTERS_JSON_JSON_TRACE_TOKENIZER_H_ |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
Lalit Maganti | 1534bb7 | 2021-04-23 14:11:26 +0100 | [diff] [blame] | 22 | #include "src/trace_processor/importers/common/chunked_trace_reader.h" |
Lalit Maganti | b5072a9 | 2021-06-01 14:32:50 +0100 | [diff] [blame] | 23 | #include "src/trace_processor/importers/json/json_utils.h" |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 24 | #include "src/trace_processor/importers/systrace/systrace_line_tokenizer.h" |
Lalit Maganti | 7010b33 | 2020-02-07 10:51:15 +0000 | [diff] [blame] | 25 | #include "src/trace_processor/storage/trace_storage.h" |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 26 | |
| 27 | namespace Json { |
| 28 | class Value; |
| 29 | } |
| 30 | |
| 31 | namespace perfetto { |
| 32 | namespace trace_processor { |
| 33 | |
| 34 | class TraceProcessorContext; |
| 35 | |
Hector Dearman | fcbc5e2 | 2019-04-08 13:12:28 +0100 | [diff] [blame] | 36 | // Visible for testing. |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 37 | enum class ReadDictRes { |
| 38 | kFoundDict, |
| 39 | kNeedsMoreData, |
| 40 | kEndOfTrace, |
| 41 | kEndOfArray, |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 42 | }; |
Hector Dearman | fcbc5e2 | 2019-04-08 13:12:28 +0100 | [diff] [blame] | 43 | |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 44 | // Parses at most one JSON dictionary and returns a pointer to the end of it, |
| 45 | // or nullptr if no dict could be detected. |
| 46 | // This is to avoid decoding the full trace in memory and reduce heap traffic. |
| 47 | // E.g. input: { a:1 b:{ c:2, d:{ e:3 } } } , { a:4, ... }, |
| 48 | // output: [ only this is parsed ] ^return value points here. |
Hector Dearman | fcbc5e2 | 2019-04-08 13:12:28 +0100 | [diff] [blame] | 49 | // Visible for testing. |
| 50 | ReadDictRes ReadOneJsonDict(const char* start, |
| 51 | const char* end, |
Lalit Maganti | 0732e00 | 2021-03-10 17:05:18 +0000 | [diff] [blame] | 52 | base::StringView* value, |
Hector Dearman | fcbc5e2 | 2019-04-08 13:12:28 +0100 | [diff] [blame] | 53 | const char** next); |
| 54 | |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 55 | enum class ReadKeyRes { |
| 56 | kFoundKey, |
| 57 | kNeedsMoreData, |
| 58 | kEndOfDictionary, |
| 59 | kFatalError, |
| 60 | }; |
| 61 | |
| 62 | // Parses at most one JSON key and returns a pointer to the start of the value |
| 63 | // associated with that key. |
| 64 | // This is to avoid decoding the full trace in memory and reduce heap traffic. |
| 65 | // E.g. input: a:1 b:{ c:2}} |
| 66 | // output: ^ return value points here, key is set to "a". |
| 67 | // Note: even if the whole key may be available, this method will return |
| 68 | // kNeedsMoreData until the first character of the value is available. |
| 69 | // Visible for testing. |
| 70 | ReadKeyRes ReadOneJsonKey(const char* start, |
| 71 | const char* end, |
| 72 | std::string* key, |
| 73 | const char** next); |
| 74 | |
Lalit Maganti | 0732e00 | 2021-03-10 17:05:18 +0000 | [diff] [blame] | 75 | // Takes as input a JSON dictionary and returns the value associated with |
| 76 | // the provided key (if it exists). |
| 77 | // Implementation note: this method does not currently support dictionaries |
| 78 | // which have arrays as JSON values because current users of this method |
| 79 | // do not require this. |
| 80 | // Visible for testing. |
| 81 | util::Status ExtractValueForJsonKey(base::StringView dict, |
| 82 | const std::string& key, |
| 83 | base::Optional<std::string>* value); |
| 84 | |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 85 | enum class ReadSystemLineRes { |
| 86 | kFoundLine, |
| 87 | kNeedsMoreData, |
| 88 | kEndOfSystemTrace, |
| 89 | kFatalError, |
| 90 | }; |
| 91 | |
| 92 | ReadSystemLineRes ReadOneSystemTraceLine(const char* start, |
| 93 | const char* end, |
| 94 | std::string* line, |
| 95 | const char** next); |
Lalit Maganti | b5072a9 | 2021-06-01 14:32:50 +0100 | [diff] [blame] | 96 | |
| 97 | // Parses the "displayTimeUnit" key from the given trace buffer |
| 98 | // and returns the associated time unit if one exists. |
| 99 | base::Optional<json::TimeUnit> MaybeParseDisplayTimeUnit( |
| 100 | base::StringView buffer); |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 101 | |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 102 | // Reads a JSON trace in chunks and extracts top level json objects. |
| 103 | class JsonTraceTokenizer : public ChunkedTraceReader { |
| 104 | public: |
| 105 | explicit JsonTraceTokenizer(TraceProcessorContext*); |
| 106 | ~JsonTraceTokenizer() override; |
| 107 | |
| 108 | // ChunkedTraceReader implementation. |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 109 | util::Status Parse(TraceBlobView) override; |
Primiano Tucci | 40da82f | 2020-02-13 18:04:35 +0000 | [diff] [blame] | 110 | void NotifyEndOfFile() override; |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 111 | |
| 112 | private: |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 113 | // Enum which tracks which type of JSON trace we are dealing with. |
| 114 | enum class TraceFormat { |
| 115 | // Enum value when ther outer-most layer is a dictionary with multiple |
| 116 | // key value pairs |
| 117 | kOuterDictionary, |
| 118 | |
| 119 | // Enum value when we only have trace events (i.e. the outermost |
| 120 | // layer is just a array of trace events). |
| 121 | kOnlyTraceEvents, |
| 122 | }; |
| 123 | |
| 124 | // Enum which tracks our current position within the trace. |
| 125 | enum class TracePosition { |
| 126 | // This indicates that we are inside the outermost dictionary of the |
| 127 | // trace and need to read the next key of the dictionary. |
| 128 | // This position is only valid when the |format_| == |kOuterDictionary|. |
| 129 | kDictionaryKey, |
| 130 | |
| 131 | // This indicates we are inside the systemTraceEvents string. |
| 132 | // This position is only valid when the |format_| == |kOuterDictionary|. |
| 133 | kSystemTraceEventsString, |
| 134 | |
| 135 | // This indicates we are waiting for the entire metadata dictionary to be |
| 136 | // available. |
| 137 | kWaitingForMetadataDictionary, |
| 138 | |
| 139 | // This indicates where are inside the traceEvents array. |
| 140 | kTraceEventsArray, |
| 141 | |
| 142 | // This indicates we cannot parse any more data in the trace. |
| 143 | kEof, |
| 144 | }; |
| 145 | |
| 146 | util::Status ParseInternal(const char* start, |
| 147 | const char* end, |
| 148 | const char** next); |
| 149 | |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 150 | TraceProcessorContext* const context_; |
| 151 | |
Lalit Maganti | 31afb82 | 2020-03-05 17:36:57 +0000 | [diff] [blame] | 152 | TraceFormat format_ = TraceFormat::kOuterDictionary; |
| 153 | TracePosition position_ = TracePosition::kDictionaryKey; |
| 154 | |
| 155 | SystraceLineTokenizer systrace_line_tokenizer_; |
| 156 | |
Deepanjan Roy | 01994ca | 2019-04-02 11:05:34 -0700 | [diff] [blame] | 157 | uint64_t offset_ = 0; |
| 158 | // Used to glue together JSON objects that span across two (or more) |
| 159 | // Parse boundaries. |
| 160 | std::vector<char> buffer_; |
| 161 | }; |
| 162 | |
| 163 | } // namespace trace_processor |
| 164 | } // namespace perfetto |
| 165 | |
Eric Seckler | d8b5208 | 2019-10-17 15:58:38 +0100 | [diff] [blame] | 166 | #endif // SRC_TRACE_PROCESSOR_IMPORTERS_JSON_JSON_TRACE_TOKENIZER_H_ |