Skip to content

Commit

Permalink
Merge "Reapply "Add trace_file metadata table"" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
carlscabgro authored and Gerrit Code Review committed Jul 29, 2024
2 parents 021422b + 1c927c1 commit 0f4c28e
Show file tree
Hide file tree
Showing 32 changed files with 541 additions and 114 deletions.
2 changes: 2 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -12246,11 +12246,13 @@ filegroup {
"src/trace_processor/importers/common/process_track_translation_table.cc",
"src/trace_processor/importers/common/process_tracker.cc",
"src/trace_processor/importers/common/sched_event_tracker.cc",
"src/trace_processor/importers/common/scoped_active_trace_file.cc",
"src/trace_processor/importers/common/slice_tracker.cc",
"src/trace_processor/importers/common/slice_translation_table.cc",
"src/trace_processor/importers/common/stack_profile_tracker.cc",
"src/trace_processor/importers/common/system_info_tracker.cc",
"src/trace_processor/importers/common/thread_state_tracker.cc",
"src/trace_processor/importers/common/trace_file_tracker.cc",
"src/trace_processor/importers/common/trace_parser.cc",
"src/trace_processor/importers/common/track_tracker.cc",
"src/trace_processor/importers/common/virtual_memory_mapping.cc",
Expand Down
4 changes: 4 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,8 @@ perfetto_filegroup(
"src/trace_processor/importers/common/sched_event_state.h",
"src/trace_processor/importers/common/sched_event_tracker.cc",
"src/trace_processor/importers/common/sched_event_tracker.h",
"src/trace_processor/importers/common/scoped_active_trace_file.cc",
"src/trace_processor/importers/common/scoped_active_trace_file.h",
"src/trace_processor/importers/common/slice_tracker.cc",
"src/trace_processor/importers/common/slice_tracker.h",
"src/trace_processor/importers/common/slice_translation_table.cc",
Expand All @@ -1546,6 +1548,8 @@ perfetto_filegroup(
"src/trace_processor/importers/common/system_info_tracker.h",
"src/trace_processor/importers/common/thread_state_tracker.cc",
"src/trace_processor/importers/common/thread_state_tracker.h",
"src/trace_processor/importers/common/trace_file_tracker.cc",
"src/trace_processor/importers/common/trace_file_tracker.h",
"src/trace_processor/importers/common/trace_parser.cc",
"src/trace_processor/importers/common/track_tracker.cc",
"src/trace_processor/importers/common/track_tracker.h",
Expand Down
21 changes: 12 additions & 9 deletions src/trace_processor/forwarding_trace_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ std::optional<TraceSorter::SortingMode> GetMinimumSortingMode(
return TraceSorter::SortingMode::kFullSort;

case kProtoTraceType:
case kSymbolsTraceType:
return ConvertSortingMode(context.config.sorting_mode);

case kAndroidDumpstateTraceType:
case kAndroidBugreportTraceType:
PERFETTO_FATAL(
"This trace type should be handled at the ZipParser level");
}
PERFETTO_FATAL("For GCC");
}
Expand All @@ -82,34 +88,31 @@ ForwardingTraceParser::~ForwardingTraceParser() {}
base::Status ForwardingTraceParser::Init(const TraceBlobView& blob) {
PERFETTO_CHECK(!reader_);

TraceType trace_type;
{
auto scoped_trace = context_->storage->TraceExecutionTimeIntoStats(
stats::guess_trace_type_duration_ns);
trace_type = GuessTraceType(blob.data(), blob.size());
context_->trace_type = trace_type;
trace_type_ = GuessTraceType(blob.data(), blob.size());
}

if (trace_type == kUnknownTraceType) {
if (trace_type_ == kUnknownTraceType) {
// If renaming this error message don't remove the "(ERR:fmt)" part.
// The UI's error_dialog.ts uses it to make the dialog more graceful.
return base::ErrStatus("Unknown trace type provided (ERR:fmt)");
}

base::StatusOr<std::unique_ptr<ChunkedTraceReader>> reader_or =
context_->reader_registry->CreateTraceReader(trace_type);
context_->reader_registry->CreateTraceReader(trace_type_);
if (!reader_or.ok()) {
return reader_or.status();
}
reader_ = std::move(*reader_or);

PERFETTO_DLOG("%s detected", ToString(trace_type));
UpdateSorterForTraceType(trace_type);
PERFETTO_DLOG("%s trace detected", TraceTypeToString(trace_type_));
UpdateSorterForTraceType(trace_type_);

// TODO(b/334978369) Make sure kProtoTraceType and kSystraceTraceType are
// parsed first so that we do not get issues with
// SetPidZeroIsUpidZeroIdleProcess()
if (trace_type == kProtoTraceType || trace_type == kSystraceTraceType) {
if (trace_type_ == kProtoTraceType || trace_type_ == kSystraceTraceType) {
context_->process_tracker->SetPidZeroIsUpidZeroIdleProcess();
}

Expand Down
3 changes: 3 additions & 0 deletions src/trace_processor/forwarding_trace_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ class ForwardingTraceParser : public ChunkedTraceReader {
base::Status Parse(TraceBlobView) override;
[[nodiscard]] base::Status NotifyEndOfFile() override;

TraceType trace_type() const { return trace_type_; }

private:
base::Status Init(const TraceBlobView&);
void UpdateSorterForTraceType(TraceType trace_type);
TraceProcessorContext* const context_;
std::unique_ptr<ChunkedTraceReader> reader_;
TraceType trace_type_ = kUnknownTraceType;
};

} // namespace perfetto::trace_processor
Expand Down
1 change: 1 addition & 0 deletions src/trace_processor/importers/android_bugreport/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ source_set("android_bugreport") {
"../../storage",
"../../tables:tables_python",
"../../types",
"../../util:trace_type",
"../../util:zip_reader",
"../common",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#include "src/trace_processor/importers/android_bugreport/android_dumpstate_reader.h"
#include "src/trace_processor/importers/android_bugreport/android_log_reader.h"
#include "src/trace_processor/importers/common/clock_tracker.h"
#include "src/trace_processor/importers/common/trace_file_tracker.h"
#include "src/trace_processor/types/trace_processor_context.h"
#include "src/trace_processor/util/status_macros.h"
#include "src/trace_processor/util/trace_type.h"
#include "src/trace_processor/util/zip_reader.h"

namespace perfetto::trace_processor {
Expand Down Expand Up @@ -105,6 +107,9 @@ util::Status AndroidBugreportReader::ParseImpl() {
base::Status AndroidBugreportReader::ParseDumpstateTxt(
std::vector<TimestampedAndroidLogEvent> logcat_events) {
PERFETTO_CHECK(dumpstate_file_);
ScopedActiveTraceFile trace_file = context_->trace_file_tracker->StartNewFile(
dumpstate_file_->name(), kAndroidDumpstateTraceType,
dumpstate_file_->uncompressed_size());
AndroidDumpstateReader reader(context_, br_year_, std::move(logcat_events));
return dumpstate_file_->DecompressLines(
[&](const std::vector<base::StringView>& lines) {
Expand Down Expand Up @@ -134,6 +139,10 @@ AndroidBugreportReader::ParsePersistentLogcat() {
// Push all events into the AndroidLogParser. It will take care of string
// interning into the pool. Appends entries into `log_events`.
for (const auto& log_file : log_files) {
ScopedActiveTraceFile trace_file =
context_->trace_file_tracker->StartNewFile(
log_file.second->name(), kAndroidLogcatTraceType,
log_file.second->uncompressed_size());
RETURN_IF_ERROR(log_file.second->DecompressLines(
[&](const std::vector<base::StringView>& lines) {
for (const auto& line : lines) {
Expand Down
5 changes: 5 additions & 0 deletions src/trace_processor/importers/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ source_set("common") {
"sched_event_state.h",
"sched_event_tracker.cc",
"sched_event_tracker.h",
"scoped_active_trace_file.cc",
"scoped_active_trace_file.h",
"slice_tracker.cc",
"slice_tracker.h",
"slice_translation_table.cc",
Expand All @@ -64,6 +66,8 @@ source_set("common") {
"system_info_tracker.h",
"thread_state_tracker.cc",
"thread_state_tracker.h",
"trace_file_tracker.cc",
"trace_file_tracker.h",
"trace_parser.cc",
"track_tracker.cc",
"track_tracker.h",
Expand All @@ -90,6 +94,7 @@ source_set("common") {
"../../types",
"../../util:build_id",
"../../util:profiler_util",
"../../util:trace_type",
"../fuchsia:fuchsia_record",
"../perf:record",
"../systrace:systrace_line",
Expand Down
52 changes: 52 additions & 0 deletions src/trace_processor/importers/common/scoped_active_trace_file.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "src/trace_processor/importers/common/scoped_active_trace_file.h"

#include <cstddef>
#include <cstdint>
#include <string>

#include "perfetto/ext/base/string_view.h"
#include "src/trace_processor/importers/common/trace_file_tracker.h"
#include "src/trace_processor/storage/trace_storage.h"
#include "src/trace_processor/tables/metadata_tables_py.h"
#include "src/trace_processor/types/trace_processor_context.h"

namespace perfetto::trace_processor {
ScopedActiveTraceFile::~ScopedActiveTraceFile() {
if (is_valid_) {
context_->trace_file_tracker->EndFile(row_);
}
}

void ScopedActiveTraceFile::SetName(const std::string& name) {
row_.set_name(context_->storage->InternString(base::StringView(name)));
}

void ScopedActiveTraceFile::SetTraceType(TraceType type) {
row_.set_trace_type(context_->storage->InternString(TraceTypeToString(type)));
}

void ScopedActiveTraceFile::SetSize(size_t size) {
row_.set_size(static_cast<int64_t>(size));
}

void ScopedActiveTraceFile::AddSize(size_t size) {
row_.set_size(static_cast<int64_t>(size) + row_.size());
}

} // namespace perfetto::trace_processor
77 changes: 77 additions & 0 deletions src/trace_processor/importers/common/scoped_active_trace_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCOPED_ACTIVE_TRACE_FILE_H_
#define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCOPED_ACTIVE_TRACE_FILE_H_

#include <string>

#include "src/trace_processor/tables/metadata_tables_py.h"
#include "src/trace_processor/util/trace_type.h"

namespace perfetto::trace_processor {

class TraceProcessorContext;

// RAII like object that represents a file currently being parsed. When
// instances of this object go out of scope they will notify the
// TraceFileTracker that we are done parsing the file.
// This class also acts a handler for setting file related properties.
class ScopedActiveTraceFile {
public:
~ScopedActiveTraceFile();

ScopedActiveTraceFile(const ScopedActiveTraceFile&) = delete;
ScopedActiveTraceFile& operator=(const ScopedActiveTraceFile&) = delete;

ScopedActiveTraceFile(ScopedActiveTraceFile&& o)
: context_(o.context_), row_(o.row_), is_valid_(o.is_valid_) {
o.is_valid_ = false;
}

ScopedActiveTraceFile& operator=(ScopedActiveTraceFile&& o) {
context_ = o.context_;
row_ = o.row_;
is_valid_ = o.is_valid_;
o.is_valid_ = false;
return *this;
}

void SetTraceType(TraceType type);

// For streamed files this method can be called for each chunk to update the
// file size incrementally.
void AddSize(size_t delta);

private:
friend class TraceFileTracker;
ScopedActiveTraceFile(TraceProcessorContext* context,
tables::TraceFileTable::RowReference row)
: context_(context), row_(row), is_valid_(true) {}

// Sets the file name. If this method is not called (sometimes we do not know
// the file name, e.g. streaming data) the name is set to null.
void SetName(const std::string& name);
void SetSize(size_t size);

TraceProcessorContext* context_;
tables::TraceFileTable::RowReference row_;
bool is_valid_;
};

} // namespace perfetto::trace_processor

#endif // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCOPED_ACTIVE_TRACE_FILE_H_
74 changes: 74 additions & 0 deletions src/trace_processor/importers/common/trace_file_tracker.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "src/trace_processor/importers/common/trace_file_tracker.h"

#include <cstddef>
#include <string>
#include <vector>

#include "src/trace_processor/importers/common/metadata_tracker.h"
#include "src/trace_processor/storage/metadata.h"
#include "src/trace_processor/storage/trace_storage.h"
#include "src/trace_processor/tables/metadata_tables_py.h"
#include "src/trace_processor/types/trace_processor_context.h"
#include "src/trace_processor/util/trace_type.h"

namespace perfetto::trace_processor {

ScopedActiveTraceFile TraceFileTracker::StartNewFile() {
tables::TraceFileTable::Row row;
if (!ancestors_.empty()) {
row.parent_id = ancestors_.back();
}

row.size = 0;
row.trace_type =
context_->storage->InternString(TraceTypeToString(kUnknownTraceType));

auto ref =
context_->storage->mutable_trace_file_table()->Insert(row).row_reference;

ancestors_.push_back(ref.id());
return ScopedActiveTraceFile(context_, std::move(ref));
}

ScopedActiveTraceFile TraceFileTracker::StartNewFile(const std::string& name,
TraceType type,
size_t size) {
auto file = StartNewFile();
file.SetName(name);
file.SetTraceType(type);
file.SetSize(size);
return file;
}

void TraceFileTracker::EndFile(
const tables::TraceFileTable::ConstRowReference& row) {
PERFETTO_CHECK(!ancestors_.empty());
PERFETTO_CHECK(ancestors_.back() == row.id());

// First file (root)
if (row.id().value == 0) {
context_->metadata_tracker->SetMetadata(metadata::trace_size_bytes,
Variadic::Integer(row.size()));
context_->metadata_tracker->SetMetadata(metadata::trace_type,
Variadic::String(row.trace_type()));
}
ancestors_.pop_back();
}

} // namespace perfetto::trace_processor
Loading

0 comments on commit 0f4c28e

Please sign in to comment.