Skip to content

Commit

Permalink
base: Make base_unittests pass with the Perfetto client library
Browse files Browse the repository at this point in the history
Make base_unittests pass with the Perfetto client library. Notable
changes:

 - Pseudostacks aren't supported with Perfetto's trace points, so
   disable pseudostack-based heap profiling (until it is removed
   separately.)
 - Any trace point that requires a pointer to the enabled word of
   a tracing category must use a real trace category as opposed to
   a dynamic/test-only one.

Bug: b/189458236
Change-Id: I83d4e02641b7bb0907e0b52454806a0f3207d478
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2922121
Commit-Queue: Sami Kyöstilä <skyostil@chromium.org>
Auto-Submit: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: Eric Seckler <eseckler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#888040}
  • Loading branch information
skyostil authored and Chromium LUCI CQ committed Jun 1, 2021
1 parent 9b817b4 commit 590a246
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2957,7 +2957,7 @@ TEST_P(SequenceManagerTest, BlameContextAttribution) {

trace_analyzer::Start("*");
{
trace_event::BlameContext blame_context("cat", "name", "type", "scope", 0,
trace_event::BlameContext blame_context("base", "name", "type", "scope", 0,
nullptr);
blame_context.Initialize();
queue->SetBlameContext(&blame_context);
Expand Down
37 changes: 33 additions & 4 deletions base/test/trace_test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/sequenced_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/tracing/perfetto_platform.h"
#include "third_party/perfetto/include/perfetto/tracing.h"

namespace base {
namespace test {
Expand Down Expand Up @@ -66,11 +67,25 @@ RebindableTaskRunner* GetClientLibTaskRunner() {

} // namespace

TracingEnvironment::TracingEnvironment() {
if (perfetto::Tracing::IsInitialized())
return;
static tracing::PerfettoPlatform perfetto_platform(
tracing::PerfettoPlatform::TaskRunnerType::kBuiltin);
perfetto::TracingInitArgs init_args;
init_args.backends = perfetto::BackendType::kInProcessBackend;
init_args.platform = &perfetto_platform;
perfetto::Tracing::Initialize(init_args);
#if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
perfetto::TrackEvent::Register();
#endif
}

TracingEnvironment::TracingEnvironment(
TaskEnvironment& task_environment,
scoped_refptr<SequencedTaskRunner> task_runner,
tracing::PerfettoPlatform* perfetto_platform)
: task_environment_(task_environment) {
: task_environment_(&task_environment) {
// Since Perfetto's platform backend can only be initialized once in a
// process, we give it a task runner that can outlive the per-test task
// environment.
Expand All @@ -80,12 +95,26 @@ TracingEnvironment::TracingEnvironment(
client_lib_task_runner->set_task_runner(std::move(task_runner));

// Wait for any posted construction tasks to execute.
task_environment_.RunUntilIdle();
task_environment_->RunUntilIdle();
}

TracingEnvironment::~TracingEnvironment() {
// Wait for any posted destruction tasks to execute.
task_environment_.RunUntilIdle();
if (task_environment_) {
// Wait for any posted destruction tasks to execute.
task_environment_->RunUntilIdle();
}
}

// static
perfetto::protos::gen::TraceConfig TracingEnvironment::GetDefaultTraceConfig() {
perfetto::protos::gen::TraceConfig trace_config;
auto* buffer_config = trace_config.add_buffers();
buffer_config->set_size_kb(1024 * 1024);
auto* data_source = trace_config.add_data_sources();
auto* source_config = data_source->mutable_config();
source_config->set_name("track_event");
source_config->set_target_buffer(0);
return trace_config;
}

} // namespace test
Expand Down
12 changes: 11 additions & 1 deletion base/test/trace_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/task/thread_pool.h"
#include "base/test/task_environment.h"
#include "base/trace_event/trace_log.h"
#include "third_party/perfetto/protos/perfetto/config/trace_config.gen.h"

namespace base {
namespace tracing {
Expand All @@ -21,14 +22,23 @@ namespace test {
// //services/tracing for recording traces in multiprocess configurations.
class TracingEnvironment {
public:
// Construct a tracing environment using the default Perfetto tracing
// platform.
TracingEnvironment();

// Constructs a tracing environment with the given task runner and Perfetto
// tracing platform.
explicit TracingEnvironment(TaskEnvironment&,
scoped_refptr<SequencedTaskRunner> =
ThreadPool::CreateSequencedTaskRunner({}),
base::tracing::PerfettoPlatform* = nullptr);
~TracingEnvironment();

// Builds a default Perfetto trace config with track events enabled.
static perfetto::protos::gen::TraceConfig GetDefaultTraceConfig();

private:
TaskEnvironment& task_environment_;
TaskEnvironment* task_environment_ = nullptr;
};

} // namespace test
Expand Down
4 changes: 2 additions & 2 deletions base/trace_event/blame_context_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace base {
namespace trace_event {
namespace {

const char kTestBlameContextCategory[] = "test";
const char kDisabledTestBlameContextCategory[] = "disabled-by-default-test";
const char kTestBlameContextCategory[] = "base";
const char kDisabledTestBlameContextCategory[] = "disabled-by-default-base";
const char kTestBlameContextName[] = "TestBlameContext";
const char kTestBlameContextType[] = "TestBlameContextType";
const char kTestBlameContextScope[] = "TestBlameContextScope";
Expand Down
8 changes: 0 additions & 8 deletions base/trace_event/builtin_categories.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,12 @@ class BASE_EXPORT BuiltinCategories {
public:
// Returns a built-in category name at |index| in the registry.
static constexpr const char* At(size_t index) {
#if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
return perfetto::internal::kCategories[index].name;
#else
return kBuiltinCategories[index];
#endif
}

// Returns the amount of built-in categories in the registry.
static constexpr size_t Size() {
#if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
return perfetto::internal::kCategoryCount;
#else
return base::size(kBuiltinCategories);
#endif
}

// Where in the builtin category list to start when populating the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
namespace base {
namespace trace_event {

// Pseudostack-based heap profiling isn't supported with Perfetto.
#if !BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)

// Define all strings once, because the pseudo stack requires pointer equality,
// and string interning is unreliable.
const char kThreadName[] = "TestThread";
Expand Down Expand Up @@ -347,5 +350,7 @@ TEST_F(AllocationContextTrackerTest, IgnoreAllocationTest) {
->GetContextSnapshot(&ctx));
}

#endif // !BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)

} // namespace trace_event
} // namespace base
17 changes: 17 additions & 0 deletions base/trace_event/trace_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ void OnAddLegacyTraceEvent(TraceEvent* trace_event,
WriteDebugAnnotations(trace_event, ctx.event());
auto* legacy_event = ctx.event()->set_legacy_event();
legacy_event->set_phase(trace_event->phase());
uint32_t id_flags =
trace_event->flags() &
(TRACE_EVENT_FLAG_HAS_ID | TRACE_EVENT_FLAG_HAS_LOCAL_ID |
TRACE_EVENT_FLAG_HAS_GLOBAL_ID);
switch (id_flags) {
case TRACE_EVENT_FLAG_HAS_ID:
legacy_event->set_unscoped_id(trace_event->id());
break;
case TRACE_EVENT_FLAG_HAS_LOCAL_ID:
legacy_event->set_local_id(trace_event->id());
break;
case TRACE_EVENT_FLAG_HAS_GLOBAL_ID:
legacy_event->set_global_id(trace_event->id());
break;
default:
break;
}
});
}
#endif // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
Expand Down
Loading

0 comments on commit 590a246

Please sign in to comment.