Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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_TAG "TimerThread" |
| 18 | |
| 19 | #include <optional> |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 20 | #include <sstream> |
| 21 | #include <unistd.h> |
| 22 | #include <vector> |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 23 | |
Andy Hung | d426582 | 2022-04-01 18:54:32 -0700 | [diff] [blame] | 24 | #include <mediautils/MediaUtilsDelayed.h> |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 25 | #include <mediautils/TimerThread.h> |
| 26 | #include <utils/ThreadDefs.h> |
| 27 | |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 28 | using namespace std::chrono_literals; |
| 29 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 30 | namespace android::mediautils { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 31 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 32 | extern std::string formatTime(std::chrono::system_clock::time_point t); |
| 33 | extern std::string_view timeSuffix(std::string_view time1, std::string_view time2); |
| 34 | |
| 35 | TimerThread::Handle TimerThread::scheduleTask( |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 36 | std::string_view tag, TimerCallback&& func, Duration timeoutDuration) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 37 | const auto now = std::chrono::system_clock::now(); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 38 | auto request = std::make_shared<const Request>(now, now + |
| 39 | std::chrono::duration_cast<std::chrono::system_clock::duration>(timeoutDuration), |
| 40 | gettid(), tag); |
| 41 | return mMonitorThread.add(std::move(request), std::move(func), timeoutDuration); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Andy Hung | c8c2dde | 2022-07-15 15:18:59 -0700 | [diff] [blame] | 44 | TimerThread::Handle TimerThread::trackTask(std::string_view tag) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 45 | const auto now = std::chrono::system_clock::now(); |
Andy Hung | c8c2dde | 2022-07-15 15:18:59 -0700 | [diff] [blame] | 46 | auto request = std::make_shared<const Request>(now, now, gettid(), tag); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 47 | return mNoTimeoutMap.add(std::move(request)); |
| 48 | } |
| 49 | |
| 50 | bool TimerThread::cancelTask(Handle handle) { |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 51 | std::shared_ptr<const Request> request = isNoTimeoutHandle(handle) ? |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 52 | mNoTimeoutMap.remove(handle) : mMonitorThread.remove(handle); |
| 53 | if (!request) return false; |
| 54 | mRetiredQueue.add(std::move(request)); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | std::string TimerThread::toString(size_t retiredCount) const { |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 59 | // Note: These request queues are snapshot very close together but |
| 60 | // not at "identical" times as we don't use a class-wide lock. |
| 61 | |
| 62 | std::vector<std::shared_ptr<const Request>> timeoutRequests; |
| 63 | std::vector<std::shared_ptr<const Request>> retiredRequests; |
| 64 | mTimeoutQueue.copyRequests(timeoutRequests); |
| 65 | mRetiredQueue.copyRequests(retiredRequests, retiredCount); |
| 66 | std::vector<std::shared_ptr<const Request>> pendingRequests = |
| 67 | getPendingRequests(); |
| 68 | |
| 69 | struct Analysis analysis = analyzeTimeout(timeoutRequests, pendingRequests); |
| 70 | std::string analysisSummary; |
| 71 | if (!analysis.summary.empty()) { |
| 72 | analysisSummary = std::string("\nanalysis [ ").append(analysis.summary).append(" ]"); |
| 73 | } |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 74 | std::string timeoutStack; |
| 75 | if (analysis.timeoutTid != -1) { |
| 76 | timeoutStack = std::string("\ntimeout(") |
| 77 | .append(std::to_string(analysis.timeoutTid)).append(") callstack [\n") |
Andy Hung | d426582 | 2022-04-01 18:54:32 -0700 | [diff] [blame] | 78 | .append(getCallStackStringForTid(analysis.timeoutTid)).append("]"); |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 79 | } |
| 80 | std::string blockedStack; |
| 81 | if (analysis.HALBlockedTid != -1) { |
| 82 | blockedStack = std::string("\nblocked(") |
| 83 | .append(std::to_string(analysis.HALBlockedTid)).append(") callstack [\n") |
Andy Hung | d426582 | 2022-04-01 18:54:32 -0700 | [diff] [blame] | 84 | .append(getCallStackStringForTid(analysis.HALBlockedTid)).append("]"); |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 87 | return std::string("now ") |
| 88 | .append(formatTime(std::chrono::system_clock::now())) |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 89 | .append(analysisSummary) |
| 90 | .append("\ntimeout [ ") |
| 91 | .append(requestsToString(timeoutRequests)) |
| 92 | .append(" ]\npending [ ") |
| 93 | .append(requestsToString(pendingRequests)) |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 94 | .append(" ]\nretired [ ") |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 95 | .append(requestsToString(retiredRequests)) |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 96 | .append(" ]") |
| 97 | .append(timeoutStack) |
| 98 | .append(blockedStack); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 101 | // A HAL method is where the substring "Hidl" is in the class name. |
| 102 | // The tag should look like: ... Hidl ... :: ... |
| 103 | // When the audio HAL is updated to AIDL perhaps we will use instead |
| 104 | // a global directory of HAL classes. |
| 105 | // |
| 106 | // See MethodStatistics.cpp: |
| 107 | // mediautils::getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL) |
| 108 | // |
| 109 | /* static */ |
| 110 | bool TimerThread::isRequestFromHal(const std::shared_ptr<const Request>& request) { |
Andy Hung | c8c2dde | 2022-07-15 15:18:59 -0700 | [diff] [blame] | 111 | const size_t hidlPos = request->tag.asStringView().find("Hidl"); |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 112 | if (hidlPos == std::string::npos) return false; |
| 113 | // should be a separator afterwards Hidl which indicates the string was in the class. |
Andy Hung | c8c2dde | 2022-07-15 15:18:59 -0700 | [diff] [blame] | 114 | const size_t separatorPos = request->tag.asStringView().find("::", hidlPos); |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 115 | return separatorPos != std::string::npos; |
| 116 | } |
| 117 | |
| 118 | /* static */ |
| 119 | struct TimerThread::Analysis TimerThread::analyzeTimeout( |
| 120 | const std::vector<std::shared_ptr<const Request>>& timeoutRequests, |
| 121 | const std::vector<std::shared_ptr<const Request>>& pendingRequests) { |
| 122 | |
| 123 | if (timeoutRequests.empty() || pendingRequests.empty()) return {}; // nothing to say. |
| 124 | |
| 125 | // for now look at last timeout (in our case, the only timeout) |
| 126 | const std::shared_ptr<const Request> timeout = timeoutRequests.back(); |
| 127 | |
| 128 | // pending Requests that are problematic. |
| 129 | std::vector<std::shared_ptr<const Request>> pendingExact; |
| 130 | std::vector<std::shared_ptr<const Request>> pendingPossible; |
| 131 | |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 132 | // We look at pending requests that were scheduled no later than kPendingDuration |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 133 | // after the timeout request. This prevents false matches with calls |
| 134 | // that naturally block for a short period of time |
| 135 | // such as HAL write() and read(). |
| 136 | // |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 137 | constexpr Duration kPendingDuration = 1000ms; |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 138 | for (const auto& pending : pendingRequests) { |
| 139 | // If the pending tid is the same as timeout tid, problem identified. |
| 140 | if (pending->tid == timeout->tid) { |
| 141 | pendingExact.emplace_back(pending); |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | // if the pending tid is scheduled within time limit |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 146 | if (pending->scheduled - timeout->scheduled < kPendingDuration) { |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 147 | pendingPossible.emplace_back(pending); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | struct Analysis analysis{}; |
| 152 | |
| 153 | analysis.timeoutTid = timeout->tid; |
| 154 | std::string& summary = analysis.summary; |
| 155 | if (!pendingExact.empty()) { |
| 156 | const auto& request = pendingExact.front(); |
| 157 | const bool hal = isRequestFromHal(request); |
| 158 | |
| 159 | if (hal) { |
| 160 | summary = std::string("Blocked directly due to HAL call: ") |
| 161 | .append(request->toString()); |
| 162 | } |
| 163 | } |
| 164 | if (summary.empty() && !pendingPossible.empty()) { |
| 165 | for (const auto& request : pendingPossible) { |
| 166 | const bool hal = isRequestFromHal(request); |
| 167 | if (hal) { |
| 168 | // The first blocked call is the most likely one. |
| 169 | // Recent calls might be temporarily blocked |
| 170 | // calls such as write() or read() depending on kDuration. |
| 171 | summary = std::string("Blocked possibly due to HAL call: ") |
| 172 | .append(request->toString()); |
| 173 | analysis.HALBlockedTid = request->tid; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | return analysis; |
| 178 | } |
| 179 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 180 | std::vector<std::shared_ptr<const TimerThread::Request>> TimerThread::getPendingRequests() const { |
| 181 | constexpr size_t kEstimatedPendingRequests = 8; // approx 128 byte alloc. |
| 182 | std::vector<std::shared_ptr<const Request>> pendingRequests; |
| 183 | pendingRequests.reserve(kEstimatedPendingRequests); // preallocate vector out of lock. |
| 184 | |
| 185 | // following are internally locked calls, which add to our local pendingRequests. |
| 186 | mMonitorThread.copyRequests(pendingRequests); |
| 187 | mNoTimeoutMap.copyRequests(pendingRequests); |
| 188 | |
| 189 | // Sort in order of scheduled time. |
| 190 | std::sort(pendingRequests.begin(), pendingRequests.end(), |
| 191 | [](const std::shared_ptr<const Request>& r1, |
| 192 | const std::shared_ptr<const Request>& r2) { |
| 193 | return r1->scheduled < r2->scheduled; |
| 194 | }); |
| 195 | return pendingRequests; |
| 196 | } |
| 197 | |
| 198 | std::string TimerThread::pendingToString() const { |
| 199 | return requestsToString(getPendingRequests()); |
| 200 | } |
| 201 | |
| 202 | std::string TimerThread::retiredToString(size_t n) const { |
| 203 | std::vector<std::shared_ptr<const Request>> retiredRequests; |
| 204 | mRetiredQueue.copyRequests(retiredRequests, n); |
| 205 | |
| 206 | // Dump to string |
| 207 | return requestsToString(retiredRequests); |
| 208 | } |
| 209 | |
| 210 | std::string TimerThread::timeoutToString(size_t n) const { |
| 211 | std::vector<std::shared_ptr<const Request>> timeoutRequests; |
| 212 | mTimeoutQueue.copyRequests(timeoutRequests, n); |
| 213 | |
| 214 | // Dump to string |
| 215 | return requestsToString(timeoutRequests); |
| 216 | } |
| 217 | |
| 218 | std::string TimerThread::Request::toString() const { |
| 219 | const auto scheduledString = formatTime(scheduled); |
| 220 | const auto deadlineString = formatTime(deadline); |
| 221 | return std::string(tag) |
| 222 | .append(" scheduled ").append(scheduledString) |
| 223 | .append(" deadline ").append(timeSuffix(scheduledString, deadlineString)) |
| 224 | .append(" tid ").append(std::to_string(tid)); |
| 225 | } |
| 226 | |
| 227 | void TimerThread::RequestQueue::add(std::shared_ptr<const Request> request) { |
| 228 | std::lock_guard lg(mRQMutex); |
| 229 | mRequestQueue.emplace_back(std::chrono::system_clock::now(), std::move(request)); |
| 230 | if (mRequestQueue.size() > mRequestQueueMax) { |
| 231 | mRequestQueue.pop_front(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void TimerThread::RequestQueue::copyRequests( |
| 236 | std::vector<std::shared_ptr<const Request>>& requests, size_t n) const { |
| 237 | std::lock_guard lg(mRQMutex); |
| 238 | const size_t size = mRequestQueue.size(); |
| 239 | size_t i = n >= size ? 0 : size - n; |
| 240 | for (; i < size; ++i) { |
| 241 | const auto &[time, request] = mRequestQueue[i]; |
| 242 | requests.emplace_back(request); |
| 243 | } |
| 244 | } |
| 245 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 246 | TimerThread::Handle TimerThread::NoTimeoutMap::add(std::shared_ptr<const Request> request) { |
| 247 | std::lock_guard lg(mNTMutex); |
| 248 | // A unique handle is obtained by mNoTimeoutRequests.fetch_add(1), |
| 249 | // This need not be under a lock, but we do so anyhow. |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 250 | const Handle handle = getUniqueHandle_l(); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 251 | mMap[handle] = request; |
| 252 | return handle; |
| 253 | } |
| 254 | |
| 255 | std::shared_ptr<const TimerThread::Request> TimerThread::NoTimeoutMap::remove(Handle handle) { |
| 256 | std::lock_guard lg(mNTMutex); |
| 257 | auto it = mMap.find(handle); |
| 258 | if (it == mMap.end()) return {}; |
| 259 | auto request = it->second; |
| 260 | mMap.erase(it); |
| 261 | return request; |
| 262 | } |
| 263 | |
| 264 | void TimerThread::NoTimeoutMap::copyRequests( |
| 265 | std::vector<std::shared_ptr<const Request>>& requests) const { |
| 266 | std::lock_guard lg(mNTMutex); |
| 267 | for (const auto &[handle, request] : mMap) { |
| 268 | requests.emplace_back(request); |
| 269 | } |
| 270 | } |
| 271 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 272 | TimerThread::MonitorThread::MonitorThread(RequestQueue& timeoutQueue) |
| 273 | : mTimeoutQueue(timeoutQueue) |
| 274 | , mThread([this] { threadFunc(); }) { |
| 275 | pthread_setname_np(mThread.native_handle(), "TimerThread"); |
| 276 | pthread_setschedprio(mThread.native_handle(), PRIORITY_URGENT_AUDIO); |
| 277 | } |
| 278 | |
| 279 | TimerThread::MonitorThread::~MonitorThread() { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 280 | { |
| 281 | std::lock_guard _l(mMutex); |
| 282 | mShouldExit = true; |
| 283 | mCond.notify_all(); |
| 284 | } |
| 285 | mThread.join(); |
| 286 | } |
| 287 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 288 | void TimerThread::MonitorThread::threadFunc() { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 289 | std::unique_lock _l(mMutex); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 290 | while (!mShouldExit) { |
| 291 | if (!mMonitorRequests.empty()) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 292 | Handle nextDeadline = mMonitorRequests.begin()->first; |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 293 | if (nextDeadline < std::chrono::steady_clock::now()) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 294 | // Deadline has expired, handle the request. |
| 295 | { |
| 296 | auto node = mMonitorRequests.extract(mMonitorRequests.begin()); |
| 297 | _l.unlock(); |
| 298 | // We add Request to retired queue early so that it can be dumped out. |
| 299 | mTimeoutQueue.add(std::move(node.mapped().first)); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 300 | node.mapped().second(nextDeadline); |
| 301 | // Caution: we don't hold lock when we call TimerCallback. |
| 302 | // This is benign issue - we permit concurrent operations |
| 303 | // while in the callback to the MonitorQueue. |
| 304 | // |
| 305 | // Anything left over is released here outside lock. |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 306 | } |
| 307 | // reacquire the lock - if something was added, we loop immediately to check. |
| 308 | _l.lock(); |
| 309 | continue; |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 310 | } |
| 311 | mCond.wait_until(_l, nextDeadline); |
| 312 | } else { |
| 313 | mCond.wait(_l); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 318 | TimerThread::Handle TimerThread::MonitorThread::add( |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 319 | std::shared_ptr<const Request> request, TimerCallback&& func, Duration timeout) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 320 | std::lock_guard _l(mMutex); |
| 321 | const Handle handle = getUniqueHandle_l(timeout); |
| 322 | mMonitorRequests.emplace(handle, std::make_pair(std::move(request), std::move(func))); |
| 323 | mCond.notify_all(); |
| 324 | return handle; |
| 325 | } |
| 326 | |
| 327 | std::shared_ptr<const TimerThread::Request> TimerThread::MonitorThread::remove(Handle handle) { |
| 328 | std::unique_lock ul(mMutex); |
| 329 | const auto it = mMonitorRequests.find(handle); |
| 330 | if (it == mMonitorRequests.end()) { |
| 331 | return {}; |
| 332 | } |
| 333 | std::shared_ptr<const TimerThread::Request> request = std::move(it->second.first); |
Andy Hung | df1ed5c | 2022-06-13 19:49:43 -0700 | [diff] [blame^] | 334 | TimerCallback func = std::move(it->second.second); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 335 | mMonitorRequests.erase(it); |
| 336 | ul.unlock(); // manually release lock here so func is released outside of lock. |
| 337 | return request; |
| 338 | } |
| 339 | |
| 340 | void TimerThread::MonitorThread::copyRequests( |
| 341 | std::vector<std::shared_ptr<const Request>>& requests) const { |
| 342 | std::lock_guard lg(mMutex); |
| 343 | for (const auto &[deadline, monitorpair] : mMonitorRequests) { |
| 344 | requests.emplace_back(monitorpair.first); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | } // namespace android::mediautils |