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/core/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 };
36
MetatraceDataSource(base::TaskRunner * task_runner,TracingSessionID session_id,std::unique_ptr<TraceWriter> writer)37 MetatraceDataSource::MetatraceDataSource(base::TaskRunner* task_runner,
38 TracingSessionID session_id,
39 std::unique_ptr<TraceWriter> writer)
40 : ProbesDataSource(session_id, &descriptor),
41 task_runner_(task_runner),
42 trace_writer_(std::move(writer)) {}
43
~MetatraceDataSource()44 MetatraceDataSource::~MetatraceDataSource() {
45 metatrace_writer_->Disable();
46 }
47
Start()48 void MetatraceDataSource::Start() {
49 metatrace_writer_.reset(new MetatraceWriter());
50 metatrace_writer_->Enable(task_runner_, std::move(trace_writer_),
51 metatrace::TAG_ANY);
52 }
53
54 // This method is also called from StopDataSource with a dummy FlushRequestID
55 // (zero), to ensure that the metatrace commits the events recorded during the
56 // final flush of the tracing session.
Flush(FlushRequestID,std::function<void ()> callback)57 void MetatraceDataSource::Flush(FlushRequestID,
58 std::function<void()> callback) {
59 metatrace_writer_->WriteAllAndFlushTraceWriter(std::move(callback));
60 }
61
62 } // namespace perfetto
63