blob: bb4d4d1c36791f6a330eb6f98a2b7d1fc86b7a56 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/ledger/bin/testing/run_with_tracing.h"
#include <lib/async/cpp/task.h>
#include <lib/fit/function.h>
#include <lib/trace-provider/provider.h>
#include <lib/trace/event.h>
#include <lib/trace/observer.h>
#include "src/ledger/lib/logging/logging.h"
namespace ledger {
int RunWithTracing(async::Loop* loop, fit::function<void()> runnable) {
trace::TraceProviderWithFdio trace_provider(loop->dispatcher());
trace::TraceObserver trace_observer;
bool started = false;
auto on_trace_state_changed = [runnable = std::move(runnable), &started]() {
if (TRACE_CATEGORY_ENABLED("benchmark") && !started) {
started = true;
runnable();
}
};
// In case tracing has already started.
on_trace_state_changed();
if (!started) {
trace_observer.Start(loop->dispatcher(), std::move(on_trace_state_changed));
}
int err = 0;
async::TaskClosure quit_task([&started, loop, &err] {
if (!started) {
// To avoid running the runnable if the tracing state changes to
// started in the immediate next task on the queue (before the quit
// task executes).
started = true;
LEDGER_LOG(ERROR) << "Timed out waiting for the tracing to start; Did you run the "
"binary with the trace tool enabled?";
err = -1;
loop->Quit();
}
});
quit_task.PostDelayed(loop->dispatcher(), zx::sec(5));
loop->Run();
return err;
}
} // namespace ledger