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
17 #include "src/android_internal/atrace_hal.h"
18
19 #include <android/hardware/atrace/1.0/IAtraceDevice.h>
20 #include <iostream>
21
22 namespace perfetto {
23 namespace android_internal {
24
25 using android::hardware::hidl_string;
26 using android::hardware::hidl_vec;
27 using android::hardware::Return;
28 using android::hardware::atrace::V1_0::IAtraceDevice;
29 using android::hardware::atrace::V1_0::TracingCategory;
30
31 namespace {
32
33 android::sp<IAtraceDevice> g_atraceHal;
34
GetService()35 bool GetService() {
36 if (!g_atraceHal)
37 g_atraceHal = IAtraceDevice::getService();
38
39 return g_atraceHal != nullptr;
40 }
41
42 } // namespace
43
ListCategories(TracingVendorCategory * categories,size_t * size_of_arr)44 bool ListCategories(TracingVendorCategory* categories, size_t* size_of_arr) {
45 const size_t in_array_size = *size_of_arr;
46 *size_of_arr = 0;
47 if (!GetService())
48 return false;
49
50 auto category_cb = [categories, size_of_arr,
51 &in_array_size](hidl_vec<TracingCategory> r) {
52 *size_of_arr = std::min(in_array_size, r.size());
53 for (int i = 0; i < *size_of_arr; ++i) {
54 const TracingCategory& cat = r[i];
55 TracingVendorCategory& result = categories[i];
56 strncpy(result.name, cat.name.c_str(), sizeof(result.name));
57 strncpy(result.description, cat.description.c_str(),
58 sizeof(result.description));
59 result.name[sizeof(result.name) - 1] = '\0';
60 result.description[sizeof(result.description) - 1] = '\0';
61 }
62 };
63
64 g_atraceHal->listCategories(category_cb);
65 return true;
66 }
67
EnableCategories(const char ** categories,size_t categories_count)68 bool EnableCategories(const char** categories, size_t categories_count) {
69 if (!GetService())
70 return false;
71 std::vector<hidl_string> args;
72 args.resize(categories_count);
73 for (size_t i = 0; i < categories_count; ++i) {
74 args[i] = categories[i];
75 }
76 g_atraceHal->enableCategories(args);
77 // TODO(hjd): Check status.
78 return true;
79 }
80
DisableAllCategories()81 bool DisableAllCategories() {
82 if (!GetService())
83 return false;
84 g_atraceHal->disableAllCategories();
85 // TODO(hjd): Check status.
86 return true;
87 }
88
ForgetService()89 void ForgetService() {
90 g_atraceHal = nullptr;
91 }
92
93 } // namespace android_internal
94 } // namespace perfetto
95