車輛監控計時器

使用車用監控計時器協助偵錯 VHAL。Car Watchdog 會監控不良程序的健康狀態,並終止這些程序。如要讓程序受到車輛監控計時器監控,該程序必須向車輛監控計時器註冊。當車輛監控計時器終止健康狀態不良的程序時,車輛監控計時器會和其他應用程式無回應 (ANR) 傾印資料一樣,將程序的狀態寫入 data/anr。這樣做會讓偵錯程序更為完善。

本文說明廠商 HAL 和服務如何向車輛監控計時器註冊程序。

供應商 HAL

通常,供應商 HAL 會為 hwbinder 使用執行緒集區。不過,車輛監控計時器用戶端會透過 binder 與車輛監控計時器 Daemon 通訊,這與 hwbinder 不同。因此,另一個 binder 執行緒集區正在使用中。

在 makefile 中指定車輛監控計時器 aidl

  1. shared_libs 中加入 carwatchdog_aidl_interface-ndk_platform

    Android.bp

    cc_defaults {
        name: "vhal_v2_0_defaults",
        shared_libs: [
            "libbinder_ndk",
            "libhidlbase",
            "liblog",
            "libutils",
            "android.hardware.automotive.vehicle@2.0",
            "carwatchdog_aidl_interface-ndk_platform",
        ],
        cflags: [
            "-Wall",
            "-Wextra",
            "-Werror",
        ],
    }
    

新增 SELinux 政策

  1. 允許 system_server 終止 HAL。 如果沒有 system_server.te,請建立一個。強烈建議您為每部裝置新增 SELinux 政策。
  2. 允許供應商 HAL 使用 Binder (binder_use 巨集),並將供應商 HAL 新增至 carwatchdog 用戶端網域 (carwatchdog_client_domain 巨集)。請參閱下列 systemserver.tevehicle_default.te 的程式碼:

    system_server.te

    # Allow system_server to kill vehicle HAL
    allow system_server hal_vehicle_server:process sigkill;
    

    hal_vehicle_default.te

    # Configuration for register VHAL to car watchdog
    carwatchdog_client_domain(hal_vehicle_default)
    binder_use(hal_vehicle_default)
    

繼承 BnCarWatchdogClient 來實作用戶端類別

  1. checkIfAlive 中執行健康狀態檢查。例如發布至執行緒迴圈處理常式。如果正常運作,請呼叫 ICarWatchdog::tellClientAlive。請參閱下列 WatchogClient.hWatchogClient.cpp 的程式碼:

    WatchogClient.h

    class WatchdogClient : public aidl::android::automotive::watchdog::BnCarWatchdogClient {
      public:
        explicit WatchdogClient(const ::android::sp<::android::Looper>& handlerLooper, VehicleHalManager* vhalManager);
    
    ndk::ScopedAStatus checkIfAlive(int32_t sessionId, aidl::android::automotive::watchdog::TimeoutLength timeout) override; ndk::ScopedAStatus prepareProcessTermination() override; };

    WatchogClient.cpp

    ndk::ScopedAStatus WatchdogClient::checkIfAlive(int32_t sessionId, TimeoutLength /*timeout*/) {
        // Implement or call your health check logic here
        return ndk::ScopedAStatus::ok();
    }
    

