• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/traced/probes/ftrace/ftrace_data_source.h"
18 
19 #include "src/traced/probes/ftrace/cpu_reader.h"
20 #include "src/traced/probes/ftrace/ftrace_controller.h"
21 
22 #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
23 #include "perfetto/trace/ftrace/ftrace_stats.pbzero.h"
24 #include "perfetto/trace/trace_packet.pbzero.h"
25 
26 namespace perfetto {
27 
28 // static
29 constexpr int FtraceDataSource::kTypeId;
30 
FtraceDataSource(base::WeakPtr<FtraceController> controller_weak,TracingSessionID session_id,const FtraceConfig & config,std::unique_ptr<TraceWriter> writer)31 FtraceDataSource::FtraceDataSource(
32     base::WeakPtr<FtraceController> controller_weak,
33     TracingSessionID session_id,
34     const FtraceConfig& config,
35     std::unique_ptr<TraceWriter> writer)
36     : ProbesDataSource(session_id, kTypeId),
37       config_(config),
38       writer_(std::move(writer)),
39       controller_weak_(std::move(controller_weak)){};
40 
~FtraceDataSource()41 FtraceDataSource::~FtraceDataSource() {
42   if (controller_weak_)
43     controller_weak_->RemoveDataSource(this);
44 };
45 
Initialize(FtraceConfigId config_id,const EventFilter * event_filter)46 void FtraceDataSource::Initialize(FtraceConfigId config_id,
47                                   const EventFilter* event_filter) {
48   PERFETTO_CHECK(config_id);
49   config_id_ = config_id;
50   event_filter_ = event_filter;
51 }
52 
Start()53 void FtraceDataSource::Start() {
54   FtraceController* ftrace = controller_weak_.get();
55   if (!ftrace)
56     return;
57   PERFETTO_CHECK(config_id_);  // Must be initialized at this point.
58   if (!ftrace->StartDataSource(this))
59     return;
60   DumpFtraceStats(&stats_before_);
61 }
62 
DumpFtraceStats(FtraceStats * stats)63 void FtraceDataSource::DumpFtraceStats(FtraceStats* stats) {
64   if (controller_weak_)
65     controller_weak_->DumpFtraceStats(stats);
66 }
67 
Flush(FlushRequestID flush_request_id,std::function<void ()> callback)68 void FtraceDataSource::Flush(FlushRequestID flush_request_id,
69                              std::function<void()> callback) {
70   if (!controller_weak_)
71     return;
72 
73   pending_flushes_[flush_request_id] = std::move(callback);
74 
75   // FtraceController will call OnFtraceFlushComplete() once the data has been
76   // drained from the ftrace buffer and written into the various writers of
77   // all its active data sources.
78   controller_weak_->Flush(flush_request_id);
79 }
80 
81 // Called by FtraceController after all CPUs have acked the flush or timed out.
OnFtraceFlushComplete(FlushRequestID flush_request_id)82 void FtraceDataSource::OnFtraceFlushComplete(FlushRequestID flush_request_id) {
83   auto it = pending_flushes_.find(flush_request_id);
84   if (it == pending_flushes_.end()) {
85     // This can genuinely happen in case of concurrent ftrace sessions. When a
86     // FtraceDataSource issues a flush, the controller has to drain ftrace data
87     // for everybody (there is only one kernel ftrace buffer for all sessions).
88     // FtraceController doesn't bother to remember which FtraceDataSource did or
89     // did not request a flush. Instead just boradcasts the
90     // OnFtraceFlushComplete() to all of them.
91     return;
92   }
93   auto callback = std::move(it->second);
94   pending_flushes_.erase(it);
95   if (writer_) {
96     WriteStats();
97     writer_->Flush(std::move(callback));
98   }
99 }
100 
WriteStats()101 void FtraceDataSource::WriteStats() {
102   {
103     auto before_packet = writer_->NewTracePacket();
104     auto out = before_packet->set_ftrace_stats();
105     out->set_phase(protos::pbzero::FtraceStats_Phase_START_OF_TRACE);
106     stats_before_.Write(out);
107   }
108   {
109     FtraceStats stats_after{};
110     DumpFtraceStats(&stats_after);
111     auto after_packet = writer_->NewTracePacket();
112     auto out = after_packet->set_ftrace_stats();
113     out->set_phase(protos::pbzero::FtraceStats_Phase_END_OF_TRACE);
114     stats_after.Write(out);
115   }
116 }
117 
118 }  // namespace perfetto
119