• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tools/trace_to_text/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 "tools/trace_to_text/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::uniform_int_distribution<char> dist('a', 'z');
63   std::string result(n, ' ');
64   for (size_t i = 0; i < n; ++i) {
65     result[i] = dist(rng);
66   }
67   return result;
68 }
69 
MaybeSymbolize(trace_processor::TraceProcessor * tp)70 void MaybeSymbolize(trace_processor::TraceProcessor* tp) {
71   std::unique_ptr<profiling::Symbolizer> symbolizer =
72       profiling::LocalSymbolizerOrDie(profiling::GetPerfettoBinaryPath(),
73                                       getenv("PERFETTO_SYMBOLIZER_MODE"));
74   if (!symbolizer)
75     return;
76   profiling::SymbolizeDatabase(tp, symbolizer.get(),
77                                [tp](const std::string& trace_proto) {
78                                  IngestTraceOrDie(tp, trace_proto);
79                                });
80   tp->NotifyEndOfFile();
81 }
82 
MaybeDeobfuscate(trace_processor::TraceProcessor * tp)83 void MaybeDeobfuscate(trace_processor::TraceProcessor* tp) {
84   auto maybe_map = profiling::GetPerfettoProguardMapPath();
85   if (maybe_map.empty()) {
86     return;
87   }
88   profiling::ReadProguardMapsToDeobfuscationPackets(
89       maybe_map, [tp](const std::string& trace_proto) {
90         IngestTraceOrDie(tp, trace_proto);
91       });
92   tp->NotifyEndOfFile();
93 }
94 
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)95 int TraceToProfile(
96     std::istream* input,
97     std::ostream* output,
98     uint64_t pid,
99     std::vector<uint64_t> timestamps,
100     ConversionMode conversion_mode,
101     uint64_t conversion_flags,
102     std::string dirname_prefix,
103     std::function<std::string(const SerializedProfile&)> filename_fn) {
104   std::vector<SerializedProfile> profiles;
105   trace_processor::Config config;
106   std::unique_ptr<trace_processor::TraceProcessor> tp =
107       trace_processor::TraceProcessor::CreateInstance(config);
108 
109   if (!ReadTrace(tp.get(), input))
110     return -1;
111 
112   tp->NotifyEndOfFile();
113   MaybeSymbolize(tp.get());
114   MaybeDeobfuscate(tp.get());
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 
172 }  // namespace trace_to_text
173 }  // namespace perfetto
174