مراقب نظام السيارة

استخدِم مراقب نظام السيارة للمساعدة في تصحيح أخطاء VHAL. أجهزة مراقبة مراقب نظام السيارة سلامة العمليات غير الصحية - وتقتل - منها. لمراقبة العملية من مراقب نظام السيارة، يجب تسجيل العملية لدى مراقب نظام السيارة. فعندما تقتل مراقب السيارات العمليات غير الصحية، ويكتب مراقب نظام السيارة حالة العمليات إلى data/anr كما هو الحال مع التطبيق الآخر "لا يستجيب" حالات التفريغ. وسيؤدي ذلك إلى تسهيل عملية تصحيح الأخطاء.

توضح هذه المقالة كيف يمكن لHALs للبائعين والخدمات تسجيل عملية مع مراقب نظام السيارة.

طبقة تجريد الأجهزة (HAL) للمورّد

في العادة، يستخدم المورد HAL مجموعة سلاسل محادثات في hwbinder. ومع ذلك، يتواصل عميل مراقب نظام السيارة مع البرنامج الخفي لمراقبة السيارة من خلال binder، والذي يختلف عن hwbinder. ولذلك، هناك مجموعة سلاسل محادثات أخرى في binder قيد الاستخدام.

تحديد مساعد مراقب النظام للسيارة في makefile

  1. تضمين carwatchdog_aidl_interface-ndk_platform في shared_libs:

    Android.bp:

    cc_defaults {
        name: "vhal_v2_0_defaults",
        shared_libs: [
            "libbinder_ndk",
            "libhidlbase",
            "liblog",
            "libutils",
            "android.hardware.automotive.vehicle@2.0",
            "carwatchdog_aidl_interface-ndk_platform",
        ],
        cflags: [
            "-Wall",
            "-Wextra",
            "-Werror",
        ],
    }
    

إضافة سياسة SELinux

  1. السماح لـ system_server بإغلاق طبقة تجريد الأجهزة (HAL) إذا لم يكن لديك حساب system_server.te، أنشئ حسابًا. من المهم جدًا بإضافة سياسة SELinux إلى كل جهاز.
  2. السماح لطبقة المقابس الآمنة (HAL) للمورّد باستخدام مادة الربط (وحدة ماكرو واحدة (binder_use)) وإضافة HAL للمورّد إلى نطاق العميل carwatchdog. (carwatchdog_client_domain وحدة ماكرو). راجِع الرمز أدناه الخاص بـ systemserver.te وvehicle_default.te:

    System_server.te

    # Allow system_server to kill vehicle HAL
    allow system_server hal_vehicle_server:process sigkill;
    

    hal_vehicle_default.te

    # Configuration for register VHAL to car watchdog
    carwatchdog_client_domain(hal_vehicle_default)
    binder_use(hal_vehicle_default)
    

تنفيذ فئة العميل من خلال اكتساب BnCarWatchdogClient

  1. في checkIfAlive، يمكنك إجراء فحص صحي. على سبيل المثال، يمكنك النشر على معالج حلقة سلسلة المحادثات. إذا كنت مستعدًا، اتصِل بالرقم ICarWatchdog::tellClientAlive. راجِع الرمز أدناه الخاص بـ WatchogClient.h وWatchogClient.cpp:

    WatchogClient.h

    class WatchdogClient : public aidl::android::automotive::watchdog::BnCarWatchdogClient {
      public:
        explicit WatchdogClient(const ::android::sp<::android::Looper>& handlerLooper, VehicleHalManager* vhalManager);
    
    ndk::ScopedAStatus checkIfAlive(int32_t sessionId, aidl::android::automotive::watchdog::TimeoutLength timeout) override; ndk::ScopedAStatus prepareProcessTermination() override; };

    WatchogClient.cpp

    ndk::ScopedAStatus WatchdogClient::checkIfAlive(int32_t sessionId, TimeoutLength /*timeout*/) {
        // Implement or call your health check logic here
        return ndk::ScopedAStatus::ok();
    }
    

