車輛監控計時器

使用車用監控計時器協助偵錯 VHAL。車輛監控計時器 不健康的過程監控程序 ,程序必須向車輛監控計時器註冊。時間 車輛監控計時器會終止健康狀態不良的過程 和其他應用程式無回應一樣,對 data/anr 的程序 (ANR) 傾印這麼做會讓偵錯程序更為完善。

本文說明廠商 HAL 和服務如何透過 也就是車輛監控計時器

供應商 HAL

供應商 HAL 通常會針對 hwbinder 使用執行緒集區。不過 汽車監控器客戶與汽車監控程式 Daemon 通訊 binder,與 hwbinder 不同。因此 「binder」的另一個執行緒集區正在使用中。

在 makefile 中指定車輛監控計時器輔助鍵

  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_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. 建立用於繫結器通訊的執行緒集區。如果廠商 HAL 使用 hwbinder 您必須建立另一個執行緒集區,以執行車輛監控的繫結器通訊)。
  2. 使用名稱搜尋 Daemon,並呼叫 ICarWatchdog::registerClient。 車輛監控計時器 Daemon 介面名稱為 android.automotive.watchdog.ICarWatchdog/default
  3. 根據服務反應,從以下三種逾時類型中選取其中一種 ,然後在呼叫 ICarWatchdog::registerClient:
    • 重大(3 秒)
    • 中等(5 秒)
    • 一般(10 秒)
    ,瞭解如何調查及移除這項存取權。 請查看下方 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_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);
    }
    

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

車輛監控計時器 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 服務供應商 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 應用程式停止運作的原因或位置,請使用程序傾印 就和使用活動 ANR 情況的情況一樣,儲存資源為 /data/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) ),然後開始分析。