1 /*
2 * Copyright (C) 2024 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/importers/common/trace_file_tracker.h"
18
19 #include <cstddef>
20 #include <cstdint>
21 #include <optional>
22 #include <string>
23 #include <vector>
24
25 #include "perfetto/base/logging.h"
26 #include "perfetto/ext/base/string_view.h"
27 #include "src/trace_processor/importers/common/metadata_tracker.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "src/trace_processor/tables/metadata_tables_py.h"
30 #include "src/trace_processor/types/trace_processor_context.h"
31 #include "src/trace_processor/util/trace_type.h"
32
33 namespace perfetto::trace_processor {
34
AddFile(const std::string & name)35 tables::TraceFileTable::Id TraceFileTracker::AddFile(const std::string& name) {
36 return AddFileImpl(context_->storage->InternString(base::StringView(name)));
37 }
38
AddFileImpl(StringId name)39 tables::TraceFileTable::Id TraceFileTracker::AddFileImpl(StringId name) {
40 std::optional<tables::TraceFileTable::Id> parent =
41 parsing_stack_.empty() ? std::nullopt
42 : std::make_optional(parsing_stack_.back());
43 return context_->storage->mutable_trace_file_table()
44 ->Insert({parent, name, 0,
45 context_->storage->InternString(
46 TraceTypeToString(kUnknownTraceType)),
47 std::nullopt})
48 .id;
49 }
50
SetSize(tables::TraceFileTable::Id id,uint64_t size)51 void TraceFileTracker::SetSize(tables::TraceFileTable::Id id, uint64_t size) {
52 auto row = *context_->storage->mutable_trace_file_table()->FindById(id);
53 row.set_size(static_cast<int64_t>(size));
54 }
55
StartParsing(tables::TraceFileTable::Id id,TraceType trace_type)56 void TraceFileTracker::StartParsing(tables::TraceFileTable::Id id,
57 TraceType trace_type) {
58 parsing_stack_.push_back(id);
59 auto row = *context_->storage->mutable_trace_file_table()->FindById(id);
60 row.set_trace_type(
61 context_->storage->InternString(TraceTypeToString(trace_type)));
62 row.set_processing_order(static_cast<int64_t>(processing_order_++));
63 }
64
DoneParsing(tables::TraceFileTable::Id id,size_t size)65 void TraceFileTracker::DoneParsing(tables::TraceFileTable::Id id, size_t size) {
66 PERFETTO_CHECK(!parsing_stack_.empty() && parsing_stack_.back() == id);
67 parsing_stack_.pop_back();
68 auto row = *context_->storage->mutable_trace_file_table()->FindById(id);
69 row.set_size(static_cast<int64_t>(size));
70
71 // First file (root)
72 if (id.value == 0) {
73 context_->metadata_tracker->SetMetadata(metadata::trace_size_bytes,
74 Variadic::Integer(row.size()));
75 context_->metadata_tracker->SetMetadata(metadata::trace_type,
76 Variadic::String(row.trace_type()));
77 }
78 }
79
80 } // namespace perfetto::trace_processor
81