blob: 4e82b0adf36ffec803d9040754a24b9420e7193d [file] [log] [blame]
Wei Wangc45ef0e2018-09-21 15:44:31 -07001/*
2 * Copyright (C) 2018 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
22namespace android {
23namespace hardware {
24namespace atrace {
25namespace V1_0 {
26namespace implementation {
27
28using ::android::hardware::atrace::V1_0::Status;
29using ::android::hardware::atrace::V1_0::TracingCategory;
30
31struct TracingConfig {
32 std::string description;
Wei Wangf0f57882019-03-28 19:04:48 -070033 // path and if error on failure
Wei Wangc45ef0e2018-09-21 15:44:31 -070034 std::vector<std::pair<std::string, bool>> paths;
35};
36
37// This is a map stores categories and their sysfs paths with required flags
38const std::map<std::string, TracingConfig> kTracingMap = {
Wei Wangf0f57882019-03-28 19:04:48 -070039 // gfx
40 {
41 "gfx",
42 {"Graphics",
43 {{"/sys/kernel/debug/tracing/events/mdss/enable", false},
44 {"/sys/kernel/debug/tracing/events/sde/enable", false},
45 {"/sys/kernel/debug/tracing/events/mali_systrace/enable", false}}},
46 },
47 {
48 "ion",
49 {"ION allocation",
50 {{"/sys/kernel/debug/tracing/events/kmem/ion_alloc_buffer_start/enable", false}}},
51 },
Wei Wangc45ef0e2018-09-21 15:44:31 -070052};
53
54// Methods from ::android::hardware::atrace::V1_0::IAtraceDevice follow.
55Return<void> AtraceDevice::listCategories(listCategories_cb _hidl_cb) {
56 hidl_vec<TracingCategory> categories;
57 categories.resize(kTracingMap.size());
58 std::size_t i = 0;
59 for (auto& c : kTracingMap) {
60 categories[i].name = c.first;
61 categories[i].description = c.second.description;
62 i++;
63 }
64 _hidl_cb(categories);
65 return Void();
66}
67
68Return<::android::hardware::atrace::V1_0::Status> AtraceDevice::enableCategories(
69 const hidl_vec<hidl_string>& categories) {
70 if (!categories.size()) {
71 return Status::ERROR_INVALID_ARGUMENT;
72 }
73 for (auto& c : categories) {
74 if (kTracingMap.count(c)) {
75 for (auto& p : kTracingMap.at(c).paths) {
76 if (!android::base::WriteStringToFile("1", p.first)) {
77 LOG(ERROR) << "Failed to enable tracing on: " << p.first;
78 if (p.second) {
79 // disable before return
80 disableAllCategories();
81 return Status::ERROR_TRACING_POINT;
82 }
83 }
84 }
85 } else {
86 return Status::ERROR_INVALID_ARGUMENT;
87 }
88 }
89 return Status::SUCCESS;
90}
91
92Return<::android::hardware::atrace::V1_0::Status> AtraceDevice::disableAllCategories() {
93 auto ret = Status::SUCCESS;
94 for (auto& c : kTracingMap) {
95 for (auto& p : c.second.paths) {
96 if (!android::base::WriteStringToFile("0", p.first)) {
Hector Dearman4c5ddf12019-04-09 14:08:13 +010097 LOG(ERROR) << "Failed to disable tracing on: " << p.first;
Wei Wangc45ef0e2018-09-21 15:44:31 -070098 if (p.second) {
99 ret = Status::ERROR_TRACING_POINT;
100 }
101 }
102 }
103 }
104 return ret;
105}
106
107} // namespace implementation
108} // namespace V1_0
109} // namespace atrace
110} // namespace hardware
111} // namespace android