blob: 4999de8b4c4564c4c388a3a25612741d35e373f3 [file] [log] [blame]
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -08001/*
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 Hunga2a1ac32022-03-18 16:12:11 -070020#include <sstream>
21#include <unistd.h>
22#include <vector>
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080023
Andy Hungd4265822022-04-01 18:54:32 -070024#include <mediautils/MediaUtilsDelayed.h>
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080025#include <mediautils/TimerThread.h>
26#include <utils/ThreadDefs.h>
27
Andy Hungdf1ed5c2022-06-13 19:49:43 -070028using namespace std::chrono_literals;
29
Andy Hunga2a1ac32022-03-18 16:12:11 -070030namespace android::mediautils {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080031
Andy Hunga2a1ac32022-03-18 16:12:11 -070032extern std::string formatTime(std::chrono::system_clock::time_point t);
33extern std::string_view timeSuffix(std::string_view time1, std::string_view time2);
34
35TimerThread::Handle TimerThread::scheduleTask(
Andy Hungdf1ed5c2022-06-13 19:49:43 -070036 std::string_view tag, TimerCallback&& func, Duration timeoutDuration) {
Andy Hunga2a1ac32022-03-18 16:12:11 -070037 const auto now = std::chrono::system_clock::now();
Andy Hungdf1ed5c2022-06-13 19:49:43 -070038 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-Tsvi1ea62c92021-11-10 14:38:27 -080042}
43
Andy Hungc8c2dde2022-07-15 15:18:59 -070044TimerThread::Handle TimerThread::trackTask(std::string_view tag) {
Andy Hunga2a1ac32022-03-18 16:12:11 -070045 const auto now = std::chrono::system_clock::now();
Andy Hungc8c2dde2022-07-15 15:18:59 -070046 auto request = std::make_shared<const Request>(now, now, gettid(), tag);
Andy Hunga2a1ac32022-03-18 16:12:11 -070047 return mNoTimeoutMap.add(std::move(request));
48}
49
50bool TimerThread::cancelTask(Handle handle) {
Andy Hungdf1ed5c2022-06-13 19:49:43 -070051 std::shared_ptr<const Request> request = isNoTimeoutHandle(handle) ?
Andy Hunga2a1ac32022-03-18 16:12:11 -070052 mNoTimeoutMap.remove(handle) : mMonitorThread.remove(handle);
53 if (!request) return false;
54 mRetiredQueue.add(std::move(request));
55 return true;
56}
57
58std::string TimerThread::toString(size_t retiredCount) const {
Andy Hungf45f34c2022-03-25 13:09:03 -070059 // 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 Hung10ac7112022-03-28 08:00:40 -070074 std::string timeoutStack;
75 if (analysis.timeoutTid != -1) {
76 timeoutStack = std::string("\ntimeout(")
77 .append(std::to_string(analysis.timeoutTid)).append(") callstack [\n")
Andy Hungd4265822022-04-01 18:54:32 -070078 .append(getCallStackStringForTid(analysis.timeoutTid)).append("]");
Andy Hung10ac7112022-03-28 08:00:40 -070079 }
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 Hungd4265822022-04-01 18:54:32 -070084 .append(getCallStackStringForTid(analysis.HALBlockedTid)).append("]");
Andy Hung10ac7112022-03-28 08:00:40 -070085 }
86
Andy Hunga2a1ac32022-03-18 16:12:11 -070087 return std::string("now ")
88 .append(formatTime(std::chrono::system_clock::now()))
Andy Hungf45f34c2022-03-25 13:09:03 -070089 .append(analysisSummary)
90 .append("\ntimeout [ ")
91 .append(requestsToString(timeoutRequests))
92 .append(" ]\npending [ ")
93 .append(requestsToString(pendingRequests))
Andy Hunga2a1ac32022-03-18 16:12:11 -070094 .append(" ]\nretired [ ")
Andy Hungf45f34c2022-03-25 13:09:03 -070095 .append(requestsToString(retiredRequests))
Andy Hung10ac7112022-03-28 08:00:40 -070096 .append(" ]")
97 .append(timeoutStack)
98 .append(blockedStack);
Andy Hunga2a1ac32022-03-18 16:12:11 -070099}
100
Andy Hungf45f34c2022-03-25 13:09:03 -0700101// 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 */
110bool TimerThread::isRequestFromHal(const std::shared_ptr<const Request>& request) {
Andy Hungc8c2dde2022-07-15 15:18:59 -0700111 const size_t hidlPos = request->tag.asStringView().find("Hidl");
Andy Hungf45f34c2022-03-25 13:09:03 -0700112 if (hidlPos == std::string::npos) return false;
113 // should be a separator afterwards Hidl which indicates the string was in the class.
Andy Hungc8c2dde2022-07-15 15:18:59 -0700114 const size_t separatorPos = request->tag.asStringView().find("::", hidlPos);
Andy Hungf45f34c2022-03-25 13:09:03 -0700115 return separatorPos != std::string::npos;
116}
117
118/* static */
119struct 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 Hungdf1ed5c2022-06-13 19:49:43 -0700132 // We look at pending requests that were scheduled no later than kPendingDuration
Andy Hungf45f34c2022-03-25 13:09:03 -0700133 // 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 Hungdf1ed5c2022-06-13 19:49:43 -0700137 constexpr Duration kPendingDuration = 1000ms;
Andy Hungf45f34c2022-03-25 13:09:03 -0700138 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 Hungdf1ed5c2022-06-13 19:49:43 -0700146 if (pending->scheduled - timeout->scheduled < kPendingDuration) {
Andy Hungf45f34c2022-03-25 13:09:03 -0700147 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 Hunga2a1ac32022-03-18 16:12:11 -0700180std::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
198std::string TimerThread::pendingToString() const {
199 return requestsToString(getPendingRequests());
200}
201
202std::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
210std::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
218std::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
227void 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
235void 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 Hunga2a1ac32022-03-18 16:12:11 -0700246TimerThread::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 Hungdf1ed5c2022-06-13 19:49:43 -0700250 const Handle handle = getUniqueHandle_l();
Andy Hunga2a1ac32022-03-18 16:12:11 -0700251 mMap[handle] = request;
252 return handle;
253}
254
255std::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
264void 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 Hunga2a1ac32022-03-18 16:12:11 -0700272TimerThread::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
279TimerThread::MonitorThread::~MonitorThread() {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800280 {
281 std::lock_guard _l(mMutex);
282 mShouldExit = true;
283 mCond.notify_all();
284 }
285 mThread.join();
286}
287
Andy Hunga2a1ac32022-03-18 16:12:11 -0700288void TimerThread::MonitorThread::threadFunc() {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800289 std::unique_lock _l(mMutex);
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800290 while (!mShouldExit) {
291 if (!mMonitorRequests.empty()) {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700292 Handle nextDeadline = mMonitorRequests.begin()->first;
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800293 if (nextDeadline < std::chrono::steady_clock::now()) {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700294 // 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 Hungdf1ed5c2022-06-13 19:49:43 -0700300 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 Hunga2a1ac32022-03-18 16:12:11 -0700306 }
307 // reacquire the lock - if something was added, we loop immediately to check.
308 _l.lock();
309 continue;
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800310 }
311 mCond.wait_until(_l, nextDeadline);
312 } else {
313 mCond.wait(_l);
314 }
315 }
316}
317
Andy Hunga2a1ac32022-03-18 16:12:11 -0700318TimerThread::Handle TimerThread::MonitorThread::add(
Andy Hungdf1ed5c2022-06-13 19:49:43 -0700319 std::shared_ptr<const Request> request, TimerCallback&& func, Duration timeout) {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700320 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
327std::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 Hungdf1ed5c2022-06-13 19:49:43 -0700334 TimerCallback func = std::move(it->second.second);
Andy Hunga2a1ac32022-03-18 16:12:11 -0700335 mMonitorRequests.erase(it);
336 ul.unlock(); // manually release lock here so func is released outside of lock.
337 return request;
338}
339
340void 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