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