بدء سلسلة التعليمات وتسجيل العميل

  1. إنشاء مجموعة سلاسل محادثات للتواصل بين المثبت إذا كان البائع HAL يستخدم hwbinder لتنفيذ يجب إنشاء مجموعة سلاسل محادثات أخرى لاتصال أداة ربط مراقب نظام السيارة).
  2. ابحث عن البرنامج الخفي الذي يحمل الاسم وانقر على ICarWatchdog::registerClient. اسم واجهة البرنامج الخفي لمراقبة السيارة هو android.automotive.watchdog.ICarWatchdog/default
  3. بناءً على استجابة الخدمة، اختَر أحد أنواع المهلة الثلاثة التالية التي يدعمها مراقب نظام السيارة ثم تحديد المهلة في الاتصال ICarWatchdog::registerClient:
    • بالغة الأهمية(3 ثوانٍ)
    • معتدلة(5 ثوانٍ)
    • عادية(10 ثوانٍ)
    راجِع الرمز الخاص بـ VehicleService.cpp وWatchogClient.cpp أدناه:

    المركبات Service.cpp

    int main(int /* argc */, char* /* argv */ []) {
        // Set up thread pool for hwbinder
        configureRpcThreadpool(4, false /* callerWillJoin */);
    
        ALOGI("Registering as service...");
        status_t status = service->registerAsService();
    
        if (status != OK) {
            ALOGE("Unable to register vehicle service (%d)", status);
            return 1;
        }
    
        // Setup a binder thread pool to be a car watchdog client.
        ABinderProcess_setThreadPoolMaxThreadCount(1);
        ABinderProcess_startThreadPool();
        sp<Looper> looper(Looper::prepare(0 /* opts */));
        std::shared_ptr<WatchdogClient> watchdogClient =
                ndk::SharedRefBase::make<WatchdogClient>(looper, service.get());
        // The current health check is done in the main thread, so it falls short of capturing the real
        // situation. Checking through HAL binder thread should be considered.
        if (!watchdogClient->initialize()) {
            ALOGE("Failed to initialize car watchdog client");
            return 1;
        }
        ALOGI("Ready");
        while (true) {
            looper->pollAll(-1 /* timeoutMillis */);
        }
    
        return 1;
    }
    

    WatchogClient.cpp

    bool WatchdogClient::initialize() {
        ndk::SpAIBinder binder(AServiceManager_getService("android.automotive.watchdog.ICarWatchdog/default"));
        if (binder.get() == nullptr) {
            ALOGE("Failed to get carwatchdog daemon");
            return false;
        }
        std::shared_ptr<ICarWatchdog> server = ICarWatchdog::fromBinder(binder);
        if (server == nullptr) {
            ALOGE("Failed to connect to carwatchdog daemon");
            return false;
        }
        mWatchdogServer = server;
    
        binder = this->asBinder();
        if (binder.get() == nullptr) {
            ALOGE("Failed to get car watchdog client binder object");
            return false;
        }
        std::shared_ptr<ICarWatchdogClient> client = ICarWatchdogClient::fromBinder(binder);
        if (client == nullptr) {
            ALOGE("Failed to get ICarWatchdogClient from binder");
            return false;
        }
        mTestClient = client;
        mWatchdogServer->registerClient(client, TimeoutLength::TIMEOUT_NORMAL);
        ALOGI("Successfully registered the client to car watchdog server");
        return true;
    }
    

خدمات المورّدين (الأصلية)

تحديد ملف تعريف مُساعد مراقب السيارة

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

    سياقات الملفات

    /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);
    }
    

بدء سلسلة ربط وتسجيل العميل

اسم واجهة البرنامج الخفي لمراقبة السيارة هو 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);
    }
    

خدمات البائعين (Android)

تنفيذ برنامج من خلال اكتساب 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);
    }
    

رصد العمليات التي أنهتها مراقب نظام السيارة

عمليات تفريغ/إتلاف مراقب نظام السيارات (HAL للبائع، والخدمات الأصلية للبائعين، خدمات Android للبائعين) التي يتم تسجيلها في مراقب نظام السيارة عندما يتم عالق وغير مستجيب. ويتم رصد عملية التفريغ هذه من خلال التحقّق من ملفات Logcats. السيارة يُخرج مراقب النظام سجل carwatchdog killed process_name (pid:process_id) عند التخلص من هذه العمليات أو إنهائها. لذلك:

$ adb logcat -s CarServiceHelper | fgrep "carwatchdog killed"

يتم تسجيل السجلات ذات الصلة. على سبيل المثال، إذا كان تطبيق KitchenSink (مراقب نظام السيارات عالِق)، تتم كتابة سطر مثل ذلك أدناه في السجل:

05-01 09:50:19.683   578  5777 W CarServiceHelper: carwatchdog killed com.google.android.car.kitchensink (pid: 5574)

لتحديد سبب أو مكان تعطُّل تطبيق KitchenSink، استخدِم تفريغ العملية. تم تخزينه في /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 -----

العثور على ملف التفريغ (على سبيل المثال، /data/anr/anr_2020-05-01-09-50-18-290) في المثال أعلاه) وابدأ التحليل.