ตรวจสอบความสมบูรณ์ของระบบ

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

การตรวจสอบสถานะบริการของผู้ให้บริการ

ระบบจะตรวจสอบบริการของผู้ให้บริการทั้งในฝั่งเนทีฟและฝั่ง Java หากต้องการตรวจสอบบริการของผู้ให้บริการ บริการนั้นต้องลงทะเบียนกระบวนการตรวจสอบสถานะ กับ Watchdog โดยระบุการหมดเวลาที่กำหนดไว้ล่วงหน้า Watchdog จะตรวจสอบสถานะของกระบวนการตรวจสอบสถานะที่ลงทะเบียนโดยการ Ping ที่ช่วงเวลาที่สัมพันธ์กับระยะหมดเวลาที่ระบุไว้ในระหว่างการลงทะเบียน เมื่อกระบวนการที่ได้รับการ Ping ไม่ตอบสนองภายในระยะหมดเวลา ระบบจะถือว่ากระบวนการนั้นไม่สมบูรณ์

การตรวจสอบความสมบูรณ์ของบริการดั้งเดิม

ระบุ Makefile ของ AIDL ของ Watchdog

  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

    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 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();
        ...
    }

    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 โดยการสมัครใช้พร็อพเพอร์ตี้VHAL_HEARTBEATของยานพาหนะ ซึ่งแตกต่างจากการตรวจสอบสถานะบริการของผู้ให้บริการ Watchdog คาดหวังให้มีการอัปเดตค่าของพร็อพเพอร์ตี้นี้ทุกๆ N วินาที เมื่อไม่มีการอัปเดตสัญญาณชีพภายในระยะหมดเวลานี้ Watchdog จะสิ้นสุดบริการ VHAL

หมายเหตุ: Watchdog จะตรวจสอบสถานะบริการ VHAL ก็ต่อเมื่อ บริการ VHAL รองรับVHAL_HEARTBEATพร็อพเพอร์ตี้ยานพาหนะ

การติดตั้งใช้งานภายใน VHAL อาจแตกต่างกันไปตามผู้ให้บริการ ใช้ตัวอย่างโค้ดต่อไปนี้ เป็นข้อมูลอ้างอิง

  1. ลงทะเบียนพร็อพเพอร์ตี้รถ VHAL_HEARTBEAT

    เมื่อเริ่มบริการ VHAL ให้ลงทะเบียนVHAL_HEARTBEATพร็อพเพอร์ตี้ยานพาหนะ ในตัวอย่างด้านล่าง unordered_map ซึ่งเชื่อมโยงรหัสพร็อพเพอร์ตี้กับการกำหนดค่า ใช้เพื่อเก็บการกำหนดค่าที่รองรับทั้งหมด เพิ่มการกำหนดค่าสำหรับ 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;
    }
  2. อัปเดตพร็อพเพอร์ตี้ VHAL_HEARTBEAT ยานพาหนะ

    อัปเดตVHAL_HEARTBEATพร็อพเพอร์ตี้ยานพาหนะทุกๆ N วินาทีตามความถี่ในการตรวจสอบประสิทธิภาพของ VHAL (อธิบายไว้ใน กำหนดความถี่ในการตรวจสอบประสิทธิภาพของ VHAL") วิธีหนึ่งในการทำเช่นนี้คือการใช้ RecurrentTimer เพื่อเรียกการดำเนินการ ที่ตรวจสอบสถานะ VHAL และอัปเดตพร็อพเพอร์ตี้ VHAL_HEARTBEAT vehicle ภายในระยะหมดเวลา

    ตัวอย่างการใช้งานโดยใช้ 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แบบอ่านอย่างเดียว ของ Watchdog จะกำหนดความถี่ในการตรวจสอบสถานะของ VHAL ความถี่ในการตรวจสอบสถานะเริ่มต้น (เมื่อไม่ได้กำหนดพร็อพเพอร์ตี้นี้) คือ 3 วินาที หาก 3 วินาทีไม่เพียงพอสำหรับบริการ VHAL ในการอัปเดตVHAL_HEARTBEATพร็อพเพอร์ตี้ยานพาหนะ ให้กำหนดความถี่ในการตรวจสอบประสิทธิภาพการทำงานของ VHAL โดยขึ้นอยู่กับการตอบสนองของบริการ

แก้ไขข้อบกพร่องของกระบวนการที่ทำงานผิดปกติซึ่ง Watchdog ปิด

Watchdog จะทิ้งสถานะของกระบวนการและสิ้นสุดกระบวนการที่ไม่สมบูรณ์ เมื่อสิ้นสุด กระบวนการที่ไม่สมบูรณ์ Watchdog จะบันทึกข้อความ carwatchdog terminated <process name> (pid:<process id>) ลงใน logcat บรรทัดบันทึกนี้ ให้ข้อมูลเกี่ยวกับกระบวนการที่สิ้นสุด เช่น ชื่อกระบวนการและรหัสกระบวนการ

  1. คุณค้นหาข้อความที่กล่าวถึงข้างต้นใน Logcat ได้โดยเรียกใช้คำสั่งต่อไปนี้
    $ adb logcat -s CarServiceHelper | fgrep "carwatchdog killed"

    ตัวอย่างเช่น เมื่อแอป KitchenSink เป็นไคลเอ็นต์ Watchdog ที่ลงทะเบียนไว้และ ไม่ตอบสนองต่อ Ping ของ Watchdog แล้ว 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 ของกระบวนการที่สิ้นสุด