Coverage Report

Created: 2025-12-31 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/perfetto/test/test_helper.h
Line
Count
Source
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
#ifndef TEST_TEST_HELPER_H_
18
#define TEST_TEST_HELPER_H_
19
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <optional>
23
24
#include "perfetto/base/build_config.h"
25
#include "perfetto/ext/base/file_utils.h"
26
#include "perfetto/ext/base/scoped_file.h"
27
#include "perfetto/ext/base/subprocess.h"
28
#include "perfetto/ext/base/thread_task_runner.h"
29
#include "perfetto/ext/base/utils.h"
30
#include "perfetto/ext/tracing/core/consumer.h"
31
#include "perfetto/ext/tracing/core/shared_memory_arbiter.h"
32
#include "perfetto/ext/tracing/core/trace_packet.h"
33
#include "perfetto/ext/tracing/core/tracing_service.h"
34
#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
35
#include "perfetto/ext/tracing/ipc/service_ipc_host.h"
36
#include "perfetto/tracing/core/trace_config.h"
37
#include "perfetto/tracing/default_socket.h"
38
#include "src/base/test/test_task_runner.h"
39
#include "test/fake_producer.h"
40
41
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
42
#include "src/tracing/ipc/shared_memory_windows.h"
43
#else
44
#include <signal.h>
45
46
#include "src/traced/probes/probes_producer.h"
47
#include "src/tracing/ipc/posix_shared_memory.h"
48
#endif
49
50
#include "protos/perfetto/trace/trace_packet.gen.h"
51
52
namespace perfetto {
53
54
// This value has been bumped to 10s in Oct 2020 because the GCE-based emulator
55
// can be sensibly slower than real hw (more than 10x) and caused flakes.
56
// See bugs duped against b/171771440.
57
constexpr uint32_t kDefaultTestTimeoutMs = 30000;
58
59
0
inline const char* GetTestProducerSockName() {
60
0
// If we're building on Android and starting the daemons ourselves,
61
0
// create the sockets in a world-writable location.
62
0
#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
63
0
    PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
64
0
  return "/data/local/tmp/traced_producer";
65
0
#else
66
0
  return ::perfetto::GetProducerSocket();
67
0
#endif
68
0
}
69
70
// Captures the values of some environment variables when constructed and
71
// restores them when destroyed.
72
class TestEnvCleaner {
73
 public:
74
311
  TestEnvCleaner() {}
75
311
  TestEnvCleaner(std::initializer_list<const char*> env_vars) {
76
311
    prev_state_.reserve(env_vars.size());
77
622
    for (const char* name : env_vars) {
78
622
      prev_state_.emplace_back();
79
622
      Var& var = prev_state_.back();
80
622
      var.name = name;
81
622
      const char* prev_value = getenv(name);
82
622
      if (prev_value) {
83
0
        var.value.emplace(prev_value);
84
0
      }
85
622
    }
86
311
  }
87
622
  ~TestEnvCleaner() { Clean(); }
88
89
  TestEnvCleaner(const TestEnvCleaner&) = delete;
90
0
  TestEnvCleaner(TestEnvCleaner&& obj) noexcept { *this = std::move(obj); }
91
  TestEnvCleaner& operator=(const TestEnvCleaner&) = delete;
92
311
  TestEnvCleaner& operator=(TestEnvCleaner&& obj) noexcept {
93
311
    PERFETTO_CHECK(prev_state_.empty());
94
311
    this->prev_state_ = std::move(obj.prev_state_);
95
311
    obj.prev_state_.clear();
96
311
    return *this;
97
311
  }
98
99
622
  void Clean() {
100
622
    for (const Var& var : prev_state_) {
101
622
      if (var.value) {
102
0
        base::SetEnv(var.name, *var.value);
103
622
      } else {
104
622
        base::UnsetEnv(var.name);
105
622
      }
106
622
    }
107
622
    prev_state_.clear();
108
622
  }
109
110
 private:
111
  struct Var {
112
    const char* name;
113
    std::optional<std::string> value;
114
  };
115
  std::vector<Var> prev_state_;
116
};
117
118
// This is used only in daemon starting integrations tests.
119
class ServiceThread {
120
 public:
121
  ServiceThread(const std::string& producer_socket,
122
                const std::string& consumer_socket,
123
                bool enable_relay_endpoint = false)
124
311
      : producer_socket_(producer_socket),
125
311
        consumer_socket_(consumer_socket),
126
311
        enable_relay_endpoint_(enable_relay_endpoint) {}
127
128
311
  ~ServiceThread() { Stop(); }
129
130
311
  TestEnvCleaner Start() {
131
311
    TestEnvCleaner env_cleaner(
132
311
        {"PERFETTO_PRODUCER_SOCK_NAME", "PERFETTO_CONSUMER_SOCK_NAME"});
133
311
    runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.svc");
134
311
    runner_->PostTaskAndWaitForTesting([this]() {
135
311
      TracingService::InitOpts init_opts = {};
136
311
      if (enable_relay_endpoint_)
137
0
        init_opts.enable_relay_endpoint = true;
138
311
      svc_ = ServiceIPCHost::CreateInstance(runner_->get(), init_opts);
139
311
      auto producer_sockets = TokenizeProducerSockets(producer_socket_.c_str());
140
311
      for (const auto& producer_socket : producer_sockets) {
141
        // In some cases the socket is a TCP or abstract unix.
142
311
        if (!base::FileExists(producer_socket))
143
0
          continue;
144
311
        if (remove(producer_socket.c_str()) == -1) {
145
0
          if (errno != ENOENT)
146
0
            PERFETTO_FATAL("Failed to remove %s", producer_socket_.c_str());
147
0
        }
148
311
      }
149
311
      if (remove(consumer_socket_.c_str()) == -1) {
150
0
        if (errno != ENOENT)
151
0
          PERFETTO_FATAL("Failed to remove %s", consumer_socket_.c_str());
152
0
      }
153
311
      base::SetEnv("PERFETTO_PRODUCER_SOCK_NAME", producer_socket_);
154
311
      base::SetEnv("PERFETTO_CONSUMER_SOCK_NAME", consumer_socket_);
155
311
      bool res =
156
311
          svc_->Start(producer_socket_.c_str(), consumer_socket_.c_str());
157
311
      if (!res) {
158
0
        PERFETTO_FATAL("Failed to start service listening on %s and %s",
159
0
                       producer_socket_.c_str(), consumer_socket_.c_str());
160
0
      }
161
311
    });
162
311
    return env_cleaner;
163
311
  }
164
165
311
  void Stop() {
166
311
    if (!runner_)
167
0
      return;
168
311
    runner_->PostTaskAndWaitForTesting([this]() { svc_.reset(); });
169
311
    runner_.reset();
170
311
  }
171
172
0
  base::ThreadTaskRunner* runner() { return runner_ ? &*runner_ : nullptr; }
173
174
 private:
175
  std::optional<base::ThreadTaskRunner> runner_;  // Keep first.
176
177
  std::string producer_socket_;
178
  std::string consumer_socket_;
179
  bool enable_relay_endpoint_ = false;
180
  std::unique_ptr<ServiceIPCHost> svc_;
181
};
182
183
// This is used only in daemon starting integrations tests.
184
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
185
// On Windows we don't have any traced_probes, make this a no-op to avoid
186
// propagating #ifdefs to the outer test.
187
class ProbesProducerThread {
188
 public:
189
  ProbesProducerThread(const std::string& /*producer_socket*/) {}
190
  void Connect() {}
191
};
192
#else
193
class ProbesProducerThread {
194
 public:
195
  ProbesProducerThread(const std::string& producer_socket)
196
0
      : producer_socket_(producer_socket) {}
197
198
0
  ~ProbesProducerThread() {
199
0
    if (!runner_)
200
0
      return;
201
0
    runner_->PostTaskAndWaitForTesting([this]() { producer_.reset(); });
202
0
  }
203
204
0
  void Connect() {
205
0
    runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.prd.probes");
206
0
    runner_->PostTaskAndWaitForTesting([this]() {
207
0
      producer_.reset(new ProbesProducer());
208
0
      producer_->ConnectWithRetries(producer_socket_.c_str(), runner_->get());
209
0
    });
210
0
  }
211
212
 private:
213
  std::optional<base::ThreadTaskRunner> runner_;  // Keep first.
214
215
  std::string producer_socket_;
216
  std::unique_ptr<ProbesProducer> producer_;
217
};
218
#endif  // !OS_WIN
219
220
class FakeProducerThread {
221
 public:
222
  FakeProducerThread(const std::string& producer_socket,
223
                     std::function<void()> connect_callback,
224
                     std::function<void()> setup_callback,
225
                     std::function<void()> start_callback,
226
                     const std::string& producer_name)
227
311
      : producer_socket_(producer_socket),
228
311
        connect_callback_(std::move(connect_callback)),
229
311
        setup_callback_(std::move(setup_callback)),
230
311
        start_callback_(std::move(start_callback)) {
231
311
    runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.prd.fake");
232
311
    runner_->PostTaskAndWaitForTesting([this, producer_name]() {
233
311
      producer_.reset(new FakeProducer(producer_name, runner_->get()));
234
311
    });
235
311
  }
236
237
311
  ~FakeProducerThread() {
238
311
    runner_->PostTaskAndWaitForTesting([this]() { producer_.reset(); });
239
311
  }
240
241
0
  void Connect() {
242
0
    runner_->PostTaskAndWaitForTesting([this]() {
243
0
      producer_->Connect(producer_socket_.c_str(), std::move(connect_callback_),
244
0
                         std::move(setup_callback_), std::move(start_callback_),
245
0
                         std::move(shm_), std::move(shm_arbiter_));
246
0
    });
247
0
  }
248
249
0
  base::ThreadTaskRunner* runner() { return runner_ ? &*runner_ : nullptr; }
250
251
0
  FakeProducer* producer() { return producer_.get(); }
252
253
0
  void CreateProducerProvidedSmb() {
254
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
255
    SharedMemoryWindows::Factory factory;
256
#else
257
0
    PosixSharedMemory::Factory factory;
258
0
#endif
259
0
    shm_ = factory.CreateSharedMemory(1024 * 1024);
260
0
    shm_arbiter_ = SharedMemoryArbiter::CreateUnboundInstance(
261
0
        shm_.get(), 4096, SharedMemoryABI::ShmemMode::kDefault);
262
0
  }
263
264
  void ProduceStartupEventBatch(const protos::gen::TestConfig& config,
265
0
                                std::function<void()> callback) {
266
0
    PERFETTO_CHECK(shm_arbiter_);
267
0
    producer_->ProduceStartupEventBatch(config, shm_arbiter_.get(), callback);
268
0
  }
269
270
 private:
271
  std::optional<base::ThreadTaskRunner> runner_;  // Keep first.
272
273
  std::string producer_socket_;
274
  std::unique_ptr<FakeProducer> producer_;
275
  std::function<void()> connect_callback_;
276
  std::function<void()> setup_callback_;
277
  std::function<void()> start_callback_;
278
  std::unique_ptr<SharedMemory> shm_;
279
  std::unique_ptr<SharedMemoryArbiter> shm_arbiter_;
280
};
281
282
class TestHelper : public Consumer {
283
 public:
284
  enum class Mode {
285
    kStartDaemons,
286
    kUseSystemService,
287
  };
288
  static Mode kDefaultMode;
289
290
  static const char* GetDefaultModeConsumerSocketName();
291
  static const char* GetDefaultModeProducerSocketName();
292
293
  explicit TestHelper(base::TestTaskRunner* task_runner)
294
311
      : TestHelper(task_runner, kDefaultMode) {}
295
296
  explicit TestHelper(base::TestTaskRunner* task_runner, Mode mode);
297
298
  explicit TestHelper(base::TestTaskRunner* task_runner,
299
                      Mode mode,
300
                      const char* producer_socket,
301
                      bool enable_relay_endpoint = false);
302
303
  // Consumer implementation.
304
  void OnConnect() override;
305
  void OnDisconnect() override;
306
  void OnTracingDisabled(const std::string& error) override;
307
  virtual void ReadTraceData(std::vector<TracePacket> packets);
308
  void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
309
  void OnDetach(bool) override;
310
  void OnAttach(bool, const TraceConfig&) override;
311
  void OnTraceStats(bool, const TraceStats&) override;
312
  void OnObservableEvents(const ObservableEvents&) override;
313
  void OnSessionCloned(const OnSessionClonedArgs&) override;
314
315
  // Starts the tracing service if in kStartDaemons mode.
316
  void StartServiceIfRequired();
317
318
  // Restarts the tracing service. Only valid in kStartDaemons mode.
319
  void RestartService();
320
321
  // Connects the producer and waits that the service has seen the
322
  // RegisterDataSource() call.
323
  FakeProducer* ConnectFakeProducer(size_t idx = 0);
324
325
  void ConnectConsumer();
326
  void StartTracing(const TraceConfig& config,
327
                    base::ScopedFile = base::ScopedFile());
328
  void DisableTracing();
329
  void FlushAndWait(uint32_t timeout_ms, FlushFlags = FlushFlags());
330
  void ReadData(uint32_t read_count = 0);
331
  void FreeBuffers();
332
  void DetachConsumer(const std::string& key);
333
  bool AttachConsumer(const std::string& key);
334
  void CreateProducerProvidedSmb();
335
  bool IsShmemProvidedByProducer(size_t idx = 0);
336
  void ProduceStartupEventBatch(const protos::gen::TestConfig& config);
337
338
  void WaitFor(std::function<bool()> predicate,
339
               const std::string& error_msg,
340
               uint32_t timeout_ms = kDefaultTestTimeoutMs);
341
  void WaitForConsumerConnect();
342
  void WaitForProducerSetup(size_t idx = 0);
343
  void WaitForProducerEnabled(size_t idx = 0);
344
  void WaitForDataSourceConnected(const std::string& ds_name);
345
  void WaitForTracingDisabled(uint32_t timeout_ms = kDefaultTestTimeoutMs);
346
  void WaitForReadData(uint32_t read_count = 0,
347
                       uint32_t timeout_ms = kDefaultTestTimeoutMs);
348
  void WaitForAllDataSourceStarted(uint32_t timeout_ms = kDefaultTestTimeoutMs);
349
  void SyncAndWaitProducer(size_t idx = 0);
350
  TracingServiceState QueryServiceStateAndWait();
351
352
2.73k
  std::string AddID(const std::string& checkpoint) {
353
2.73k
    return checkpoint + "." + std::to_string(instance_num_);
354
2.73k
  }
355
356
2.13k
  std::function<void()> CreateCheckpoint(const std::string& checkpoint) {
357
2.13k
    return task_runner_->CreateCheckpoint(AddID(checkpoint));
358
2.13k
  }
359
360
  void RunUntilCheckpoint(const std::string& checkpoint,
361
600
                          uint32_t timeout_ms = kDefaultTestTimeoutMs) {
362
600
    return task_runner_->RunUntilCheckpoint(AddID(checkpoint), timeout_ms);
363
600
  }
364
365
  std::function<void()> WrapTask(const std::function<void()>& function);
366
367
0
  base::ThreadTaskRunner* service_thread() { return service_thread_.runner(); }
368
0
  base::ThreadTaskRunner* producer_thread(size_t i = 0) {
369
0
    PERFETTO_DCHECK(i < fake_producer_threads_.size());
370
0
    return fake_producer_threads_[i]->runner();
371
0
  }
372
373
0
  size_t num_producers() { return fake_producer_threads_.size(); }
374
0
  const std::vector<protos::gen::TracePacket>& full_trace() {
375
0
    return full_trace_;
376
0
  }
377
0
  const std::vector<protos::gen::TracePacket>& trace() { return trace_; }
378
379
  // Some fixtures want to reuse a global TestHelper in different testcases
380
  // without destroying and recreating it, but they still need to avoid
381
  // polluting environment variables.
382
  //
383
  // This restores the previous environment variables.
384
0
  void CleanEnv() { env_cleaner_.Clean(); }
385
386
 private:
387
  static uint64_t next_instance_num_;
388
  uint64_t instance_num_;
389
  base::TestTaskRunner* task_runner_ = nullptr;
390
  int cur_consumer_num_ = 0;
391
  uint64_t trace_count_ = 0;
392
393
  std::function<void()> on_all_ds_started_callback_;
394
  std::function<void()> on_connect_callback_;
395
  std::function<void()> on_packets_finished_callback_;
396
  std::function<void()> on_stop_tracing_callback_;
397
  std::function<void()> on_detach_callback_;
398
  std::function<void(bool)> on_attach_callback_;
399
400
  std::vector<protos::gen::TracePacket> full_trace_;
401
  std::vector<protos::gen::TracePacket> trace_;
402
403
  Mode mode_;
404
  const char* producer_socket_;
405
  const char* consumer_socket_;
406
  ServiceThread service_thread_;
407
  std::vector<std::unique_ptr<FakeProducerThread>> fake_producer_threads_;
408
409
  TestEnvCleaner env_cleaner_;
410
411
  std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_;  // Keep last.
412
};
413
414
#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
415
416
// This class is a reference to a child process that has in essence been execv
417
// to the requested binary. The process will start and then wait for Run()
418
// before proceeding. We use this to fork new processes before starting any
419
// additional threads in the parent process (otherwise you would risk
420
// deadlocks), but pause the forked processes until remaining setup (including
421
// any necessary threads) in the parent process is complete.
422
class Exec {
423
 public:
424
  // Starts the forked process that was created. If not null then |stderr_out|
425
  // will contain the stderr of the process.
426
0
  int Run(std::string* stderr_out = nullptr) {
427
0
    // We can't be the child process.
428
0
    PERFETTO_CHECK(getpid() != subprocess_.pid());
429
0
    // Will cause the entrypoint to continue.
430
0
    PERFETTO_CHECK(write(*sync_pipe_.wr, "1", 1) == 1);
431
0
    sync_pipe_.wr.reset();
432
0
    subprocess_.Wait();
433
0
434
0
    if (stderr_out) {
435
0
      *stderr_out = std::move(subprocess_.output());
436
0
    } else {
437
0
      PERFETTO_LOG("Child proc %d exited with stderr: \"%s\"",
438
0
                   subprocess_.pid(), subprocess_.output().c_str());
439
0
    }
440
0
    return subprocess_.returncode();
441
0
  }
442
443
  Exec(const std::string& argv0,
444
       std::initializer_list<std::string> args,
445
0
       std::string input = "") {
446
0
    subprocess_.args.stderr_mode = base::Subprocess::OutputMode::kBuffer;
447
0
    subprocess_.args.stdout_mode = base::Subprocess::OutputMode::kDevNull;
448
0
    subprocess_.args.input = input;
449
0
450
0
#if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
451
0
    constexpr bool kUseSystemBinaries = false;
452
0
#else
453
0
    constexpr bool kUseSystemBinaries = true;
454
0
#endif
455
0
456
0
    auto pass_env = [](const std::string& var, base::Subprocess* proc) {
457
0
      const char* val = getenv(var.c_str());
458
0
      if (val)
459
0
        proc->args.env.push_back(var + "=" + val);
460
0
    };
461
0
462
0
    std::vector<std::string>& cmd = subprocess_.args.exec_cmd;
463
0
    if (kUseSystemBinaries) {
464
0
      PERFETTO_CHECK(TestHelper::kDefaultMode ==
465
0
                     TestHelper::Mode::kUseSystemService);
466
0
      cmd.push_back("/system/bin/" + argv0);
467
0
      cmd.insert(cmd.end(), args.begin(), args.end());
468
0
    } else {
469
0
      PERFETTO_CHECK(TestHelper::kDefaultMode ==
470
0
                     TestHelper::Mode::kStartDaemons);
471
0
      subprocess_.args.env.push_back(
472
0
          std::string("PERFETTO_PRODUCER_SOCK_NAME=") +
473
0
          TestHelper::GetDefaultModeProducerSocketName());
474
0
      subprocess_.args.env.push_back(
475
0
          std::string("PERFETTO_CONSUMER_SOCK_NAME=") +
476
0
          TestHelper::GetDefaultModeConsumerSocketName());
477
0
      pass_env("TMPDIR", &subprocess_);
478
0
      pass_env("TMP", &subprocess_);
479
0
      pass_env("TEMP", &subprocess_);
480
0
      pass_env("LD_LIBRARY_PATH", &subprocess_);
481
0
      cmd.push_back(base::GetCurExecutableDir() + "/" + argv0);
482
0
      cmd.insert(cmd.end(), args.begin(), args.end());
483
0
    }
484
0
485
0
    if (!base::FileExists(cmd[0])) {
486
0
      PERFETTO_FATAL(
487
0
          "Cannot find %s. Make sure that the target has been built and, on "
488
0
          "Android, pushed to the device.",
489
0
          cmd[0].c_str());
490
0
    }
491
0
492
0
    // This pipe blocks the execution of the child process until the main test
493
0
    // process calls Run(). There are two conflicting problems here:
494
0
    // 1) We can't fork() subprocesses too late, because the test spawns threads
495
0
    //    for hosting the service. fork+threads = bad (see aosp/1089744).
496
0
    // 2) We can't run the subprocess too early, because we need to wait that
497
0
    //    the service threads are ready before trying to connect from the child
498
0
    //    process.
499
0
    sync_pipe_ = base::Pipe::Create();
500
0
    int sync_pipe_rd = *sync_pipe_.rd;
501
0
    subprocess_.args.preserve_fds.push_back(sync_pipe_rd);
502
0
503
0
    // This lambda will be called on the forked child process after having
504
0
    // setup pipe redirection and closed all FDs, right before the exec().
505
0
    // The Subprocesss harness will take care of closing also |sync_pipe_.wr|.
506
0
    subprocess_.args.posix_entrypoint_for_testing = [sync_pipe_rd] {
507
0
      // Don't add any logging here, all file descriptors are closed and trying
508
0
      // to log will likely cause undefined behaviors.
509
0
      char ignored = 0;
510
0
      PERFETTO_CHECK(PERFETTO_EINTR(read(sync_pipe_rd, &ignored, 1)) > 0);
511
0
      PERFETTO_CHECK(close(sync_pipe_rd) == 0 || errno == EINTR);
512
0
    };
513
0
514
0
    subprocess_.Start();
515
0
    sync_pipe_.rd.reset();
516
0
  }
517
518
0
  void SendSigterm() {
519
0
#ifdef SIGTERM
520
0
    kill(subprocess_.pid(), SIGTERM);
521
0
#else
522
0
    // This code is never used on Windows tests, not bothering.
523
0
    if (subprocess_.pid())  // Always true, but avoids Wnoreturn compile errors.
524
0
      PERFETTO_FATAL("SendSigterm() not implemented on this platform");
525
0
#endif
526
0
  }
527
528
 private:
529
  base::Subprocess subprocess_;
530
  base::Pipe sync_pipe_;
531
};
532
533
#endif  // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
534
535
}  // namespace perfetto
536
537
#endif  // TEST_TEST_HELPER_H_