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/ext/base/scoped_file.h" 28 #include "perfetto/ext/base/weak_ptr.h" 29 #include "perfetto/ext/tracing/core/basic_types.h" 30 #include "perfetto/ext/tracing/core/trace_writer.h" 31 #include "perfetto/protozero/message_handle.h" 32 #include "src/traced/probes/ftrace/ftrace_config_utils.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 FtraceController; 40 class ProcessStatsDataSource; 41 class InodeFileDataSource; 42 struct FtraceDataSourceConfig; 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 const ProbesDataSource::Descriptor descriptor; 58 59 FtraceDataSource(base::WeakPtr<FtraceController>, 60 TracingSessionID, 61 const FtraceConfig&, 62 std::unique_ptr<TraceWriter>); 63 ~FtraceDataSource() override; 64 65 // Called by FtraceController soon after ProbesProducer creates the data 66 // source, to inject ftrace dependencies. 67 void Initialize(FtraceConfigId, const FtraceDataSourceConfig* parsing_config); 68 69 // ProbesDataSource implementation. 70 void Start() override; 71 72 // Flushes the ftrace buffers into the userspace trace buffers and writes 73 // also ftrace stats. 74 void Flush(FlushRequestID, std::function<void()> callback) override; 75 void OnFtraceFlushComplete(FlushRequestID); 76 config_id()77 FtraceConfigId config_id() const { return config_id_; } config()78 const FtraceConfig& config() const { return config_; } parsing_config()79 const FtraceDataSourceConfig* parsing_config() const { 80 return parsing_config_; 81 } 82 mutable_metadata()83 FtraceMetadata* mutable_metadata() { return &metadata_; } trace_writer()84 TraceWriter* trace_writer() { return writer_.get(); } 85 86 private: 87 FtraceDataSource(const FtraceDataSource&) = delete; 88 FtraceDataSource& operator=(const FtraceDataSource&) = delete; 89 90 void WriteStats(); 91 void DumpFtraceStats(FtraceStats*); 92 93 const FtraceConfig config_; 94 FtraceMetadata metadata_; 95 FtraceStats stats_before_ = {}; 96 std::map<FlushRequestID, std::function<void()>> pending_flushes_; 97 98 // -- Fields initialized by the Initialize() call: 99 FtraceConfigId config_id_ = 0; 100 std::unique_ptr<TraceWriter> writer_; 101 base::WeakPtr<FtraceController> controller_weak_; 102 // Muxer-held state for parsing ftrace according to this data source's 103 // configuration. Not the raw FtraceConfig proto (held by |config_|). 104 const FtraceDataSourceConfig* parsing_config_; 105 // -- End of fields set by Initialize(). 106 }; 107 108 } // namespace perfetto 109 110 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_DATA_SOURCE_H_ 111