blob: 09783ed3f8a47c8b5403366298c67c3240bd5548 [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 Hunga2a1ac32022-03-18 16:12:11 -070028namespace android::mediautils {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080029
Andy Hunga2a1ac32022-03-18 16:12:11 -070030extern std::string formatTime(std::chrono::system_clock::time_point t);
31extern std::string_view timeSuffix(std::string_view time1, std::string_view time2);
32
33TimerThread::Handle TimerThread::scheduleTask(
Andy Hungc8c2dde2022-07-15 15:18:59 -070034 std::string_view tag, std::function<void()>&& func, std::chrono::milliseconds timeout) {
Andy Hunga2a1ac32022-03-18 16:12:11 -070035 const auto now = std::chrono::system_clock::now();
Andy Hungc8c2dde2022-07-15 15:18:59 -070036 auto request = std::make_shared<const Request>(now, now + timeout, gettid(), tag);
Andy Hunga2a1ac32022-03-18 16:12:11 -070037 return mMonitorThread.add(std::move(request), std::move(func), timeout);
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080038}
39
Andy Hungc8c2dde2022-07-15 15:18:59 -070040TimerThread::Handle TimerThread::trackTask(std::string_view tag) {
Andy Hunga2a1ac32022-03-18 16:12:11 -070041 const auto now = std::chrono::system_clock::now();
Andy Hungc8c2dde2022-07-15 15:18:59 -070042 auto request = std::make_shared<const Request>(now, now, gettid(), tag);
Andy Hunga2a1ac32022-03-18 16:12:11 -070043 return mNoTimeoutMap.add(std::move(request));
44}
45
46bool TimerThread::cancelTask(Handle handle) {
47 std::shared_ptr<const Request> request = mNoTimeoutMap.isValidHandle(handle) ?
48 mNoTimeoutMap.remove(handle) : mMonitorThread.remove(handle);
49 if (!request) return false;
50 mRetiredQueue.add(std::move(request));
51 return true;
52}
53
54std::string TimerThread::toString(size_t retiredCount) const {
Andy Hungf45f34c2022-03-25 13:09:03 -070055 // Note: These request queues are snapshot very close together but
56 // not at "identical" times as we don't use a class-wide lock.
57
58 std::vector<std::shared_ptr<const Request>> timeoutRequests;
59 std::vector<std::shared_ptr<const Request>> retiredRequests;
60 mTimeoutQueue.copyRequests(timeoutRequests);
61 mRetiredQueue.copyRequests(retiredRequests, retiredCount);
62 std::vector<std::shared_ptr<const Request>> pendingRequests =
63 getPendingRequests();
64
65 struct Analysis analysis = analyzeTimeout(timeoutRequests, pendingRequests);
66 std::string analysisSummary;
67 if (!analysis.summary.empty()) {
68 analysisSummary = std::string("\nanalysis [ ").append(analysis.summary).append(" ]");
69 }
Andy Hung10ac7112022-03-28 08:00:40 -070070 std::string timeoutStack;
71 if (analysis.timeoutTid != -1) {
72 timeoutStack = std::string("\ntimeout(")
73 .append(std::to_string(analysis.timeoutTid)).append(") callstack [\n")
Andy Hungd4265822022-04-01 18:54:32 -070074 .append(getCallStackStringForTid(analysis.timeoutTid)).append("]");
Andy Hung10ac7112022-03-28 08:00:40 -070075 }
76 std::string blockedStack;
77 if (analysis.HALBlockedTid != -1) {
78 blockedStack = std::string("\nblocked(")
79 .append(std::to_string(analysis.HALBlockedTid)).append(") callstack [\n")
Andy Hungd4265822022-04-01 18:54:32 -070080 .append(getCallStackStringForTid(analysis.HALBlockedTid)).append("]");
Andy Hung10ac7112022-03-28 08:00:40 -070081 }
82
Andy Hunga2a1ac32022-03-18 16:12:11 -070083 return std::string("now ")
84 .append(formatTime(std::chrono::system_clock::now()))
Andy Hungf45f34c2022-03-25 13:09:03 -070085 .append(analysisSummary)
86 .append("\ntimeout [ ")
87 .append(requestsToString(timeoutRequests))
88 .append(" ]\npending [ ")
89 .append(requestsToString(pendingRequests))
Andy Hunga2a1ac32022-03-18 16:12:11 -070090 .append(" ]\nretired [ ")
Andy Hungf45f34c2022-03-25 13:09:03 -070091 .append(requestsToString(retiredRequests))
Andy Hung10ac7112022-03-28 08:00:40 -070092 .append(" ]")
93 .append(timeoutStack)
94 .append(blockedStack);
Andy Hunga2a1ac32022-03-18 16:12:11 -070095}
96
Andy Hungf45f34c2022-03-25 13:09:03 -070097// A HAL method is where the substring "Hidl" is in the class name.
98// The tag should look like: ... Hidl ... :: ...
99// When the audio HAL is updated to AIDL perhaps we will use instead
100// a global directory of HAL classes.
101//
102// See MethodStatistics.cpp:
103// mediautils::getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL)
104//
105/* static */
106bool TimerThread::isRequestFromHal(const std::shared_ptr<const Request>& request) {
Andy Hungc8c2dde2022-07-15 15:18:59 -0700107 const size_t hidlPos = request->tag.asStringView().find("Hidl");
Andy Hungf45f34c2022-03-25 13:09:03 -0700108 if (hidlPos == std::string::npos) return false;
109 // should be a separator afterwards Hidl which indicates the string was in the class.
Andy Hungc8c2dde2022-07-15 15:18:59 -0700110 const size_t separatorPos = request->tag.asStringView().find("::", hidlPos);
Andy Hungf45f34c2022-03-25 13:09:03 -0700111 return separatorPos != std::string::npos;
112}
113
114/* static */
115struct TimerThread::Analysis TimerThread::analyzeTimeout(
116 const std::vector<std::shared_ptr<const Request>>& timeoutRequests,
117 const std::vector<std::shared_ptr<const Request>>& pendingRequests) {
118
119 if (timeoutRequests.empty() || pendingRequests.empty()) return {}; // nothing to say.
120
121 // for now look at last timeout (in our case, the only timeout)
122 const std::shared_ptr<const Request> timeout = timeoutRequests.back();
123
124 // pending Requests that are problematic.
125 std::vector<std::shared_ptr<const Request>> pendingExact;
126 std::vector<std::shared_ptr<const Request>> pendingPossible;
127
128 // We look at pending requests that were scheduled no later than kDuration
129 // after the timeout request. This prevents false matches with calls
130 // that naturally block for a short period of time
131 // such as HAL write() and read().
132 //
133 auto constexpr kDuration = std::chrono::milliseconds(1000);
134 for (const auto& pending : pendingRequests) {
135 // If the pending tid is the same as timeout tid, problem identified.
136 if (pending->tid == timeout->tid) {
137 pendingExact.emplace_back(pending);
138 continue;
139 }
140
141 // if the pending tid is scheduled within time limit
142 if (pending->scheduled - timeout->scheduled < kDuration) {
143 pendingPossible.emplace_back(pending);
144 }
145 }
146
147 struct Analysis analysis{};
148
149 analysis.timeoutTid = timeout->tid;
150 std::string& summary = analysis.summary;
151 if (!pendingExact.empty()) {
152 const auto& request = pendingExact.front();
153 const bool hal = isRequestFromHal(request);
154
155 if (hal) {
156 summary = std::string("Blocked directly due to HAL call: ")
157 .append(request->toString());
158 }
159 }
160 if (summary.empty() && !pendingPossible.empty()) {
161 for (const auto& request : pendingPossible) {
162 const bool hal = isRequestFromHal(request);
163 if (hal) {
164 // The first blocked call is the most likely one.
165 // Recent calls might be temporarily blocked
166 // calls such as write() or read() depending on kDuration.
167 summary = std::string("Blocked possibly due to HAL call: ")
168 .append(request->toString());
169 analysis.HALBlockedTid = request->tid;
170 }
171 }
172 }
173 return analysis;
174}
175
Andy Hunga2a1ac32022-03-18 16:12:11 -0700176std::vector<std::shared_ptr<const TimerThread::Request>> TimerThread::getPendingRequests() const {
177 constexpr size_t kEstimatedPendingRequests = 8; // approx 128 byte alloc.
178 std::vector<std::shared_ptr<const Request>> pendingRequests;
179 pendingRequests.reserve(kEstimatedPendingRequests); // preallocate vector out of lock.
180
181 // following are internally locked calls, which add to our local pendingRequests.
182 mMonitorThread.copyRequests(pendingRequests);
183 mNoTimeoutMap.copyRequests(pendingRequests);
184
185 // Sort in order of scheduled time.
186 std::sort(pendingRequests.begin(), pendingRequests.end(),
187 [](const std::shared_ptr<const Request>& r1,
188 const std::shared_ptr<const Request>& r2) {
189 return r1->scheduled < r2->scheduled;
190 });
191 return pendingRequests;
192}
193
194std::string TimerThread::pendingToString() const {
195 return requestsToString(getPendingRequests());
196}
197
198std::string TimerThread::retiredToString(size_t n) const {
199 std::vector<std::shared_ptr<const Request>> retiredRequests;
200 mRetiredQueue.copyRequests(retiredRequests, n);
201
202 // Dump to string
203 return requestsToString(retiredRequests);
204}
205
206std::string TimerThread::timeoutToString(size_t n) const {
207 std::vector<std::shared_ptr<const Request>> timeoutRequests;
208 mTimeoutQueue.copyRequests(timeoutRequests, n);
209
210 // Dump to string
211 return requestsToString(timeoutRequests);
212}
213
214std::string TimerThread::Request::toString() const {
215 const auto scheduledString = formatTime(scheduled);
216 const auto deadlineString = formatTime(deadline);
217 return std::string(tag)
218 .append(" scheduled ").append(scheduledString)
219 .append(" deadline ").append(timeSuffix(scheduledString, deadlineString))
220 .append(" tid ").append(std::to_string(tid));
221}
222
223void TimerThread::RequestQueue::add(std::shared_ptr<const Request> request) {
224 std::lock_guard lg(mRQMutex);
225 mRequestQueue.emplace_back(std::chrono::system_clock::now(), std::move(request));
226 if (mRequestQueue.size() > mRequestQueueMax) {
227 mRequestQueue.pop_front();
228 }
229}
230
231void TimerThread::RequestQueue::copyRequests(
232 std::vector<std::shared_ptr<const Request>>& requests, size_t n) const {
233 std::lock_guard lg(mRQMutex);
234 const size_t size = mRequestQueue.size();
235 size_t i = n >= size ? 0 : size - n;
236 for (; i < size; ++i) {
237 const auto &[time, request] = mRequestQueue[i];
238 requests.emplace_back(request);
239 }
240}
241
242bool TimerThread::NoTimeoutMap::isValidHandle(Handle handle) const {
243 return handle > getIndexedHandle(mNoTimeoutRequests);
244}
245
246TimerThread::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.
250 const Handle handle = getIndexedHandle(mNoTimeoutRequests++);
251 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
272TimerThread::Handle TimerThread::MonitorThread::getUniqueHandle_l(
273 std::chrono::milliseconds timeout) {
274 // To avoid key collisions, advance by 1 tick until the key is unique.
275 auto deadline = std::chrono::steady_clock::now() + timeout;
276 for (; mMonitorRequests.find(deadline) != mMonitorRequests.end();
277 deadline += std::chrono::steady_clock::duration(1))
278 ;
279 return deadline;
280}
281
282TimerThread::MonitorThread::MonitorThread(RequestQueue& timeoutQueue)
283 : mTimeoutQueue(timeoutQueue)
284 , mThread([this] { threadFunc(); }) {
285 pthread_setname_np(mThread.native_handle(), "TimerThread");
286 pthread_setschedprio(mThread.native_handle(), PRIORITY_URGENT_AUDIO);
287}
288
289TimerThread::MonitorThread::~MonitorThread() {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800290 {
291 std::lock_guard _l(mMutex);
292 mShouldExit = true;
293 mCond.notify_all();
294 }
295 mThread.join();
296}
297
Andy Hunga2a1ac32022-03-18 16:12:11 -0700298void TimerThread::MonitorThread::threadFunc() {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800299 std::unique_lock _l(mMutex);
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800300 while (!mShouldExit) {
301 if (!mMonitorRequests.empty()) {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700302 Handle nextDeadline = mMonitorRequests.begin()->first;
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800303 if (nextDeadline < std::chrono::steady_clock::now()) {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700304 // Deadline has expired, handle the request.
305 {
306 auto node = mMonitorRequests.extract(mMonitorRequests.begin());
307 _l.unlock();
308 // We add Request to retired queue early so that it can be dumped out.
309 mTimeoutQueue.add(std::move(node.mapped().first));
310 node.mapped().second(); // Caution: we don't hold lock here - but do we care?
311 // this is the timeout case! We will crash soon,
312 // maybe before returning.
313 // anything left over is released here outside lock.
314 }
315 // reacquire the lock - if something was added, we loop immediately to check.
316 _l.lock();
317 continue;
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800318 }
319 mCond.wait_until(_l, nextDeadline);
320 } else {
321 mCond.wait(_l);
322 }
323 }
324}
325
Andy Hunga2a1ac32022-03-18 16:12:11 -0700326TimerThread::Handle TimerThread::MonitorThread::add(
327 std::shared_ptr<const Request> request, std::function<void()>&& func,
328 std::chrono::milliseconds timeout) {
329 std::lock_guard _l(mMutex);
330 const Handle handle = getUniqueHandle_l(timeout);
331 mMonitorRequests.emplace(handle, std::make_pair(std::move(request), std::move(func)));
332 mCond.notify_all();
333 return handle;
334}
335
336std::shared_ptr<const TimerThread::Request> TimerThread::MonitorThread::remove(Handle handle) {
337 std::unique_lock ul(mMutex);
338 const auto it = mMonitorRequests.find(handle);
339 if (it == mMonitorRequests.end()) {
340 return {};
341 }
342 std::shared_ptr<const TimerThread::Request> request = std::move(it->second.first);
343 std::function<void()> func = std::move(it->second.second);
344 mMonitorRequests.erase(it);
345 ul.unlock(); // manually release lock here so func is released outside of lock.
346 return request;
347}
348
349void TimerThread::MonitorThread::copyRequests(
350 std::vector<std::shared_ptr<const Request>>& requests) const {
351 std::lock_guard lg(mMutex);
352 for (const auto &[deadline, monitorpair] : mMonitorRequests) {
353 requests.emplace_back(monitorpair.first);
354 }
355}
356
357} // namespace android::mediautils