Lalit Maganti | 79f2d7b | 2018-01-23 18: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 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 17 | #include <unistd.h> |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 18 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 19 | #include <chrono> |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 20 | #include <functional> |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 21 | #include <initializer_list> |
Lalit Maganti | 3f5705c | 2018-03-09 12:09:44 +0000 | [diff] [blame] | 22 | #include <random> |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 23 | #include <thread> |
| 24 | |
Primiano Tucci | fe92233 | 2018-03-22 16:15:04 -0700 | [diff] [blame] | 25 | #include "perfetto/base/build_config.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 26 | #include "perfetto/base/logging.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 27 | #include "perfetto/ext/base/file_utils.h" |
| 28 | #include "perfetto/ext/base/pipe.h" |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 29 | #include "perfetto/ext/base/scoped_file.h" |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 30 | #include "perfetto/ext/base/string_utils.h" |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 31 | #include "perfetto/ext/base/subprocess.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 32 | #include "perfetto/ext/base/temp_file.h" |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 33 | #include "perfetto/ext/ipc/basic_types.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 34 | #include "perfetto/ext/traced/traced.h" |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 35 | #include "perfetto/ext/tracing/core/commit_data_request.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 36 | #include "perfetto/ext/tracing/core/trace_packet.h" |
Stephen Nusko | ac0c197 | 2019-06-25 13:57:13 +0100 | [diff] [blame] | 37 | #include "perfetto/ext/tracing/ipc/default_socket.h" |
Primiano Tucci | 0f2f3b4 | 2019-05-21 19:37:01 +0100 | [diff] [blame] | 38 | #include "perfetto/protozero/scattered_heap_buffer.h" |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 39 | #include "perfetto/tracing/core/tracing_service_state.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 40 | #include "src/base/test/test_task_runner.h" |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 41 | #include "src/base/test/utils.h" |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 42 | #include "src/traced/probes/ftrace/ftrace_controller.h" |
| 43 | #include "src/traced/probes/ftrace/ftrace_procfs.h" |
Primiano Tucci | 919ca1e | 2019-08-21 20:26:58 +0200 | [diff] [blame] | 44 | #include "test/gtest_and_gmock.h" |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 45 | #include "test/test_helper.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 46 | |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 47 | #include "protos/perfetto/config/power/android_power_config.pbzero.h" |
| 48 | #include "protos/perfetto/config/test_config.gen.h" |
| 49 | #include "protos/perfetto/config/trace_config.gen.h" |
| 50 | #include "protos/perfetto/trace/ftrace/ftrace.gen.h" |
| 51 | #include "protos/perfetto/trace/ftrace/ftrace_event.gen.h" |
| 52 | #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.gen.h" |
| 53 | #include "protos/perfetto/trace/power/battery_counters.gen.h" |
| 54 | #include "protos/perfetto/trace/test_event.gen.h" |
| 55 | #include "protos/perfetto/trace/trace.gen.h" |
| 56 | #include "protos/perfetto/trace/trace_packet.gen.h" |
| 57 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
| 58 | #include "protos/perfetto/trace/trigger.gen.h" |
| 59 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 60 | namespace perfetto { |
| 61 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 62 | namespace { |
| 63 | |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 64 | using ::testing::ContainsRegex; |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 65 | using ::testing::ElementsAreArray; |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 66 | using ::testing::HasSubstr; |
| 67 | |
Primiano Tucci | 79e4dcb | 2020-04-08 09:51:02 +0100 | [diff] [blame] | 68 | constexpr size_t kBuiltinPackets = 8; |
| 69 | |
Stephen Nusko | 49c5a3c | 2019-04-17 13:36:09 +0100 | [diff] [blame] | 70 | std::string RandomTraceFileName() { |
| 71 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
| 72 | constexpr char kSysTmpPath[] = "/data/misc/perfetto-traces"; |
| 73 | #else |
| 74 | constexpr char kSysTmpPath[] = "/tmp"; |
| 75 | #endif |
| 76 | static int suffix = 0; |
| 77 | |
| 78 | std::string path; |
| 79 | path.assign(kSysTmpPath); |
| 80 | path.append("/trace-"); |
| 81 | path.append(std::to_string(base::GetBootTimeNs().count())); |
| 82 | path.append("-"); |
| 83 | path.append(std::to_string(suffix++)); |
| 84 | return path; |
| 85 | } |
| 86 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 87 | // This class is a reference to a child process that has in essence been execv |
| 88 | // to the requested binary. The process will start and then wait for Run() |
| 89 | // before proceeding. We use this to fork new processes before starting any |
Hector Dearman | 5145e50 | 2019-09-18 16:52:24 +0100 | [diff] [blame] | 90 | // additional threads in the parent process (otherwise you would risk |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 91 | // deadlocks), but pause the forked processes until remaining setup (including |
| 92 | // any necessary threads) in the parent process is complete. |
| 93 | class Exec { |
| 94 | public: |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 95 | // Starts the forked process that was created. If not null then |stderr_out| |
| 96 | // will contain the stderr of the process. |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 97 | int Run(std::string* stderr_out = nullptr) { |
| 98 | // We can't be the child process. |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 99 | PERFETTO_CHECK(getpid() != subprocess_.pid()); |
| 100 | // Will cause the entrypoint to continue. |
| 101 | PERFETTO_CHECK(write(*sync_pipe_.wr, "1", 1) == 1); |
| 102 | sync_pipe_.wr.reset(); |
| 103 | subprocess_.Wait(); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 104 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 105 | if (stderr_out) { |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 106 | *stderr_out = std::move(subprocess_.output()); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 107 | } else { |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 108 | PERFETTO_LOG("Child proc %d exited with stderr: \"%s\"", |
| 109 | subprocess_.pid(), subprocess_.output().c_str()); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 110 | } |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 111 | return subprocess_.returncode(); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private: |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 115 | Exec(const std::string& argv0, |
| 116 | std::initializer_list<std::string> args, |
| 117 | std::string input = "") { |
| 118 | subprocess_.args.stderr_mode = base::Subprocess::kBuffer; |
| 119 | subprocess_.args.stdout_mode = base::Subprocess::kDevNull; |
| 120 | subprocess_.args.input = input; |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 121 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 122 | #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 123 | constexpr bool kUseSystemBinaries = false; |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 124 | #else |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 125 | constexpr bool kUseSystemBinaries = true; |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 126 | #endif |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 127 | |
| 128 | std::vector<std::string>& cmd = subprocess_.args.exec_cmd; |
| 129 | if (kUseSystemBinaries) { |
| 130 | cmd.push_back("/system/bin/" + argv0); |
| 131 | cmd.insert(cmd.end(), args.begin(), args.end()); |
| 132 | } else { |
| 133 | subprocess_.args.env.push_back( |
| 134 | std::string("PERFETTO_PRODUCER_SOCK_NAME=") + |
| 135 | TestHelper::GetProducerSocketName()); |
| 136 | subprocess_.args.env.push_back( |
| 137 | std::string("PERFETTO_CONSUMER_SOCK_NAME=") + |
| 138 | TestHelper::GetConsumerSocketName()); |
| 139 | cmd.push_back(base::GetCurExecutableDir() + "/" + argv0); |
| 140 | cmd.insert(cmd.end(), args.begin(), args.end()); |
| 141 | } |
| 142 | |
| 143 | if (access(cmd[0].c_str(), F_OK)) { |
| 144 | PERFETTO_FATAL( |
| 145 | "Cannot find %s. Make sure that the target has been built and, on " |
| 146 | "Android, pushed to the device.", |
| 147 | cmd[0].c_str()); |
| 148 | } |
| 149 | |
| 150 | // This pipe blocks the execution of the child process until the main test |
| 151 | // process calls Run(). There are two conflicting problems here: |
| 152 | // 1) We can't fork() subprocesses too late, because the test spawns threads |
| 153 | // for hosting the service. fork+threads = bad (see aosp/1089744). |
| 154 | // 2) We can't run the subprocess too early, because we need to wait that |
| 155 | // the service threads are ready before trying to connect from the child |
| 156 | // process. |
| 157 | sync_pipe_ = base::Pipe::Create(); |
| 158 | int sync_pipe_rd = *sync_pipe_.rd; |
| 159 | subprocess_.args.preserve_fds.push_back(sync_pipe_rd); |
| 160 | |
| 161 | // This lambda will be called on the forked child process after having |
| 162 | // setup pipe redirection and closed all FDs, right before the exec(). |
| 163 | // The Subprocesss harness will take care of closing also |sync_pipe_.wr|. |
| 164 | subprocess_.args.entrypoint_for_testing = [sync_pipe_rd] { |
| 165 | // Don't add any logging here, all file descriptors are closed and trying |
| 166 | // to log will likely cause undefined behaviors. |
| 167 | char ignored = 0; |
| 168 | PERFETTO_CHECK(PERFETTO_EINTR(read(sync_pipe_rd, &ignored, 1)) > 0); |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 169 | PERFETTO_CHECK(close(sync_pipe_rd) == 0 || errno == EINTR); |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | subprocess_.Start(); |
| 173 | sync_pipe_.rd.reset(); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | friend class PerfettoCmdlineTest; |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 177 | base::Subprocess subprocess_; |
| 178 | base::Pipe sync_pipe_; |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 179 | }; |
| 180 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 181 | class PerfettoTest : public ::testing::Test { |
| 182 | public: |
| 183 | void SetUp() override { |
| 184 | // TODO(primiano): refactor this, it's copy/pasted in three places now. |
| 185 | size_t index = 0; |
| 186 | constexpr auto kTracingPaths = FtraceController::kTracingPaths; |
| 187 | while (!ftrace_procfs_ && kTracingPaths[index]) { |
| 188 | ftrace_procfs_ = FtraceProcfs::Create(kTracingPaths[index++]); |
| 189 | } |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | std::unique_ptr<FtraceProcfs> ftrace_procfs_; |
| 193 | }; |
| 194 | |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 195 | class PerfettoCmdlineTest : public ::testing::Test { |
| 196 | public: |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 197 | void SetUp() override { exec_allowed_ = true; } |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 198 | |
| 199 | void TearDown() override {} |
| 200 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 201 | void StartServiceIfRequiredNoNewExecsAfterThis() { |
| 202 | exec_allowed_ = false; |
| 203 | test_helper_.StartServiceIfRequired(); |
Hector Dearman | 696ff77 | 2019-04-23 18:38:53 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 206 | FakeProducer* ConnectFakeProducer() { |
| 207 | return test_helper_.ConnectFakeProducer(); |
Hector Dearman | 696ff77 | 2019-04-23 18:38:53 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 210 | std::function<void()> WrapTask(const std::function<void()>& function) { |
| 211 | return test_helper_.WrapTask(function); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 214 | void WaitForProducerSetup() { test_helper_.WaitForProducerSetup(); } |
| 215 | |
| 216 | void WaitForProducerEnabled() { test_helper_.WaitForProducerEnabled(); } |
| 217 | |
| 218 | // Creates a process that represents the perfetto binary that will |
| 219 | // start when Run() is called. |args| will be passed as part of |
| 220 | // the command line and |std_in| will be piped into std::cin. |
| 221 | Exec ExecPerfetto(std::initializer_list<std::string> args, |
| 222 | std::string std_in = "") { |
| 223 | // You can not fork after you've started the service due to risk of |
| 224 | // deadlocks. |
| 225 | PERFETTO_CHECK(exec_allowed_); |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 226 | return Exec("perfetto", std::move(args), std::move(std_in)); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Creates a process that represents the trigger_perfetto binary that will |
| 230 | // start when Run() is called. |args| will be passed as part of |
| 231 | // the command line and |std_in| will be piped into std::cin. |
| 232 | Exec ExecTrigger(std::initializer_list<std::string> args, |
| 233 | std::string std_in = "") { |
| 234 | // You can not fork after you've started the service due to risk of |
| 235 | // deadlocks. |
| 236 | PERFETTO_CHECK(exec_allowed_); |
Primiano Tucci | e094eec | 2020-03-18 16:58:21 +0000 | [diff] [blame] | 237 | return Exec("trigger_perfetto", std::move(args), std::move(std_in)); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Tests are allowed to freely use these variables. |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 241 | std::string stderr_; |
| 242 | base::TestTaskRunner task_runner_; |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 243 | |
| 244 | private: |
| 245 | bool exec_allowed_; |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 246 | TestHelper test_helper_{&task_runner_}; |
| 247 | }; |
| 248 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 249 | } // namespace |
| 250 | |
Lalit Maganti | d0e7679 | 2018-02-12 14:25:01 +0000 | [diff] [blame] | 251 | // If we're building on Android and starting the daemons ourselves, |
| 252 | // create the sockets in a world-writable location. |
Oystein Eftevaag | ff72959 | 2018-02-12 14:24:06 -0800 | [diff] [blame] | 253 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \ |
| 254 | PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 255 | #define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 256 | #else |
Florian Mayer | c29e0d3 | 2018-04-04 15:55:46 +0100 | [diff] [blame] | 257 | #define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket() |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 258 | #endif |
| 259 | |
Primiano Tucci | fe92233 | 2018-03-22 16:15:04 -0700 | [diff] [blame] | 260 | #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 261 | #define TreeHuggerOnly(x) x |
Lalit Maganti | 7d3e30a | 2018-02-16 10:20:36 +0000 | [diff] [blame] | 262 | #else |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 263 | #define TreeHuggerOnly(x) DISABLED_##x |
Lalit Maganti | 7d3e30a | 2018-02-16 10:20:36 +0000 | [diff] [blame] | 264 | #endif |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 265 | |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 266 | // TODO(b/73453011): reenable on more platforms (including standalone Android). |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 267 | TEST_F(PerfettoTest, TreeHuggerOnly(TestFtraceProducer)) { |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 268 | base::TestTaskRunner task_runner; |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 269 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 270 | TestHelper helper(&task_runner); |
| 271 | helper.StartServiceIfRequired(); |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 272 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 273 | #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 274 | ProbesProducerThread probes(TEST_PRODUCER_SOCK_NAME); |
| 275 | probes.Connect(); |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 276 | #endif |
| 277 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 278 | helper.ConnectConsumer(); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 279 | helper.WaitForConsumerConnect(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 280 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 281 | TraceConfig trace_config; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 282 | trace_config.add_buffers()->set_size_kb(1024); |
| 283 | trace_config.set_duration_ms(3000); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 284 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 285 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
Primiano Tucci | 578d784 | 2018-03-29 15:27:05 +0100 | [diff] [blame] | 286 | ds_config->set_name("linux.ftrace"); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 287 | ds_config->set_target_buffer(0); |
| 288 | |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 289 | protos::gen::FtraceConfig ftrace_config; |
Primiano Tucci | 0f2f3b4 | 2019-05-21 19:37:01 +0100 | [diff] [blame] | 290 | ftrace_config.add_ftrace_events("sched_switch"); |
| 291 | ftrace_config.add_ftrace_events("bar"); |
| 292 | ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString()); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 293 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 294 | helper.StartTracing(trace_config); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 295 | helper.WaitForTracingDisabled(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 296 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 297 | helper.ReadData(); |
| 298 | helper.WaitForReadData(); |
| 299 | |
| 300 | const auto& packets = helper.trace(); |
| 301 | ASSERT_GT(packets.size(), 0u); |
| 302 | |
| 303 | for (const auto& packet : packets) { |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 304 | for (int ev = 0; ev < packet.ftrace_events().event_size(); ev++) { |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 305 | ASSERT_TRUE(packet.ftrace_events() |
| 306 | .event()[static_cast<size_t>(ev)] |
| 307 | .has_sched_switch()); |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 308 | } |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 309 | } |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 312 | // TODO(b/73453011): reenable on more platforms (including standalone Android). |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 313 | TEST_F(PerfettoTest, TreeHuggerOnly(TestFtraceFlush)) { |
| 314 | base::TestTaskRunner task_runner; |
| 315 | |
| 316 | TestHelper helper(&task_runner); |
| 317 | helper.StartServiceIfRequired(); |
| 318 | |
| 319 | #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 320 | ProbesProducerThread probes(TEST_PRODUCER_SOCK_NAME); |
| 321 | probes.Connect(); |
Primiano Tucci | 0ea9a37 | 2018-12-03 20:21:42 +0100 | [diff] [blame] | 322 | #endif |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 323 | |
| 324 | helper.ConnectConsumer(); |
| 325 | helper.WaitForConsumerConnect(); |
| 326 | |
| 327 | const uint32_t kTestTimeoutMs = 30000; |
| 328 | TraceConfig trace_config; |
Ryan Savitski | e3235e8 | 2020-02-03 18:11:51 +0000 | [diff] [blame] | 329 | trace_config.add_buffers()->set_size_kb(32); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 330 | trace_config.set_duration_ms(kTestTimeoutMs); |
| 331 | |
| 332 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 333 | ds_config->set_name("linux.ftrace"); |
| 334 | |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 335 | protos::gen::FtraceConfig ftrace_config; |
Primiano Tucci | 0f2f3b4 | 2019-05-21 19:37:01 +0100 | [diff] [blame] | 336 | ftrace_config.add_ftrace_events("print"); |
| 337 | ds_config->set_ftrace_config_raw(ftrace_config.SerializeAsString()); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 338 | |
| 339 | helper.StartTracing(trace_config); |
| 340 | |
| 341 | // Do a first flush just to synchronize with the producer. The problem here |
| 342 | // is that, on a Linux workstation, the producer can take several seconds just |
| 343 | // to get to the point where ftrace is ready. We use the flush ack as a |
| 344 | // synchronization point. |
| 345 | helper.FlushAndWait(kTestTimeoutMs); |
| 346 | |
| 347 | EXPECT_TRUE(ftrace_procfs_->IsTracingEnabled()); |
| 348 | const char kMarker[] = "just_one_event"; |
| 349 | EXPECT_TRUE(ftrace_procfs_->WriteTraceMarker(kMarker)); |
| 350 | |
| 351 | // This is the real flush we are testing. |
| 352 | helper.FlushAndWait(kTestTimeoutMs); |
| 353 | |
| 354 | helper.DisableTracing(); |
| 355 | helper.WaitForTracingDisabled(kTestTimeoutMs); |
| 356 | |
| 357 | helper.ReadData(); |
| 358 | helper.WaitForReadData(); |
| 359 | |
| 360 | int marker_found = 0; |
| 361 | for (const auto& packet : helper.trace()) { |
| 362 | for (int i = 0; i < packet.ftrace_events().event_size(); i++) { |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 363 | const auto& ev = packet.ftrace_events().event()[static_cast<size_t>(i)]; |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 364 | if (ev.has_print() && ev.print().buf().find(kMarker) != std::string::npos) |
| 365 | marker_found++; |
| 366 | } |
| 367 | } |
| 368 | ASSERT_EQ(marker_found, 1); |
| 369 | } |
| 370 | |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 371 | // TODO(b/73453011): reenable on more platforms (including standalone Android). |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 372 | TEST_F(PerfettoTest, TreeHuggerOnly(TestBatteryTracing)) { |
Primiano Tucci | 0ea9a37 | 2018-12-03 20:21:42 +0100 | [diff] [blame] | 373 | base::TestTaskRunner task_runner; |
| 374 | |
| 375 | TestHelper helper(&task_runner); |
| 376 | helper.StartServiceIfRequired(); |
| 377 | |
| 378 | #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 379 | ProbesProducerThread probes(TEST_PRODUCER_SOCK_NAME); |
| 380 | probes.Connect(); |
Florian Mayer | 4280249 | 2018-12-13 11:00:16 +0000 | [diff] [blame] | 381 | #else |
| 382 | base::ignore_result(TEST_PRODUCER_SOCK_NAME); |
Primiano Tucci | 0ea9a37 | 2018-12-03 20:21:42 +0100 | [diff] [blame] | 383 | #endif |
| 384 | |
| 385 | helper.ConnectConsumer(); |
| 386 | helper.WaitForConsumerConnect(); |
| 387 | |
| 388 | TraceConfig trace_config; |
| 389 | trace_config.add_buffers()->set_size_kb(128); |
| 390 | trace_config.set_duration_ms(3000); |
| 391 | |
| 392 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 393 | ds_config->set_name("android.power"); |
| 394 | ds_config->set_target_buffer(0); |
Primiano Tucci | 0f2f3b4 | 2019-05-21 19:37:01 +0100 | [diff] [blame] | 395 | |
| 396 | using protos::pbzero::AndroidPowerConfig; |
| 397 | protozero::HeapBuffered<AndroidPowerConfig> power_config; |
Primiano Tucci | 0ea9a37 | 2018-12-03 20:21:42 +0100 | [diff] [blame] | 398 | power_config->set_battery_poll_ms(250); |
Primiano Tucci | 0f2f3b4 | 2019-05-21 19:37:01 +0100 | [diff] [blame] | 399 | power_config->add_battery_counters( |
| 400 | AndroidPowerConfig::BATTERY_COUNTER_CHARGE); |
| 401 | power_config->add_battery_counters( |
| 402 | AndroidPowerConfig::BATTERY_COUNTER_CAPACITY_PERCENT); |
| 403 | ds_config->set_android_power_config_raw(power_config.SerializeAsString()); |
Primiano Tucci | 0ea9a37 | 2018-12-03 20:21:42 +0100 | [diff] [blame] | 404 | |
| 405 | helper.StartTracing(trace_config); |
| 406 | helper.WaitForTracingDisabled(); |
| 407 | |
| 408 | helper.ReadData(); |
| 409 | helper.WaitForReadData(); |
| 410 | |
| 411 | const auto& packets = helper.trace(); |
| 412 | ASSERT_GT(packets.size(), 0u); |
| 413 | |
| 414 | bool has_battery_packet = false; |
| 415 | for (const auto& packet : packets) { |
| 416 | if (!packet.has_battery()) |
| 417 | continue; |
| 418 | has_battery_packet = true; |
Primiano Tucci | 61804c0 | 2018-12-14 11:34:44 +0000 | [diff] [blame] | 419 | // Unfortunately we cannot make any assertions on the charge counter. |
| 420 | // On some devices it can reach negative values (b/64685329). |
Primiano Tucci | 0ea9a37 | 2018-12-03 20:21:42 +0100 | [diff] [blame] | 421 | EXPECT_GE(packet.battery().capacity_percent(), 0); |
| 422 | EXPECT_LE(packet.battery().capacity_percent(), 100); |
| 423 | } |
| 424 | |
| 425 | ASSERT_TRUE(has_battery_packet); |
| 426 | } |
| 427 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 428 | TEST_F(PerfettoTest, TestFakeProducer) { |
Lalit Maganti | dd95ef9 | 2018-03-23 09:42:48 +0000 | [diff] [blame] | 429 | base::TestTaskRunner task_runner; |
| 430 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 431 | TestHelper helper(&task_runner); |
| 432 | helper.StartServiceIfRequired(); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 433 | helper.ConnectFakeProducer(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 434 | helper.ConnectConsumer(); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 435 | helper.WaitForConsumerConnect(); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 436 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 437 | TraceConfig trace_config; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 438 | trace_config.add_buffers()->set_size_kb(1024); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 439 | trace_config.set_duration_ms(200); |
| 440 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 441 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 442 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 443 | ds_config->set_target_buffer(0); |
| 444 | |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 445 | static constexpr size_t kNumPackets = 11; |
Lalit Maganti | 3f5705c | 2018-03-09 12:09:44 +0000 | [diff] [blame] | 446 | static constexpr uint32_t kRandomSeed = 42; |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 447 | static constexpr uint32_t kMsgSize = 1024; |
Lalit Maganti | 3f5705c | 2018-03-09 12:09:44 +0000 | [diff] [blame] | 448 | ds_config->mutable_for_testing()->set_seed(kRandomSeed); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 449 | ds_config->mutable_for_testing()->set_message_count(kNumPackets); |
| 450 | ds_config->mutable_for_testing()->set_message_size(kMsgSize); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 451 | ds_config->mutable_for_testing()->set_send_batch_on_register(true); |
Lalit Maganti | 3f5705c | 2018-03-09 12:09:44 +0000 | [diff] [blame] | 452 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 453 | helper.StartTracing(trace_config); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 454 | helper.WaitForTracingDisabled(); |
Lalit Maganti | 3f5705c | 2018-03-09 12:09:44 +0000 | [diff] [blame] | 455 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 456 | helper.ReadData(); |
| 457 | helper.WaitForReadData(); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 458 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 459 | const auto& packets = helper.trace(); |
| 460 | ASSERT_EQ(packets.size(), kNumPackets); |
| 461 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 462 | std::minstd_rand0 rnd_engine(kRandomSeed); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 463 | for (const auto& packet : packets) { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 464 | ASSERT_TRUE(packet.has_for_testing()); |
| 465 | ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine()); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 466 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 469 | TEST_F(PerfettoTest, VeryLargePackets) { |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 470 | base::TestTaskRunner task_runner; |
| 471 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 472 | TestHelper helper(&task_runner); |
| 473 | helper.StartServiceIfRequired(); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 474 | helper.ConnectFakeProducer(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 475 | helper.ConnectConsumer(); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 476 | helper.WaitForConsumerConnect(); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 477 | |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 478 | TraceConfig trace_config; |
| 479 | trace_config.add_buffers()->set_size_kb(4096 * 10); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 480 | trace_config.set_duration_ms(500); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 481 | |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 482 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 483 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 484 | ds_config->set_target_buffer(0); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 485 | |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 486 | static constexpr size_t kNumPackets = 7; |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 487 | static constexpr uint32_t kRandomSeed = 42; |
| 488 | static constexpr uint32_t kMsgSize = 1024 * 1024 - 42; |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 489 | ds_config->mutable_for_testing()->set_seed(kRandomSeed); |
| 490 | ds_config->mutable_for_testing()->set_message_count(kNumPackets); |
| 491 | ds_config->mutable_for_testing()->set_message_size(kMsgSize); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 492 | ds_config->mutable_for_testing()->set_send_batch_on_register(true); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 493 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 494 | helper.StartTracing(trace_config); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 495 | helper.WaitForTracingDisabled(); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 496 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 497 | helper.ReadData(); |
Sami Kyostila | b35c674 | 2020-02-12 16:06:45 +0000 | [diff] [blame] | 498 | helper.WaitForReadData(/* read_count */ 0, /* timeout_ms */ 10000); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 499 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 500 | const auto& packets = helper.trace(); |
| 501 | ASSERT_EQ(packets.size(), kNumPackets); |
| 502 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 503 | std::minstd_rand0 rnd_engine(kRandomSeed); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 504 | for (const auto& packet : packets) { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 505 | ASSERT_TRUE(packet.has_for_testing()); |
| 506 | ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine()); |
| 507 | size_t msg_size = packet.for_testing().str().size(); |
| 508 | ASSERT_EQ(kMsgSize, msg_size); |
| 509 | for (size_t i = 0; i < msg_size; i++) |
| 510 | ASSERT_EQ(i < msg_size - 1 ? '.' : 0, packet.for_testing().str()[i]); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 511 | } |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 514 | TEST_F(PerfettoTest, DetachAndReattach) { |
| 515 | base::TestTaskRunner task_runner; |
| 516 | |
| 517 | TraceConfig trace_config; |
| 518 | trace_config.add_buffers()->set_size_kb(1024); |
Primiano Tucci | 5a17545 | 2018-12-22 11:48:48 +0100 | [diff] [blame] | 519 | trace_config.set_duration_ms(10000); // Max timeout, session is ended before. |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 520 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 521 | ds_config->set_name("android.perfetto.FakeProducer"); |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 522 | static constexpr size_t kNumPackets = 11; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 523 | ds_config->mutable_for_testing()->set_message_count(kNumPackets); |
| 524 | ds_config->mutable_for_testing()->set_message_size(32); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 525 | |
| 526 | // Enable tracing and detach as soon as it gets started. |
| 527 | TestHelper helper(&task_runner); |
| 528 | helper.StartServiceIfRequired(); |
Primiano Tucci | 5a17545 | 2018-12-22 11:48:48 +0100 | [diff] [blame] | 529 | auto* fake_producer = helper.ConnectFakeProducer(); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 530 | helper.ConnectConsumer(); |
| 531 | helper.WaitForConsumerConnect(); |
| 532 | helper.StartTracing(trace_config); |
| 533 | |
| 534 | // Detach. |
| 535 | helper.DetachConsumer("key"); |
| 536 | |
Primiano Tucci | 5a17545 | 2018-12-22 11:48:48 +0100 | [diff] [blame] | 537 | // Write data while detached. |
| 538 | helper.WaitForProducerEnabled(); |
| 539 | auto on_data_written = task_runner.CreateCheckpoint("data_written"); |
| 540 | fake_producer->ProduceEventBatch(helper.WrapTask(on_data_written)); |
| 541 | task_runner.RunUntilCheckpoint("data_written"); |
| 542 | |
| 543 | // Then reattach the consumer. |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 544 | helper.ConnectConsumer(); |
| 545 | helper.WaitForConsumerConnect(); |
| 546 | helper.AttachConsumer("key"); |
Primiano Tucci | 5a17545 | 2018-12-22 11:48:48 +0100 | [diff] [blame] | 547 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 548 | helper.DisableTracing(); |
| 549 | helper.WaitForTracingDisabled(); |
| 550 | |
| 551 | helper.ReadData(); |
| 552 | helper.WaitForReadData(); |
| 553 | const auto& packets = helper.trace(); |
| 554 | ASSERT_EQ(packets.size(), kNumPackets); |
| 555 | } |
| 556 | |
| 557 | // Tests that a detached trace session is automatically cleaned up if the |
| 558 | // consumer doesn't re-attach before its expiration time. |
| 559 | TEST_F(PerfettoTest, ReattachFailsAfterTimeout) { |
| 560 | base::TestTaskRunner task_runner; |
| 561 | |
| 562 | TraceConfig trace_config; |
| 563 | trace_config.add_buffers()->set_size_kb(1024); |
| 564 | trace_config.set_duration_ms(250); |
| 565 | trace_config.set_write_into_file(true); |
| 566 | trace_config.set_file_write_period_ms(100000); |
| 567 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 568 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 569 | ds_config->mutable_for_testing()->set_message_count(1); |
| 570 | ds_config->mutable_for_testing()->set_message_size(32); |
| 571 | ds_config->mutable_for_testing()->set_send_batch_on_register(true); |
| 572 | |
| 573 | // Enable tracing and detach as soon as it gets started. |
| 574 | TestHelper helper(&task_runner); |
| 575 | helper.StartServiceIfRequired(); |
| 576 | helper.ConnectFakeProducer(); |
| 577 | helper.ConnectConsumer(); |
| 578 | helper.WaitForConsumerConnect(); |
| 579 | |
| 580 | auto pipe_pair = base::Pipe::Create(); |
| 581 | helper.StartTracing(trace_config, std::move(pipe_pair.wr)); |
| 582 | |
| 583 | // Detach. |
| 584 | helper.DetachConsumer("key"); |
| 585 | |
| 586 | // Use the file EOF (write end closed) as a way to detect when the trace |
| 587 | // session is ended. |
| 588 | char buf[1024]; |
| 589 | while (PERFETTO_EINTR(read(*pipe_pair.rd, buf, sizeof(buf))) > 0) { |
| 590 | } |
| 591 | |
| 592 | // Give some margin for the tracing service to destroy the session. |
| 593 | usleep(250000); |
| 594 | |
| 595 | // Reconnect and find out that it's too late and the session is gone. |
| 596 | helper.ConnectConsumer(); |
| 597 | helper.WaitForConsumerConnect(); |
| 598 | EXPECT_FALSE(helper.AttachConsumer("key")); |
| 599 | } |
| 600 | |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 601 | TEST_F(PerfettoTest, TestProducerProvidedSMB) { |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 602 | base::TestTaskRunner task_runner; |
| 603 | |
| 604 | TestHelper helper(&task_runner); |
| 605 | helper.CreateProducerProvidedSmb(); |
| 606 | |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 607 | protos::gen::TestConfig test_config; |
| 608 | test_config.set_seed(42); |
| 609 | test_config.set_message_count(1); |
| 610 | test_config.set_message_size(1024); |
| 611 | test_config.set_send_batch_on_register(true); |
| 612 | |
| 613 | // Write a first batch before connection. |
| 614 | helper.ProduceStartupEventBatch(test_config); |
| 615 | |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 616 | helper.StartServiceIfRequired(); |
| 617 | helper.ConnectFakeProducer(); |
| 618 | helper.ConnectConsumer(); |
| 619 | helper.WaitForConsumerConnect(); |
| 620 | |
| 621 | TraceConfig trace_config; |
| 622 | trace_config.add_buffers()->set_size_kb(1024); |
| 623 | trace_config.set_duration_ms(200); |
| 624 | |
| 625 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 626 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 627 | ds_config->set_target_buffer(0); |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 628 | *ds_config->mutable_for_testing() = test_config; |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 629 | |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 630 | // The data source is configured to emit another batch when it is started via |
| 631 | // send_batch_on_register in the TestConfig. |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 632 | helper.StartTracing(trace_config); |
| 633 | helper.WaitForTracingDisabled(); |
| 634 | |
| 635 | EXPECT_TRUE(helper.IsShmemProvidedByProducer()); |
| 636 | |
| 637 | helper.ReadData(); |
| 638 | helper.WaitForReadData(); |
| 639 | |
| 640 | const auto& packets = helper.trace(); |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 641 | // We should have produced two batches, one before the producer connected and |
| 642 | // another one when the data source was started. |
| 643 | ASSERT_EQ(packets.size(), 2u); |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 644 | ASSERT_TRUE(packets[0].has_for_testing()); |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 645 | ASSERT_TRUE(packets[1].has_for_testing()); |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 648 | // Regression test for b/153142114. |
| 649 | TEST_F(PerfettoTest, QueryServiceStateLargeResponse) { |
| 650 | base::TestTaskRunner task_runner; |
| 651 | |
| 652 | TestHelper helper(&task_runner); |
| 653 | helper.StartServiceIfRequired(); |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 654 | helper.ConnectConsumer(); |
| 655 | helper.WaitForConsumerConnect(); |
| 656 | |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 657 | FakeProducer* producer = helper.ConnectFakeProducer(); |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 658 | |
| 659 | // Register 5 data sources with very large descriptors. Each descriptor will |
| 660 | // max out the IPC message size, so that the service has no other choice |
| 661 | // than chunking them. |
| 662 | std::map<std::string, std::string> ds_expected; |
| 663 | for (int i = 0; i < 5; i++) { |
| 664 | DataSourceDescriptor dsd; |
| 665 | std::string name = "big_ds_" + std::to_string(i); |
| 666 | dsd.set_name(name); |
| 667 | std::string descriptor(ipc::kIPCBufferSize - 64, (' ' + i) % 64); |
| 668 | dsd.set_track_event_descriptor_raw(descriptor); |
| 669 | ds_expected[name] = std::move(descriptor); |
| 670 | producer->RegisterDataSource(dsd); |
| 671 | } |
| 672 | |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 673 | // Linearize the producer with the service. We need to make sure that all the |
| 674 | // RegisterDataSource() calls above have been seen by the service before |
| 675 | // continuing. |
| 676 | helper.SyncAndWaitProducer(); |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 677 | |
| 678 | // Now invoke QueryServiceState() and wait for the reply. The service will |
| 679 | // send 6 (1 + 5) IPCs which will be merged together in |
| 680 | // producer_ipc_client_impl.cc. |
| 681 | auto svc_state = helper.QueryServiceStateAndWait(); |
| 682 | |
| 683 | ASSERT_GE(svc_state.producers().size(), 1u); |
| 684 | |
| 685 | std::map<std::string, std::string> ds_found; |
| 686 | for (const auto& ds : svc_state.data_sources()) { |
| 687 | if (!base::StartsWith(ds.ds_descriptor().name(), "big_ds_")) |
| 688 | continue; |
| 689 | ds_found[ds.ds_descriptor().name()] = |
| 690 | ds.ds_descriptor().track_event_descriptor_raw(); |
| 691 | } |
| 692 | EXPECT_THAT(ds_found, ElementsAreArray(ds_expected)); |
| 693 | } |
| 694 | |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 695 | // Disable cmdline tests on sanitizets because they use fork() and that messes |
| 696 | // up leak / races detections, which has been fixed only recently (see |
| 697 | // https://github.com/google/sanitizers/issues/836 ). |
| 698 | #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 699 | defined(MEMORY_SANITIZER) || defined(LEAK_SANITIZER) |
| 700 | #define NoSanitizers(X) DISABLED_##X |
| 701 | #else |
| 702 | #define NoSanitizers(X) X |
| 703 | #endif |
| 704 | |
| 705 | TEST_F(PerfettoCmdlineTest, NoSanitizers(InvalidCases)) { |
| 706 | std::string cfg("duration_ms: 100"); |
| 707 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 708 | auto invalid_arg = ExecPerfetto({"--invalid-arg"}); |
| 709 | auto empty_config = ExecPerfetto({"-c", "-", "-o", "-"}, ""); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 710 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 711 | // Cannot make assertions on --dropbox because on standalone builds it fails |
| 712 | // prematurely due to lack of dropbox. |
| 713 | auto missing_dropbox = |
| 714 | ExecPerfetto({"-c", "-", "--txt", "-o", "-", "--dropbox=foo"}, cfg); |
| 715 | auto either_out_or_dropbox = ExecPerfetto({"-c", "-", "--txt"}, cfg); |
| 716 | |
| 717 | // Disallow mixing simple and file config. |
| 718 | auto simple_and_file_1 = |
| 719 | ExecPerfetto({"-o", "-", "-c", "-", "-t", "2s"}, cfg); |
| 720 | auto simple_and_file_2 = |
| 721 | ExecPerfetto({"-o", "-", "-c", "-", "-b", "2m"}, cfg); |
| 722 | auto simple_and_file_3 = |
| 723 | ExecPerfetto({"-o", "-", "-c", "-", "-s", "2m"}, cfg); |
| 724 | |
| 725 | // Invalid --attach / --detach cases. |
| 726 | auto invalid_stop = |
| 727 | ExecPerfetto({"-c", "-", "--txt", "-o", "-", "--stop"}, cfg); |
| 728 | auto attach_and_config_1 = |
| 729 | ExecPerfetto({"-c", "-", "--txt", "-o", "-", "--attach=foo"}, cfg); |
| 730 | auto attach_and_config_2 = |
| 731 | ExecPerfetto({"-t", "2s", "-o", "-", "--attach=foo"}, cfg); |
| 732 | auto attach_needs_argument = ExecPerfetto({"--attach"}, cfg); |
| 733 | auto detach_needs_argument = |
| 734 | ExecPerfetto({"-t", "2s", "-o", "-", "--detach"}, cfg); |
| 735 | auto detach_without_out_or_dropbox = |
| 736 | ExecPerfetto({"-t", "2s", "--detach=foo"}, cfg); |
| 737 | |
| 738 | // Cannot trace and use --query. |
| 739 | auto trace_and_query_1 = ExecPerfetto({"-t", "2s", "--query"}, cfg); |
| 740 | auto trace_and_query_2 = ExecPerfetto({"-c", "-", "--query"}, cfg); |
| 741 | |
| 742 | // Ensure all Exec:: calls have been saved to prevent deadlocks. |
| 743 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 744 | |
| 745 | EXPECT_EQ(1, invalid_arg.Run(&stderr_)); |
| 746 | |
| 747 | EXPECT_EQ(1, empty_config.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 748 | EXPECT_THAT(stderr_, HasSubstr("TraceConfig is empty")); |
| 749 | |
| 750 | // Cannot make assertions on --dropbox because on standalone builds it fails |
| 751 | // prematurely due to lack of dropbox. |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 752 | EXPECT_EQ(1, missing_dropbox.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 753 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 754 | EXPECT_EQ(1, either_out_or_dropbox.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 755 | EXPECT_THAT(stderr_, HasSubstr("Either --out or --dropbox")); |
| 756 | |
| 757 | // Disallow mixing simple and file config. |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 758 | EXPECT_EQ(1, simple_and_file_1.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 759 | EXPECT_THAT(stderr_, HasSubstr("Cannot specify both -c")); |
| 760 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 761 | EXPECT_EQ(1, simple_and_file_2.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 762 | EXPECT_THAT(stderr_, HasSubstr("Cannot specify both -c")); |
| 763 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 764 | EXPECT_EQ(1, simple_and_file_3.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 765 | EXPECT_THAT(stderr_, HasSubstr("Cannot specify both -c")); |
| 766 | |
| 767 | // Invalid --attach / --detach cases. |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 768 | EXPECT_EQ(1, invalid_stop.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 769 | EXPECT_THAT(stderr_, HasSubstr("--stop is supported only in combination")); |
| 770 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 771 | EXPECT_EQ(1, attach_and_config_1.Run(&stderr_)); |
Primiano Tucci | 2854a0a | 2019-06-03 14:51:18 +0100 | [diff] [blame] | 772 | EXPECT_THAT(stderr_, HasSubstr("Cannot specify a trace config")); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 773 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 774 | EXPECT_EQ(1, attach_and_config_2.Run(&stderr_)); |
Primiano Tucci | 2854a0a | 2019-06-03 14:51:18 +0100 | [diff] [blame] | 775 | EXPECT_THAT(stderr_, HasSubstr("Cannot specify a trace config")); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 776 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 777 | EXPECT_EQ(1, attach_needs_argument.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 778 | EXPECT_THAT(stderr_, ContainsRegex("option.*--attach.*requires an argument")); |
| 779 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 780 | EXPECT_EQ(1, detach_needs_argument.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 781 | EXPECT_THAT(stderr_, ContainsRegex("option.*--detach.*requires an argument")); |
| 782 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 783 | EXPECT_EQ(1, detach_without_out_or_dropbox.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 784 | EXPECT_THAT(stderr_, HasSubstr("--out or --dropbox is required")); |
Primiano Tucci | 2854a0a | 2019-06-03 14:51:18 +0100 | [diff] [blame] | 785 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 786 | // Cannot trace and use --query. |
| 787 | EXPECT_EQ(1, trace_and_query_1.Run(&stderr_)); |
Primiano Tucci | 2854a0a | 2019-06-03 14:51:18 +0100 | [diff] [blame] | 788 | EXPECT_THAT(stderr_, HasSubstr("Cannot specify a trace config")); |
| 789 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 790 | EXPECT_EQ(1, trace_and_query_2.Run(&stderr_)); |
Primiano Tucci | 2854a0a | 2019-06-03 14:51:18 +0100 | [diff] [blame] | 791 | EXPECT_THAT(stderr_, HasSubstr("Cannot specify a trace config")); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | TEST_F(PerfettoCmdlineTest, NoSanitizers(TxtConfig)) { |
| 795 | std::string cfg("duration_ms: 100"); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 796 | auto perfetto = ExecPerfetto({"-c", "-", "--txt", "-o", "-"}, cfg); |
| 797 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 798 | EXPECT_EQ(0, perfetto.Run(&stderr_)) << stderr_; |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | TEST_F(PerfettoCmdlineTest, NoSanitizers(SimpleConfig)) { |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 802 | auto perfetto = ExecPerfetto({"-o", "-", "-c", "-", "-t", "100ms"}); |
| 803 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 804 | EXPECT_EQ(0, perfetto.Run(&stderr_)) << stderr_; |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | TEST_F(PerfettoCmdlineTest, NoSanitizers(DetachAndAttach)) { |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 808 | auto attach_to_not_existing = ExecPerfetto({"--attach=not_existent"}); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 809 | |
| 810 | std::string cfg("duration_ms: 10000; write_into_file: true"); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 811 | auto detach_valid_stop = |
| 812 | ExecPerfetto({"-o", "-", "-c", "-", "--txt", "--detach=valid_stop"}, cfg); |
| 813 | auto stop_valid_stop = ExecPerfetto({"--attach=valid_stop", "--stop"}); |
| 814 | |
| 815 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 816 | |
| 817 | EXPECT_NE(0, attach_to_not_existing.Run(&stderr_)); |
| 818 | EXPECT_THAT(stderr_, HasSubstr("Session re-attach failed")); |
| 819 | |
| 820 | EXPECT_EQ(0, detach_valid_stop.Run(&stderr_)) << stderr_; |
| 821 | EXPECT_EQ(0, stop_valid_stop.Run(&stderr_)); |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 824 | TEST_F(PerfettoCmdlineTest, NoSanitizers(StartTracingTrigger)) { |
| 825 | // See |message_count| and |message_size| in the TraceConfig above. |
| 826 | constexpr size_t kMessageCount = 11; |
| 827 | constexpr size_t kMessageSize = 32; |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 828 | protos::gen::TraceConfig trace_config; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 829 | trace_config.add_buffers()->set_size_kb(1024); |
| 830 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 831 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 832 | ds_config->mutable_for_testing()->set_message_count(kMessageCount); |
| 833 | ds_config->mutable_for_testing()->set_message_size(kMessageSize); |
| 834 | auto* trigger_cfg = trace_config.mutable_trigger_config(); |
| 835 | trigger_cfg->set_trigger_mode( |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 836 | protos::gen::TraceConfig::TriggerConfig::START_TRACING); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 837 | trigger_cfg->set_trigger_timeout_ms(15000); |
| 838 | auto* trigger = trigger_cfg->add_triggers(); |
| 839 | trigger->set_name("trigger_name"); |
| 840 | // |stop_delay_ms| must be long enough that we can write the packets in |
| 841 | // before the trace finishes. This has to be long enough for the slowest |
| 842 | // emulator. But as short as possible to prevent the test running a long |
| 843 | // time. |
| 844 | trigger->set_stop_delay_ms(500); |
| 845 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 846 | // We have to construct all the processes we want to fork before we start the |
| 847 | // service with |StartServiceIfRequired()|. this is because it is unsafe |
| 848 | // (could deadlock) to fork after we've spawned some threads which might |
| 849 | // printf (and thus hold locks). |
Stephen Nusko | 49c5a3c | 2019-04-17 13:36:09 +0100 | [diff] [blame] | 850 | const std::string path = RandomTraceFileName(); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 851 | auto perfetto_proc = ExecPerfetto( |
| 852 | { |
| 853 | "-o", |
| 854 | path, |
| 855 | "-c", |
| 856 | "-", |
| 857 | }, |
| 858 | trace_config.SerializeAsString()); |
| 859 | |
| 860 | auto trigger_proc = ExecTrigger({"trigger_name"}); |
| 861 | |
| 862 | // Start the service and connect a simple fake producer. |
| 863 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 864 | |
| 865 | auto* fake_producer = ConnectFakeProducer(); |
| 866 | EXPECT_TRUE(fake_producer); |
| 867 | |
| 868 | // Start a background thread that will deliver the config now that we've |
| 869 | // started the service. See |perfetto_proc| above for the args passed. |
| 870 | std::thread background_trace([&perfetto_proc]() { |
| 871 | std::string stderr_str; |
| 872 | EXPECT_EQ(0, perfetto_proc.Run(&stderr_str)) << stderr_str; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 873 | }); |
| 874 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 875 | WaitForProducerSetup(); |
| 876 | EXPECT_EQ(0, trigger_proc.Run(&stderr_)); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 877 | |
| 878 | // Wait for the producer to start, and then write out 11 packets. |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 879 | WaitForProducerEnabled(); |
| 880 | auto on_data_written = task_runner_.CreateCheckpoint("data_written"); |
| 881 | fake_producer->ProduceEventBatch(WrapTask(on_data_written)); |
| 882 | task_runner_.RunUntilCheckpoint("data_written"); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 883 | background_trace.join(); |
| 884 | |
| 885 | std::string trace_str; |
| 886 | base::ReadFile(path, &trace_str); |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 887 | protos::gen::Trace trace; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 888 | ASSERT_TRUE(trace.ParseFromString(trace_str)); |
Primiano Tucci | 79e4dcb | 2020-04-08 09:51:02 +0100 | [diff] [blame] | 889 | EXPECT_EQ(static_cast<int>(kBuiltinPackets + kMessageCount), |
Primiano Tucci | 919ca1e | 2019-08-21 20:26:58 +0200 | [diff] [blame] | 890 | trace.packet_size()); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 891 | for (const auto& packet : trace.packet()) { |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 892 | if (packet.has_trace_config()) { |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 893 | // Ensure the trace config properly includes the trigger mode we set. |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 894 | auto kStartTrig = protos::gen::TraceConfig::TriggerConfig::START_TRACING; |
| 895 | EXPECT_EQ(kStartTrig, |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 896 | packet.trace_config().trigger_config().trigger_mode()); |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 897 | } else if (packet.has_trigger()) { |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 898 | // validate that the triggers are properly added to the trace. |
| 899 | EXPECT_EQ("trigger_name", packet.trigger().trigger_name()); |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 900 | } else if (packet.has_for_testing()) { |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 901 | // Make sure that the data size is correctly set based on what we |
| 902 | // requested. |
| 903 | EXPECT_EQ(kMessageSize, packet.for_testing().str().size()); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | TEST_F(PerfettoCmdlineTest, NoSanitizers(StopTracingTrigger)) { |
| 909 | // See |message_count| and |message_size| in the TraceConfig above. |
| 910 | constexpr size_t kMessageCount = 11; |
| 911 | constexpr size_t kMessageSize = 32; |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 912 | protos::gen::TraceConfig trace_config; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 913 | trace_config.add_buffers()->set_size_kb(1024); |
| 914 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 915 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 916 | ds_config->mutable_for_testing()->set_message_count(kMessageCount); |
| 917 | ds_config->mutable_for_testing()->set_message_size(kMessageSize); |
| 918 | auto* trigger_cfg = trace_config.mutable_trigger_config(); |
| 919 | trigger_cfg->set_trigger_mode( |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 920 | protos::gen::TraceConfig::TriggerConfig::STOP_TRACING); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 921 | trigger_cfg->set_trigger_timeout_ms(15000); |
| 922 | auto* trigger = trigger_cfg->add_triggers(); |
| 923 | trigger->set_name("trigger_name"); |
| 924 | // |stop_delay_ms| must be long enough that we can write the packets in |
| 925 | // before the trace finishes. This has to be long enough for the slowest |
| 926 | // emulator. But as short as possible to prevent the test running a long |
| 927 | // time. |
| 928 | trigger->set_stop_delay_ms(500); |
| 929 | trigger = trigger_cfg->add_triggers(); |
| 930 | trigger->set_name("trigger_name_3"); |
| 931 | trigger->set_stop_delay_ms(60000); |
| 932 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 933 | // We have to construct all the processes we want to fork before we start the |
| 934 | // service with |StartServiceIfRequired()|. this is because it is unsafe |
| 935 | // (could deadlock) to fork after we've spawned some threads which might |
| 936 | // printf (and thus hold locks). |
| 937 | const std::string path = RandomTraceFileName(); |
| 938 | auto perfetto_proc = ExecPerfetto( |
| 939 | { |
| 940 | "-o", |
| 941 | path, |
| 942 | "-c", |
| 943 | "-", |
| 944 | }, |
| 945 | trace_config.SerializeAsString()); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 946 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 947 | auto trigger_proc = |
| 948 | ExecTrigger({"trigger_name_2", "trigger_name", "trigger_name_3"}); |
| 949 | |
| 950 | // Start the service and connect a simple fake producer. |
| 951 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 952 | auto* fake_producer = ConnectFakeProducer(); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 953 | EXPECT_TRUE(fake_producer); |
| 954 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 955 | // Start a background thread that will deliver the config now that we've |
| 956 | // started the service. See |perfetto_proc| above for the args passed. |
| 957 | std::thread background_trace([&perfetto_proc]() { |
| 958 | std::string stderr_str; |
| 959 | EXPECT_EQ(0, perfetto_proc.Run(&stderr_str)) << stderr_str; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 960 | }); |
| 961 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 962 | WaitForProducerEnabled(); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 963 | // Wait for the producer to start, and then write out 11 packets, before the |
| 964 | // trace actually starts (the trigger is seen). |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 965 | auto on_data_written = task_runner_.CreateCheckpoint("data_written_1"); |
| 966 | fake_producer->ProduceEventBatch(WrapTask(on_data_written)); |
| 967 | task_runner_.RunUntilCheckpoint("data_written_1"); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 968 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 969 | EXPECT_EQ(0, trigger_proc.Run(&stderr_)) << "stderr: " << stderr_; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 970 | |
| 971 | background_trace.join(); |
| 972 | |
| 973 | std::string trace_str; |
| 974 | base::ReadFile(path, &trace_str); |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 975 | protos::gen::Trace trace; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 976 | ASSERT_TRUE(trace.ParseFromString(trace_str)); |
Primiano Tucci | 79e4dcb | 2020-04-08 09:51:02 +0100 | [diff] [blame] | 977 | EXPECT_EQ(static_cast<int>(kBuiltinPackets + 1 + kMessageCount), |
Primiano Tucci | 919ca1e | 2019-08-21 20:26:58 +0200 | [diff] [blame] | 978 | trace.packet_size()); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 979 | bool seen_first_trigger = false; |
| 980 | for (const auto& packet : trace.packet()) { |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 981 | if (packet.has_trace_config()) { |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 982 | // Ensure the trace config properly includes the trigger mode we set. |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 983 | auto kStopTrig = protos::gen::TraceConfig::TriggerConfig::STOP_TRACING; |
| 984 | EXPECT_EQ(kStopTrig, |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 985 | packet.trace_config().trigger_config().trigger_mode()); |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 986 | } else if (packet.has_trigger()) { |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 987 | // validate that the triggers are properly added to the trace. |
| 988 | if (!seen_first_trigger) { |
| 989 | EXPECT_EQ("trigger_name", packet.trigger().trigger_name()); |
| 990 | seen_first_trigger = true; |
| 991 | } else { |
| 992 | EXPECT_EQ("trigger_name_3", packet.trigger().trigger_name()); |
| 993 | } |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 994 | } else if (packet.has_for_testing()) { |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 995 | // Make sure that the data size is correctly set based on what we |
| 996 | // requested. |
| 997 | EXPECT_EQ(kMessageSize, packet.for_testing().str().size()); |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | // Dropbox on the commandline client only works on android builds. So disable |
| 1003 | // this test on all other builds. |
| 1004 | #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) |
| 1005 | TEST_F(PerfettoCmdlineTest, NoSanitizers(NoDataNoFileWithoutTrigger)) { |
| 1006 | #else |
| 1007 | TEST_F(PerfettoCmdlineTest, DISABLED_NoDataNoFileWithoutTrigger) { |
| 1008 | #endif |
| 1009 | // See |message_count| and |message_size| in the TraceConfig above. |
| 1010 | constexpr size_t kMessageCount = 11; |
| 1011 | constexpr size_t kMessageSize = 32; |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1012 | protos::gen::TraceConfig trace_config; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 1013 | trace_config.add_buffers()->set_size_kb(1024); |
Hector Dearman | b5ad80b | 2019-05-02 12:53:30 +0100 | [diff] [blame] | 1014 | trace_config.set_allow_user_build_tracing(true); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 1015 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 1016 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 1017 | ds_config->mutable_for_testing()->set_message_count(kMessageCount); |
| 1018 | ds_config->mutable_for_testing()->set_message_size(kMessageSize); |
| 1019 | auto* trigger_cfg = trace_config.mutable_trigger_config(); |
| 1020 | trigger_cfg->set_trigger_mode( |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1021 | protos::gen::TraceConfig::TriggerConfig::STOP_TRACING); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 1022 | trigger_cfg->set_trigger_timeout_ms(1000); |
| 1023 | auto* trigger = trigger_cfg->add_triggers(); |
| 1024 | trigger->set_name("trigger_name"); |
| 1025 | // |stop_delay_ms| must be long enough that we can write the packets in |
| 1026 | // before the trace finishes. This has to be long enough for the slowest |
| 1027 | // emulator. But as short as possible to prevent the test running a long |
| 1028 | // time. |
| 1029 | trigger->set_stop_delay_ms(500); |
| 1030 | trigger = trigger_cfg->add_triggers(); |
| 1031 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1032 | // We have to construct all the processes we want to fork before we start the |
| 1033 | // service with |StartServiceIfRequired()|. this is because it is unsafe |
| 1034 | // (could deadlock) to fork after we've spawned some threads which might |
| 1035 | // printf (and thus hold locks). |
| 1036 | const std::string path = RandomTraceFileName(); |
| 1037 | auto perfetto_proc = ExecPerfetto( |
| 1038 | { |
| 1039 | "--dropbox", |
| 1040 | "TAG", |
| 1041 | "--no-guardrails", |
| 1042 | "-c", |
| 1043 | "-", |
| 1044 | }, |
| 1045 | trace_config.SerializeAsString()); |
| 1046 | |
| 1047 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 1048 | auto* fake_producer = ConnectFakeProducer(); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 1049 | EXPECT_TRUE(fake_producer); |
| 1050 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1051 | std::string stderr_str; |
| 1052 | std::thread background_trace([&perfetto_proc, &stderr_str]() { |
| 1053 | EXPECT_EQ(0, perfetto_proc.Run(&stderr_str)); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 1054 | }); |
| 1055 | background_trace.join(); |
| 1056 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1057 | EXPECT_THAT(stderr_str, |
Ryan Savitski | 53ca60b | 2019-06-03 13:04:40 +0100 | [diff] [blame] | 1058 | ::testing::HasSubstr("Skipping write to dropbox. Empty trace.")); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1061 | TEST_F(PerfettoCmdlineTest, NoSanitizers(StopTracingTriggerFromConfig)) { |
| 1062 | // See |message_count| and |message_size| in the TraceConfig above. |
| 1063 | constexpr size_t kMessageCount = 11; |
| 1064 | constexpr size_t kMessageSize = 32; |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1065 | protos::gen::TraceConfig trace_config; |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1066 | trace_config.add_buffers()->set_size_kb(1024); |
| 1067 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 1068 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 1069 | ds_config->mutable_for_testing()->set_message_count(kMessageCount); |
| 1070 | ds_config->mutable_for_testing()->set_message_size(kMessageSize); |
| 1071 | auto* trigger_cfg = trace_config.mutable_trigger_config(); |
| 1072 | trigger_cfg->set_trigger_mode( |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1073 | protos::gen::TraceConfig::TriggerConfig::STOP_TRACING); |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1074 | trigger_cfg->set_trigger_timeout_ms(15000); |
| 1075 | auto* trigger = trigger_cfg->add_triggers(); |
| 1076 | trigger->set_name("trigger_name"); |
| 1077 | // |stop_delay_ms| must be long enough that we can write the packets in |
| 1078 | // before the trace finishes. This has to be long enough for the slowest |
| 1079 | // emulator. But as short as possible to prevent the test running a long |
| 1080 | // time. |
| 1081 | trigger->set_stop_delay_ms(500); |
| 1082 | trigger = trigger_cfg->add_triggers(); |
| 1083 | trigger->set_name("trigger_name_3"); |
| 1084 | trigger->set_stop_delay_ms(60000); |
| 1085 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1086 | // We have to construct all the processes we want to fork before we start the |
| 1087 | // service with |StartServiceIfRequired()|. this is because it is unsafe |
| 1088 | // (could deadlock) to fork after we've spawned some threads which might |
| 1089 | // printf (and thus hold locks). |
Stephen Nusko | 49c5a3c | 2019-04-17 13:36:09 +0100 | [diff] [blame] | 1090 | const std::string path = RandomTraceFileName(); |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1091 | auto perfetto_proc = ExecPerfetto( |
| 1092 | { |
| 1093 | "-o", |
| 1094 | path, |
| 1095 | "-c", |
| 1096 | "-", |
| 1097 | }, |
| 1098 | trace_config.SerializeAsString()); |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1099 | |
| 1100 | std::string triggers = R"( |
| 1101 | activate_triggers: "trigger_name_2" |
| 1102 | activate_triggers: "trigger_name" |
| 1103 | activate_triggers: "trigger_name_3" |
| 1104 | )"; |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1105 | auto perfetto_proc_2 = ExecPerfetto( |
| 1106 | { |
| 1107 | "-o", |
| 1108 | path, |
| 1109 | "-c", |
| 1110 | "-", |
| 1111 | "--txt", |
| 1112 | }, |
| 1113 | triggers); |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1114 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1115 | // Start the service and connect a simple fake producer. |
| 1116 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 1117 | auto* fake_producer = ConnectFakeProducer(); |
| 1118 | EXPECT_TRUE(fake_producer); |
| 1119 | |
| 1120 | std::thread background_trace([&perfetto_proc]() { |
| 1121 | std::string stderr_str; |
| 1122 | EXPECT_EQ(0, perfetto_proc.Run(&stderr_str)) << stderr_str; |
| 1123 | }); |
| 1124 | |
| 1125 | WaitForProducerEnabled(); |
| 1126 | // Wait for the producer to start, and then write out 11 packets, before the |
| 1127 | // trace actually starts (the trigger is seen). |
| 1128 | auto on_data_written = task_runner_.CreateCheckpoint("data_written_1"); |
| 1129 | fake_producer->ProduceEventBatch(WrapTask(on_data_written)); |
| 1130 | task_runner_.RunUntilCheckpoint("data_written_1"); |
| 1131 | |
| 1132 | EXPECT_EQ(0, perfetto_proc_2.Run(&stderr_)) << "stderr: " << stderr_; |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1133 | |
| 1134 | background_trace.join(); |
| 1135 | |
| 1136 | std::string trace_str; |
Stephen Nusko | 49c5a3c | 2019-04-17 13:36:09 +0100 | [diff] [blame] | 1137 | base::ReadFile(path, &trace_str); |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1138 | protos::gen::Trace trace; |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1139 | ASSERT_TRUE(trace.ParseFromString(trace_str)); |
Primiano Tucci | 919ca1e | 2019-08-21 20:26:58 +0200 | [diff] [blame] | 1140 | EXPECT_LT(static_cast<int>(kMessageCount), trace.packet_size()); |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1141 | bool seen_first_trigger = false; |
| 1142 | for (const auto& packet : trace.packet()) { |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1143 | if (packet.has_trace_config()) { |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1144 | // Ensure the trace config properly includes the trigger mode we set. |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1145 | auto kStopTrig = protos::gen::TraceConfig::TriggerConfig::STOP_TRACING; |
| 1146 | EXPECT_EQ(kStopTrig, |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1147 | packet.trace_config().trigger_config().trigger_mode()); |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1148 | } else if (packet.has_trigger()) { |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1149 | // validate that the triggers are properly added to the trace. |
| 1150 | if (!seen_first_trigger) { |
| 1151 | EXPECT_EQ("trigger_name", packet.trigger().trigger_name()); |
| 1152 | seen_first_trigger = true; |
| 1153 | } else { |
| 1154 | EXPECT_EQ("trigger_name_3", packet.trigger().trigger_name()); |
| 1155 | } |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1156 | } else if (packet.has_for_testing()) { |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1157 | // Make sure that the data size is correctly set based on what we |
| 1158 | // requested. |
| 1159 | EXPECT_EQ(kMessageSize, packet.for_testing().str().size()); |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | TEST_F(PerfettoCmdlineTest, NoSanitizers(TriggerFromConfigStopsFileOpening)) { |
| 1165 | // See |message_count| and |message_size| in the TraceConfig above. |
| 1166 | constexpr size_t kMessageCount = 11; |
| 1167 | constexpr size_t kMessageSize = 32; |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1168 | protos::gen::TraceConfig trace_config; |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1169 | trace_config.add_buffers()->set_size_kb(1024); |
| 1170 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 1171 | ds_config->set_name("android.perfetto.FakeProducer"); |
| 1172 | ds_config->mutable_for_testing()->set_message_count(kMessageCount); |
| 1173 | ds_config->mutable_for_testing()->set_message_size(kMessageSize); |
| 1174 | auto* trigger_cfg = trace_config.mutable_trigger_config(); |
| 1175 | trigger_cfg->set_trigger_mode( |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 1176 | protos::gen::TraceConfig::TriggerConfig::STOP_TRACING); |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1177 | trigger_cfg->set_trigger_timeout_ms(15000); |
| 1178 | auto* trigger = trigger_cfg->add_triggers(); |
| 1179 | trigger->set_name("trigger_name"); |
| 1180 | // |stop_delay_ms| must be long enough that we can write the packets in |
| 1181 | // before the trace finishes. This has to be long enough for the slowest |
| 1182 | // emulator. But as short as possible to prevent the test running a long |
| 1183 | // time. |
| 1184 | trigger->set_stop_delay_ms(500); |
| 1185 | trigger = trigger_cfg->add_triggers(); |
| 1186 | trigger->set_name("trigger_name_3"); |
| 1187 | trigger->set_stop_delay_ms(60000); |
| 1188 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1189 | // We have to construct all the processes we want to fork before we start the |
| 1190 | // service with |StartServiceIfRequired()|. this is because it is unsafe |
| 1191 | // (could deadlock) to fork after we've spawned some threads which might |
| 1192 | // printf (and thus hold locks). |
Stephen Nusko | 49c5a3c | 2019-04-17 13:36:09 +0100 | [diff] [blame] | 1193 | const std::string path = RandomTraceFileName(); |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1194 | std::string triggers = R"( |
| 1195 | activate_triggers: "trigger_name_2" |
| 1196 | activate_triggers: "trigger_name" |
| 1197 | activate_triggers: "trigger_name_3" |
| 1198 | )"; |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1199 | auto perfetto_proc = ExecPerfetto( |
| 1200 | { |
| 1201 | "-o", |
| 1202 | path, |
| 1203 | "-c", |
| 1204 | "-", |
| 1205 | "--txt", |
| 1206 | }, |
| 1207 | triggers); |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1208 | |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1209 | // Start the service and connect a simple fake producer. |
| 1210 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 1211 | auto* fake_producer = ConnectFakeProducer(); |
| 1212 | EXPECT_TRUE(fake_producer); |
| 1213 | |
| 1214 | std::string trace_str; |
| 1215 | EXPECT_FALSE(base::ReadFile(path, &trace_str)); |
| 1216 | |
| 1217 | EXPECT_EQ(0, perfetto_proc.Run(&stderr_)) << "stderr: " << stderr_; |
Stephen Nusko | 9fa59cb | 2019-04-15 04:19:16 +0100 | [diff] [blame] | 1218 | |
| 1219 | EXPECT_FALSE(base::ReadFile(path, &trace_str)); |
| 1220 | } |
| 1221 | |
Primiano Tucci | 2854a0a | 2019-06-03 14:51:18 +0100 | [diff] [blame] | 1222 | TEST_F(PerfettoCmdlineTest, NoSanitizers(Query)) { |
Stephen Nusko | 87a12a8 | 2019-07-24 17:56:46 +0100 | [diff] [blame] | 1223 | auto query = ExecPerfetto({"--query"}); |
| 1224 | auto query_raw = ExecPerfetto({"--query-raw"}); |
| 1225 | StartServiceIfRequiredNoNewExecsAfterThis(); |
| 1226 | EXPECT_EQ(0, query.Run(&stderr_)) << stderr_; |
| 1227 | EXPECT_EQ(0, query_raw.Run(&stderr_)) << stderr_; |
Primiano Tucci | 2854a0a | 2019-06-03 14:51:18 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 1230 | } // namespace perfetto |