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