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