# 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 C++ APIs

Class

API

Description

XCollieChecker

virtual void CheckBlock()

Provides the callback of the suspension detection result.

Input arguments: none

Output arguments: none

Return value: none

XCollieChecker

virtual void CheckThreadBlock()

Provides the callback of the thread suspension detection result.

Input arguments: none

Output arguments: none

Return value: none

XCollie

void RegisterXCollieChecker(const sptr<XCollieChecker> &checker, unsigned int type)

Registers the callback of the thread suspension detection result.

Input arguments:

  • checker: Indicates the pointer to the XCollieChecker instance.
  • type: Indicates the suspension detection type. Set it to XCOLLIE_THREAD.

Output arguments: none

Return value: none

XCollie

int SetTimer(const std::string &name, unsigned int timeout, std::function<void (void *)> func, void *arg, unsigned int flag)

Adds timers.

Input arguments:

  • name: Indicates the timer name.
  • timeout: Indicates the timeout duration, in seconds.
  • func: Indicates the timeout callback.
  • arg: Indicates the pointer to the timeout callback.
  • flag: Indicates the timer operation type.

    XCOLLIE_FLAG_DEFAULT // Indicates the 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: Returns the timer ID if the operation is successful; returns -1 otherwise.

XCollie

bool UpdateTimer(int id, unsigned int timeout)

Updates timers.

Input arguments:

  • id: Indicates the timer ID.
  • timeout: Indicates the timeout duration, in seconds.

Output arguments: none

Return value: Returns true if the operation is successful; returns false otherwise.

XCollie

void CancelTimer(int id)

Cancels timers.

Input arguments:

id: Indicates the timer ID.

Output arguments: none

Return value: none

## Example Print logs. ``` timeout: TimeoutTimer start at 1611040305 to check 1s ago ----------StacktraceCatcher CurrentTime:2021-01-19 15:11:45 Unexecuted(-1)(LogType:Stacktrace Pid:27689 Process:XCollieTimeoutModuleTest)---------- ----- pid 27689 at 2021-01-19 15:11:45 ----- Cmd line: ./XCollieTimeoutModuleTest ABI: 'arm64' "XCollieTimeoutM" sysTid=27689 #01 pc 00000000000174cc /data/test/XCollieTimeoutModuleTest ``` ## How to Develop ### C++ ### 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" ] ```