blob: 4a3e470e05318dd3746cbccf48b594de1d240c00 [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
Eric Laurentb680e952019-09-27 15:40:33 -070017#define LOG_TAG "TimeCheck"
Eric Laurent3528c932018-02-23 17:17:22 -080018
Eric Laurent39b09b52018-06-29 12:24:40 -070019#include <utils/Log.h>
Marco Nelissen3230ed82019-09-26 11:20:54 -070020#include <mediautils/TimeCheck.h>
21#include <mediautils/EventLog.h>
Eric Laurentb680e952019-09-27 15:40:33 -070022#include "debuggerd/handler.h"
Eric Laurent3528c932018-02-23 17:17:22 -080023
24namespace android {
25
Eric Laurentb680e952019-09-27 15:40:33 -070026// Audio HAL server pids vector used to generate audio HAL processes tombstone
27// when audioserver watchdog triggers.
28// We use a lockless storage to avoid potential deadlocks in the context of watchdog
29// trigger.
30// Protection again simultaneous writes is not needed given one update takes place
31// during AudioFlinger construction and other comes necessarily later once the IAudioFlinger
32// interface is available.
33// The use of an atomic index just guaranties that current vector is fully initialized
34// when read.
35/* static */
36void TimeCheck::accessAudioHalPids(std::vector<pid_t>* pids, bool update) {
37 static constexpr int kNumAudioHalPidsVectors = 3;
38 static std::vector<pid_t> audioHalPids[kNumAudioHalPidsVectors];
39 static std::atomic<int> curAudioHalPids = 0;
40
41 if (update) {
42 audioHalPids[(curAudioHalPids + 1) % kNumAudioHalPidsVectors] = *pids;
43 curAudioHalPids++;
44 } else {
45 *pids = audioHalPids[curAudioHalPids];
46 }
47}
48
49/* static */
50void TimeCheck::setAudioHalPids(const std::vector<pid_t>& pids) {
51 accessAudioHalPids(&(const_cast<std::vector<pid_t>&>(pids)), true);
52}
53
54/* static */
55std::vector<pid_t> TimeCheck::getAudioHalPids() {
56 std::vector<pid_t> pids;
57 accessAudioHalPids(&pids, false);
58 return pids;
59}
60
Eric Laurent3528c932018-02-23 17:17:22 -080061/* static */
62sp<TimeCheck::TimeCheckThread> TimeCheck::getTimeCheckThread()
63{
64 static sp<TimeCheck::TimeCheckThread> sTimeCheckThread = new TimeCheck::TimeCheckThread();
65 return sTimeCheckThread;
66}
67
68TimeCheck::TimeCheck(const char *tag, uint32_t timeoutMs)
69 : mEndTimeNs(getTimeCheckThread()->startMonitoring(tag, timeoutMs))
70{
71}
72
73TimeCheck::~TimeCheck() {
74 getTimeCheckThread()->stopMonitoring(mEndTimeNs);
75}
76
77TimeCheck::TimeCheckThread::~TimeCheckThread()
78{
79 AutoMutex _l(mMutex);
80 requestExit();
81 mMonitorRequests.clear();
82 mCond.signal();
83}
84
85nsecs_t TimeCheck::TimeCheckThread::startMonitoring(const char *tag, uint32_t timeoutMs) {
86 Mutex::Autolock _l(mMutex);
87 nsecs_t endTimeNs = systemTime() + milliseconds(timeoutMs);
88 for (; mMonitorRequests.indexOfKey(endTimeNs) >= 0; ++endTimeNs);
89 mMonitorRequests.add(endTimeNs, tag);
90 mCond.signal();
91 return endTimeNs;
92}
93
94void TimeCheck::TimeCheckThread::stopMonitoring(nsecs_t endTimeNs) {
95 Mutex::Autolock _l(mMutex);
96 mMonitorRequests.removeItem(endTimeNs);
97 mCond.signal();
98}
99
100bool TimeCheck::TimeCheckThread::threadLoop()
101{
102 status_t status = TIMED_OUT;
103 const char *tag;
104 {
105 AutoMutex _l(mMutex);
106
107 if (exitPending()) {
108 return false;
109 }
110
111 nsecs_t endTimeNs = INT64_MAX;
112 // KeyedVector mMonitorRequests is ordered so take first entry as next timeout
113 if (mMonitorRequests.size() != 0) {
114 endTimeNs = mMonitorRequests.keyAt(0);
115 tag = mMonitorRequests.valueAt(0);
116 }
117
118 const nsecs_t waitTimeNs = endTimeNs - systemTime();
119 if (waitTimeNs > 0) {
120 status = mCond.waitRelative(mMutex, waitTimeNs);
121 }
Eric Laurent9e1e25d2019-10-14 18:47:34 -0700122 if (status != NO_ERROR) {
Eric Laurentb680e952019-09-27 15:40:33 -0700123 // Generate audio HAL processes tombstones and allow time to complete
124 // before forcing restart
125 std::vector<pid_t> pids = getAudioHalPids();
126 if (pids.size() != 0) {
127 for (const auto& pid : pids) {
128 ALOGI("requesting tombstone for pid: %d", pid);
129 sigqueue(pid, DEBUGGER_SIGNAL, {.sival_int = 0});
130 }
131 sleep(1);
132 } else {
133 ALOGI("No HAL process pid available, skipping tombstones");
134 }
Eric Laurent9e1e25d2019-10-14 18:47:34 -0700135 LOG_EVENT_STRING(LOGTAG_AUDIO_BINDER_TIMEOUT, tag);
136 LOG_ALWAYS_FATAL("TimeCheck timeout for %s", tag);
137 }
Eric Laurent39b09b52018-06-29 12:24:40 -0700138 }
Eric Laurent3528c932018-02-23 17:17:22 -0800139 return true;
140}
141
142}; // namespace android