blob: c0149d933095f75245687ab8fa777b90cec31886 [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"
Sami Kyostila26a04372020-01-13 12:46:48 +000018#include "perfetto/tracing/internal/track_event_internal.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010019#include "src/tracing/internal/tracing_muxer_impl.h"
Primiano Tuccib5798842019-05-21 23:54:03 +010020
Primiano Tucci990b5fe2019-05-23 10:20:50 +010021#include <condition_variable>
22#include <mutex>
23
Primiano Tuccib5798842019-05-21 23:54:03 +010024namespace perfetto {
25
26// static
Primiano Tucci3feec552020-02-04 11:14:42 +000027void Tracing::InitializeInternal(const TracingInitArgs& args) {
Eric Secklerf1c74b72020-01-20 16:55:15 +000028 static bool was_initialized = false;
29 static TracingInitArgs init_args;
30 if (was_initialized) {
31 // Should not be reinitialized with different args.
32 PERFETTO_DCHECK(init_args == args);
33 return;
34 }
35
Sami Kyostilac71dc462019-07-03 14:30:39 +010036 // Make sure the headers and implementation files agree on the build config.
37 PERFETTO_CHECK(args.dcheck_is_on_ == PERFETTO_DCHECK_IS_ON());
Primiano Tucci5944dd72019-05-21 23:56:17 +010038 internal::TracingMuxerImpl::InitializeInstance(args);
Sami Kyostila26a04372020-01-13 12:46:48 +000039 internal::TrackRegistry::InitializeInstance();
Eric Secklerf1c74b72020-01-20 16:55:15 +000040 was_initialized = true;
41 init_args = args;
Primiano Tuccib5798842019-05-21 23:54:03 +010042}
43
44// static
45std::unique_ptr<TracingSession> Tracing::NewTrace(BackendType backend) {
Primiano Tucci5944dd72019-05-21 23:56:17 +010046 return static_cast<internal::TracingMuxerImpl*>(internal::TracingMuxer::Get())
47 ->CreateTracingSession(backend);
Primiano Tuccib5798842019-05-21 23:54:03 +010048}
49
Primiano Tucci990b5fe2019-05-23 10:20:50 +010050std::vector<char> TracingSession::ReadTraceBlocking() {
51 std::vector<char> raw_trace;
52 std::mutex mutex;
53 std::condition_variable cv;
54 bool all_read = false;
55
56 ReadTrace([&mutex, &raw_trace, &all_read, &cv](ReadTraceCallbackArgs cb) {
57 raw_trace.insert(raw_trace.end(), cb.data, cb.data + cb.size);
58 std::unique_lock<std::mutex> lock(mutex);
59 all_read = !cb.has_more;
60 if (all_read)
61 cv.notify_one();
62 });
63
64 {
65 std::unique_lock<std::mutex> lock(mutex);
66 cv.wait(lock, [&all_read] { return all_read; });
67 }
68 return raw_trace;
69}
70
Primiano Tuccib5798842019-05-21 23:54:03 +010071} // namespace perfetto