Add --trace_folder to aapt2
Add a tracing API and instrument key functions in order to profile
aapt2 bottleneck. The API allows to generate systrace fragment files.
Impact on performance is neglibible with each Trace requiring less
than 1us and the final Flush operation at the end of a command
requiring around 40us.
Bug: None
Test: None
Change-Id: I51b564d3694e9384679f43b878b32295527dddf6
diff --git a/tools/aapt2/cmd/Command.cpp b/tools/aapt2/cmd/Command.cpp
index 4424a35..919b4c9 100644
--- a/tools/aapt2/cmd/Command.cpp
+++ b/tools/aapt2/cmd/Command.cpp
@@ -25,6 +25,7 @@
#include "android-base/utf8.h"
#include "androidfw/StringPiece.h"
+#include "trace/TraceBuffer.h"
#include "util/Util.h"
using android::base::StringPrintf;
@@ -178,6 +179,7 @@
}
int Command::Execute(const std::vector<StringPiece>& args, std::ostream* out_error) {
+ TRACE_NAME_ARGS("Command::Execute", args);
std::vector<std::string> file_args;
for (size_t i = 0; i < args.size(); i++) {
diff --git a/tools/aapt2/cmd/Compile.cpp b/tools/aapt2/cmd/Compile.cpp
index bec6c69..42dc74c 100644
--- a/tools/aapt2/cmd/Compile.cpp
+++ b/tools/aapt2/cmd/Compile.cpp
@@ -45,6 +45,7 @@
#include "io/StringStream.h"
#include "io/Util.h"
#include "io/ZipArchive.h"
+#include "trace/TraceBuffer.h"
#include "util/Files.h"
#include "util/Maybe.h"
#include "util/Util.h"
@@ -141,6 +142,7 @@
static bool CompileTable(IAaptContext* context, const CompileOptions& options,
const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
const std::string& output_path) {
+ TRACE_CALL();
ResourceTable table;
{
auto fin = file->OpenInputStream();
@@ -286,6 +288,7 @@
static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
io::KnownSizeInputStream* in, IArchiveWriter* writer,
IDiagnostics* diag) {
+ TRACE_CALL();
// Start the entry so we can write the header.
if (!writer->StartEntry(output_path, 0)) {
diag->Error(DiagMessage(output_path) << "failed to open file");
@@ -352,6 +355,7 @@
static bool CompileXml(IAaptContext* context, const CompileOptions& options,
const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
const std::string& output_path) {
+ TRACE_CALL();
if (context->IsVerbose()) {
context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
}
@@ -451,6 +455,7 @@
static bool CompilePng(IAaptContext* context, const CompileOptions& options,
const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
const std::string& output_path) {
+ TRACE_CALL();
if (context->IsVerbose()) {
context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
}
@@ -558,6 +563,7 @@
static bool CompileFile(IAaptContext* context, const CompileOptions& options,
const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
const std::string& output_path) {
+ TRACE_CALL();
if (context->IsVerbose()) {
context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
}
@@ -632,6 +638,7 @@
int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
CompileOptions& options) {
+ TRACE_CALL();
bool error = false;
// Iterate over the input files in a stable, platform-independent manner
@@ -707,6 +714,7 @@
}
int CompileCommand::Action(const std::vector<std::string>& args) {
+ TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
CompileContext context(diagnostic_);
context.SetVerbose(options_.verbose);
diff --git a/tools/aapt2/cmd/Compile.h b/tools/aapt2/cmd/Compile.h
index 9b32cb3..d3456b2 100644
--- a/tools/aapt2/cmd/Compile.h
+++ b/tools/aapt2/cmd/Compile.h
@@ -60,6 +60,8 @@
"Sets the visibility of the compiled resources to the specified\n"
"level. Accepted levels: public, private, default", &visibility_);
AddOptionalSwitch("-v", "Enables verbose logging", &options_.verbose);
+ AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
+ &trace_folder_);
}
int Action(const std::vector<std::string>& args) override;
@@ -68,6 +70,7 @@
IDiagnostics* diagnostic_;
CompileOptions options_;
Maybe<std::string> visibility_;
+ Maybe<std::string> trace_folder_;
};
int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index a7b8d25..f354bb6 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -67,6 +67,7 @@
#include "process/IResourceTableConsumer.h"
#include "process/SymbolTable.h"
#include "split/TableSplitter.h"
+#include "trace/TraceBuffer.h"
#include "util/Files.h"
#include "xml/XmlDom.h"
@@ -213,6 +214,7 @@
static bool FlattenXml(IAaptContext* context, const xml::XmlResource& xml_res,
const StringPiece& path, bool keep_raw_values, bool utf16,
OutputFormat format, IArchiveWriter* writer) {
+ TRACE_CALL();
if (context->IsVerbose()) {
context->GetDiagnostics()->Note(DiagMessage(path) << "writing to archive (keep_raw_values="
<< (keep_raw_values ? "true" : "false")
@@ -250,6 +252,7 @@
// Inflates an XML file from the source path.
static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) {
+ TRACE_CALL();
FileInputStream fin(path);
if (fin.HadError()) {
diag->Error(DiagMessage(path) << "failed to load XML file: " << fin.GetError());
@@ -421,6 +424,7 @@
std::vector<std::unique_ptr<xml::XmlResource>> ResourceFileFlattener::LinkAndVersionXmlFile(
ResourceTable* table, FileOperation* file_op) {
+ TRACE_CALL();
xml::XmlResource* doc = file_op->xml_to_flatten.get();
const Source& src = doc->file.source;
@@ -489,6 +493,7 @@
};
bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) {
+ TRACE_CALL();
bool error = false;
std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files;
@@ -806,6 +811,7 @@
// Creates a SymbolTable that loads symbols from the various APKs.
// Pre-condition: context_->GetCompilationPackage() needs to be set.
bool LoadSymbolsFromIncludePaths() {
+ TRACE_NAME("LoadSymbolsFromIncludePaths: #" + std::to_string(options_.include_paths.size()));
auto asset_source = util::make_unique<AssetManagerSymbolSource>();
for (const std::string& path : options_.include_paths) {
if (context_->IsVerbose()) {
@@ -891,6 +897,7 @@
}
Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
+ TRACE_CALL();
// Make sure the first element is <manifest> with package attribute.
xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
if (manifest_el == nullptr) {
@@ -1041,6 +1048,7 @@
}
bool FlattenTable(ResourceTable* table, OutputFormat format, IArchiveWriter* writer) {
+ TRACE_CALL();
switch (format) {
case OutputFormat::kApk: {
BigBuffer buffer(1024);
@@ -1114,6 +1122,7 @@
}
bool GenerateJavaClasses() {
+ TRACE_CALL();
// The set of packages whose R class to call in the main classes onResourcesLoaded callback.
std::vector<std::string> packages_to_callback;
@@ -1197,6 +1206,7 @@
}
bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) {
+ TRACE_CALL();
if (!options_.generate_java_class_path) {
return true;
}
@@ -1254,6 +1264,7 @@
}
bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
+ TRACE_CALL();
if (!out) {
return true;
}
@@ -1278,6 +1289,7 @@
}
bool MergeStaticLibrary(const std::string& input, bool override) {
+ TRACE_CALL();
if (context_->IsVerbose()) {
context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input);
}
@@ -1328,6 +1340,7 @@
bool MergeExportedSymbols(const Source& source,
const std::vector<SourcedResourceName>& exported_symbols) {
+ TRACE_CALL();
// Add the exports of this file to the table.
for (const SourcedResourceName& exported_symbol : exported_symbols) {
ResourceName res_name = exported_symbol.name;
@@ -1353,6 +1366,7 @@
}
bool MergeCompiledFile(const ResourceFile& compiled_file, io::IFile* file, bool override) {
+ TRACE_CALL();
if (context_->IsVerbose()) {
context_->GetDiagnostics()->Note(DiagMessage()
<< "merging '" << compiled_file.name
@@ -1371,6 +1385,7 @@
// An io::IFileCollection is created from the ZIP file and added to the set of
// io::IFileCollections that are open.
bool MergeArchive(const std::string& input, bool override) {
+ TRACE_CALL();
if (context_->IsVerbose()) {
context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input);
}
@@ -1418,6 +1433,7 @@
// All other file types are ignored. This is because these files could be coming from a zip,
// where we could have other files like classes.dex.
bool MergeFile(io::IFile* file, bool override) {
+ TRACE_CALL();
const Source& src = file->GetSource();
if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) {
@@ -1458,6 +1474,7 @@
while ((entry = reader.Next()) != nullptr) {
if (entry->Type() == ContainerEntryType::kResTable) {
+ TRACE_NAME(std::string("Process ResTable:") + file->GetSource().path);
pb::ResourceTable pb_table;
if (!entry->GetResTable(&pb_table)) {
context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read resource table: "
@@ -1478,6 +1495,7 @@
return false;
}
} else if (entry->Type() == ContainerEntryType::kResFile) {
+ TRACE_NAME(std::string("Process ResFile") + file->GetSource().path);
pb::internal::CompiledFile pb_compiled_file;
off64_t offset;
size_t len;
@@ -1551,6 +1569,7 @@
// to the IArchiveWriter.
bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest,
ResourceTable* table) {
+ TRACE_CALL();
const bool keep_raw_values = (context_->GetPackageType() == PackageType::kStaticLib)
|| options_.keep_raw_values;
bool result = FlattenXml(context_, *manifest, kAndroidManifestPath, keep_raw_values,
@@ -1632,6 +1651,7 @@
}
int Run(const std::vector<std::string>& input_files) {
+ TRACE_CALL();
// Load the AndroidManifest.xml
std::unique_ptr<xml::XmlResource> manifest_xml =
LoadXml(options_.manifest_path, context_->GetDiagnostics());
@@ -1839,6 +1859,7 @@
std::vector<ConfigDescription> excluded_configs;
for (auto& config_string : options_.exclude_configs_) {
+ TRACE_NAME("ConfigDescription::Parse");
ConfigDescription config_description;
if (!ConfigDescription::Parse(config_string, &config_description)) {
@@ -2038,6 +2059,7 @@
};
int LinkCommand::Action(const std::vector<std::string>& args) {
+ TRACE_FLUSH(trace_folder_ ? trace_folder_.value() : "", "LinkCommand::Action");
LinkContext context(diag_);
// Expand all argument-files passed into the command line. These start with '@'.
diff --git a/tools/aapt2/cmd/Link.h b/tools/aapt2/cmd/Link.h
index 1fc149a..7c58385 100644
--- a/tools/aapt2/cmd/Link.h
+++ b/tools/aapt2/cmd/Link.h
@@ -25,6 +25,7 @@
#include "split/TableSplitter.h"
#include "format/binary/TableFlattener.h"
#include "link/ManifestFixer.h"
+#include "trace/TraceBuffer.h"
namespace aapt {
@@ -277,6 +278,8 @@
"Do not allow overlays with different visibility levels.",
&options_.strict_visibility);
AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
+ AddOptionalFlag("--trace-folder", "Generate systrace json trace fragment to specified folder.",
+ &trace_folder_);
}
int Action(const std::vector<std::string>& args) override;
@@ -300,6 +303,7 @@
bool proto_format_ = false;
Maybe<std::string> stable_id_file_path_;
std::vector<std::string> split_args_;
+ Maybe<std::string> trace_folder_;
};
}// namespace aapt