1e41f4b71Sopenharmony_ci# HiCollie Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciHiCollie provides the software watchdog function. It provides a unified framework for fault detection and fault log generation to help you locate software timeout faults resulting from system service deadlock, application main thread blocking, and service process timeout. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Available APIs 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci **Table 1** Description of XCollieChecker APIs 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci| API| Description| 14e41f4b71Sopenharmony_ci| -------- | -------- | 15e41f4b71Sopenharmony_ci| virtual void CheckBlock() | Provides the callback of the suspension detection result.<br>Input arguments: none<br>Output arguments: none<br>Return value: none| 16e41f4b71Sopenharmony_ci| virtual void CheckThreadBlock() | Provides the callback of the thread suspension detection result.<br>Input arguments: none<br>Output arguments: none<br>Return value: none| 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci **Table 2** Description of XCollie APIs 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| API| Description| 22e41f4b71Sopenharmony_ci| -------- | -------- | 23e41f4b71Sopenharmony_ci| void RegisterXCollieChecker(const sptr<XCollieChecker> &checker, unsigned int type) | Registers the callback of the thread suspension detection result.<br>Input arguments:<br>- **checker**: pointer to the XCollieChecker instance.<br>- **type**: suspension detection type. Set it to **XCOLLIE_THREAD**.<br>Output arguments: none<br>Return value: none| 24e41f4b71Sopenharmony_ci| int SetTimer(const std::string &name, unsigned int timeout, std::function<void(void*)> func, void *arg, unsigned int flag) | Adds timers.<br>Input arguments:<br>- **name**: timer name.<br>- **timeout**: timeout duration, in seconds.<br>- **func**: timeout callback.<br>- **arg**: pointer to the timeout callback.<br>- **flag**: timer operation type.<br> - **XCOLLIE_FLAG_DEFAULT**: default flag, which is the combination of the other three options.<br> - **XCOLLIE_FLAG_NOOP**: Calls only the timeout callback.<br> - **XCOLLIE_FLAG_LOG**: Generates a timeout fault log.<br> - **XCOLLIE_FLAG_RECOVERY**: Exits the process.<br>Output arguments: none<br>Return value: timer ID if the operation is successful; **-1** otherwise.| 25e41f4b71Sopenharmony_ci| bool UpdateTimer(int id, unsigned int timeout) | Updates timers.<br>Input arguments:<br>- **id**: timer ID.<br>- **timeout**: timeout duration, in seconds.<br>Output arguments: none<br>Return value: **true** if the operation is successful; **false** otherwise.| 26e41f4b71Sopenharmony_ci| void CancelTimer(int id) | Cancels a timer.<br>Input arguments:<br>- **id**: timer ID.<br>Output arguments: none<br>Return value: none| 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## How to Develop 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci### Thread Suspension Monitoring 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciThis function requires you to implement two callback functions: **CheckBlock** and **CheckThreadBlock** of the **XCollieChecker** class. After the callbacks are implemented, you need to use the **RegisterXCollieChecker** function of the **XCollie** class to register their instances. The suspension monitoring thread periodically executes all successfully registered callbacks, checks the thread logic completion flag, and determines whether the service logic of any registered thread is suspended. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci1. Develop the source code. 37e41f4b71Sopenharmony_ci Include the **xcollie** header file in the source file. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci ``` 40e41f4b71Sopenharmony_ci #include "xcollie.h" 41e41f4b71Sopenharmony_ci ``` 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci Add the following code to the service code: 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci ``` 47e41f4b71Sopenharmony_ci void MyXCollieChecker::CheckLock() 48e41f4b71Sopenharmony_ci { 49e41f4b71Sopenharmony_ci /* time consuming job */ 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci void MyXCollieChecker::CheckThreadBlock() 53e41f4b71Sopenharmony_ci { 54e41f4b71Sopenharmony_ci /* time consuming job */ 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci sptr<XCollieChecker> checker = new MyXCollieChecker("MyXCollieChecker"); 58e41f4b71Sopenharmony_ci XCollie::GetInstance().RegisterXCollieChecker(checker, 59e41f4b71Sopenharmony_ci (XCOLLIE_LOCK | XCOLLIE_THREAD)); 60e41f4b71Sopenharmony_ci ...... 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci2. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci ``` 66e41f4b71Sopenharmony_ci external_deps = [ "hiviewdfx:libxcollie" ] 67e41f4b71Sopenharmony_ci ``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci### Timeout Monitoring 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciYou can add a maximum of 128 timers for a single process by using the **SetTimer** function. Adding timers will fail if the number of timers has reached the upper limit. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci1. Develop the source code. 75e41f4b71Sopenharmony_ci Include the **xcollie** header file in the source file. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci ``` 78e41f4b71Sopenharmony_ci #include "xcollie.h" 79e41f4b71Sopenharmony_ci ``` 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci Add the code to add, update, and cancel timers. 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci ``` 84e41f4b71Sopenharmony_ci std::function<void(void *)> callback = [](void *args) 85e41f4b71Sopenharmony_ci { 86e41f4b71Sopenharmony_ci /* dump helpful information */ 87e41f4b71Sopenharmony_ci }; 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci int id = XCollie::GetInstance().SetTimer("MyXCollieTimer", 10, callback ,nullptr, XCOLLIE_FLAG_LOG); 90e41f4b71Sopenharmony_ci /* time consuming job */ 91e41f4b71Sopenharmony_ci XCollie::GetInstance().UpdateTimer(id, 5); 92e41f4b71Sopenharmony_ci /* time consuming job */ 93e41f4b71Sopenharmony_ci XCollie::GetInstance().CancelTimer(id); 94e41f4b71Sopenharmony_ci ...... 95e41f4b71Sopenharmony_ci ``` 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci2. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**. 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci ``` 100e41f4b71Sopenharmony_ci external_deps = [ "hiviewdfx:libxcollie" ] 101e41f4b71Sopenharmony_ci ``` 102