Watchdog 监控供应商服务和 VHAL 服务的运行状况,并终止任何运行状况不佳的进程。当运行状况不佳的进程被终止时,Watchdog 会将进程状态转储到 /data/anr
,就像其他应用程序无响应 (ANR) 转储一样。这样做有助于调试过程。
供应商服务运行状况监控
供应商服务在原生端和 Java 端都受到监控。要监控供应商服务,该服务必须通过指定预定义的超时时间在 Watchdog 中注册运行状况检查进程。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(
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); }
启动 binder 线程并注册客户端
汽车 watchdog 守护进程接口名称为 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 转储进程状态并终止运行状况不佳的进程。在终止运行状况不佳的进程时,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 转储文件开始分析。