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 if (auto status = tp->NotifyEndOfFile(); !status.ok()) {
114 return -1;
115 }
116 TraceToPprof(tp.get(), &profiles, conversion_mode, conversion_flags, pid,
117 timestamps);
118 if (profiles.empty()) {
119 return 0;
120 }
121
122 std::string temp_dir = GetTemp() + "/" + dirname_prefix +
123 base::GetTimeFmt("%y%m%d%H%M%S") + GetRandomString(5);
124 PERFETTO_CHECK(base::Mkdir(temp_dir));
125 for (const auto& profile : profiles) {
126 std::string filename = temp_dir + "/" + filename_fn(profile);
127 base::ScopedFile fd(base::OpenFile(filename, O_CREAT | O_WRONLY, 0700));
128 if (!fd)
129 PERFETTO_FATAL("Failed to open %s", filename.c_str());
130 PERFETTO_CHECK(base::WriteAll(*fd, profile.serialized.c_str(),
131 profile.serialized.size()) ==
132 static_cast<ssize_t>(profile.serialized.size()));
133 }
134 *output << "Wrote profiles to " << temp_dir << std::endl;
135 return 0;
136 }
137
138 } // namespace
139
TraceToHeapProfile(std::istream * input,std::ostream * output,uint64_t pid,std::vector<uint64_t> timestamps,bool annotate_frames)140 int TraceToHeapProfile(std::istream* input,
141 std::ostream* output,
142 uint64_t pid,
143 std::vector<uint64_t> timestamps,
144 bool annotate_frames) {
145 int file_idx = 0;
146 auto filename_fn = [&file_idx](const SerializedProfile& profile) {
147 return "heap_dump." + std::to_string(++file_idx) + "." +
148 std::to_string(profile.pid) + "." + profile.heap_name + ".pb";
149 };
150
151 return TraceToProfile(
152 input, output, pid, timestamps, ConversionMode::kHeapProfile,
153 ToConversionFlags(annotate_frames), "heap_profile-", filename_fn);
154 }
155
TraceToPerfProfile(std::istream * input,std::ostream * output,uint64_t pid,std::vector<uint64_t> timestamps,bool annotate_frames)156 int TraceToPerfProfile(std::istream* input,
157 std::ostream* output,
158 uint64_t pid,
159 std::vector<uint64_t> timestamps,
160 bool annotate_frames) {
161 int file_idx = 0;
162 auto filename_fn = [&file_idx](const SerializedProfile& profile) {
163 return "profile." + std::to_string(++file_idx) + ".pid." +
164 std::to_string(profile.pid) + ".pb";
165 };
166
167 return TraceToProfile(
168 input, output, pid, timestamps, ConversionMode::kPerfProfile,
169 ToConversionFlags(annotate_frames), "perf_profile-", filename_fn);
170 }
171
TraceToJavaHeapProfile(std::istream * input,std::ostream * output,const uint64_t pid,const std::vector<uint64_t> & timestamps,const bool annotate_frames)172 int TraceToJavaHeapProfile(std::istream* input,
173 std::ostream* output,
174 const uint64_t pid,
175 const std::vector<uint64_t>& timestamps,
176 const bool annotate_frames) {
177 int file_idx = 0;
178 auto filename_fn = [&file_idx](const SerializedProfile& profile) {
179 return "java_heap_dump." + std::to_string(++file_idx) + "." +
180 std::to_string(profile.pid) + ".pb";
181 };
182
183 return TraceToProfile(
184 input, output, pid, timestamps, ConversionMode::kJavaHeapProfile,
185 ToConversionFlags(annotate_frames), "heap_profile-", filename_fn);
186 }
187 } // namespace trace_to_text
188 } // namespace perfetto
189