監控系統健康狀態

監控器會監控供應商服務和 VHAL 服務的健康狀態,並終止任何不健康的程序。當不健康的程序終止時,Watchdog 會將處理程序狀態轉儲至 /data/anr,就像其他應用程式無回應 (ANR) 轉儲作業一樣。這麼做有助於偵錯程序。

供應商服務健康狀態監控

供應商服務會在原生和 Java 端監控。如要監控供應商服務,該服務必須指定預先定義的逾時時間,並向 Watchdog 註冊健康檢查程序。監視器會以註冊期間指定的逾時時間間隔,對註冊的健康檢查程序執行 Ping 作業,以監控該程序的健康狀態。如果被執行 ICMP 的程序未在逾時期間內回應,系統就會將該程序視為不良。

原生服務健康狀態監控

指定 Watchdog AIDL makefile

  1. 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 政策

  1. 如要新增 SELinux 政策,請允許供應商服務網域使用 Binder (binder_use 巨集),並將供應商服務網域新增至 carwatchdog 用戶端網域 (carwatchdog_client_domain 巨集)。請參閱下方 sample_client.tefile_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

  1. checkIfAlive 中執行健康狀態檢查。您可以選擇發布至執行緒迴圈處理常式。如果正常運作,請呼叫 ICarWatchdog::tellClientAlive。請參閱下方 SampleNativeClient.hSampleNativeClient.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 執行緒並註冊用戶端

車輛監控計時器守護程式介面名稱為 android.automotive.watchdog.ICarWatchdog/default

  1. 使用名稱搜尋 Daemon,並呼叫 ICarWatchdog::registerClient。請參閱下方 main.cppSampleNativeClient.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 來實作用戶端

  1. 按照下列方式編輯新檔案:
    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() {}
    };

註冊用戶端

  1. 撥打 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);
    }

取消註冊用戶端

  1. 在服務完成後呼叫 CarWatchdogManager.unregisterClient()
    private void finishClient() {
       
    CarWatchdogManager manager =
           
    (CarWatchdogManager) car.getCarManager(
           
    Car.CAR_WATCHDOG_SERVICE);
        manager
    .unregisterClient(mClientCallback);
    }

VHAL 健康狀態監控

與廠商服務健康狀態監控不同,Watchdog 會訂閱 VHAL_HEARTBEAT 車輛屬性來監控 VHAL 服務健康狀態。監控器預期這個屬性的值每 N 秒更新一次。如果心跳未在逾時期限內更新,監視器就會終止 VHAL 服務。

注意:只有在 VHAL 服務支援 VHAL_HEARTBEAT 車輛屬性時,監控器才會監控 VHAL 服務的健康狀況。

VHAL 內部實作因供應商而異。請使用下列程式碼範例做為參照。

  1. 註冊 VHAL_HEARTBEAT 車輛屬性。

    啟動 VHAL 服務時,請註冊 VHAL_HEARTBEAT 車輛屬性。在以下範例中,unordered_map 會將資源 ID 對應至設定,用於儲存所有支援的設定。VHAL_HEARTBEAT 的設定會新增至對應的 map,因此在查詢 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;
    }
  2. 更新 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));
           
    }
    }
  3. (選用) 定義 VHAL 健康狀態檢查的頻率。

    監視器的 ro.carwatchdog.vhal_healthcheck.interval 唯讀產品屬性會定義 VHAL 健康狀態檢查頻率。預設的健康狀態檢查頻率 (未定義此屬性時) 為三秒。如果三秒不足以讓 VHAL 服務更新 VHAL_HEARTBEAT 車輛屬性,請根據服務反應定義 VHAL 健康狀態檢查頻率。

偵錯 Watchdog 終止的不良程序

監視器會傾印程序狀態,並終止不健康的程序。終止健康狀態不良的程序時,Watchdog 會將文字 carwatchdog terminated <process name> (pid:<process id>) 記錄到 logcat。這個記錄行會提供已終止程序的相關資訊,例如程序名稱和程序 ID。

  1. 您可以執行以下命令,在 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)
  2. 如要找出無回應的根本原因,請使用儲存在 /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 快照檔案開始分析。