xref: /aosp_15_r20/external/perfetto/src/android_internal/atrace_hal.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #include <string.h>
23 
24 namespace perfetto {
25 namespace android_internal {
26 
27 using android::hardware::hidl_string;
28 using android::hardware::hidl_vec;
29 using android::hardware::Return;
30 using android::hardware::atrace::V1_0::IAtraceDevice;
31 using android::hardware::atrace::V1_0::TracingCategory;
32 
33 namespace {
34 
35 android::sp<IAtraceDevice> g_atraceHal;
36 
GetService()37 bool GetService() {
38   if (!g_atraceHal)
39     g_atraceHal = IAtraceDevice::getService();
40 
41   return g_atraceHal != nullptr;
42 }
43 
44 }  // namespace
45 
ListCategories(TracingVendorCategory * categories,size_t * size_of_arr)46 bool ListCategories(TracingVendorCategory* categories, size_t* size_of_arr) {
47   const size_t in_array_size = *size_of_arr;
48   *size_of_arr = 0;
49   if (!GetService())
50     return false;
51 
52   auto category_cb = [categories, size_of_arr,
53                       &in_array_size](hidl_vec<TracingCategory> r) {
54     *size_of_arr = std::min(in_array_size, r.size());
55     for (int i = 0; i < *size_of_arr; ++i) {
56       const TracingCategory& cat = r[i];
57       TracingVendorCategory& result = categories[i];
58       strlcpy(result.name, cat.name.c_str(), sizeof(result.name));
59       strlcpy(result.description, cat.description.c_str(),
60               sizeof(result.description));
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