blob: b82816f66fdae4aeb4fd24dd0d151f7a7a5c36ba [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
24#include <mediautils/TimerThread.h>
25#include <utils/ThreadDefs.h>
26
Andy Hunga2a1ac32022-03-18 16:12:11 -070027namespace android::mediautils {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080028
Andy Hunga2a1ac32022-03-18 16:12:11 -070029extern std::string formatTime(std::chrono::system_clock::time_point t);
30extern std::string_view timeSuffix(std::string_view time1, std::string_view time2);
31
32TimerThread::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-Tsvi1ea62c92021-11-10 14:38:27 -080038}
39
Andy Hunga2a1ac32022-03-18 16:12:11 -070040TimerThread::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
47bool 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
55std::string TimerThread::toString(size_t retiredCount) const {
Andy Hungf45f34c2022-03-25 13:09:03 -070056 // 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 Hunga2a1ac32022-03-18 16:12:11 -070071 return std::string("now ")
72 .append(formatTime(std::chrono::system_clock::now()))
Andy Hungf45f34c2022-03-25 13:09:03 -070073 .append(analysisSummary)
74 .append("\ntimeout [ ")
75 .append(requestsToString(timeoutRequests))
76 .append(" ]\npending [ ")
77 .append(requestsToString(pendingRequests))
Andy Hunga2a1ac32022-03-18 16:12:11 -070078 .append(" ]\nretired [ ")
Andy Hungf45f34c2022-03-25 13:09:03 -070079 .append(requestsToString(retiredRequests))
Andy Hunga2a1ac32022-03-18 16:12:11 -070080 .append(" ]");
81}
82
Andy Hungf45f34c2022-03-25 13:09:03 -070083// 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 */
92bool 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 */
101struct 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 Hunga2a1ac32022-03-18 16:12:11 -0700162std::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
180std::string TimerThread::pendingToString() const {
181 return requestsToString(getPendingRequests());
182}
183
184std::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
192std::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
200std::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
209void 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
217void 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
228bool TimerThread::NoTimeoutMap::isValidHandle(Handle handle) const {
229 return handle > getIndexedHandle(mNoTimeoutRequests);
230}
231
232TimerThread::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
241std::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
250void 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
258TimerThread::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
268TimerThread::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
275TimerThread::MonitorThread::~MonitorThread() {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800276 {
277 std::lock_guard _l(mMutex);
278 mShouldExit = true;
279 mCond.notify_all();
280 }
281 mThread.join();
282}
283
Andy Hunga2a1ac32022-03-18 16:12:11 -0700284void TimerThread::MonitorThread::threadFunc() {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800285 std::unique_lock _l(mMutex);
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800286 while (!mShouldExit) {
287 if (!mMonitorRequests.empty()) {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700288 Handle nextDeadline = mMonitorRequests.begin()->first;
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800289 if (nextDeadline < std::chrono::steady_clock::now()) {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700290 // 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-Tsvi1ea62c92021-11-10 14:38:27 -0800304 }
305 mCond.wait_until(_l, nextDeadline);
306 } else {
307 mCond.wait(_l);
308 }
309 }
310}
311
Andy Hunga2a1ac32022-03-18 16:12:11 -0700312TimerThread::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
322std::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
335void 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