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 ורישום הלקוח
שם הממשק של הדימון של הטיימר המפקח (watchdog) של המכונית הוא android.automotive.watchdog.ICarWatchdog/default
.
- מחפשים את הדימון לפי השם שלו ומפעילים את
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 תומך במאפיין הרכב 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 (כפי שמוסבר בקטע הגדרת התדירות של בדיקת התקינות של 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. שורת היומן הזו מספקת מידע על התהליך שהופסק, כמו שם התהליך ומזהה התהליך.
- כדי לחפש את הטקסט שצוין למעלה ב-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)
- כדי לזהות את שורש הבעיה של חוסר התגובה, משתמשים ב-dump של התהליך שנשמר ב-
/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 של התהליך שהופסק.