xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/Tracing/tools/main.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2022 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 #undef LOG_TAG
18 #define LOG_TAG "LayerTraceGenerator"
19 
20 #include <fstream>
21 #include <iostream>
22 #include <string>
23 
24 #include <Tracing/LayerTracing.h>
25 #include "LayerTraceGenerator.h"
26 
27 using namespace android;
28 
main(int argc,char ** argv)29 int main(int argc, char** argv) {
30     if (argc > 4) {
31         std::cout << "Usage: " << argv[0]
32                   << " [transaction-trace-path] [output-layers-trace-path] [--last-entry-only]\n";
33         return -1;
34     }
35 
36     const char* transactionTracePath =
37             (argc > 1) ? argv[1] : "/data/misc/wmtrace/transactions_trace.winscope";
38     std::cout << "Parsing " << transactionTracePath << "\n";
39     std::fstream input(transactionTracePath, std::ios::in | std::ios::binary);
40     if (!input) {
41         std::cout << "Error: Could not open " << transactionTracePath;
42         return -1;
43     }
44 
45     perfetto::protos::TransactionTraceFile transactionTraceFile;
46     if (!transactionTraceFile.ParseFromIstream(&input)) {
47         std::cout << "Error: Failed to parse " << transactionTracePath;
48         return -1;
49     }
50 
51     const auto* outputLayersTracePath =
52             (argc == 3) ? argv[2] : "/data/misc/wmtrace/layers_trace.winscope";
53     auto outStream = std::ofstream{outputLayersTracePath, std::ios::binary | std::ios::out};
54 
55     auto layerTracing = LayerTracing{outStream};
56 
57     const bool generateLastEntryOnly =
58             argc >= 4 && std::string_view(argv[3]) == "--last-entry-only";
59 
60     auto traceFlags = LayerTracing::Flag::TRACE_INPUT | LayerTracing::Flag::TRACE_BUFFERS;
61 
62     ALOGD("Generating %s...", outputLayersTracePath);
63     std::cout << "Generating " << outputLayersTracePath << "\n";
64 
65     if (!LayerTraceGenerator().generate(transactionTraceFile, traceFlags, layerTracing,
66                                         generateLastEntryOnly)) {
67         std::cout << "Error: Failed to generate layers trace " << outputLayersTracePath << "\n";
68         return -1;
69     }
70 
71     // Set output file permissions (-rw-r--r--)
72     outStream.close();
73     const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
74     if (chmod(outputLayersTracePath, mode) != 0) {
75         std::cout << "Error: Failed to set permissions of " << outputLayersTracePath << "\n";
76         return -1;
77     }
78 
79     return 0;
80 }
81