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
17 #include "src/trace_processor/trace_processor_storage_impl.h"
18
19 #include <algorithm>
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include <optional>
24 #include <utility>
25
26 #include "perfetto/base/logging.h"
27 #include "perfetto/base/status.h"
28 #include "perfetto/ext/base/string_view.h"
29 #include "perfetto/ext/base/uuid.h"
30 #include "perfetto/trace_processor/basic_types.h"
31 #include "src/trace_processor/forwarding_trace_parser.h"
32 #include "src/trace_processor/importers/common/args_tracker.h"
33 #include "src/trace_processor/importers/common/async_track_set_tracker.h"
34 #include "src/trace_processor/importers/common/clock_converter.h" // IWYU pragma: keep
35 #include "src/trace_processor/importers/common/clock_tracker.h"
36 #include "src/trace_processor/importers/common/event_tracker.h"
37 #include "src/trace_processor/importers/common/metadata_tracker.h"
38 #include "src/trace_processor/importers/common/process_tracker.h"
39 #include "src/trace_processor/importers/common/slice_tracker.h"
40 #include "src/trace_processor/importers/common/stack_profile_tracker.h"
41 #include "src/trace_processor/importers/common/trace_file_tracker.h"
42 #include "src/trace_processor/importers/proto/default_modules.h"
43 #include "src/trace_processor/importers/proto/packet_analyzer.h"
44 #include "src/trace_processor/importers/proto/perf_sample_tracker.h"
45 #include "src/trace_processor/importers/proto/proto_importer_module.h"
46 #include "src/trace_processor/importers/proto/proto_trace_parser_impl.h"
47 #include "src/trace_processor/importers/proto/proto_trace_reader.h"
48 #include "src/trace_processor/sorter/trace_sorter.h"
49 #include "src/trace_processor/storage/metadata.h"
50 #include "src/trace_processor/storage/stats.h"
51 #include "src/trace_processor/storage/trace_storage.h"
52 #include "src/trace_processor/trace_reader_registry.h"
53 #include "src/trace_processor/types/variadic.h"
54 #include "src/trace_processor/util/descriptors.h"
55 #include "src/trace_processor/util/status_macros.h"
56 #include "src/trace_processor/util/trace_type.h"
57
58 namespace perfetto::trace_processor {
59
TraceProcessorStorageImpl(const Config & cfg)60 TraceProcessorStorageImpl::TraceProcessorStorageImpl(const Config& cfg)
61 : context_({cfg, std::make_shared<TraceStorage>(cfg)}) {
62 context_.reader_registry->RegisterTraceReader<ProtoTraceReader>(
63 kProtoTraceType);
64 context_.reader_registry->RegisterTraceReader<ProtoTraceReader>(
65 kSymbolsTraceType);
66 context_.proto_trace_parser =
67 std::make_unique<ProtoTraceParserImpl>(&context_);
68 RegisterDefaultModules(&context_);
69 }
70
~TraceProcessorStorageImpl()71 TraceProcessorStorageImpl::~TraceProcessorStorageImpl() {}
72
Parse(TraceBlobView blob)73 base::Status TraceProcessorStorageImpl::Parse(TraceBlobView blob) {
74 if (blob.size() == 0)
75 return base::OkStatus();
76 if (unrecoverable_parse_error_)
77 return base::ErrStatus(
78 "Failed unrecoverably while parsing in a previous Parse call");
79 if (!parser_) {
80 auto parser = std::make_unique<ForwardingTraceParser>(
81 &context_, context_.trace_file_tracker->AddFile());
82 parser_ = parser.get();
83 context_.chunk_readers.push_back(std::move(parser));
84 }
85
86 auto scoped_trace = context_.storage->TraceExecutionTimeIntoStats(
87 stats::parse_trace_duration_ns);
88
89 if (hash_input_size_remaining_ > 0 && !context_.uuid_found_in_trace) {
90 const size_t hash_size = std::min(hash_input_size_remaining_, blob.size());
91 hash_input_size_remaining_ -= hash_size;
92
93 trace_hash_.Update(reinterpret_cast<const char*>(blob.data()), hash_size);
94 base::Uuid uuid(static_cast<int64_t>(trace_hash_.digest()), 0);
95 const StringId id_for_uuid =
96 context_.storage->InternString(base::StringView(uuid.ToPrettyString()));
97 context_.metadata_tracker->SetMetadata(metadata::trace_uuid,
98 Variadic::String(id_for_uuid));
99 }
100
101 base::Status status = parser_->Parse(std::move(blob));
102 unrecoverable_parse_error_ |= !status.ok();
103 return status;
104 }
105
Flush()106 void TraceProcessorStorageImpl::Flush() {
107 if (unrecoverable_parse_error_)
108 return;
109
110 if (context_.sorter)
111 context_.sorter->ExtractEventsForced();
112 context_.args_tracker->Flush();
113 }
114
NotifyEndOfFile()115 base::Status TraceProcessorStorageImpl::NotifyEndOfFile() {
116 if (!parser_) {
117 return base::OkStatus();
118 }
119 if (unrecoverable_parse_error_) {
120 return base::ErrStatus("Unrecoverable parsing error already occurred");
121 }
122 Flush();
123 RETURN_IF_ERROR(parser_->NotifyEndOfFile());
124 // NotifyEndOfFile might have pushed packets to the sorter.
125 Flush();
126 for (std::unique_ptr<ProtoImporterModule>& module : context_.modules) {
127 module->NotifyEndOfFile();
128 }
129 if (context_.content_analyzer) {
130 PacketAnalyzer::Get(&context_)->NotifyEndOfFile();
131 }
132
133 context_.event_tracker->FlushPendingEvents();
134 context_.slice_tracker->FlushPendingSlices();
135 context_.args_tracker->Flush();
136 context_.process_tracker->NotifyEndOfFile();
137 return base::OkStatus();
138 }
139
DestroyContext()140 void TraceProcessorStorageImpl::DestroyContext() {
141 TraceProcessorContext context;
142 context.storage = std::move(context_.storage);
143
144 // TODO(b/309623584): Decouple from storage and remove from here. This
145 // function should only move storage and delete everything else.
146 context.heap_graph_tracker = std::move(context_.heap_graph_tracker);
147 context.clock_converter = std::move(context_.clock_converter);
148 // "to_ftrace" textual converter of the "raw" table requires remembering the
149 // kernel version (inside system_info_tracker) to know how to textualise
150 // sched_switch.prev_state bitflags.
151 context.system_info_tracker = std::move(context_.system_info_tracker);
152 // "__intrinsic_winscope_proto_to_args_with_defaults" requires proto
153 // descriptors.
154 context.descriptor_pool_ = std::move(context_.descriptor_pool_);
155
156 context_ = std::move(context);
157
158 // This is now a dangling pointer, reset it.
159 parser_ = nullptr;
160
161 // TODO(chinglinyu): also need to destroy secondary contextes.
162 }
163
164 } // namespace perfetto::trace_processor
165