blob: ec68de75d6af6f86419043bb75ea5857195d1197 [file] [log] [blame]
Eric Laurent3528c932018-02-23 17:17:22 -08001/*
2 * Copyright (C) 2018 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
Atneya Naircce14202022-09-05 20:17:50 -070017#include <csignal>
18#include "mediautils/TimerThread.h"
Eric Laurent42896a02019-09-27 15:40:33 -070019#define LOG_TAG "TimeCheck"
Eric Laurent3528c932018-02-23 17:17:22 -080020
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080021#include <optional>
22
Andy Hung10ac7112022-03-28 08:00:40 -070023#include <android-base/logging.h>
Atneya Naircce14202022-09-05 20:17:50 -070024#include <android-base/strings.h>
Andy Hunga2a1ac32022-03-18 16:12:11 -070025#include <audio_utils/clock.h>
Marco Nelissencf90b492019-09-26 11:20:54 -070026#include <mediautils/EventLog.h>
Andy Hung35f96152022-07-15 15:18:59 -070027#include <mediautils/FixedString.h>
Andy Hung224f82f2022-03-22 00:00:49 -070028#include <mediautils/MethodStatistics.h>
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080029#include <mediautils/TimeCheck.h>
Atneya Nairf5b68512022-05-23 20:02:49 -040030#include <mediautils/TidWrapper.h>
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080031#include <utils/Log.h>
Atneya Nairf5b68512022-05-23 20:02:49 -040032
Atneya Naircce14202022-09-05 20:17:50 -070033#if defined(__ANDROID__)
Eric Laurent42896a02019-09-27 15:40:33 -070034#include "debuggerd/handler.h"
Atneya Nairf5b68512022-05-23 20:02:49 -040035#endif
36
Atneya Naircce14202022-09-05 20:17:50 -070037
38namespace android::mediautils {
Atneya Nairf5b68512022-05-23 20:02:49 -040039// This function appropriately signals a pid to dump a backtrace if we are
Atneya Naircce14202022-09-05 20:17:50 -070040// running on device (and the HAL exists). If we are not running on an Android
41// device, there is no HAL to signal (so we do nothing).
Atneya Nairf5b68512022-05-23 20:02:49 -040042static inline void signalAudioHAL([[maybe_unused]] pid_t pid) {
Atneya Naircce14202022-09-05 20:17:50 -070043#if defined(__ANDROID__)
Atneya Nairf5b68512022-05-23 20:02:49 -040044 sigqueue(pid, DEBUGGER_SIGNAL, {.sival_int = 0});
45#endif
46}
Eric Laurent3528c932018-02-23 17:17:22 -080047
Andy Hunga2a1ac32022-03-18 16:12:11 -070048/**
49 * Returns the std::string "HH:MM:SS.MSc" from a system_clock time_point.
50 */
Ytai Ben-Tsvi34f26b12021-12-02 13:58:38 -080051std::string formatTime(std::chrono::system_clock::time_point t) {
Andy Hunga2a1ac32022-03-18 16:12:11 -070052 auto time_string = audio_utils_time_string_from_ns(
53 std::chrono::nanoseconds(t.time_since_epoch()).count());
54
55 // The time string is 19 characters (including null termination).
56 // Example: "03-27 16:47:06.187"
57 // MM DD HH MM SS MS
58 // We offset by 6 to get HH:MM:SS.MSc
59 //
60 return time_string.time + 6; // offset to remove month/day.
Ytai Ben-Tsvi34f26b12021-12-02 13:58:38 -080061}
62
Andy Hunga2a1ac32022-03-18 16:12:11 -070063/**
64 * Finds the end of the common time prefix.
65 *
66 * This is as an option to remove the common time prefix to avoid
67 * unnecessary duplicated strings.
68 *
69 * \param time1 a time string
70 * \param time2 a time string
71 * \return the position where the common time prefix ends. For abbreviated
72 * printing of time2, offset the character pointer by this position.
73 */
74static size_t commonTimePrefixPosition(std::string_view time1, std::string_view time2) {
75 const size_t endPos = std::min(time1.size(), time2.size());
76 size_t i;
77
78 // Find location of the first mismatch between strings
79 for (i = 0; ; ++i) {
80 if (i == endPos) {
81 return i; // strings match completely to the length of one of the strings.
82 }
83 if (time1[i] != time2[i]) {
84 break;
85 }
86 if (time1[i] == '\0') {
87 return i; // "printed" strings match completely. No need to check further.
88 }
89 }
90
91 // Go backwards until we find a delimeter or space.
92 for (; i > 0
93 && isdigit(time1[i]) // still a number
94 && time1[i - 1] != ' '
95 ; --i) {
96 }
97 return i;
98}
99
100/**
101 * Returns the unique suffix of time2 that isn't present in time1.
102 *
103 * If time2 is identical to time1, then an empty string_view is returned.
104 * This method is used to elide the common prefix when printing times.
105 */
106std::string_view timeSuffix(std::string_view time1, std::string_view time2) {
107 const size_t pos = commonTimePrefixPosition(time1, time2);
108 return time2.substr(pos);
109}
Ytai Ben-Tsvi34f26b12021-12-02 13:58:38 -0800110
Eric Laurent42896a02019-09-27 15:40:33 -0700111// Audio HAL server pids vector used to generate audio HAL processes tombstone
112// when audioserver watchdog triggers.
113// We use a lockless storage to avoid potential deadlocks in the context of watchdog
114// trigger.
115// Protection again simultaneous writes is not needed given one update takes place
116// during AudioFlinger construction and other comes necessarily later once the IAudioFlinger
117// interface is available.
118// The use of an atomic index just guaranties that current vector is fully initialized
119// when read.
120/* static */
121void TimeCheck::accessAudioHalPids(std::vector<pid_t>* pids, bool update) {
122 static constexpr int kNumAudioHalPidsVectors = 3;
123 static std::vector<pid_t> audioHalPids[kNumAudioHalPidsVectors];
Andy Hung5c6d68a2022-03-09 21:54:59 -0800124 static std::atomic<unsigned> curAudioHalPids = 0;
Eric Laurent42896a02019-09-27 15:40:33 -0700125
126 if (update) {
Eric Laurent1ad278b2021-03-05 18:09:01 +0100127 audioHalPids[(curAudioHalPids++ + 1) % kNumAudioHalPidsVectors] = *pids;
Eric Laurent42896a02019-09-27 15:40:33 -0700128 } else {
Eric Laurent1ad278b2021-03-05 18:09:01 +0100129 *pids = audioHalPids[curAudioHalPids % kNumAudioHalPidsVectors];
Eric Laurent42896a02019-09-27 15:40:33 -0700130 }
131}
132
133/* static */
134void TimeCheck::setAudioHalPids(const std::vector<pid_t>& pids) {
135 accessAudioHalPids(&(const_cast<std::vector<pid_t>&>(pids)), true);
136}
137
138/* static */
139std::vector<pid_t> TimeCheck::getAudioHalPids() {
140 std::vector<pid_t> pids;
141 accessAudioHalPids(&pids, false);
142 return pids;
143}
144
Eric Laurent3528c932018-02-23 17:17:22 -0800145/* static */
Andy Hung5c6d68a2022-03-09 21:54:59 -0800146TimerThread& TimeCheck::getTimeCheckThread() {
147 static TimerThread sTimeCheckThread{};
Eric Laurent3528c932018-02-23 17:17:22 -0800148 return sTimeCheckThread;
149}
150
Andy Hunga2a1ac32022-03-18 16:12:11 -0700151/* static */
152std::string TimeCheck::toString() {
153 // note pending and retired are individually locked for maximum concurrency,
154 // snapshot is not instantaneous at a single time.
Atneya Naircce14202022-09-05 20:17:50 -0700155 return getTimeCheckThread().getSnapshotAnalysis().toString();
Andy Hunga2a1ac32022-03-18 16:12:11 -0700156}
157
Andy Hungf8ab0932022-06-13 19:49:43 -0700158TimeCheck::TimeCheck(std::string_view tag, OnTimerFunc&& onTimer, Duration requestedTimeoutDuration,
159 Duration secondChanceDuration, bool crashOnTimeout)
Andy Hung35f96152022-07-15 15:18:59 -0700160 : mTimeCheckHandler{ std::make_shared<TimeCheckHandler>(
Andy Hungf8ab0932022-06-13 19:49:43 -0700161 tag, std::move(onTimer), crashOnTimeout, requestedTimeoutDuration,
Atneya Nairf5b68512022-05-23 20:02:49 -0400162 secondChanceDuration, std::chrono::system_clock::now(), getThreadIdWrapper()) }
Andy Hungf8ab0932022-06-13 19:49:43 -0700163 , mTimerHandle(requestedTimeoutDuration.count() == 0
164 /* for TimeCheck we don't consider a non-zero secondChanceDuration here */
Andy Hunga2a1ac32022-03-18 16:12:11 -0700165 ? getTimeCheckThread().trackTask(mTimeCheckHandler->tag)
166 : getTimeCheckThread().scheduleTask(
167 mTimeCheckHandler->tag,
168 // Pass in all the arguments by value to this task for safety.
169 // The thread could call the callback before the constructor is finished.
170 // The destructor is not blocked on callback.
Andy Hung2aa15102022-06-13 19:49:43 -0700171 [ timeCheckHandler = mTimeCheckHandler ](TimerThread::Handle timerHandle) {
172 timeCheckHandler->onTimeout(timerHandle);
Andy Hunga2a1ac32022-03-18 16:12:11 -0700173 },
Andy Hungf8ab0932022-06-13 19:49:43 -0700174 requestedTimeoutDuration,
175 secondChanceDuration)) {}
Eric Laurent3528c932018-02-23 17:17:22 -0800176
177TimeCheck::~TimeCheck() {
Andy Hunga2a1ac32022-03-18 16:12:11 -0700178 if (mTimeCheckHandler) {
179 mTimeCheckHandler->onCancel(mTimerHandle);
180 }
Eric Laurent3528c932018-02-23 17:17:22 -0800181}
182
Andy Hung2aa15102022-06-13 19:49:43 -0700183/* static */
184std::string TimeCheck::analyzeTimeouts(
185 float requestedTimeoutMs, float elapsedSteadyMs, float elapsedSystemMs) {
186 // Track any OS clock issues with suspend.
187 // It is possible that the elapsedSystemMs is much greater than elapsedSteadyMs if
188 // a suspend occurs; however, we always expect the timeout ms should always be slightly
189 // less than the elapsed steady ms regardless of whether a suspend occurs or not.
190
191 std::string s("Timeout ms ");
192 s.append(std::to_string(requestedTimeoutMs))
193 .append(" elapsed steady ms ").append(std::to_string(elapsedSteadyMs))
194 .append(" elapsed system ms ").append(std::to_string(elapsedSystemMs));
195
196 // Is there something unusual?
197 static constexpr float TOLERANCE_CONTEXT_SWITCH_MS = 200.f;
198
199 if (requestedTimeoutMs > elapsedSteadyMs || requestedTimeoutMs > elapsedSystemMs) {
200 s.append("\nError: early expiration - "
201 "requestedTimeoutMs should be less than elapsed time");
202 }
203
204 if (elapsedSteadyMs > elapsedSystemMs + TOLERANCE_CONTEXT_SWITCH_MS) {
205 s.append("\nWarning: steady time should not advance faster than system time");
206 }
207
208 // This has been found in suspend stress testing.
209 if (elapsedSteadyMs > requestedTimeoutMs + TOLERANCE_CONTEXT_SWITCH_MS) {
210 s.append("\nWarning: steady time significantly exceeds timeout "
211 "- possible thread stall or aborted suspend");
212 }
213
214 // This has been found in suspend stress testing.
215 if (elapsedSystemMs > requestedTimeoutMs + TOLERANCE_CONTEXT_SWITCH_MS) {
216 s.append("\nInformation: system time significantly exceeds timeout "
217 "- possible suspend");
218 }
219 return s;
220}
221
222// To avoid any potential race conditions, the timer handle
223// (expiration = clock steady start + timeout) is passed into the callback.
Andy Hung5c6d68a2022-03-09 21:54:59 -0800224void TimeCheck::TimeCheckHandler::onCancel(TimerThread::Handle timerHandle) const
225{
226 if (TimeCheck::getTimeCheckThread().cancelTask(timerHandle) && onTimer) {
Andy Hung2aa15102022-06-13 19:49:43 -0700227 const std::chrono::steady_clock::time_point endSteadyTime =
228 std::chrono::steady_clock::now();
229 const float elapsedSteadyMs = std::chrono::duration_cast<FloatMs>(
230 endSteadyTime - timerHandle + timeoutDuration).count();
231 // send the elapsed steady time for statistics.
232 onTimer(false /* timeout */, elapsedSteadyMs);
Andy Hung5c6d68a2022-03-09 21:54:59 -0800233 }
234}
235
Andy Hung2aa15102022-06-13 19:49:43 -0700236// To avoid any potential race conditions, the timer handle
237// (expiration = clock steady start + timeout) is passed into the callback.
238void TimeCheck::TimeCheckHandler::onTimeout(TimerThread::Handle timerHandle) const
Andy Hung5c6d68a2022-03-09 21:54:59 -0800239{
Andy Hung2aa15102022-06-13 19:49:43 -0700240 const std::chrono::steady_clock::time_point endSteadyTime = std::chrono::steady_clock::now();
241 const std::chrono::system_clock::time_point endSystemTime = std::chrono::system_clock::now();
242 // timerHandle incorporates the timeout
243 const float elapsedSteadyMs = std::chrono::duration_cast<FloatMs>(
244 endSteadyTime - (timerHandle - timeoutDuration)).count();
245 const float elapsedSystemMs = std::chrono::duration_cast<FloatMs>(
246 endSystemTime - startSystemTime).count();
247 const float requestedTimeoutMs = std::chrono::duration_cast<FloatMs>(
248 timeoutDuration).count();
Andy Hungf8ab0932022-06-13 19:49:43 -0700249 const float secondChanceMs = std::chrono::duration_cast<FloatMs>(
250 secondChanceDuration).count();
Andy Hung2aa15102022-06-13 19:49:43 -0700251
Andy Hung5c6d68a2022-03-09 21:54:59 -0800252 if (onTimer) {
Andy Hung2aa15102022-06-13 19:49:43 -0700253 onTimer(true /* timeout */, elapsedSteadyMs);
Andy Hung5c6d68a2022-03-09 21:54:59 -0800254 }
255
256 if (!crashOnTimeout) return;
Ytai Ben-Tsvi34f26b12021-12-02 13:58:38 -0800257
Andy Hunga2a1ac32022-03-18 16:12:11 -0700258 // Generate the TimerThread summary string early before sending signals to the
259 // HAL processes which can affect thread behavior.
Atneya Naircce14202022-09-05 20:17:50 -0700260 const auto snapshotAnalysis = getTimeCheckThread().getSnapshotAnalysis(4 /* retiredCount */);
Andy Hunga2a1ac32022-03-18 16:12:11 -0700261
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800262 // Generate audio HAL processes tombstones and allow time to complete
263 // before forcing restart
Andy Hung5c6d68a2022-03-09 21:54:59 -0800264 std::vector<pid_t> pids = TimeCheck::getAudioHalPids();
Jaideep Sharmaeaf9d9c2022-05-13 15:15:27 +0530265 std::string halPids = "HAL pids [ ";
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800266 if (pids.size() != 0) {
267 for (const auto& pid : pids) {
268 ALOGI("requesting tombstone for pid: %d", pid);
Jaideep Sharmaeaf9d9c2022-05-13 15:15:27 +0530269 halPids.append(std::to_string(pid)).append(" ");
Atneya Nairf5b68512022-05-23 20:02:49 -0400270 signalAudioHAL(pid);
Eric Laurent3528c932018-02-23 17:17:22 -0800271 }
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800272 sleep(1);
273 } else {
274 ALOGI("No HAL process pid available, skipping tombstones");
Eric Laurent39b09b52018-06-29 12:24:40 -0700275 }
Jaideep Sharmaeaf9d9c2022-05-13 15:15:27 +0530276 halPids.append("]");
Andy Hungf45f34c2022-03-25 13:09:03 -0700277
Andy Hung5c6d68a2022-03-09 21:54:59 -0800278 LOG_EVENT_STRING(LOGTAG_AUDIO_BINDER_TIMEOUT, tag.c_str());
Andy Hung10ac7112022-03-28 08:00:40 -0700279
280 // Create abort message string - caution: this can be very large.
281 const std::string abortMessage = std::string("TimeCheck timeout for ")
282 .append(tag)
Andy Hung2aa15102022-06-13 19:49:43 -0700283 .append(" scheduled ").append(formatTime(startSystemTime))
Andy Hung10ac7112022-03-28 08:00:40 -0700284 .append(" on thread ").append(std::to_string(tid)).append("\n")
Andy Hungf8ab0932022-06-13 19:49:43 -0700285 .append(analyzeTimeouts(requestedTimeoutMs + secondChanceMs,
286 elapsedSteadyMs, elapsedSystemMs)).append("\n")
Jaideep Sharmaeaf9d9c2022-05-13 15:15:27 +0530287 .append(halPids).append("\n")
Atneya Naircce14202022-09-05 20:17:50 -0700288 .append(snapshotAnalysis.toString());
Andy Hung10ac7112022-03-28 08:00:40 -0700289
Andy Hung4c544c32023-11-03 15:56:24 -0700290 // In many cases, the initial timeout stack differs from the abort backtrace because
291 // (1) the time difference between initial timeout and the final abort signal
292 // and (2) signalling the HAL audio service may cause
293 // the thread to unblock and continue.
294
Andy Hung10ac7112022-03-28 08:00:40 -0700295 // Note: LOG_ALWAYS_FATAL limits the size of the string - per log/log.h:
296 // Log message text may be truncated to less than an
297 // implementation-specific limit (1023 bytes).
298 //
299 // Here, we send the string through android-base/logging.h LOG()
300 // to avoid the size limitation. LOG(FATAL) does an abort whereas
301 // LOG(FATAL_WITHOUT_ABORT) does not abort.
302
Atneya Naircce14202022-09-05 20:17:50 -0700303 static constexpr pid_t invalidPid = TimerThread::SnapshotAnalysis::INVALID_PID;
304 pid_t tidToAbort = invalidPid;
305 if (snapshotAnalysis.suspectTid != invalidPid) {
306 tidToAbort = snapshotAnalysis.suspectTid;
307 } else if (snapshotAnalysis.timeoutTid != invalidPid) {
308 tidToAbort = snapshotAnalysis.timeoutTid;
309 }
310
311 LOG(FATAL_WITHOUT_ABORT) << abortMessage;
312 const auto ret = abortTid(tidToAbort);
313 if (ret < 0) {
314 LOG(FATAL) << "TimeCheck thread signal failed, aborting process. "
315 "errno: " << errno << base::ErrnoNumberAsString(errno);
316 }
Eric Laurent3528c932018-02-23 17:17:22 -0800317}
318
Andy Hung224f82f2022-03-22 00:00:49 -0700319// Automatically create a TimeCheck class for a class and method.
Mikhail Naganov31d46652023-01-10 18:29:25 +0000320// This is used for Audio HAL support.
Andy Hung224f82f2022-03-22 00:00:49 -0700321mediautils::TimeCheck makeTimeCheckStatsForClassMethod(
322 std::string_view className, std::string_view methodName) {
323 std::shared_ptr<MethodStatistics<std::string>> statistics =
324 mediautils::getStatisticsForClass(className);
325 if (!statistics) return {}; // empty TimeCheck.
326 return mediautils::TimeCheck(
Andy Hung35f96152022-07-15 15:18:59 -0700327 FixedString62(className).append("::").append(methodName),
328 [ safeMethodName = FixedString30(methodName),
Andy Hung224f82f2022-03-22 00:00:49 -0700329 stats = std::move(statistics) ]
330 (bool timeout, float elapsedMs) {
331 if (timeout) {
332 ; // ignored, there is no timeout value.
333 } else {
Andy Hung35f96152022-07-15 15:18:59 -0700334 stats->event(safeMethodName.asStringView(), elapsedMs);
Andy Hung224f82f2022-03-22 00:00:49 -0700335 }
Andy Hungf8ab0932022-06-13 19:49:43 -0700336 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hung224f82f2022-03-22 00:00:49 -0700337}
338
Andy Hung5c6d68a2022-03-09 21:54:59 -0800339} // namespace android::mediautils