ตรวจสอบการทำงานของระบบ

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

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

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

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