blob: fc6a6964ad20412087e1f42c28861f49fbd5e8c4 [file] [log] [blame]
Primiano Tuccib5798842019-05-21 23:54:03 +01001/*
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
Primiano Tucci2c5488f2019-06-01 03:27:28 +010017#include "perfetto/tracing/tracing.h"
18#include "src/tracing/internal/tracing_muxer_impl.h"
Primiano Tuccib5798842019-05-21 23:54:03 +010019
Primiano Tucci990b5fe2019-05-23 10:20:50 +010020#include <condition_variable>
21#include <mutex>
22
Primiano Tuccib5798842019-05-21 23:54:03 +010023namespace perfetto {
24
25// static
26void Tracing::Initialize(const TracingInitArgs& args) {
Sami Kyostilac71dc462019-07-03 14:30:39 +010027 // Make sure the headers and implementation files agree on the build config.
28 PERFETTO_CHECK(args.dcheck_is_on_ == PERFETTO_DCHECK_IS_ON());
Primiano Tucci5944dd72019-05-21 23:56:17 +010029 internal::TracingMuxerImpl::InitializeInstance(args);
Primiano Tuccib5798842019-05-21 23:54:03 +010030}
31
32// static
33std::unique_ptr<TracingSession> Tracing::NewTrace(BackendType backend) {
Primiano Tucci5944dd72019-05-21 23:56:17 +010034 return static_cast<internal::TracingMuxerImpl*>(internal::TracingMuxer::Get())
35 ->CreateTracingSession(backend);
Primiano Tuccib5798842019-05-21 23:54:03 +010036}
37
Primiano Tucci990b5fe2019-05-23 10:20:50 +010038std::vector<char> TracingSession::ReadTraceBlocking() {
39 std::vector<char> raw_trace;
40 std::mutex mutex;
41 std::condition_variable cv;
42 bool all_read = false;
43
44 ReadTrace([&mutex, &raw_trace, &all_read, &cv](ReadTraceCallbackArgs cb) {
45 raw_trace.insert(raw_trace.end(), cb.data, cb.data + cb.size);
46 std::unique_lock<std::mutex> lock(mutex);
47 all_read = !cb.has_more;
48 if (all_read)
49 cv.notify_one();
50 });
51
52 {
53 std::unique_lock<std::mutex> lock(mutex);
54 cv.wait(lock, [&all_read] { return all_read; });
55 }
56 return raw_trace;
57}
58
Primiano Tuccib5798842019-05-21 23:54:03 +010059} // namespace perfetto