• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
17 #include <android/hidl/manager/1.0/IServiceManager.h>
18 #include <cutils/properties.h>
19 #include <hidl/ServiceManagement.h>
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 = fqInstanceName.substr(
45           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(),
50                 interfaceRet.description().c_str());
51         continue;
52       }
53       sp<IBase> interface = interfaceRet;
54       auto notifyRet = interface->setHALInstrumentation();
55       if (!notifyRet.isOk()) {
56         fprintf(stderr, "failed to setHALInstrumentation on service %s: %s\n",
57                 fqInstanceName.c_str(), notifyRet.description().c_str());
58       }
59       printf("- updated the HAL instrumentation mode setting for %s\n",
60              fqInstanceName.c_str());
61     }
62   });
63   if (!listRet.isOk()) {
64     fprintf(stderr, "failed to list services: %s\n",
65             listRet.description().c_str());
66     return false;
67   }
68   return true;
69 }
70 
EnableHALProfiling()71 bool EnableHALProfiling() {
72   property_set("hal.instrumentation.enable", "true");
73   if (!SetHALInstrumentation()) {
74     fprintf(stderr, "failed to set instrumentation on services.\n");
75     return false;
76   }
77   return true;
78 }
79 
DisableHALProfiling()80 bool DisableHALProfiling() {
81   property_set("hal.instrumentation.enable", "false");
82   if (!SetHALInstrumentation()) {
83     fprintf(stderr, "failed to set instrumentation on services.\n");
84     return false;
85   }
86   return true;
87 }
88 
89 // Usage examples:
90 //   To enable, <binary> enable <lib path>
91 //   To disable, <binary> disable clear
main(int argc,char * argv[])92 int main(int argc, char* argv[]) {
93   bool enable_profiling = false;
94   if (argc >= 2) {
95     if (!strcmp(argv[1], "enable")) {
96       enable_profiling = true;
97     }
98     if (argc == 3 && strlen(argv[2]) > 0) {
99       if (!strcmp(argv[2], "clear")) {
100         property_set("hal.instrumentation.lib.path", "");
101         printf("* setprop hal.instrumentation.lib.path \"\"\n");
102       } else {
103         property_set("hal.instrumentation.lib.path", argv[2]);
104         printf("* setprop hal.instrumentation.lib.path %s\n", argv[2]);
105       }
106     }
107   }
108 
109   if (enable_profiling) {
110     printf("* enable profiling.\n");
111     if (!EnableHALProfiling()) {
112       fprintf(stderr, "failed to enable profiling.\n");
113     }
114   } else {
115     printf("* disable profiling.\n");
116     if (!DisableHALProfiling()) {
117       fprintf(stderr, "failed to disable profiling.\n");
118     }
119   }
120   return 0;
121 }
122