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 <android-base/file.h>
18 #include <android-base/logging.h>
19
20 #include "AtraceDevice.h"
21
22 namespace android {
23 namespace hardware {
24 namespace atrace {
25 namespace V1_0 {
26 namespace implementation {
27
28 using ::android::hardware::atrace::V1_0::Status;
29 using ::android::hardware::atrace::V1_0::TracingCategory;
30
31 struct TracingConfig {
32 std::string description;
33 // path and if error on failure
34 std::vector<std::pair<std::string, bool>> paths;
35 };
36
37 // This is a map stores categories and their sysfs paths with required flags
38 const std::map<std::string, TracingConfig> kTracingMap = {
39 {
40 "gfx",
41 {"Graphics",
42 {{"/sys/kernel/debug/tracing/events/mdss/enable", false},
43 {"/sys/kernel/debug/tracing/events/sde/enable", false},
44 {"/sys/kernel/debug/tracing/events/mali_systrace/enable", false}}},
45 },
46 {
47 "ion",
48 {"ION Allocation",
49 {{"/sys/kernel/debug/tracing/events/kmem/ion_alloc_buffer_start/enable", false}}},
50 },
51 {
52 "sched",
53 {"CPU Scheduling and Trustzone",
54 {{"/sys/kernel/debug/tracing/events/scm/enable", false},
55 {"/sys/kernel/debug/tracing/events/systrace/enable", false}}},
56 },
57 {
58 "freq",
59 {"CPU Frequency and System Clock",
60 {{"/sys/kernel/debug/tracing/events/msm_bus/enable", false}}},
61 },
62 {
63 "lmh_dcvs",
64 {"QCT HW Lmh-dcvs Frequency",
65 {{"/sys/kernel/debug/tracing/events/lmh/lmh_dcvs_freq/enable", false}}},
66 },
67 };
68
69 // Methods from ::android::hardware::atrace::V1_0::IAtraceDevice follow.
listCategories(listCategories_cb _hidl_cb)70 Return<void> AtraceDevice::listCategories(listCategories_cb _hidl_cb) {
71 hidl_vec<TracingCategory> categories;
72 categories.resize(kTracingMap.size());
73 std::size_t i = 0;
74 for (auto &c : kTracingMap) {
75 categories[i].name = c.first;
76 categories[i].description = c.second.description;
77 i++;
78 }
79 _hidl_cb(categories);
80 return Void();
81 }
82
enableCategories(const hidl_vec<hidl_string> & categories)83 Return<::android::hardware::atrace::V1_0::Status> AtraceDevice::enableCategories(
84 const hidl_vec<hidl_string> &categories) {
85 if (!categories.size()) {
86 return Status::ERROR_INVALID_ARGUMENT;
87 }
88 for (auto &c : categories) {
89 if (kTracingMap.count(c)) {
90 for (auto &p : kTracingMap.at(c).paths) {
91 if (!android::base::WriteStringToFile("1", p.first)) {
92 LOG(ERROR) << "Failed to enable tracing on: " << p.first;
93 if (p.second) {
94 // disable before return
95 disableAllCategories();
96 return Status::ERROR_TRACING_POINT;
97 }
98 }
99 }
100 } else {
101 return Status::ERROR_INVALID_ARGUMENT;
102 }
103 }
104 return Status::SUCCESS;
105 }
106
disableAllCategories()107 Return<::android::hardware::atrace::V1_0::Status> AtraceDevice::disableAllCategories() {
108 auto ret = Status::SUCCESS;
109 for (auto &c : kTracingMap) {
110 for (auto &p : c.second.paths) {
111 if (!android::base::WriteStringToFile("0", p.first)) {
112 LOG(ERROR) << "Failed to disable tracing on: " << p.first;
113 if (p.second) {
114 ret = Status::ERROR_TRACING_POINT;
115 }
116 }
117 }
118 }
119 return ret;
120 }
121
122 } // namespace implementation
123 } // namespace V1_0
124 } // namespace atrace
125 } // namespace hardware
126 } // namespace android
127