1 /* 2 * Copyright (C) 2020 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 17 #include "TraceProviderFuchsia.h" 18 19 #include <log/log.h> 20 21 #include <lib/async-loop/default.h> 22 #include <lib/async/cpp/task.h> 23 #include <lib/fdio/directory.h> 24 #include <lib/zx/channel.h> 25 26 #include "services/service_connector.h" 27 ~TraceProviderFuchsia()28TraceProviderFuchsia::~TraceProviderFuchsia() { 29 if (mTraceProvider) { 30 async::PostTask(mLoop.dispatcher(), [this]() { 31 // trace_provider_.reset() needs to run on loop_'s dispatcher or 32 // else its teardown can be racy and crash. 33 mTraceProvider.reset(); 34 // Run Quit() in the loop to ensure this task executes before 35 // JoinThreads() returns and the destructor finishes. 36 mLoop.Quit(); 37 }); 38 } else { 39 mLoop.Quit(); 40 } 41 mLoop.JoinThreads(); 42 } 43 TraceProviderFuchsia()44TraceProviderFuchsia::TraceProviderFuchsia() 45 : mLoop(&kAsyncLoopConfigNeverAttachToThread) {} 46 Initialize()47bool TraceProviderFuchsia::Initialize() { 48 // Connect to fuchsia.tracing.provider.Registry service. 49 zx_handle_t client_channel = 50 GetConnectToServiceFunction()("/svc/fuchsia.tracing.provider.Registry"); 51 if (client_channel == ZX_HANDLE_INVALID) { 52 ALOGE("Failed to connect to tracing provider service"); 53 return false; 54 } 55 56 zx_status_t status = mLoop.StartThread(); 57 if (status != ZX_OK) { 58 ALOGE("Failed to start async loop: %d", status); 59 return false; 60 } 61 62 mTraceProvider = std::make_unique<trace::TraceProvider>( 63 zx::channel(client_channel), mLoop.dispatcher()); 64 return true; 65 } 66