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 SRC_TRACED_PROBES_FTRACE_FTRACE_DATA_SOURCE_H_ 18 #define SRC_TRACED_PROBES_FTRACE_FTRACE_DATA_SOURCE_H_ 19 20 #include <functional> 21 #include <map> 22 #include <memory> 23 #include <set> 24 #include <string> 25 #include <utility> 26 27 #include "perfetto/base/scoped_file.h" 28 #include "perfetto/base/weak_ptr.h" 29 #include "perfetto/protozero/message_handle.h" 30 #include "perfetto/tracing/core/basic_types.h" 31 #include "perfetto/tracing/core/trace_writer.h" 32 #include "src/traced/probes/ftrace/ftrace_config.h" 33 #include "src/traced/probes/ftrace/ftrace_metadata.h" 34 #include "src/traced/probes/ftrace/ftrace_stats.h" 35 #include "src/traced/probes/probes_data_source.h" 36 37 namespace perfetto { 38 39 class EventFilter; 40 class FtraceController; 41 class ProcessStatsDataSource; 42 class InodeFileDataSource; 43 44 namespace protos { 45 namespace pbzero { 46 class FtraceEventBundle; 47 } // namespace pbzero 48 } // namespace protos 49 50 // This class handles the state for one particular tracing session involving 51 // ftrace. There can be several concurrent tracing sessions involving ftrace 52 // and this class is essentially the building block used to multiplex them. 53 // This class is instantiated by ProbesProducer. ProbesProducer also owns the 54 // FtraceController. 55 class FtraceDataSource : public ProbesDataSource { 56 public: 57 static constexpr int kTypeId = 1; 58 FtraceDataSource(base::WeakPtr<FtraceController>, 59 TracingSessionID, 60 const FtraceConfig&, 61 std::unique_ptr<TraceWriter>); 62 ~FtraceDataSource() override; 63 64 // Called by FtraceController soon after ProbesProducer creates the data 65 // source, to inject ftrace dependencies. 66 void Initialize(FtraceConfigId, const EventFilter* event_filter); 67 68 // ProbesDataSource implementation. 69 void Start() override; 70 71 // Flushes the ftrace buffers into the userspace trace buffers and writes 72 // also ftrace stats. 73 void Flush(FlushRequestID, std::function<void()> callback) override; 74 void OnFtraceFlushComplete(FlushRequestID); 75 config_id()76 FtraceConfigId config_id() const { return config_id_; } config()77 const FtraceConfig& config() const { return config_; } event_filter()78 const EventFilter* event_filter() { return event_filter_; } mutable_metadata()79 FtraceMetadata* mutable_metadata() { return &metadata_; } trace_writer()80 TraceWriter* trace_writer() { return writer_.get(); } 81 82 private: 83 FtraceDataSource(const FtraceDataSource&) = delete; 84 FtraceDataSource& operator=(const FtraceDataSource&) = delete; 85 86 void WriteStats(); 87 void DumpFtraceStats(FtraceStats*); 88 89 const FtraceConfig config_; 90 FtraceMetadata metadata_; 91 FtraceStats stats_before_ = {}; 92 std::map<FlushRequestID, std::function<void()>> pending_flushes_; 93 94 // Initialized by the Initialize() call. 95 FtraceConfigId config_id_ = 0; 96 std::unique_ptr<TraceWriter> writer_; 97 base::WeakPtr<FtraceController> controller_weak_; 98 const EventFilter* event_filter_; 99 }; 100 101 } // namespace perfetto 102 103 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_DATA_SOURCE_H_ 104