Watchdog ตรวจสอบความสมบูรณ์ของบริการของผู้ขายและบริการ VHAL และยุติกระบวนการที่ไม่แข็งแรงใด ๆ เมื่อกระบวนการที่ไม่แข็งแรงถูกยกเลิก Watchdog จะดัมพ์สถานะของกระบวนการไปที่ /data/anr
เช่นเดียวกับการดัมพ์ Application Not Responding (ANR) อื่นๆ การทำเช่นนี้ช่วยอำนวยความสะดวกในกระบวนการแก้ไขจุดบกพร่อง
การตรวจติดตามสุขภาพการบริการของผู้ขาย
บริการของผู้ขายจะได้รับการตรวจสอบทั้งฝั่งเนทีฟและฝั่ง Java สำหรับบริการของผู้จำหน่ายที่จะได้รับการตรวจสอบ บริการจะต้องลงทะเบียนกระบวนการตรวจสุขภาพกับ Watchdog โดยการระบุการหมดเวลาที่กำหนดไว้ล่วงหน้า Watchdog จะตรวจสอบความสมบูรณ์ของกระบวนการตรวจสุขภาพที่ลงทะเบียนไว้โดยส่ง Ping ไปตามช่วงเวลาที่สัมพันธ์กับระยะหมดเวลาที่ระบุไว้ระหว่างการลงทะเบียน เมื่อกระบวนการส่ง Ping ไม่ตอบสนองภายในระยะหมดเวลา กระบวนการจะถือว่าไม่มีประสิทธิภาพ
การตรวจสอบสุขภาพบริการดั้งเดิม
ระบุ makefile ของ Watchdog AIDL
- รวม
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
- หากต้องการเพิ่มนโยบาย 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
- ใน
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 และลงทะเบียนไคลเอ็นต์
ชื่ออินเทอร์เฟซ car watchdog daemon คือ android.automotive.watchdog.ICarWatchdog/default
- ค้นหา 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
- แก้ไขไฟล์ใหม่ดังต่อไปนี้:
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 วินาที เมื่อฮาร์ทบีทไม่ได้รับการอัพเดตภายในระยะหมดเวลานี้ Watchdog จะยุติบริการ VHAL
หมายเหตุ: Watchdog จะตรวจสอบความสมบูรณ์ของบริการ VHAL เฉพาะเมื่อคุณสมบัติยานพาหนะ VHAL_HEARTBEAT
ได้รับการสนับสนุนโดยบริการ VHAL
การใช้งานภายในของ 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 (อธิบายไว้ใน กำหนดความถี่ของการตรวจสุขภาพ VHAL" ) ให้อัปเดตคุณสมบัติยานพาหนะ
VHAL_HEARTBEAT
ทุกๆ N วินาที วิธีหนึ่งในการทำเช่นนี้คือการใช้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)); } }
- ( ไม่บังคับ ) กำหนดความถี่ของการตรวจสุขภาพ VHAL
คุณสมบัติผลิตภัณฑ์
ro.carwatchdog.vhal_healthcheck.interval
แบบอ่านอย่างเดียวของ Watchdog จะกำหนดความถี่ในการตรวจสุขภาพ VHAL ความถี่ในการตรวจสอบประสิทธิภาพการทำงานเริ่มต้น (เมื่อไม่ได้กำหนดคุณสมบัตินี้) คือสามวินาที หากสามวินาทีไม่เพียงพอสำหรับบริการ VHAL ในการอัปเดตคุณสมบัติของยานพาหนะVHAL_HEARTBEAT
ให้กำหนดความถี่ในการตรวจสอบสภาพ VHAL ขึ้นอยู่กับการตอบสนองของบริการ
ดีบักกระบวนการที่ไม่แข็งแรงซึ่งถูกยกเลิกโดย Watchdog
Watchdog ทิ้งสถานะกระบวนการและยุติกระบวนการที่ไม่แข็งแรง เมื่อยุติกระบวนการที่ไม่แข็งแรง Watchdog จะบันทึกข้อความ carwatchdog terminated <process name> (pid:<process id>)
ไปยัง logcat บรรทัดบันทึกนี้ให้ข้อมูลเกี่ยวกับกระบวนการที่สิ้นสุด เช่น ชื่อกระบวนการและ ID กระบวนการ
- 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)
- หากต้องการระบุสาเหตุหลักของการไม่ตอบสนอง ให้ใช้ดัมพ์กระบวนการที่เก็บไว้ที่
/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 ของกระบวนการที่สิ้นสุด