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/perfetto_cmd/config.h"
18 
19 #include <stdlib.h>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/ext/base/string_utils.h"
23 #include "perfetto/tracing/core/data_source_config.h"
24 #include "perfetto/tracing/core/trace_config.h"
25 
26 #include "protos/perfetto/config/ftrace/ftrace_config.gen.h"
27 
28 namespace perfetto {
29 namespace {
30 using ValueUnit = std::pair<uint64_t, std::string>;
31 using UnitMultipler = std::pair<const char*, uint64_t>;
32 
SplitValueAndUnit(const std::string & arg,ValueUnit * out)33 bool SplitValueAndUnit(const std::string& arg, ValueUnit* out) {
34   char* end;
35   if (arg.empty())
36     return false;
37   out->first = strtoull(arg.c_str(), &end, 10);
38   if (end == arg.data())
39     return false;
40   std::string unit = arg.substr(static_cast<size_t>(end - arg.data()));
41   out->second = std::move(unit);
42   return true;
43 }
44 
ConvertValue(const std::string & arg,std::vector<UnitMultipler> units,uint64_t * out)45 bool ConvertValue(const std::string& arg,
46                   std::vector<UnitMultipler> units,
47                   uint64_t* out) {
48   if (arg.empty()) {
49     *out = 0;
50     return true;
51   }
52 
53   if (arg == "0") {
54     *out = 0;
55     return true;
56   }
57 
58   ValueUnit value_unit{};
59   if (!SplitValueAndUnit(arg, &value_unit))
60     return false;
61 
62   for (const auto& unit_multiplier : units) {
63     if (value_unit.second != unit_multiplier.first)
64       continue;
65     *out = value_unit.first * unit_multiplier.second;
66     return true;
67   }
68   return false;
69 }
70 
ConvertTimeToMs(const std::string & arg,uint64_t * out)71 bool ConvertTimeToMs(const std::string& arg, uint64_t* out) {
72   return ConvertValue(
73       arg,
74       {
75           {"ms", 1}, {"s", 1000}, {"m", 1000 * 60}, {"h", 1000 * 60 * 60},
76       },
77       out);
78 }
79 
ConvertSizeToKb(const std::string & arg,uint64_t * out)80 bool ConvertSizeToKb(const std::string& arg, uint64_t* out) {
81   return ConvertValue(arg,
82                       {
83                           {"kb", 1},
84                           {"mb", 1024},
85                           {"gb", 1024 * 1024},
86                           {"k", 1},
87                           {"m", 1024},
88                           {"g", 1024 * 1024},
89                       },
90                       out);
91 }
92 
93 }  // namespace
94 
CreateConfigFromOptions(const ConfigOptions & options,TraceConfig * config)95 bool CreateConfigFromOptions(const ConfigOptions& options,
96                              TraceConfig* config) {
97   uint64_t duration_ms = 0;
98   if (!ConvertTimeToMs(options.time, &duration_ms)) {
99     PERFETTO_ELOG("--time argument is invalid");
100     return false;
101   }
102 
103   uint64_t buffer_size_kb = 0;
104   if (!ConvertSizeToKb(options.buffer_size, &buffer_size_kb)) {
105     PERFETTO_ELOG("--buffer argument is invalid");
106     return false;
107   }
108 
109   uint64_t max_file_size_kb = 0;
110   if (!ConvertSizeToKb(options.max_file_size, &max_file_size_kb)) {
111     PERFETTO_ELOG("--size argument is invalid");
112     return false;
113   }
114 
115   std::vector<std::string> ftrace_events;
116   std::vector<std::string> atrace_categories;
117   std::vector<std::string> atrace_apps = options.atrace_apps;
118 
119   for (const auto& category : options.categories) {
120     if (base::Contains(category, '/')) {
121       ftrace_events.push_back(category);
122     } else {
123       atrace_categories.push_back(category);
124     }
125 
126     // For the gfx category, also add the frame timeline data source
127     // as it's very useful for debugging gfx issues.
128     if (category == "gfx") {
129       auto* frame_timeline = config->add_data_sources();
130       frame_timeline->mutable_config()->set_name(
131           "android.surfaceflinger.frametimeline");
132     }
133   }
134 
135   config->set_duration_ms(static_cast<unsigned int>(duration_ms));
136   config->set_max_file_size_bytes(max_file_size_kb * 1024);
137   config->set_flush_period_ms(30 * 1000);
138   if (max_file_size_kb)
139     config->set_write_into_file(true);
140   config->add_buffers()->set_size_kb(static_cast<unsigned int>(buffer_size_kb));
141   auto* ds_config = config->add_data_sources()->mutable_config();
142   ds_config->set_name("linux.ftrace");
143   protos::gen::FtraceConfig ftrace_cfg;
144   for (const auto& evt : ftrace_events)
145     ftrace_cfg.add_ftrace_events(evt);
146   for (const auto& cat : atrace_categories)
147     ftrace_cfg.add_atrace_categories(cat);
148   for (const auto& app : atrace_apps)
149     ftrace_cfg.add_atrace_apps(app);
150   ds_config->set_ftrace_config_raw(ftrace_cfg.SerializeAsString());
151 
152   auto* ps_config = config->add_data_sources()->mutable_config();
153   ps_config->set_name("linux.process_stats");
154   ps_config->set_target_buffer(0);
155 
156   return true;
157 }
158 
159 }  // namespace perfetto
160