Watchdog จะตรวจสอบสถานะของบริการของผู้ให้บริการและบริการ VHAL รวมถึง
สิ้นสุดกระบวนการที่ไม่สมบูรณ์ เมื่อสิ้นสุดกระบวนการที่ไม่สมบูรณ์ Watchdog จะ
ส่งออกสถานะกระบวนการไปยัง /data/anr เช่นเดียวกับการส่งออกข้อมูล "แอปพลิเคชันไม่ตอบสนอง"
(ANR) อื่นๆ การทำเช่นนี้จะช่วยให้กระบวนการแก้ไขข้อบกพร่องง่ายขึ้น
การตรวจสอบสถานะบริการของผู้ให้บริการ
ระบบจะตรวจสอบบริการของผู้ให้บริการทั้งในฝั่งเนทีฟและฝั่ง Java หากต้องการตรวจสอบบริการของผู้ให้บริการ บริการนั้นต้องลงทะเบียนกระบวนการตรวจสอบสถานะ กับ Watchdog โดยระบุการหมดเวลาที่กำหนดไว้ล่วงหน้า Watchdog จะตรวจสอบสถานะของกระบวนการตรวจสอบสถานะที่ลงทะเบียนโดยการปิงกระบวนการดังกล่าวเป็นช่วงๆ ตามระยะหมดเวลาที่ระบุไว้ในระหว่างการลงทะเบียน เมื่อกระบวนการที่ได้รับการ Ping ไม่ตอบสนองภายในระยะหมดเวลา ระบบจะถือว่ากระบวนการดังกล่าวไม่สมบูรณ์
การตรวจสอบความสมบูรณ์ของบริการดั้งเดิม
ระบุ Makefile ของ AIDL สำหรับ Watchdog
- รวม
carwatchdog_aidl_interface-ndk_platformในshared_libsAndroid.bpcc_binary { name: "sample_native_client", srcs: [ "src/*.cpp" ], shared_libs: [ "carwatchdog_aidl_interface-ndk_platform", "libbinder_ndk", ], vendor: true, }
เพิ่มนโยบาย SELinux
- หากต้องการเพิ่มนโยบาย SELinux ให้โดเมนบริการของผู้ให้บริการใช้ Binder
(
binder_useมาโคร) และเพิ่มโดเมนบริการของผู้ให้บริการไปยัง โดเมนไคลเอ็นต์carwatchdog(carwatchdog_client_domainมาโคร) ดูโค้ดด้านล่างสำหรับsample_client.teและfile_contextssample_client.tetype 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
- ใน
checkIfAliveให้ทำการตรวจสอบประสิทธิภาพการทำงาน ตัวเลือกหนึ่งคือการโพสต์ไปยัง ตัวแฮนเดิลลูปของเธรด หากมีสุขภาพแข็งแรง โปรดโทรหาICarWatchdog::tellClientAliveดูโค้ดด้านล่างสำหรับSampleNativeClient.hและSampleNativeClient.cppSampleNativeClient.hclass 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.cppndk::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
- ค้นหา Daemon ที่มีชื่อและเรียกใช้
ICarWatchdog::registerClientดูโค้ดด้านล่างสำหรับmain.cppและSampleNativeClient.cppmain.cppint 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.cppvoid 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
- แก้ไขไฟล์ใหม่ดังนี้
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() {} };
ลงทะเบียนไคลเอ็นต์
- โทรหา
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); }
ยกเลิกการลงทะเบียนไคลเอ็นต์
- โทรหา
CarWatchdogManager.unregisterClient()เมื่อบริการเสร็จสิ้นprivate void finishClient() { CarWatchdogManager manager = (CarWatchdogManager) car.getCarManager( Car.CAR_WATCHDOG_SERVICE); manager.unregisterClient(mClientCallback); }
การตรวจสอบสถานะของ VHAL
Watchdog จะตรวจสอบสถานะบริการ VHAL โดยการติดตามVHAL_HEARTBEATพร็อพเพอร์ตี้ยานพาหนะ ซึ่งแตกต่างจากการตรวจสอบสถานะบริการของผู้ให้บริการ
Watchdog คาดหวังว่าจะมีการอัปเดตค่าของพร็อพเพอร์ตี้นี้ทุกๆ N วินาที
เมื่อไม่มีการอัปเดต Heartbeat ภายในระยะหมดเวลานี้ Watchdog จะสิ้นสุดบริการ VHAL
หมายเหตุ: Watchdog จะตรวจสอบสถานะบริการ VHAL เมื่อบริการ VHAL รองรับVHAL_HEARTBEATพร็อพเพอร์ตี้ยานพาหนะเท่านั้น
การติดตั้งใช้งานภายในของ VHAL อาจแตกต่างกันไปตามผู้ให้บริการ ใช้ตัวอย่างโค้ดต่อไปนี้ เป็นข้อมูลอ้างอิง
- ลงทะเบียนพร็อพเพอร์ตี้
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; }
- อัปเดต
VHAL_HEARTBEATพร็อพเพอร์ตี้ยานพาหนะอัปเดตพร็อพเพอร์ตี้ยานพาหนะ
VHAL_HEARTBEATทุกๆ N วินาทีตามความถี่ในการตรวจสอบประสิทธิภาพของ VHAL (อธิบายไว้ใน กำหนดความถี่ในการตรวจสอบประสิทธิภาพของ VHAL") วิธีหนึ่งในการทำเช่นนี้คือการใช้RecurrentTimerเพื่อเรียกการดำเนินการ ที่ตรวจสอบสถานะ VHAL และอัปเดตพร็อพเพอร์ตี้VHAL_HEARTBEATvehicle ภายในระยะหมดเวลาด้านล่างนี้คือตัวอย่างการใช้งานโดยใช้
RecurrentTimerint 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)); } }
- (ไม่บังคับ) กำหนดความถี่ของการตรวจสอบประสิทธิภาพการทำงานของ 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 บรรทัดบันทึกนี้
ให้ข้อมูลเกี่ยวกับกระบวนการที่สิ้นสุด เช่น ชื่อกระบวนการและรหัสกระบวนการ
- คุณค้นหาข้อความที่กล่าวถึงข้างต้นใน Logcat ได้โดยเรียกใช้คำสั่งต่อไปนี้
$ adb logcat -s CarServiceHelper | fgrep "carwatchdog killed"
ตัวอย่างเช่น เมื่อแอป KitchenSink เป็นไคลเอ็นต์ Watchdog ที่ลงทะเบียนและ ไม่ตอบสนองต่อการปิงของ Watchdog แล้ว Watchdog จะบันทึกบรรทัดเช่นบรรทัดด้านล่าง เมื่อสิ้นสุดกระบวนการ KitchenSink ที่ลงทะเบียน
05-01 09:50:19.683 578 5777 W CarServiceHelper: carwatchdog killed com.google.android.car.kitchensink (pid: 5574)
- หากต้องการระบุสาเหตุของปัญหาที่ทำให้แอปไม่ตอบสนอง ให้ใช้กระบวนการ
ดัมพ์ที่จัดเก็บไว้ที่
/data/anrเช่นเดียวกับที่ใช้สำหรับกรณี ANR ของกิจกรรม หากต้องการดึงไฟล์ Dump สำหรับกระบวนการที่สิ้นสุด ให้ใช้คำสั่งด้านล่าง$ 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เริ่มการวิเคราะห์โดยใช้ ไฟล์ Dump ANR ของกระบวนการที่สิ้นสุด