Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 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 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 17 | #include "perfetto/tracing/tracing.h" |
| 18 | #include "src/tracing/internal/tracing_muxer_impl.h" |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 19 | |
Primiano Tucci | 990b5fe | 2019-05-23 10:20:50 +0100 | [diff] [blame] | 20 | #include <condition_variable> |
| 21 | #include <mutex> |
| 22 | |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 23 | namespace perfetto { |
| 24 | |
| 25 | // static |
| 26 | void Tracing::Initialize(const TracingInitArgs& args) { |
Sami Kyostila | c71dc46 | 2019-07-03 14:30:39 +0100 | [diff] [blame] | 27 | // Make sure the headers and implementation files agree on the build config. |
| 28 | PERFETTO_CHECK(args.dcheck_is_on_ == PERFETTO_DCHECK_IS_ON()); |
Primiano Tucci | 5944dd7 | 2019-05-21 23:56:17 +0100 | [diff] [blame] | 29 | internal::TracingMuxerImpl::InitializeInstance(args); |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // static |
| 33 | std::unique_ptr<TracingSession> Tracing::NewTrace(BackendType backend) { |
Primiano Tucci | 5944dd7 | 2019-05-21 23:56:17 +0100 | [diff] [blame] | 34 | return static_cast<internal::TracingMuxerImpl*>(internal::TracingMuxer::Get()) |
| 35 | ->CreateTracingSession(backend); |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Primiano Tucci | 990b5fe | 2019-05-23 10:20:50 +0100 | [diff] [blame] | 38 | std::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 Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 59 | } // namespace perfetto |