1 /*
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 
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         // 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         },
52 };
53 
54 // Methods from ::android::hardware::atrace::V1_0::IAtraceDevice follow.
listCategories(listCategories_cb _hidl_cb)55 Return<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 
enableCategories(const hidl_vec<hidl_string> & categories)68 Return<::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 
disableAllCategories()92 Return<::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)) {
97                 LOG(ERROR) << "Failed to disable tracing on: " << p.first;
98                 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
112