blob: f1d572f9097bff34fdb1a13d9ccff8b0caa1cf64 [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
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080017#pragma once
Eric Laurent3528c932018-02-23 17:17:22 -080018
Andy Hungf8ab0932022-06-13 19:49:43 -070019#include <chrono>
Eric Laurent42896a02019-09-27 15:40:33 -070020#include <vector>
Eric Laurent3528c932018-02-23 17:17:22 -080021
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080022#include <mediautils/TimerThread.h>
23
Andy Hung5c6d68a2022-03-09 21:54:59 -080024namespace android::mediautils {
Eric Laurent3528c932018-02-23 17:17:22 -080025
26// A class monitoring execution time for a code block (scoped variable) and causing an assert
27// if it exceeds a certain time
28
29class TimeCheck {
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080030 public:
Andy Hung2aa15102022-06-13 19:49:43 -070031
32 // Duration for TimeCheck is based on steady_clock, typically nanoseconds.
33 using Duration = std::chrono::steady_clock::duration;
34
35 // Duration for printing is in milliseconds, using float for additional precision.
36 using FloatMs = std::chrono::duration<float, std::milli>;
37
38 // OnTimerFunc is the callback function with 2 parameters.
39 // bool timeout (which is true when the TimeCheck object
40 // times out, false when the TimeCheck object is
41 // destroyed or leaves scope before the timer expires.)
42 // float elapsedMs (the elapsed time to this event).
Andy Hung5c6d68a2022-03-09 21:54:59 -080043 using OnTimerFunc = std::function<void(bool /* timeout */, float /* elapsedMs */ )>;
44
Eric Laurent3528c932018-02-23 17:17:22 -080045 // The default timeout is chosen to be less than system server watchdog timeout
Andy Hung2aa15102022-06-13 19:49:43 -070046 // Note: kDefaultTimeOutMs should be no less than 2 seconds, otherwise spurious timeouts
47 // may occur with system suspend.
Andy Hungf8ab0932022-06-13 19:49:43 -070048 static constexpr TimeCheck::Duration kDefaultTimeoutDuration = std::chrono::milliseconds(3000);
49
50 // Due to suspend abort not incrementing the monotonic clock,
51 // we allow another second chance timeout after the first timeout expires.
52 //
53 // The total timeout is therefore kDefaultTimeoutDuration + kDefaultSecondChanceDuration,
54 // and the result is more stable when the monotonic clock increments during suspend.
55 //
56 static constexpr TimeCheck::Duration kDefaultSecondChanceDuration =
57 std::chrono::milliseconds(2000);
Eric Laurent3528c932018-02-23 17:17:22 -080058
Andy Hung5c6d68a2022-03-09 21:54:59 -080059 /**
60 * TimeCheck is a RAII object which will notify a callback
61 * on timer expiration or when the object is deallocated.
62 *
63 * TimeCheck is used as a watchdog and aborts by default on timer expiration.
64 * When it aborts, it will also send a debugger signal to pids passed in through
65 * setAudioHalPids().
66 *
67 * If the callback function returns for timeout it will not be called again for
68 * the deallocation.
69 *
70 * \param tag string associated with the TimeCheck object.
Andy Hung2aa15102022-06-13 19:49:43 -070071 * \param onTimer callback function with 2 parameters (described above in OnTimerFunc).
Andy Hung5c6d68a2022-03-09 21:54:59 -080072 * The callback when timeout is true will be called on a different thread.
Andy Hunga2a1ac32022-03-18 16:12:11 -070073 * This will cancel the callback on the destructor but is not guaranteed
74 * to block for callback completion if it is already in progress
75 * (for maximum concurrency and reduced deadlock potential), so use proper
76 * lifetime analysis (e.g. shared or weak pointers).
Andy Hungf8ab0932022-06-13 19:49:43 -070077 * \param requestedTimeoutDuration timeout in milliseconds.
Andy Hunga2a1ac32022-03-18 16:12:11 -070078 * A zero timeout means no timeout is set -
79 * the callback is called only when
80 * the TimeCheck object is destroyed or leaves scope.
Andy Hungf8ab0932022-06-13 19:49:43 -070081 * \param secondChanceDuration additional milliseconds to wait if the first timeout expires.
82 * This is used to prevent false timeouts if the steady (monotonic)
83 * clock advances on aborted suspend.
Andy Hung5c6d68a2022-03-09 21:54:59 -080084 * \param crashOnTimeout true if the object issues an abort on timeout.
85 */
Andy Hungf8ab0932022-06-13 19:49:43 -070086 explicit TimeCheck(std::string_view tag, OnTimerFunc&& onTimer,
87 Duration requestedTimeoutDuration, Duration secondChanceDuration,
88 bool crashOnTimeout);
Andy Hunga2a1ac32022-03-18 16:12:11 -070089
90 TimeCheck() = default;
Andy Hung5c6d68a2022-03-09 21:54:59 -080091 // Remove copy constructors as there should only be one call to the destructor.
92 // Move is kept implicitly disabled, but would be logically consistent if enabled.
93 TimeCheck(const TimeCheck& other) = delete;
94 TimeCheck& operator=(const TimeCheck&) = delete;
95
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080096 ~TimeCheck();
Andy Hunga2a1ac32022-03-18 16:12:11 -070097 static std::string toString();
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -080098 static void setAudioHalPids(const std::vector<pid_t>& pids);
99 static std::vector<pid_t> getAudioHalPids();
Eric Laurent3528c932018-02-23 17:17:22 -0800100
Ytai Ben-Tsvi1ea62c92021-11-10 14:38:27 -0800101 private:
Andy Hung5c6d68a2022-03-09 21:54:59 -0800102 // Helper class for handling events.
103 // The usage here is const safe.
104 class TimeCheckHandler {
105 public:
Andy Hung35f96152022-07-15 15:18:59 -0700106 template <typename S, typename F>
107 TimeCheckHandler(S&& _tag, F&& _onTimer, bool _crashOnTimeout,
Andy Hungf8ab0932022-06-13 19:49:43 -0700108 Duration _timeoutDuration, Duration _secondChanceDuration,
Andy Hung2aa15102022-06-13 19:49:43 -0700109 std::chrono::system_clock::time_point _startSystemTime,
Andy Hung35f96152022-07-15 15:18:59 -0700110 pid_t _tid)
111 : tag(std::forward<S>(_tag))
112 , onTimer(std::forward<F>(_onTimer))
113 , crashOnTimeout(_crashOnTimeout)
Andy Hung2aa15102022-06-13 19:49:43 -0700114 , timeoutDuration(_timeoutDuration)
Andy Hungf8ab0932022-06-13 19:49:43 -0700115 , secondChanceDuration(_secondChanceDuration)
Andy Hung2aa15102022-06-13 19:49:43 -0700116 , startSystemTime(_startSystemTime)
Andy Hung35f96152022-07-15 15:18:59 -0700117 , tid(_tid)
118 {}
119 const FixedString62 tag;
Andy Hung5c6d68a2022-03-09 21:54:59 -0800120 const OnTimerFunc onTimer;
121 const bool crashOnTimeout;
Andy Hung2aa15102022-06-13 19:49:43 -0700122 const Duration timeoutDuration;
Andy Hungf8ab0932022-06-13 19:49:43 -0700123 const Duration secondChanceDuration;
Andy Hung2aa15102022-06-13 19:49:43 -0700124 const std::chrono::system_clock::time_point startSystemTime;
Andy Hung5c6d68a2022-03-09 21:54:59 -0800125 const pid_t tid;
Andy Hung5c6d68a2022-03-09 21:54:59 -0800126 void onCancel(TimerThread::Handle handle) const;
Andy Hung2aa15102022-06-13 19:49:43 -0700127 void onTimeout(TimerThread::Handle handle) const;
Andy Hung5c6d68a2022-03-09 21:54:59 -0800128 };
129
Andy Hung2aa15102022-06-13 19:49:43 -0700130 // Returns a string that represents the timeout vs elapsed time,
131 // and diagnostics if there are any potential issues.
132 static std::string analyzeTimeouts(
133 float timeoutMs, float elapsedSteadyMs, float elapsedSystemMs);
134
Andy Hunga2a1ac32022-03-18 16:12:11 -0700135 static TimerThread& getTimeCheckThread();
136 static void accessAudioHalPids(std::vector<pid_t>* pids, bool update);
137
Andy Hung5c6d68a2022-03-09 21:54:59 -0800138 // mTimeCheckHandler is immutable, prefer to be first initialized, last destroyed.
139 // Technically speaking, we do not need a shared_ptr here because TimerThread::cancelTask()
140 // is mutually exclusive of the callback, but the price paid for lifetime safety is minimal.
141 const std::shared_ptr<const TimeCheckHandler> mTimeCheckHandler;
Andy Hunga2a1ac32022-03-18 16:12:11 -0700142 const TimerThread::Handle mTimerHandle = TimerThread::INVALID_HANDLE;
Eric Laurent3528c932018-02-23 17:17:22 -0800143};
144
Andy Hung224f82f2022-03-22 00:00:49 -0700145// Returns a TimeCheck object that sends info to MethodStatistics
146// obtained from getStatisticsForClass(className).
147TimeCheck makeTimeCheckStatsForClassMethod(
148 std::string_view className, std::string_view methodName);
149
Mikhail Naganov31d46652023-01-10 18:29:25 +0000150// A handy statement-like macro to put at the beginning of almost every method
151// which calls into HAL. Note that it requires the class to implement 'getClassName'.
152#define TIME_CHECK() auto timeCheck = \
153 mediautils::makeTimeCheckStatsForClassMethod(getClassName(), __func__)
154
Andy Hung5c6d68a2022-03-09 21:54:59 -0800155} // namespace android::mediautils