# HiCollie Development ## Overview HiCollie 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. ## Available APIs **Table 1** Description of XCollieChecker APIs | API| Description| | -------- | -------- | | virtual void CheckBlock() | Provides the callback of the suspension detection result.
Input arguments: none
Output arguments: none
Return value: none| | virtual void CheckThreadBlock() | Provides the callback of the thread suspension detection result.
Input arguments: none
Output arguments: none
Return value: none| **Table 2** Description of XCollie APIs | API| Description| | -------- | -------- | | void RegisterXCollieChecker(const sptr<XCollieChecker> &checker, unsigned int type) | Registers the callback of the thread suspension detection result.
Input arguments:
- **checker**: pointer to the XCollieChecker instance.
- **type**: suspension detection type. Set it to **XCOLLIE_THREAD**.
Output arguments: none
Return value: none| | int SetTimer(const std::string &name, unsigned int timeout, std::function<void(void*)> func, void *arg, unsigned int flag) | Adds timers.
Input arguments:
- **name**: timer name.
- **timeout**: timeout duration, in seconds.
- **func**: timeout callback.
- **arg**: pointer to the timeout callback.
- **flag**: timer operation type.
- **XCOLLIE_FLAG_DEFAULT**: default flag, which is the combination of the other three options.
- **XCOLLIE_FLAG_NOOP**: Calls only the timeout callback.
- **XCOLLIE_FLAG_LOG**: Generates a timeout fault log.
- **XCOLLIE_FLAG_RECOVERY**: Exits the process.
Output arguments: none
Return value: timer ID if the operation is successful; **-1** otherwise.| | bool UpdateTimer(int id, unsigned int timeout) | Updates timers.
Input arguments:
- **id**: timer ID.
- **timeout**: timeout duration, in seconds.
Output arguments: none
Return value: **true** if the operation is successful; **false** otherwise.| | void CancelTimer(int id) | Cancels a timer.
Input arguments:
- **id**: timer ID.
Output arguments: none
Return value: none| ## How to Develop ### Thread Suspension Monitoring This 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. 1. Develop the source code. Include the **xcollie** header file in the source file. ``` #include "xcollie.h" ``` Add the following code to the service code: ``` void MyXCollieChecker::CheckLock() { /* time consuming job */ } void MyXCollieChecker::CheckThreadBlock() { /* time consuming job */ } sptr checker = new MyXCollieChecker("MyXCollieChecker"); XCollie::GetInstance().RegisterXCollieChecker(checker, (XCOLLIE_LOCK | XCOLLIE_THREAD)); ...... ``` 2. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**. ``` external_deps = [ "hiviewdfx:libxcollie" ] ``` ### Timeout Monitoring You 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. 1. Develop the source code. Include the **xcollie** header file in the source file. ``` #include "xcollie.h" ``` Add the code to add, update, and cancel timers. ``` std::function callback = [](void *args) { /* dump helpful information */ }; int id = XCollie::GetInstance().SetTimer("MyXCollieTimer", 10, callback ,nullptr, XCOLLIE_FLAG_LOG); /* time consuming job */ XCollie::GetInstance().UpdateTimer(id, 5); /* time consuming job */ XCollie::GetInstance().CancelTimer(id); ...... ``` 2. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**. ``` external_deps = [ "hiviewdfx:libxcollie" ] ```