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

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

การตรวจสอบประสิทธิภาพการทำงานของบริการของผู้ให้บริการ

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

การตรวจสอบประสิทธิภาพของบริการแบบดั้งเดิม

ระบุไฟล์ยอดนิยมของ 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

    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. ค้นหาเดรัมที่มีชื่อดังกล่าวและเรียกใช้ 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 service.

หมายเหตุ: เครื่องมือตรวจสอบจะตรวจสอบประสิทธิภาพของบริการ 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 ภายในระยะหมดเวลา

    ด้านล่างนี้เป็นตัวอย่างการใช้งานที่ใช้ 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 บรรทัดบันทึกนี้ ให้ข้อมูลเกี่ยวกับกระบวนการที่สิ้นสุด เช่น ชื่อและกระบวนการ ID

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

    ไฟล์ Dump ของกระบวนการ KitchenSink ที่สิ้นสุดแล้วอยู่ที่ /data/anr/anr_2020-05-01-09-50-18-290 เริ่มต้นการวิเคราะห์โดยใช้ ไฟล์ ANR ของกระบวนการที่สิ้นสุดแล้ว