xref: /aosp_15_r20/external/perfetto/src/traced/probes/metatrace/metatrace_data_source.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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/metatrace/metatrace_data_source.h"
18 
19 #include <vector>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/base/task_runner.h"
23 #include "perfetto/ext/tracing/core/trace_packet.h"
24 #include "perfetto/ext/tracing/core/trace_writer.h"
25 #include "src/tracing/service/metatrace_writer.h"
26 
27 #include "protos/perfetto/trace/trace_packet.pbzero.h"
28 
29 namespace perfetto {
30 
31 // static
32 const ProbesDataSource::Descriptor MetatraceDataSource::descriptor = {
33     /*name*/ MetatraceWriter::kDataSourceName,
34     /*flags*/ Descriptor::kFlagsNone,
35     /*fill_descriptor_func*/ nullptr,
36 };
37 
MetatraceDataSource(base::TaskRunner * task_runner,TracingSessionID session_id,std::unique_ptr<TraceWriter> writer)38 MetatraceDataSource::MetatraceDataSource(base::TaskRunner* task_runner,
39                                          TracingSessionID session_id,
40                                          std::unique_ptr<TraceWriter> writer)
41     : ProbesDataSource(session_id, &descriptor),
42       task_runner_(task_runner),
43       trace_writer_(std::move(writer)) {}
44 
~MetatraceDataSource()45 MetatraceDataSource::~MetatraceDataSource() {
46   metatrace_writer_->Disable();
47 }
48 
Start()49 void MetatraceDataSource::Start() {
50   metatrace_writer_.reset(new MetatraceWriter());
51   metatrace_writer_->Enable(task_runner_, std::move(trace_writer_),
52                             metatrace::TAG_ANY);
53 }
54 
55 // This method is also called from StopDataSource with a dummy FlushRequestID
56 // (zero), to ensure that the metatrace commits the events recorded during the
57 // final flush of the tracing session.
Flush(FlushRequestID,std::function<void ()> callback)58 void MetatraceDataSource::Flush(FlushRequestID,
59                                 std::function<void()> callback) {
60   metatrace_writer_->WriteAllAndFlushTraceWriter(std::move(callback));
61 }
62 
63 }  // namespace perfetto
64