Watchdog 會監控供應商服務和 VHAL 服務的健康狀態,並終止任何健康狀態不良的程序。當不正常的程序終止時,Watchdog 會將程序狀態傾印至 /data/anr
,就像其他「應用程式無回應」(ANR) 傾印一樣。這樣做有助於偵錯程序。
監控供應商服務健康狀態
系統會在本機和 Java 端監控供應商服務。如要監控供應商服務,服務必須向 Watchdog 註冊健康狀態檢查程序,並指定預先定義的逾時時間。監控程式會以相對於註冊期間指定逾時的間隔,對已註冊的健康狀態檢查程序執行 Ping 作業,藉此監控該程序的健康狀態。如果遭 Ping 的程序未在逾時時間內回應,系統就會將該程序視為健康狀態不良。
原生服務健康狀態監控
指定 Watchdog AIDL makefile
- 在
shared_libs
中加入carwatchdog_aidl_interface-ndk_platform
。Android.bp
cc_binary { name: "sample_native_client", srcs: [ "src/*.cpp" ], shared_libs: [ "carwatchdog_aidl_interface-ndk_platform", "libbinder_ndk", ], vendor: true, }
新增 SELinux 政策
- 如要新增 SELinux 政策,請允許供應商服務網域使用繫結器 (
binder_use
巨集),並將供應商服務網域新增至carwatchdog
用戶端網域 (carwatchdog_client_domain
巨集)。 請參閱下方的sample_client.te
和file_contexts
程式碼:sample_client.te
type sample_client, domain; type sample_client_exec, exec_type, file_type, vendor_file_type; carwatchdog_client_domain(sample_client) init_daemon_domain(sample_client) binder_use(sample_client)
file_contexts
/vendor/bin/sample_native_client u:object_r:sample_client_exec:s0
透過繼承 BnCarWatchdogClient 實作用戶端類別
- 在
checkIfAlive
中執行健康狀態檢查。其中一種做法是將訊息發布至執行緒迴圈處理常式。如果健康狀態良好,請呼叫ICarWatchdog::tellClientAlive
。 請參閱下方的SampleNativeClient.h
和SampleNativeClient.cpp
程式碼:SampleNativeClient.h
class SampleNativeClient : public BnCarWatchdogClient { public: ndk::ScopedAStatus checkIfAlive(int32_t sessionId, TimeoutLength timeout) override; ndk::ScopedAStatus prepareProcessTermination() override; void initialize(); private: void respondToDaemon(); private: ::android::sp<::android::Looper> mHandlerLooper; std::shared_ptr<ICarWatchdog> mWatchdogServer; std::shared_ptr<ICarWatchdogClient> mClient; int32_t mSessionId; };
SampleNativeClient.cpp
ndk::ScopedAStatus WatchdogClient::checkIfAlive(int32_t sessionId, TimeoutLength timeout) { mHandlerLooper->removeMessages(mMessageHandler, WHAT_CHECK_ALIVE); mSessionId = sessionId; mHandlerLooper->sendMessage(mMessageHandler, Message(WHAT_CHECK_ALIVE)); return ndk::ScopedAStatus::ok(); } // WHAT_CHECK_ALIVE triggers respondToDaemon from thread handler void WatchdogClient::respondToDaemon() { // your health checking method here ndk::ScopedAStatus status = mWatchdogServer->tellClientAlive(mClient, mSessionId); }
啟動繫結器執行緒並註冊用戶端
車輛監控計時器精靈介面名稱為 android.automotive.watchdog.ICarWatchdog/default
。
- 搜尋具有該名稱的精靈,並呼叫
ICarWatchdog::registerClient
。 請參閱下方的main.cpp
和SampleNativeClient.cpp
程式碼:main.cpp
int main(int argc, char** argv) { sp<Looper> looper(Looper::prepare(/*opts=*/0)); ABinderProcess_setThreadPoolMaxThreadCount(1); ABinderProcess_startThreadPool(); std::shared_ptr<SampleNativeClient> client = ndk::SharedRefBase::make<SampleNatvieClient>(looper); // The client is registered in initialize() client->initialize(); ... }
SampleNativeClient.cpp
void SampleNativeClient::initialize() { ndk::SpAIBinder binder(AServiceManager_getService( "android.automotive.watchdog.ICarWatchdog/default")); std::shared_ptr<ICarWatchdog> server = ICarWatchdog::fromBinder(binder); mWatchdogServer = server; ndk::SpAIBinder binder = this->asBinder(); std::shared_ptr<ICarWatchdogClient> client = ICarWatchdogClient::fromBinder(binder) mClient = client; server->registerClient(client, TimeoutLength::TIMEOUT_NORMAL); }
Java 服務健康狀態監控
透過繼承 CarWatchdogClientCallback 實作用戶端
- 按照下列方式編輯新檔案:
private final CarWatchdogClientCallback mClientCallback = new CarWatchdogClientCallback() { @Override public boolean onCheckHealthStatus(int sessionId, int timeout) { // Your health check logic here // Returning true implies the client is healthy // If false is returned, the client should call // CarWatchdogManager.tellClientAlive after health check is // completed } @Override public void onPrepareProcessTermination() {} };
註冊用戶端
- 撥打
CarWatchdogManager.registerClient()
:private void startClient() { CarWatchdogManager manager = (CarWatchdogManager) car.getCarManager( Car.CAR_WATCHDOG_SERVICE); // Choose a proper executor according to your health check method ExecutorService executor = Executors.newFixedThreadPool(1); manager.registerClient(executor, mClientCallback, CarWatchdogManager.TIMEOUT_NORMAL); }
取消註冊用戶端
- 服務完成時,請呼叫
CarWatchdogManager.unregisterClient()
:private void finishClient() { CarWatchdogManager manager = (CarWatchdogManager) car.getCarManager( Car.CAR_WATCHDOG_SERVICE); manager.unregisterClient(mClientCallback); }
VHAL 健康狀態監控
與供應商服務健康狀態監控不同,Watchdog 會訂閱 VHAL_HEARTBEAT
車輛屬性,監控 VHAL 服務健康狀態。Watchdog 預期每 N 秒更新一次這個屬性的值。如果未在逾時時間內更新心跳訊號,Watchdog 會終止 VHAL 服務。
注意:只有在 VHAL 服務支援 VHAL_HEARTBEAT
車輛屬性時,Watchdog 才會監控 VHAL 服務健康狀態。
VHAL 內部實作方式可能因供應商而異。請參考下列程式碼範例。
- 註冊
VHAL_HEARTBEAT
車輛屬性。啟動 VHAL 服務時,請註冊
VHAL_HEARTBEAT
車輛屬性。在以下範例中,unordered_map
(將資源 ID 對應至設定) 用於保留所有支援的設定。VHAL_HEARTBEAT
的設定會新增至地圖,因此查詢VHAL_HEARTBEAT
時,系統會傳回對應的設定。void registerVhalHeartbeatProperty() { const VehiclePropConfig config = { .prop = toInt(VehicleProperty::VHAL_HEARTBEAT), .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, }; // mConfigsById is declared as std::unordered_map<int32_t, VehiclePropConfig>. mConfigsById[config.prop] = config; }
- 更新
VHAL_HEARTBEAT
車輛屬性。根據 VHAL 健康狀態檢查頻率 (請參閱「定義 VHAL 健康狀態檢查的頻率」),每 N 秒更新一次
VHAL_HEARTBEAT
車輛屬性。其中一種方法是使用RecurrentTimer
呼叫動作,檢查 VHAL 健康狀態並在逾時前更新VHAL_HEARTBEAT
車輛屬性。以下是使用
RecurrentTimer
的導入範例:int main(int argc, char** argv) { RecurrentTimer recurrentTimer(updateVhalHeartbeat); recurrentTimer.registerRecurrentEvent(kHeartBeatIntervalNs, static_cast<int32_t>(VehicleProperty::VHAL_HEARTBEAT)); … Run service … recurrentTimer.unregisterRecurrentEvent( static_cast<int32_t>(VehicleProperty::VHAL_HEARTBEAT)); } void updateVhalHeartbeat(const std::vector<int32_t>& cookies) { for (int32_t property : cookies) { if (property != static_cast<int32_t>(VehicleProperty::VHAL_HEARTBEAT)) { continue; } // Perform internal health checking such as retrieving a vehicle property to ensure // the service is responsive. doHealthCheck(); // Construct the VHAL_HEARTBEAT property with system uptime. VehiclePropValuePool valuePool; VehicleHal::VehiclePropValuePtr propValuePtr = valuePool.obtainInt64(uptimeMillis()); propValuePtr->prop = static_cast<int32_t>(VehicleProperty::VHAL_HEARTBEAT); propValuePtr->areaId = 0; propValuePtr->status = VehiclePropertyStatus::AVAILABLE; propValuePtr->timestamp = elapsedRealtimeNano(); // Propagate the HAL event. onHalEvent(std::move(propValuePtr)); } }
- (選用) 定義 VHAL 健康狀態檢查的頻率。
Watchdog 的
ro.carwatchdog.vhal_healthcheck.interval
唯讀產品屬性會定義 VHAL 健康狀態檢查頻率。如果未定義這項屬性,預設健康狀態檢查頻率為三秒。如果 VHAL 服務無法在三秒內更新VHAL_HEARTBEAT
車輛屬性,請根據服務回應速度定義 VHAL 健康狀態檢查頻率。
偵錯 Watchdog 終止的健康狀態不良程序
看門狗會傾印程序狀態,並終止健康狀態不良的程序。終止健康狀態不良的程序時,Watchdog 會將 carwatchdog terminated
<process name> (pid:<process id>)
文字記錄到 Logcat。這行記錄提供終止程序相關資訊,例如程序名稱和程序 ID。
- 執行下列指令,即可在 logcat 中搜尋上述文字:
$ adb logcat -s CarServiceHelper | fgrep "carwatchdog killed"
舉例來說,如果 KitchenSink 應用程式是已註冊的 Watchdog 用戶端,且對 Watchdog Ping 沒有回應,Watchdog 就會在終止已註冊的 KitchenSink 程序時,記錄類似下列的行。
05-01 09:50:19.683 578 5777 W CarServiceHelper: carwatchdog killed com.google.android.car.kitchensink (pid: 5574)
- 如要找出無回應的根本原因,請使用儲存在
/data/anr
的程序傾印,就像處理活動 ANR 案例一樣。如要擷取已終止程序的傾印檔案,請使用下列指令。$ adb root $ adb shell grep -Hn "pid process_pid" /data/anr/*
以下範例輸出內容適用於 KitchenSink 應用程式:
$ adb shell su root grep -Hn "pid 5574" /data/anr/*.
/data/anr/anr_2020-05-01-09-50-18-290:3:----- pid 5574 at 2020-05-01 09:50:18 ----- /data/anr/anr_2020-05-01-09-50-18-290:285:----- Waiting Channels: pid 5574 at 2020-05-01 09:50:18 -----
已終止的 KitchenSink 程序傾印檔案位於
/data/anr/anr_2020-05-01-09-50-18-290
。使用終止程序的 ANR 傾印檔案開始分析。