• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using HiCollie to Monitor the Function Execution Time (C/C++)
2
3HiCollie provides APIs for detecting the function execution timeout events.
4
5## Available APIs
6
7| API                         | Description                             |
8| ------------------------------  | --------------------------------- |
9| OH_HiCollie_SetTimer | Enables a timer for checking the function execution duration. Use this function before calling a time-consuming function or code block.         |
10| OH_HiCollie_CancelTimer | Cancels a timer for checking the function execution duration. Use this function after calling a time-consuming function or code block.      |
11
12> **NOTE**
13>
14> You can obtain the function execution timeout log in the following paths:
15> 1. The **APP_HICOLLIE-*process ID*-*time*.log** file in **device/data/log/eventlog/**.
16> 2. 2. The **syswarning-*bundle name*-*application UID*-*second-level time*** file in **device/data/log/faultlog/faultlogger/**.
17
18For details (such as parameter usage and value ranges), see [HiCollie](../reference/apis-performance-analysis-kit/_hi_collie.md).
19
20## How to Develop
21
22The following describes how to add a button in the application and click the button to call the HiCollie APIs.
23
241. Create a native C++ project. The directory structure is as follows:
25
26   ```yml
27   entry:
28     src:
29       main:
30         cpp:
31           - types:
32               libentry:
33                 - index.d.ts
34           - CMakeLists.txt
35           - napi_init.cpp
36         ets:
37           - entryability:
38               - EntryAbility.ts
39           - pages:
40               - Index.ets
41   ```
42
432. In the **CMakeLists.txt** file, add the source file and dynamic libraries.
44
45   ```cmake
46   # Add libhilog_ndk.z.so (log output) and libohhicollie.so (HiCollie external APIs).
47   target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libohhicollie.so)
48   ```
49
503. Import the dependencies to the **napi_init.cpp** file, and define **LOG_TAG** and the test method.
51
52   ```c++
53    #include "napi/native_api.h"
54    #include "hicollie/hicollie.h"
55    #include "hilog/log.h"
56
57    #include <unistd.h>
58
59    #undef LOG_TAG
60    #define LOG_TAG "testTag"
61
62    // Define the callback.
63    void CallBack(void* callBackParam)
64    {
65      int* num = reinterpret_cast<int*> (callBackParam);
66      OH_LOG_INFO(LogType::LOG_APP, "HiCollieTimerNdk callBackParam: %{public}d", *num);  // Print logs in the callback.
67      delete num;
68    }
69
70    static napi_value TestHiCollieTimerNdk(napi_env env, napi_callback_info info)
71    {
72      int id;
73      int* callBackParam = new int(2024);
74      HiCollie_SetTimerParam param = {"testTimer", 1, CallBack, callBackParam, HiCollie_Flag::HICOLLIE_FLAG_LOG};  // Set HiCollieTimer parameters (timer name, timeout interval, callback, callback parameters, and behavior after timeout).
75      HiCollie_ErrorCode errorCode = OH_HiCollie_SetTimer(param, &id);  // Register the HiCollieTimer function to execute a one-off task for timeout detection.
76      if (errorCode == HICOLLIE_SUCCESS) {  // The HiCollieTiimer task is successfully registered.
77        OH_LOG_INFO(LogType::LOG_APP, "HiCollieTimer taskId: %{public}d", id); // Print the task ID.
78        sleep (2); // Simulate the execution of a time-consuming function. Here, the thread is blocked for 2s.
79        OH_HiCollie_CancelTimer (id); // Cancel a registered timer based on the ID.
80      }
81      delete callBackParam;
82      return 0;
83    }
84   ```
85
864. Register **TestHiCollieTimerNdk** as an ArkTS API.
87
88   In the **napi_init.cpp** file, register **TestHiCollieTimerNdk** as an ArkTS API.
89
90   ```c++
91    static napi_value Init(napi_env env, napi_value exports)
92    {
93        napi_property_descriptor desc[] = {
94            { "testHiCollieTimerNdk", nullptr, TestHiCollieTimerNdk, nullptr, nullptr, nullptr, napi_default, nullptr }
95        };
96        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
97        return exports;
98    }
99   ```
100
101   In the **index.d.ts** file, define the ArkTS API.
102
103   ```typescript
104   export const testHiCollieTimerNdk: () => void;
105   ```
106
1075. Edit the **Index.ets** file.
108
109   ```ts
110   import testNapi from 'libentry.so'
111
112   @Entry
113   @Component
114   struct Index {
115     @State message: string = 'Hello World'
116
117     build() {
118       Row() {
119         Column() {
120           Button("testHiCollieTimerNdk")
121             .fontSize(50)
122             .fontWeight(FontWeight.Bold)
123             .onClick(testNapi.testHiCollieTimerNdk);  //Add a click event to trigger the testHiCollieTimerNdk method.
124         }
125         .width('100%')
126       }
127       .height('100%')
128     }
129   }
130   ```
131
1326. Click the **Run** button in DevEco Studio to run the project.
133
1347. At the bottom of DevEco Studio, switch to the **Log** tab and set the filter criteria to **testTag**.
135
136   Click the **testHiCollieTimerNdk** button to execute the program. In the log window, the task ID is displayed. After 2 seconds, the callback content is displayed.
137
138   The timeout logs are stored in: **data/log/eventlog/APP_HICOLLIE-*process ID*-*time*.log** and **syswarning-*bundle name*-*application UID*-*second-level time***.
139