啟動繫結器執行緒並註冊用戶端

  1. 建立執行緒集區,用於 Binder 通訊。如果供應商 HAL 將 hwbinder 用於自身用途,您必須為汽車監控機制繫結程式通訊建立另一個執行緒集區。
  2. 搜尋名稱為 daemon 的服務,並呼叫 ICarWatchdog::registerClient。車輛監控計時器守護程序介面名稱為 android.automotive.watchdog.ICarWatchdog/default
  3. 根據服務回應速度,選取車輛監控器支援的下列三種超時類型之一,然後在呼叫中傳遞超時值至 ICarWatchdog::registerClient
    • 重要(3 秒)
    • 中等(5 秒)
    • normal(10s)
    請參閱下方 VehicleService.cppWatchogClient.cpp 的程式碼:

    VehicleService.cpp (車輛服務)

    int main(int /* argc */, char* /* argv */ []) {
        // Set up thread pool for hwbinder
        configureRpcThreadpool(4, false /* callerWillJoin */);
    
        ALOGI("Registering as service...");
        status_t status = service->registerAsService();
    
        if (status != OK) {
            ALOGE("Unable to register vehicle service (%d)", status);
            return 1;
        }
    
        // Setup a binder thread pool to be a car watchdog client.
        ABinderProcess_setThreadPoolMaxThreadCount(1);
        ABinderProcess_startThreadPool();
        sp<Looper> looper(Looper::prepare(0 /* opts */));
        std::shared_ptr<WatchdogClient> watchdogClient =
                ndk::SharedRefBase::make<WatchdogClient>(looper, service.get());
        // The current health check is done in the main thread, so it falls short of capturing the real
        // situation. Checking through HAL binder thread should be considered.
        if (!watchdogClient->initialize()) {
            ALOGE("Failed to initialize car watchdog client");
            return 1;
        }
        ALOGI("Ready");
        while (true) {
            looper->pollAll(-1 /* timeoutMillis */);
        }
    
        return 1;
    }
    

    WatchogClient.cpp

    bool WatchdogClient::initialize() {
        ndk::SpAIBinder binder(AServiceManager_getService("android.automotive.watchdog.ICarWatchdog/default"));
        if (binder.get() == nullptr) {
            ALOGE("Failed to get carwatchdog daemon");
            return false;
        }
        std::shared_ptr<ICarWatchdog> server = ICarWatchdog::fromBinder(binder);
        if (server == nullptr) {
            ALOGE("Failed to connect to carwatchdog daemon");
            return false;
        }
        mWatchdogServer = server;
    
        binder = this->asBinder();
        if (binder.get() == nullptr) {
            ALOGE("Failed to get car watchdog client binder object");
            return false;
        }
        std::shared_ptr<ICarWatchdogClient> client = ICarWatchdogClient::fromBinder(binder);
        if (client == nullptr) {
            ALOGE("Failed to get ICarWatchdogClient from binder");
            return false;
        }
        mTestClient = client;
        mWatchdogServer->registerClient(client, TimeoutLength::TIMEOUT_NORMAL);
        ALOGI("Successfully registered the client to car watchdog server");
        return true;
    }
    

供應商服務 (原生)

指定車輛監控狗的輔助 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 執行緒並註冊用戶端

車輛監控計時器 Daemon 介面名稱為 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);
    }
    

供應商服務 (Android)

繼承 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);
    }
    

偵測由車輛監控計時器終止的程序

車輛監控計時器在卡車卡住或無回應時,註冊於車輛監控計時器的傾印/終止程序 (供應商 HAL、供應商原生服務、供應商 Android 服務)。檢查 logcat 即可偵測到這類傾印。當有問題的處理程序遭到傾印或終止時,車輛監視器會輸出記錄 carwatchdog killed process_name (pid:process_id)。因此:

$ adb logcat -s CarServiceHelper | fgrep "carwatchdog killed"

系統會擷取相關記錄。舉例來說,如果 KitchenSink 應用程式 (車輛監控程式用戶端) 停止運作,系統會將類似下方的行寫入記錄檔:

05-01 09:50:19.683   578  5777 W CarServiceHelper: carwatchdog killed com.google.android.car.kitchensink (pid: 5574)

如要判斷 KitchenSink 應用程式發生卡頓的原因或位置,請使用儲存在 /data/anr 的程序傾印,就像使用 Activity 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 -----

找出傾印檔案 (例如上述範例中的 /data/anr/anr_2020-05-01-09-50-18-290),然後開始分析。