Simplify ftrace architecture and integration with traced_probes
Historically the ftrace reader code has been strongly decoupled
from the rest of the codebase. The use case that was justifying
it (fall back into a library for other perf tools) is no more,
and we are left with extra layers that are unneeded and hurt
code readability. This CL removes the glue layers between
ftrace and probes_producer, in preparation of upcoming behavioral
changes (Flush). The main changes introduced by this CL are:
- Introduce a base class with hand-rolled RTTI for probes_producer.
This simplifies the bookkeeping logic within the traced_probes
binary.
- Collapse Ftrace's Sink and SinkDelegate into a FtraceDataSource
class. FDS keeps track of all the state of ftrace for a given
tracing session.
- Remove ftrace/end_to_end_integrationtest.cc, it had just two
tests and they were disabled.
- Minor cleanups: introduce PERFETTO_WARN_UNUSED_RESULT; move
stats to a dedicated header.
Change-Id: I7047fc07bbaf9f9bf862cdb81c87e567ffbc6779
diff --git a/src/traced/probes/ftrace/ftrace_data_source.cc b/src/traced/probes/ftrace/ftrace_data_source.cc
new file mode 100644
index 0000000..da5391e
--- /dev/null
+++ b/src/traced/probes/ftrace/ftrace_data_source.cc
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "src/traced/probes/ftrace/ftrace_data_source.h"
+
+#include "src/traced/probes/ftrace/cpu_reader.h"
+#include "src/traced/probes/ftrace/ftrace_controller.h"
+
+#include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
+#include "perfetto/trace/ftrace/ftrace_stats.pbzero.h"
+#include "perfetto/trace/trace_packet.pbzero.h"
+
+namespace perfetto {
+
+// static
+constexpr int FtraceDataSource::kTypeId;
+
+FtraceDataSource::FtraceDataSource(
+ base::WeakPtr<FtraceController> controller_weak,
+ TracingSessionID session_id,
+ const FtraceConfig& config,
+ std::unique_ptr<TraceWriter> writer)
+ : ProbesDataSource(session_id, kTypeId),
+ config_(config),
+ writer_(std::move(writer)),
+ controller_weak_(std::move(controller_weak)){};
+
+FtraceDataSource::~FtraceDataSource() {
+ if (controller_weak_)
+ controller_weak_->RemoveDataSource(this);
+};
+
+void FtraceDataSource::Initialize(FtraceConfigId config_id,
+ std::unique_ptr<EventFilter> event_filter) {
+ config_id_ = config_id;
+ event_filter_ = std::move(event_filter);
+ DumpFtraceStats(&stats_before_);
+}
+
+void FtraceDataSource::DumpFtraceStats(FtraceStats* stats) {
+ if (controller_weak_)
+ controller_weak_->DumpFtraceStats(stats);
+}
+
+void FtraceDataSource::Flush() {
+ // TODO(primiano): this still doesn't flush data from the kernel ftrace
+ // buffers (see b/73886018). We should do that and delay the
+ // NotifyFlushComplete() until the ftrace data has been drained from the
+ // kernel ftrace buffer and written in the SMB.
+ if (!writer_)
+ return;
+ WriteStats();
+ writer_->Flush();
+}
+
+void FtraceDataSource::WriteStats() {
+ {
+ auto before_packet = writer_->NewTracePacket();
+ auto out = before_packet->set_ftrace_stats();
+ out->set_phase(protos::pbzero::FtraceStats_Phase_START_OF_TRACE);
+ stats_before_.Write(out);
+ }
+ {
+ FtraceStats stats_after{};
+ DumpFtraceStats(&stats_after);
+ auto after_packet = writer_->NewTracePacket();
+ auto out = after_packet->set_ftrace_stats();
+ out->set_phase(protos::pbzero::FtraceStats_Phase_END_OF_TRACE);
+ stats_after.Write(out);
+ }
+}
+
+} // namespace perfetto