blob: 43bcd9a29c2dbefb95e45b654dd593942ca984c3 [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;
33 std::vector<std::pair<std::string, bool>> paths;
34};
35
36// This is a map stores categories and their sysfs paths with required flags
37const std::map<std::string, TracingConfig> kTracingMap = {
38 // gfx
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", true}}},
50 },
51};
52
53// Methods from ::android::hardware::atrace::V1_0::IAtraceDevice follow.
54Return<void> AtraceDevice::listCategories(listCategories_cb _hidl_cb) {
55 hidl_vec<TracingCategory> categories;
56 categories.resize(kTracingMap.size());
57 std::size_t i = 0;
58 for (auto& c : kTracingMap) {
59 categories[i].name = c.first;
60 categories[i].description = c.second.description;
61 i++;
62 }
63 _hidl_cb(categories);
64 return Void();
65}
66
67Return<::android::hardware::atrace::V1_0::Status> AtraceDevice::enableCategories(
68 const hidl_vec<hidl_string>& categories) {
69 if (!categories.size()) {
70 return Status::ERROR_INVALID_ARGUMENT;
71 }
72 for (auto& c : categories) {
73 if (kTracingMap.count(c)) {
74 for (auto& p : kTracingMap.at(c).paths) {
75 if (!android::base::WriteStringToFile("1", p.first)) {
76 LOG(ERROR) << "Failed to enable tracing on: " << p.first;
77 if (p.second) {
78 // disable before return
79 disableAllCategories();
80 return Status::ERROR_TRACING_POINT;
81 }
82 }
83 }
84 } else {
85 return Status::ERROR_INVALID_ARGUMENT;
86 }
87 }
88 return Status::SUCCESS;
89}
90
91Return<::android::hardware::atrace::V1_0::Status> AtraceDevice::disableAllCategories() {
92 auto ret = Status::SUCCESS;
93 for (auto& c : kTracingMap) {
94 for (auto& p : c.second.paths) {
95 if (!android::base::WriteStringToFile("0", p.first)) {
96 LOG(ERROR) << "Failed to enable tracing on: " << p.first;
97 if (p.second) {
98 ret = Status::ERROR_TRACING_POINT;
99 }
100 }
101 }
102 }
103 return ret;
104}
105
106} // namespace implementation
107} // namespace V1_0
108} // namespace atrace
109} // namespace hardware
110} // namespace android