1 /*
2 * Copyright 2016 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 #include <android/hidl/manager/1.0/IServiceManager.h>
17 #include <cutils/properties.h>
18 #include <hidl/ServiceManagement.h>
19 #include <iostream>
20
21 using namespace std;
22 using namespace android;
23
24 // Turns on the HAL instrumentation feature on all registered HIDL HALs.
SetHALInstrumentation()25 bool SetHALInstrumentation() {
26 using ::android::hidl::base::V1_0::IBase;
27 using ::android::hidl::manager::V1_0::IServiceManager;
28 using ::android::hardware::hidl_string;
29 using ::android::hardware::Return;
30
31 sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
32
33 if (sm == nullptr) {
34 fprintf(stderr, "failed to get IServiceManager to poke HAL services.\n");
35 return false;
36 }
37
38 auto listRet = sm->list([&](const auto &interfaces) {
39 for (const string &fqInstanceName : interfaces) {
40 string::size_type n = fqInstanceName.find("/");
41 if (n == std::string::npos || fqInstanceName.size() == n + 1) continue;
42
43 hidl_string fqInterfaceName = fqInstanceName.substr(0, n);
44 hidl_string instanceName =
45 fqInstanceName.substr(n + 1, std::string::npos);
46 Return<sp<IBase>> interfaceRet = sm->get(fqInterfaceName, instanceName);
47 if (!interfaceRet.isOk()) {
48 fprintf(stderr, "failed to get service %s: %s\n",
49 fqInstanceName.c_str(), interfaceRet.description().c_str());
50 continue;
51 }
52 sp<IBase> interface = interfaceRet;
53 auto notifyRet = interface->setHALInstrumentation();
54 if (!notifyRet.isOk()) {
55 fprintf(stderr, "failed to setHALInstrumentation on service %s: %s\n",
56 fqInstanceName.c_str(), notifyRet.description().c_str());
57 }
58 printf("- updated the HAL instrumentation mode setting for %s\n",
59 fqInstanceName.c_str());
60 }
61 });
62 if (!listRet.isOk()) {
63 fprintf(stderr, "failed to list services: %s\n",
64 listRet.description().c_str());
65 return false;
66 }
67 return true;
68 }
69
EnableHALProfiling()70 bool EnableHALProfiling() {
71 property_set("hal.instrumentation.enable", "true");
72 if (!SetHALInstrumentation()) {
73 fprintf(stderr, "failed to set instrumentation on services.\n");
74 return false;
75 }
76 return true;
77 }
78
DisableHALProfiling()79 bool DisableHALProfiling() {
80 property_set("hal.instrumentation.enable", "false");
81 if (!SetHALInstrumentation()) {
82 fprintf(stderr, "failed to set instrumentation on services.\n");
83 return false;
84 }
85 return true;
86 }
87
PrintUsage()88 void PrintUsage() {
89 printf(
90 "Usage: \n"
91 "To enable profiling: <binary> enable <lib path 32> <lib path 64>"
92 "To disable profiling <binary> disable");
93 }
94
main(int argc,char * argv[])95 int main(int argc, char *argv[]) {
96 if (argc == 2 && !strcmp(argv[1], "disable")) {
97 printf("* disable profiling.\n");
98 property_set("hal.instrumentation.lib.path.32", "");
99 property_set("hal.instrumentation.lib.path.64", "");
100 if (!DisableHALProfiling()) {
101 printf("failed to disable profiling.\n");
102 return -1;
103 }
104 } else if (argc >= 2 && !strcmp(argv[1], "enable")) {
105 printf("* enable profiling.\n");
106 if (argc == 4) {
107 property_set("hal.instrumentation.lib.path.32", argv[2]);
108 property_set("hal.instrumentation.lib.path.64", argv[3]);
109 }
110 if (!EnableHALProfiling()) {
111 printf("failed to enable profiling.\n");
112 return -1;
113 }
114 } else {
115 PrintUsage();
116 }
117 return 0;
118 }
119