Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 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" |
Hector Dearman | 2ccf2d7 | 2021-04-20 11:28:50 +0100 | [diff] [blame] | 22 | #include "perfetto/ext/base/string_utils.h" |
Primiano Tucci | 54985c2 | 2019-12-03 12:14:08 +0000 | [diff] [blame] | 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" |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 27 | |
| 28 | namespace perfetto { |
| 29 | namespace { |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 30 | using ValueUnit = std::pair<uint64_t, std::string>; |
| 31 | using UnitMultipler = std::pair<const char*, uint64_t>; |
| 32 | |
| 33 | bool SplitValueAndUnit(const std::string& arg, ValueUnit* out) { |
| 34 | char* end; |
Hector Dearman | e97c89b | 2021-01-08 13:00:13 +0000 | [diff] [blame] | 35 | if (arg.empty()) |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 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 | |
| 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 | |
Hector Dearman | 146b5b7 | 2020-07-31 18:46:32 +0100 | [diff] [blame] | 53 | if (arg == "0") { |
| 54 | *out = 0; |
| 55 | return true; |
| 56 | } |
| 57 | |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 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 | |
| 71 | bool ConvertTimeToMs(const std::string& arg, uint64_t* out) { |
Hector Dearman | 859d8f8 | 2022-05-10 15:13:45 +0100 | [diff] [blame] | 72 | return ConvertValue(arg, |
| 73 | { |
| 74 | {"ms", 1}, |
| 75 | {"s", 1000}, |
| 76 | {"m", 1000 * 60}, |
| 77 | {"h", 1000 * 60 * 60}, |
| 78 | }, |
| 79 | out); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool ConvertSizeToKb(const std::string& arg, uint64_t* out) { |
| 83 | return ConvertValue(arg, |
| 84 | { |
| 85 | {"kb", 1}, |
| 86 | {"mb", 1024}, |
| 87 | {"gb", 1024 * 1024}, |
| 88 | {"k", 1}, |
| 89 | {"m", 1024}, |
| 90 | {"g", 1024 * 1024}, |
| 91 | }, |
| 92 | out); |
| 93 | } |
| 94 | |
| 95 | } // namespace |
| 96 | |
| 97 | bool CreateConfigFromOptions(const ConfigOptions& options, |
Primiano Tucci | 54985c2 | 2019-12-03 12:14:08 +0000 | [diff] [blame] | 98 | TraceConfig* config) { |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 99 | uint64_t duration_ms = 0; |
Primiano Tucci | aaf562c | 2018-12-04 21:21:36 +0000 | [diff] [blame] | 100 | if (!ConvertTimeToMs(options.time, &duration_ms)) { |
| 101 | PERFETTO_ELOG("--time argument is invalid"); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 102 | return false; |
Primiano Tucci | aaf562c | 2018-12-04 21:21:36 +0000 | [diff] [blame] | 103 | } |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 104 | |
| 105 | uint64_t buffer_size_kb = 0; |
Primiano Tucci | aaf562c | 2018-12-04 21:21:36 +0000 | [diff] [blame] | 106 | if (!ConvertSizeToKb(options.buffer_size, &buffer_size_kb)) { |
| 107 | PERFETTO_ELOG("--buffer argument is invalid"); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 108 | return false; |
Primiano Tucci | aaf562c | 2018-12-04 21:21:36 +0000 | [diff] [blame] | 109 | } |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 110 | |
| 111 | uint64_t max_file_size_kb = 0; |
Primiano Tucci | aaf562c | 2018-12-04 21:21:36 +0000 | [diff] [blame] | 112 | if (!ConvertSizeToKb(options.max_file_size, &max_file_size_kb)) { |
| 113 | PERFETTO_ELOG("--size argument is invalid"); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 114 | return false; |
Primiano Tucci | aaf562c | 2018-12-04 21:21:36 +0000 | [diff] [blame] | 115 | } |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 116 | |
| 117 | std::vector<std::string> ftrace_events; |
| 118 | std::vector<std::string> atrace_categories; |
| 119 | std::vector<std::string> atrace_apps = options.atrace_apps; |
| 120 | |
| 121 | for (const auto& category : options.categories) { |
Hector Dearman | 2ccf2d7 | 2021-04-20 11:28:50 +0100 | [diff] [blame] | 122 | if (base::Contains(category, '/')) { |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 123 | ftrace_events.push_back(category); |
Hector Dearman | 2ccf2d7 | 2021-04-20 11:28:50 +0100 | [diff] [blame] | 124 | } else { |
| 125 | atrace_categories.push_back(category); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 126 | } |
Lalit Maganti | 3a5a52f | 2021-05-04 13:12:43 +0100 | [diff] [blame] | 127 | |
| 128 | // For the gfx category, also add the frame timeline data source |
| 129 | // as it's very useful for debugging gfx issues. |
| 130 | if (category == "gfx") { |
| 131 | auto* frame_timeline = config->add_data_sources(); |
| 132 | frame_timeline->mutable_config()->set_name( |
| 133 | "android.surfaceflinger.frametimeline"); |
| 134 | } |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | config->set_duration_ms(static_cast<unsigned int>(duration_ms)); |
Primiano Tucci | aaf562c | 2018-12-04 21:21:36 +0000 | [diff] [blame] | 138 | config->set_max_file_size_bytes(max_file_size_kb * 1024); |
Hector Dearman | 2f38029 | 2019-02-27 13:15:44 +0000 | [diff] [blame] | 139 | config->set_flush_period_ms(30 * 1000); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 140 | if (max_file_size_kb) |
| 141 | config->set_write_into_file(true); |
| 142 | config->add_buffers()->set_size_kb(static_cast<unsigned int>(buffer_size_kb)); |
| 143 | auto* ds_config = config->add_data_sources()->mutable_config(); |
| 144 | ds_config->set_name("linux.ftrace"); |
Primiano Tucci | 54985c2 | 2019-12-03 12:14:08 +0000 | [diff] [blame] | 145 | protos::gen::FtraceConfig ftrace_cfg; |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 146 | for (const auto& evt : ftrace_events) |
Primiano Tucci | 54985c2 | 2019-12-03 12:14:08 +0000 | [diff] [blame] | 147 | ftrace_cfg.add_ftrace_events(evt); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 148 | for (const auto& cat : atrace_categories) |
Primiano Tucci | 54985c2 | 2019-12-03 12:14:08 +0000 | [diff] [blame] | 149 | ftrace_cfg.add_atrace_categories(cat); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 150 | for (const auto& app : atrace_apps) |
Primiano Tucci | 54985c2 | 2019-12-03 12:14:08 +0000 | [diff] [blame] | 151 | ftrace_cfg.add_atrace_apps(app); |
Hector Dearman | 35c86de | 2022-03-23 13:31:01 +0000 | [diff] [blame] | 152 | ftrace_cfg.set_symbolize_ksyms(true); |
Primiano Tucci | 54985c2 | 2019-12-03 12:14:08 +0000 | [diff] [blame] | 153 | ds_config->set_ftrace_config_raw(ftrace_cfg.SerializeAsString()); |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 154 | |
Hector Dearman | 5edeafc | 2018-11-29 13:27:33 +0000 | [diff] [blame] | 155 | auto* ps_config = config->add_data_sources()->mutable_config(); |
| 156 | ps_config->set_name("linux.process_stats"); |
| 157 | ps_config->set_target_buffer(0); |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | } // namespace perfetto |