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" |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 18 | #include "perfetto/tracing/internal/track_event_internal.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 19 | #include "src/tracing/internal/tracing_muxer_impl.h" |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 20 | |
Primiano Tucci | 990b5fe | 2019-05-23 10:20:50 +0100 | [diff] [blame] | 21 | #include <condition_variable> |
| 22 | #include <mutex> |
| 23 | |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 24 | namespace perfetto { |
| 25 | |
| 26 | // static |
Primiano Tucci | 3feec55 | 2020-02-04 11:14:42 +0000 | [diff] [blame] | 27 | void Tracing::InitializeInternal(const TracingInitArgs& args) { |
Eric Seckler | f1c74b7 | 2020-01-20 16:55:15 +0000 | [diff] [blame] | 28 | 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 Kyostila | c71dc46 | 2019-07-03 14:30:39 +0100 | [diff] [blame] | 36 | // Make sure the headers and implementation files agree on the build config. |
| 37 | PERFETTO_CHECK(args.dcheck_is_on_ == PERFETTO_DCHECK_IS_ON()); |
Primiano Tucci | 5944dd7 | 2019-05-21 23:56:17 +0100 | [diff] [blame] | 38 | internal::TracingMuxerImpl::InitializeInstance(args); |
Sami Kyostila | 26a0437 | 2020-01-13 12:46:48 +0000 | [diff] [blame] | 39 | internal::TrackRegistry::InitializeInstance(); |
Eric Seckler | f1c74b7 | 2020-01-20 16:55:15 +0000 | [diff] [blame] | 40 | was_initialized = true; |
| 41 | init_args = args; |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // static |
| 45 | std::unique_ptr<TracingSession> Tracing::NewTrace(BackendType backend) { |
Primiano Tucci | 5944dd7 | 2019-05-21 23:56:17 +0100 | [diff] [blame] | 46 | return static_cast<internal::TracingMuxerImpl*>(internal::TracingMuxer::Get()) |
| 47 | ->CreateTracingSession(backend); |
Primiano Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Primiano Tucci | 990b5fe | 2019-05-23 10:20:50 +0100 | [diff] [blame] | 50 | std::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 Tucci | b579884 | 2019-05-21 23:54:03 +0100 | [diff] [blame] | 71 | } // namespace perfetto |