blob: daa90be83fa636cc7252b058769efabc2116de84 [file] [log] [blame]
Andreas Huber72961232010-06-07 10:18:57 -07001/*
2 * Copyright (C) 2010 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//#define LOG_NDEBUG 0
18#define LOG_TAG "ALooperRoster"
19#include <utils/Log.h>
Marco Nelissenf09611f2015-02-13 14:12:42 -080020#include <utils/String8.h>
Andreas Huber72961232010-06-07 10:18:57 -070021
Ray Essick05764872022-03-01 19:37:24 -080022#include <inttypes.h>
23
Andreas Huber72961232010-06-07 10:18:57 -070024#include "ALooperRoster.h"
25
26#include "ADebug.h"
27#include "AHandler.h"
28#include "AMessage.h"
29
30namespace android {
31
Marco Nelissenf09611f2015-02-13 14:12:42 -080032static bool verboseStats = false;
33
Andreas Huber72961232010-06-07 10:18:57 -070034ALooperRoster::ALooperRoster()
Lajos Molnar3f274362015-03-05 14:35:41 -080035 : mNextHandlerID(1) {
Andreas Huber72961232010-06-07 10:18:57 -070036}
37
38ALooper::handler_id ALooperRoster::registerHandler(
Chih-Hung Hsieh40d88992016-08-09 14:28:14 -070039 const sp<ALooper> &looper, const sp<AHandler> &handler) {
Andreas Huber72961232010-06-07 10:18:57 -070040 Mutex::Autolock autoLock(mLock);
41
42 if (handler->id() != 0) {
43 CHECK(!"A handler must only be registered once.");
44 return INVALID_OPERATION;
45 }
46
47 HandlerInfo info;
48 info.mLooper = looper;
49 info.mHandler = handler;
50 ALooper::handler_id handlerID = mNextHandlerID++;
51 mHandlers.add(handlerID, info);
52
Lajos Molnar5804a762015-03-04 17:00:10 -080053 handler->setID(handlerID, looper);
Andreas Huber72961232010-06-07 10:18:57 -070054
55 return handlerID;
56}
57
58void ALooperRoster::unregisterHandler(ALooper::handler_id handlerID) {
59 Mutex::Autolock autoLock(mLock);
60
61 ssize_t index = mHandlers.indexOfKey(handlerID);
Andreas Huberf2007772010-10-18 15:06:04 -070062
63 if (index < 0) {
64 return;
65 }
Andreas Huber72961232010-06-07 10:18:57 -070066
67 const HandlerInfo &info = mHandlers.valueAt(index);
Andreas Huber11cc2702010-07-07 09:17:41 -070068
69 sp<AHandler> handler = info.mHandler.promote();
70
71 if (handler != NULL) {
Lajos Molnar5804a762015-03-04 17:00:10 -080072 handler->setID(0, NULL);
Andreas Huber11cc2702010-07-07 09:17:41 -070073 }
Andreas Huber72961232010-06-07 10:18:57 -070074
75 mHandlers.removeItemsAt(index);
76}
77
Andreas Huber30873bf2013-07-31 13:04:50 -070078void ALooperRoster::unregisterStaleHandlers() {
Andreas Huber30873bf2013-07-31 13:04:50 -070079
Marco Nelissen75c672f2014-08-27 15:20:45 -070080 Vector<sp<ALooper> > activeLoopers;
81 {
82 Mutex::Autolock autoLock(mLock);
Andreas Huber30873bf2013-07-31 13:04:50 -070083
Nick Kralevich938e2b32015-06-13 17:03:06 -070084 for (size_t i = mHandlers.size(); i > 0;) {
85 i--;
Marco Nelissen75c672f2014-08-27 15:20:45 -070086 const HandlerInfo &info = mHandlers.valueAt(i);
87
88 sp<ALooper> looper = info.mLooper.promote();
89 if (looper == NULL) {
90 ALOGV("Unregistering stale handler %d", mHandlers.keyAt(i));
91 mHandlers.removeItemsAt(i);
92 } else {
93 // At this point 'looper' might be the only sp<> keeping
94 // the object alive. To prevent it from going out of scope
95 // and having ~ALooper call this method again recursively
96 // and then deadlocking because of the Autolock above, add
97 // it to a Vector which will go out of scope after the lock
98 // has been released.
99 activeLoopers.add(looper);
100 }
Andreas Huber30873bf2013-07-31 13:04:50 -0700101 }
102 }
103}
104
George Burgess IV2c9cb622016-02-29 13:39:17 -0800105static void makeFourCC(uint32_t fourcc, char *s, size_t bufsz) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800106 s[0] = (fourcc >> 24) & 0xff;
107 if (s[0]) {
108 s[1] = (fourcc >> 16) & 0xff;
109 s[2] = (fourcc >> 8) & 0xff;
110 s[3] = fourcc & 0xff;
111 s[4] = 0;
112 } else {
George Burgess IV2c9cb622016-02-29 13:39:17 -0800113 snprintf(s, bufsz, "%u", fourcc);
Marco Nelissenf09611f2015-02-13 14:12:42 -0800114 }
115}
116
117void ALooperRoster::dump(int fd, const Vector<String16>& args) {
118 bool clear = false;
119 bool oldVerbose = verboseStats;
Lajos Molnar5804a762015-03-04 17:00:10 -0800120 for (size_t i = 0; i < args.size(); i++) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800121 if (args[i] == String16("-c")) {
122 clear = true;
123 } else if (args[i] == String16("-von")) {
124 verboseStats = true;
125 } else if (args[i] == String16("-voff")) {
126 verboseStats = false;
127 }
128 }
129 String8 s;
130 if (verboseStats && !oldVerbose) {
131 s.append("(verbose stats collection enabled, stats will be cleared)\n");
132 }
133
134 Mutex::Autolock autoLock(mLock);
135 size_t n = mHandlers.size();
Lajos Molnar5804a762015-03-04 17:00:10 -0800136 s.appendFormat(" %zu registered handlers:\n", n);
Marco Nelissenf09611f2015-02-13 14:12:42 -0800137
138 for (size_t i = 0; i < n; i++) {
Lajos Molnar5804a762015-03-04 17:00:10 -0800139 s.appendFormat(" %d: ", mHandlers.keyAt(i));
Marco Nelissenf09611f2015-02-13 14:12:42 -0800140 HandlerInfo &info = mHandlers.editValueAt(i);
141 sp<ALooper> looper = info.mLooper.promote();
142 if (looper != NULL) {
Lajos Molnar5804a762015-03-04 17:00:10 -0800143 s.append(looper->getName());
Marco Nelissenf09611f2015-02-13 14:12:42 -0800144 sp<AHandler> handler = info.mHandler.promote();
145 if (handler != NULL) {
christosts9aacf192023-03-09 14:12:36 +0000146 bool deliveringMessages;
147 uint32_t currentMessageWhat;
148 int64_t currentDeliveryDurationUs;
149 handler->getDeliveryStatus(deliveringMessages,
150 currentMessageWhat,
151 currentDeliveryDurationUs);
Lajos Molnar5804a762015-03-04 17:00:10 -0800152 handler->mVerboseStats = verboseStats;
christosts9aacf192023-03-09 14:12:36 +0000153 s.appendFormat(": %" PRIu64 " messages processed, delivering "
154 "%d, current msg %" PRIu32 ", current msg "
155 "durationUs %" PRIu64 "",
156 handler->mMessageCounter,
157 deliveringMessages,
158 currentMessageWhat,
159 currentDeliveryDurationUs);
Marco Nelissenf09611f2015-02-13 14:12:42 -0800160 if (verboseStats) {
161 for (size_t j = 0; j < handler->mMessages.size(); j++) {
162 char fourcc[15];
George Burgess IV2c9cb622016-02-29 13:39:17 -0800163 makeFourCC(handler->mMessages.keyAt(j), fourcc, sizeof(fourcc));
Lajos Molnar5804a762015-03-04 17:00:10 -0800164 s.appendFormat("\n %s: %u",
Marco Nelissenf09611f2015-02-13 14:12:42 -0800165 fourcc,
166 handler->mMessages.valueAt(j));
167 }
168 } else {
169 handler->mMessages.clear();
170 }
171 if (clear || (verboseStats && !oldVerbose)) {
172 handler->mMessageCounter = 0;
173 handler->mMessages.clear();
174 }
175 } else {
176 s.append(": <stale handler>");
177 }
178 } else {
179 s.append("<stale>");
180 }
181 s.append("\n");
182 }
Tomasz Wasilczyk03fc55f2023-08-11 17:05:05 +0000183 (void)write(fd, s.c_str(), s.size());
Marco Nelissenf09611f2015-02-13 14:12:42 -0800184}
185
Andreas Huber72961232010-06-07 10:18:57 -0700186} // namespace android