เฝ้าระวังรถยนต์

ใช้ Watchdog เพื่อช่วยแก้ไขข้อบกพร่องของ VHAL จอเฝ้าระวังรถยนต์ กระบวนการที่มีประสิทธิภาพไม่ดีของ — และนำไปสู่การเสียชีวิต เพื่อให้กระบวนการได้รับการตรวจสอบ Watchdog ของรถยนต์ จะต้องจดทะเบียนกับ Watchdog ของรถยนต์ วันและเวลา Watchdog ของรถยนต์จะฆ่ากระบวนการที่ไม่มีสุขภาพ ส่วน Watchdog ของรถยนต์ก็จะเขียนสถานะของ กระบวนการใน data/anr เช่นเดียวกับแอปพลิเคชันอื่นๆ ที่ไม่ตอบสนอง ไฟล์ดัมพ์ (ANR) การทำเช่นนี้ทำให้กระบวนการแก้ไขข้อบกพร่องเกิดขึ้น

บทความนี้อธิบายวิธีที่ HAL และบริการของผู้ให้บริการลงทะเบียนกระบวนการกับ Watchdog ของรถ

HAL ของผู้ให้บริการ

โดยปกติแล้ว HAL ของผู้ให้บริการจะใช้ Thread Pool สำหรับ hwbinder อย่างไรก็ตาม ไคลเอ็นต์ Watchdog ของรถยนต์สื่อสารกับ Daemon ของ Watchdog ของรถ binder ซึ่งแตกต่างจาก hwbinder ดังนั้น ใช้งาน Thread Pool อีกรายการสำหรับ binder อยู่

ระบุตัวช่วย Watchdog ของรถยนต์ใน Makefile

  1. รวม carwatchdog_aidl_interface-ndk_platform ใน shared_libs:

    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.te และ vehicle_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.h และ WatchogClient.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();
    }
    

เริ่มเทรด Binder และลงทะเบียนไคลเอ็นต์

  1. สร้าง Thread Pool สำหรับการสื่อสาร Binder กรณีที่ HAL ของผู้ให้บริการใช้ hwbinder สำหรับ เพื่อจุดประสงค์ของตนเอง คุณต้องสร้าง Thread Pool อีกรายการสำหรับการสื่อสาร Binder ของ Watchdog ของรถยนต์)
  2. ค้นหา Daemon ด้วยชื่อนั้นแล้วเรียก ICarWatchdog::registerClient ชื่ออินเทอร์เฟซ Daemon ของ Watchdog ของรถยนต์คือ android.automotive.watchdog.ICarWatchdog/default
  3. เลือกการหมดเวลา 1 ใน 3 ประเภทต่อไปนี้ตามการตอบสนองของบริการ ที่ Watchdog ของรถยนต์รองรับ แล้วผ่านระยะหมดเวลาในการเรียกไปยัง ICarWatchdog::registerClient:
    • วิกฤติ(3 วินาที)
    • ปานกลาง(5 วินาที)
    • ปกติ(10 วินาที)
    ดูโค้ดสำหรับ VehicleService.cpp และ WatchogClient.cpp ด้านล่าง

    บริการยานพาหนะ.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;
    }
    

บริการของผู้ให้บริการ (เนทีฟ)

ระบุไฟล์ Watchdog Aidl ของรถยนต์

  1. รวม carwatchdog_aidl_interface-ndk_platform ใน shared_libs

    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.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

  1. ใน checkIfAlive ให้ดำเนินการตรวจสอบประสิทธิภาพการทำงาน ทางเลือกหนึ่งคือให้โพสต์ไปที่ เครื่องจัดการลูปเทรด หากมีประสิทธิภาพดี ให้โทรหา ICarWatchdog::tellClientAlive ดูโค้ดสำหรับ SampleNativeClient.h และ SampleNativeClient.cpp ด้านล่าง

    ตัวอย่าง NativeClient.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;
    };
    

    ตัวอย่าง NativeClient.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 ของ Watchdog ของรถยนต์คือ android.automotive.watchdog.ICarWatchdog/default

  1. ค้นหา Daemon ด้วยชื่อนั้นแล้วเรียก 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();
        ...
    }
    

    ตัวอย่าง NativeClient.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);
    }
    

ตรวจจับกระบวนการที่ Watchdog ของรถยนต์สิ้นสุด

การทิ้ง/ทิ้งกระบวนการฆ่าสัตว์ (HAL) ของผู้ให้บริการ, บริการจากผู้ให้บริการโดยตรง ผู้ให้บริการ Android) ซึ่งลงทะเบียนไว้กับ Watchdog ของรถยนต์ ค้างและไม่ตอบสนอง ระบบจะตรวจพบการดัมพ์ดังกล่าวโดยตรวจสอบ Logcat รถยนต์ Watchdog แสดงบันทึก carwatchdog killed process_name (pid:process_id) เมื่อกระบวนการที่เป็นปัญหาถูกทิ้งหรือยุติลง ดังนั้น

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

ระบบจะจัดเก็บบันทึกที่เกี่ยวข้องไว้ ตัวอย่างเช่น หากแอป KitchenSink (Watchdog ของรถยนต์ ) ค้าง ระบบจะเขียนบรรทัดต่อไปนี้ลงในบันทึก

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

หากต้องการหาสาเหตุหรือตำแหน่งที่แอป KitchenSink ค้าง ให้ใช้ดัมพ์ของกระบวนการ จัดเก็บไว้ที่ /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 -----

ค้นหาไฟล์ Dump (เช่น /data/anr/anr_2020-05-01-09-50-18-290 ในตัวอย่างด้านบน) และเริ่มการวิเคราะห์ของคุณ