1 /*
2 * Copyright (C) 2018 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/traceconv/trace_to_profile.h"
18
19 #include <random>
20 #include <string>
21 #include <vector>
22
23 #include "perfetto/trace_processor/trace_processor.h"
24 #include "src/profiling/symbolizer/local_symbolizer.h"
25 #include "src/profiling/symbolizer/symbolize_database.h"
26 #include "src/traceconv/utils.h"
27
28 #include "perfetto/base/logging.h"
29 #include "perfetto/base/time.h"
30 #include "perfetto/ext/base/file_utils.h"
31 #include "perfetto/ext/base/temp_file.h"
32 #include "perfetto/ext/base/utils.h"
33 #include "perfetto/profiling/pprof_builder.h"
34 #include "src/profiling/symbolizer/symbolizer.h"
35
36 namespace {
37 constexpr const char* kDefaultTmp = "/tmp";
38
GetTemp()39 std::string GetTemp() {
40 const char* tmp = nullptr;
41 if ((tmp = getenv("TMPDIR")))
42 return tmp;
43 if ((tmp = getenv("TEMP")))
44 return tmp;
45 return kDefaultTmp;
46 }
47 } // namespace
48
49 namespace perfetto {
50 namespace trace_to_text {
51 namespace {
52
ToConversionFlags(bool annotate_frames)53 uint64_t ToConversionFlags(bool annotate_frames) {
54 return static_cast<uint64_t>(annotate_frames
55 ? ConversionFlags::kAnnotateFrames
56 : ConversionFlags::kNone);
57 }
58
GetRandomString(size_t n)59 std::string GetRandomString(size_t n) {
60 std::random_device r;
61 auto rng = std::default_random_engine(r());
62 std::string result(n, ' ');
63 for (size_t i = 0; i < n; ++i) {
64 result[i] = 'a' + (rng() % ('z' - 'a'));
65 }
66 return result;
67 }
68
MaybeSymbolize(trace_processor::TraceProcessor * tp)69 void MaybeSymbolize(trace_processor::TraceProcessor* tp) {
70 std::unique_ptr<profiling::Symbolizer> symbolizer =
71 profiling::LocalSymbolizerOrDie(profiling::GetPerfettoBinaryPath(),
72 getenv("PERFETTO_SYMBOLIZER_MODE"));
73 if (!symbolizer)
74 return;
75 profiling::SymbolizeDatabase(tp, symbolizer.get(),
76 [tp](const std::string& trace_proto) {
77 IngestTraceOrDie(tp, trace_proto);
78 });
79 tp->Flush();
80 }
81
MaybeDeobfuscate(trace_processor::TraceProcessor * tp)82 void MaybeDeobfuscate(trace_processor::TraceProcessor* tp) {
83 auto maybe_map = profiling::GetPerfettoProguardMapPath();
84 if (maybe_map.empty()) {
85 return;
86 }
87 profiling::ReadProguardMapsToDeobfuscationPackets(
88 maybe_map, [tp](const std::string& trace_proto) {
89 IngestTraceOrDie(tp, trace_proto);
90 });
91 tp->Flush();
92 }
93
TraceToProfile(std::istream * input,std::ostream * output,uint64_t pid,std::vector<uint64_t> timestamps,ConversionMode conversion_mode,uint64_t conversion_flags,std::string dirname_prefix,std::function<std::string (const SerializedProfile &)> filename_fn)94 int TraceToProfile(
95 std::istream* input,
96 std::ostream* output,
97 uint64_t pid,
98 std::vector<uint64_t> timestamps,
99 ConversionMode conversion_mode,
100 uint64_t conversion_flags,
101 std::string dirname_prefix,
102 std::function<std::string(const SerializedProfile&)> filename_fn) {
103 std::vector<SerializedProfile> profiles;
104 trace_processor::Config config;
105 std::unique_ptr<trace_processor::TraceProcessor> tp =
106 trace_processor::TraceProcessor::CreateInstance(config);
107
108 if (!ReadTraceUnfinalized(tp.get(), input))
109 return -1;
110 tp->Flush();
111 MaybeSymbolize(tp.get());
112 MaybeDeobfuscate(tp.get());
113
114 TraceToPprof(tp.get(), &profiles, conversion_mode, conversion_flags, pid,
115 timestamps);
116 tp->NotifyEndOfFile();
117 if (profiles.empty()) {
118 return 0;
119 }
120
121 std::string temp_dir = GetTemp() + "/" + dirname_prefix +
122 base::GetTimeFmt("%y%m%d%H%M%S") + GetRandomString(5);
123 PERFETTO_CHECK(base::Mkdir(temp_dir));
124 for (const auto& profile : profiles) {
125 std::string filename = temp_dir + "/" + filename_fn(profile);
126 base::ScopedFile fd(base::OpenFile(filename, O_CREAT | O_WRONLY, 0700));
127 if (!fd)
128 PERFETTO_FATAL("Failed to open %s", filename.c_str());
129 PERFETTO_CHECK(base::WriteAll(*fd, profile.serialized.c_str(),
130 profile.serialized.size()) ==
131 static_cast<ssize_t>(profile.serialized.size()));
132 }
133 *output << "Wrote profiles to " << temp_dir << std::endl;
134 return 0;
135 }
136
137 } // namespace
138
TraceToHeapProfile(std::istream * input,std::ostream * output,uint64_t pid,std::vector<uint64_t> timestamps,bool annotate_frames)139 int TraceToHeapProfile(std::istream* input,
140 std::ostream* output,
141 uint64_t pid,
142 std::vector<uint64_t> timestamps,
143 bool annotate_frames) {
144 int file_idx = 0;
145 auto filename_fn = [&file_idx](const SerializedProfile& profile) {
146 return "heap_dump." + std::to_string(++file_idx) + "." +
147 std::to_string(profile.pid) + "." + profile.heap_name + ".pb";
148 };
149
150 return TraceToProfile(
151 input, output, pid, timestamps, ConversionMode::kHeapProfile,
152 ToConversionFlags(annotate_frames), "heap_profile-", filename_fn);
153 }
154
TraceToPerfProfile(std::istream * input,std::ostream * output,uint64_t pid,std::vector<uint64_t> timestamps,bool annotate_frames)155 int TraceToPerfProfile(std::istream* input,
156 std::ostream* output,
157 uint64_t pid,
158 std::vector<uint64_t> timestamps,
159 bool annotate_frames) {
160 int file_idx = 0;
161 auto filename_fn = [&file_idx](const SerializedProfile& profile) {
162 return "profile." + std::to_string(++file_idx) + ".pid." +
163 std::to_string(profile.pid) + ".pb";
164 };
165
166 return TraceToProfile(
167 input, output, pid, timestamps, ConversionMode::kPerfProfile,
168 ToConversionFlags(annotate_frames), "perf_profile-", filename_fn);
169 }
170
171 } // namespace trace_to_text
172 } // namespace perfetto
173