xref: /aosp_15_r20/frameworks/native/libs/binderdebug/stats.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2023 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/logging.h>
18 #include <binder/BpBinder.h>
19 #include <binder/IServiceManager.h>
20 #include <binderdebug/BinderDebug.h>
21 #include <utils/Errors.h>
22 
23 #include <inttypes.h>
24 
25 namespace android {
26 
main()27 extern "C" int main() {
28     // ignore args - we only print csv
29 
30     // we should use a csv library here for escaping, because
31     // the name is coming from another process
32     printf("name,binder_threads_in_use,binder_threads_started,client_count\n");
33 
34     for (const String16& name : defaultServiceManager()->listServices()) {
35         sp<IBinder> binder = defaultServiceManager()->checkService(name);
36         if (binder == nullptr) {
37             fprintf(stderr, "%s is null", String8(name).c_str());
38             continue;
39         }
40 
41         BpBinder* remote = binder->remoteBinder();
42         const auto handle = remote->getDebugBinderHandle();
43         CHECK(handle != std::nullopt);
44 
45         pid_t pid;
46         CHECK_EQ(OK, binder->getDebugPid(&pid));
47 
48         BinderPidInfo info;
49         CHECK_EQ(OK, getBinderPidInfo(BinderDebugContext::BINDER, pid, &info));
50 
51         std::vector<pid_t> clientPids;
52         CHECK_EQ(OK,
53                  getBinderClientPids(BinderDebugContext::BINDER, getpid(), pid, *handle,
54                                      &clientPids));
55 
56         printf("%s,%" PRIu32 ",%" PRIu32 ",%zu\n", String8(name).c_str(), info.threadUsage,
57                info.threadCount, clientPids.size());
58     }
59     return 0;
60 }
61 
62 } // namespace android
63