1# Using HiCollie to Detect Function Execution Timeout Events (C/C++) 2 3<!--Kit: Performance Analysis Kit--> 4<!--Subsystem: HiviewDFX--> 5<!--Owner: @rr_cn--> 6<!--Designer: @peterhuangyu--> 7<!--Tester: @gcw_KuLfPSbe--> 8<!--Adviser: @foryourself--> 9 10## Overview 11 12Task execution timeout indicates that the execution duration of the monitored service code logic exceeds the expected duration. This topic describes the capability of the HiCollie module to detect function execution timeout events. 13 14## Available APIs 15 16| API| Description| 17| -------- | -------- | 18| OH_HiCollie_SetTimer | Sets a timer to check whether the execution time of a function or code block exceeds the custom time.<br>This API is used before the time-consuming function is called. It must be used together with the **OH_HiCollie_CancelTimer** API.<br>Note: This API is supported since API version 18.| 19| OH_HiCollie_CancelTimer | Cancels a timer based on task ID.<br>This API is used after the function or code block is executed. It must be used together with the **OH_HiCollie_SetTimer** API.<br>If the timer is not canceled within the custom time, a callback function is executed to generate fault logs for the specified timeout event.<br>Note: This API is supported since API version 18.| 20 21- For details about how to use the APIs (such as parameter usage restrictions and value ranges), see [HiCollie](../reference/apis-performance-analysis-kit/capi-hicollie-h.md). 22 23- The fault log file is saved in the **/data/log/faultlog/faultlogger/** directory. The file name format is **syswarning-application bundle name-application UID-second-level time.log**. 24 25## How to Develop 26 27The following describes how to add a button in the application and click the button to call the HiCollie APIs. 28 291. Create a native C++ project. The directory structure is as follows: 30 31 ```yml 32 entry: 33 src: 34 main: 35 cpp: 36 types: 37 libentry: 38 - index.d.ts 39 - CMakeLists.txt 40 - napi_init.cpp 41 ets: 42 entryability: 43 - EntryAbility.ts 44 pages: 45 - Index.ets 46 ``` 47 482. In the **CMakeLists.txt** file, add the source file and dynamic libraries. 49 50 ```cmake 51 # Add libhilog_ndk.z.so (log output) and libohhicollie.so (HiCollie external APIs). 52 target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libohhicollie.so) 53 ``` 54 553. In the **napi_init.cpp** file, import dependency header files, define **LOG_TAG** and test methods, and register **TestHiCollieTimerNdk** as an ArkTS API. 56 57 ```c++ 58 #include "napi/native_api.h" 59 #include "hicollie/hicollie.h" 60 #include "hilog/log.h" 61 62 #include <unistd.h> 63 64 #undef LOG_TAG 65 #define LOG_TAG "testTag" 66 67 // Define the callback. 68 void CallBack(void*) 69 { 70 OH_LOG_INFO(LogType::LOG_APP, "HiCollieTimerNdk CallBack"); // Logs are printed in the callback. 71 } 72 73 static napi_value TestHiCollieTimerNdk(napi_env env, napi_callback_info info) 74 { 75 int id; 76 HiCollie_SetTimerParam param = {"testTimer", 1, CallBack, nullptr, HiCollie_Flag::HICOLLIE_FLAG_LOG}; // Set HiCollieTimer parameters (timer name, timeout interval, callback, callback parameters, and behavior after timeout). 77 HiCollie_ErrorCode errorCode = OH_HiCollie_SetTimer(param, &id); // Register a HiCollieTimer function to execute a one-off timeout detection task. 78 if (errorCode == HICOLLIE_SUCCESS) { // The HiCollieTimer task is successfully registered. 79 OH_LOG_INFO(LogType::LOG_APP, "HiCollieTimer taskId: %{public}d", id); // Log the task ID. 80 sleep (2); // Simulate a time-consuming function to block the thread for 2s. 81 OH_HiCollie_CancelTimer (id); // Cancel the registered timer based on the ID. 82 } 83 return nullptr; 84 } 85 86 EXTERN_C_START 87 static napi_value Init(napi_env env, napi_value exports) 88 { 89 napi_property_descriptor desc[] = { 90 { "TestHiCollieTimerNdk", nullptr, TestHiCollieTimerNdk, nullptr, nullptr, nullptr, napi_default, nullptr } // Register TestHiCollieTimerNdk as an ArkTS API. 91 }; 92 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 93 return exports; 94 } 95 EXTERN_C_END 96 97 static napi_module demoModule = { 98 .nm_version = 1, 99 .nm_flags = 0, 100 .nm_filename = nullptr, 101 .nm_register_func = Init, 102 .nm_modname = "entry", 103 .nm_priv = ((void*)0), 104 .reserved = { 0 }, 105 }; 106 107 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) 108 { 109 napi_module_register(&demoModule); 110 } 111 ``` 112 1134. In the **index.d.ts** file, define the ArkTS API. 114 115 ```ts 116 export const TestHiCollieTimerNdk: () => void; 117 ``` 118 1195. Edit the **Index.ets** file. 120 121 ```ts 122 import testNapi from 'libentry.so'; 123 124 @Entry 125 @Component 126 struct Index { 127 @State message: string = 'Hello World'; 128 129 build() { 130 Row() { 131 Column() { 132 Button("TestHiCollieTimerNdk") 133 .fontSize(50) 134 .fontWeight(FontWeight.Bold) 135 .onClick(testNapi.TestHiCollieTimerNdk); //Add a click event to trigger the testHiCollieTimerNdk method. 136 } 137 .width('100%') 138 } 139 .height('100%') 140 } 141 } 142 ``` 143 1446. Click the **Run** button in DevEco Studio to run the project. 145 1467. At the bottom of DevEco Studio, switch to the **Log** tab, choose **HiLog** and set the filter criteria to **testTag**. 147 148 (1) Click the **testHiCollieTimerNdk** button to execute the timer, and the task ID is logged. 149 150 ``` 151 .../testTag ... HiCollieTimer taskId: x 152 ``` 153 154 (2) After 2s, the callback function is executed and logs are displayed. 155 156 ``` 157 .../testTag ... HiCollieTimerNdk CallBack 158 ``` 159 160 For details about how to obtain the fault file information, see [Subscribing to Task Execution Timeout Events (C/C++)](hiappevent-watcher-apphicollie-events-ndk.md). 161