blob: 18022b169760bdae46df931ac4f813f4f12e002f [file] [log] [blame]
Vishnu Nair4df8eb62022-02-17 15:32:02 -08001/*
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
Kean Mariotti639b54f2023-04-20 12:06:29 +000024#include <Tracing/LayerTracing.h>
Vishnu Nair4df8eb62022-02-17 15:32:02 -080025#include "LayerTraceGenerator.h"
26
27using namespace android;
28
29int main(int argc, char** argv) {
Vishnu Nair52c4f252023-06-14 15:25:12 -070030 if (argc > 4) {
Vishnu Nair4df8eb62022-02-17 15:32:02 -080031 std::cout << "Usage: " << argv[0]
Vishnu Nair52c4f252023-06-14 15:25:12 -070032 << " [transaction-trace-path] [output-layers-trace-path] [--last-entry-only]\n";
Vishnu Nair4df8eb62022-02-17 15:32:02 -080033 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
Kean Mariotti4ba343c2023-04-19 13:31:02 +000045 perfetto::protos::TransactionTraceFile transactionTraceFile;
Vishnu Nair4df8eb62022-02-17 15:32:02 -080046 if (!transactionTraceFile.ParseFromIstream(&input)) {
47 std::cout << "Error: Failed to parse " << transactionTracePath;
48 return -1;
49 }
50
Kean Mariotti639b54f2023-04-20 12:06:29 +000051 const auto* outputLayersTracePath =
52 (argc == 3) ? argv[2] : "/data/misc/wmtrace/layers_trace.winscope";
Kean Mariottif0f25882023-09-26 09:37:52 +000053 auto outStream = std::ofstream{outputLayersTracePath, std::ios::binary | std::ios::out};
Vishnu Nair52c4f252023-06-14 15:25:12 -070054
Kean Mariotti29aad4e2023-09-20 12:53:33 +000055 auto layerTracing = LayerTracing{outStream};
56
Vishnu Nair52c4f252023-06-14 15:25:12 -070057 const bool generateLastEntryOnly =
58 argc >= 4 && std::string_view(argv[3]) == "--last-entry-only";
59
Kean Mariotti639b54f2023-04-20 12:06:29 +000060 auto traceFlags = LayerTracing::Flag::TRACE_INPUT | LayerTracing::Flag::TRACE_BUFFERS;
61
Vishnu Nair4df8eb62022-02-17 15:32:02 -080062 ALOGD("Generating %s...", outputLayersTracePath);
63 std::cout << "Generating " << outputLayersTracePath << "\n";
Vishnu Nair5d60bbe2022-10-18 16:59:10 -070064
Kean Mariotti29aad4e2023-09-20 12:53:33 +000065 if (!LayerTraceGenerator().generate(transactionTraceFile, traceFlags, layerTracing,
Vishnu Nair52c4f252023-06-14 15:25:12 -070066 generateLastEntryOnly)) {
Kean Mariottic34489c2023-10-20 13:52:29 +000067 std::cout << "Error: Failed to generate layers trace " << outputLayersTracePath << "\n";
Vishnu Nair4df8eb62022-02-17 15:32:02 -080068 return -1;
69 }
Kean Mariottic34489c2023-10-20 13:52:29 +000070
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
Vishnu Nair4df8eb62022-02-17 15:32:02 -080079 return 0;
Kean Mariotti639b54f2023-04-20 12:06:29 +000080}