blob: fc6a6964ad20412087e1f42c28861f49fbd5e8c4 [file] [log] [blame]
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "perfetto/tracing/tracing.h"
#include "src/tracing/internal/tracing_muxer_impl.h"
#include <condition_variable>
#include <mutex>
namespace perfetto {
// static
void Tracing::Initialize(const TracingInitArgs& args) {
// Make sure the headers and implementation files agree on the build config.
PERFETTO_CHECK(args.dcheck_is_on_ == PERFETTO_DCHECK_IS_ON());
internal::TracingMuxerImpl::InitializeInstance(args);
}
// static
std::unique_ptr<TracingSession> Tracing::NewTrace(BackendType backend) {
return static_cast<internal::TracingMuxerImpl*>(internal::TracingMuxer::Get())
->CreateTracingSession(backend);
}
std::vector<char> TracingSession::ReadTraceBlocking() {
std::vector<char> raw_trace;
std::mutex mutex;
std::condition_variable cv;
bool all_read = false;
ReadTrace([&mutex, &raw_trace, &all_read, &cv](ReadTraceCallbackArgs cb) {
raw_trace.insert(raw_trace.end(), cb.data, cb.data + cb.size);
std::unique_lock<std::mutex> lock(mutex);
all_read = !cb.has_more;
if (all_read)
cv.notify_one();
});
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&all_read] { return all_read; });
}
return raw_trace;
}
} // namespace perfetto