js-apis-hidebug.md 10.6 KB
Newer Older
S
shawn_he 已提交
1 2
# HiDebug

S
shawn_he 已提交
3
> **NOTE**
S
shawn_he 已提交
4 5
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

S
shawn_he 已提交
6
This module provides APIs for you to obtain the memory usage of an application, including the static heap memory (native heap) and proportional set size (PSS) occupied by the application process. You can also export VM memory slices and collect VM CPU profiling data.
S
shawn_he 已提交
7 8 9

## Modules to Import

P
Peter_1988 已提交
10
```js
S
shawn_he 已提交
11 12 13 14 15 16 17 18
import hidebug from '@ohos.hidebug';
```


## hidebug.getNativeHeapSize

getNativeHeapSize(): bigint

S
shawn_he 已提交
19
Obtains the total heap memory size of this application.
S
shawn_he 已提交
20

S
shawn_he 已提交
21 22
This API is defined but not implemented in OpenHarmony 3.1 Release.

S
shawn_he 已提交
23
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
24

S
shawn_he 已提交
25
**Return value**
S
shawn_he 已提交
26

S
shawn_he 已提交
27 28
| Type  | Description                       |
| ------ | --------------------------- |
S
shawn_he 已提交
29
| bigint | Total heap memory size of the application, in KB.|
S
shawn_he 已提交
30

S
shawn_he 已提交
31 32

**Example**
S
shawn_he 已提交
33 34 35
  ```js
  let nativeHeapSize = hidebug.getNativeHeapSize();
  ```
S
shawn_he 已提交
36 37 38 39 40 41


## hidebug.getNativeHeapAllocatedSize

getNativeHeapAllocatedSize(): bigint

S
shawn_he 已提交
42
Obtains the allocated heap memory size of this application.
S
shawn_he 已提交
43

S
shawn_he 已提交
44 45
This API is defined but not implemented in OpenHarmony 3.1 Release.

S
shawn_he 已提交
46
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
47

S
shawn_he 已提交
48
**Return value**
S
shawn_he 已提交
49

S
shawn_he 已提交
50 51
| Type  | Description                             |
| ------ | --------------------------------- |
S
shawn_he 已提交
52
| bigint | Allocated heap memory of the application, in KB.|
S
shawn_he 已提交
53

S
shawn_he 已提交
54 55

**Example**
S
shawn_he 已提交
56 57 58
  ```js
  let nativeHeapAllocatedSize = hidebug.getNativeHeapAllocatedSize();
  ```
S
shawn_he 已提交
59 60 61 62 63 64


## hidebug.getNativeHeapFreeSize

getNativeHeapFreeSize(): bigint

S
shawn_he 已提交
65
Obtains the free heap memory size of this application.
S
shawn_he 已提交
66

S
shawn_he 已提交
67 68
This API is defined but not implemented in OpenHarmony 3.1 Release.

S
shawn_he 已提交
69
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
70

S
shawn_he 已提交
71
**Return value**
S
shawn_he 已提交
72

S
shawn_he 已提交
73 74
| Type  | Description                           |
| ------ | ------------------------------- |
S
shawn_he 已提交
75
| bigint | Free heap memory size of the application, in KB.|
S
shawn_he 已提交
76 77

**Example**
S
shawn_he 已提交
78 79 80
  ```js
  let nativeHeapFreeSize = hidebug.getNativeHeapFreeSize();
  ```
S
shawn_he 已提交
81 82 83 84 85 86


## hidebug.getPss

getPss(): bigint

S
shawn_he 已提交
87
Obtains the size of the physical memory actually used by the application process.
S
shawn_he 已提交
88

S
shawn_he 已提交
89
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
90

S
shawn_he 已提交
91
**Return value**
S
shawn_he 已提交
92

S
shawn_he 已提交
93 94
| Type  | Description                     |
| ------ | ------------------------- |
S
shawn_he 已提交
95
| bigint | Size of the physical memory actually used by the application process, in KB.|
S
shawn_he 已提交
96 97

**Example**
S
shawn_he 已提交
98 99 100
  ```js
  let pss = hidebug.getPss();
  ```
S
shawn_he 已提交
101 102 103 104 105 106


## hidebug.getSharedDirty

getSharedDirty(): bigint

S
shawn_he 已提交
107
Obtains the size of the shared dirty memory of a process.
S
shawn_he 已提交
108

S
shawn_he 已提交
109
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
110

S
shawn_he 已提交
111
**Return value**
S
shawn_he 已提交
112

S
shawn_he 已提交
113 114
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
115
| bigint | Size of the shared dirty memory of the process, in KB.|
S
shawn_he 已提交
116

S
shawn_he 已提交
117 118

**Example**
S
shawn_he 已提交
119 120 121
  ```js
  let sharedDirty = hidebug.getSharedDirty();
  ```
S
shawn_he 已提交
122

S
shawn_he 已提交
123 124 125 126
## hidebug.getPrivateDirty<sup>9+<sup>

getPrivateDirty(): bigint

S
shawn_he 已提交
127
Obtains the size of the private dirty memory of a process.
S
shawn_he 已提交
128 129 130

**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
131
**Return value**
S
shawn_he 已提交
132

S
shawn_he 已提交
133 134
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
135
| bigint | Size of the private dirty memory of the process, in KB.|
S
shawn_he 已提交
136 137 138 139 140 141 142 143 144 145 146


**Example**
  ```js
  let privateDirty = hidebug.getPrivateDirty();
  ```

## hidebug.getCpuUsage<sup>9+<sup>

getCpuUsage(): number

S
shawn_he 已提交
147
Obtains the CPU usage of a process.
S
shawn_he 已提交
148 149 150 151 152

For example, if the CPU usage is **50%**, **0.5** is returned.

**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
153
**Return value**
S
shawn_he 已提交
154

S
shawn_he 已提交
155 156
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
157
| number | CPU usage of the process.|
S
shawn_he 已提交
158 159 160


**Example**
S
shawn_he 已提交
161 162 163
  ```js
  let cpuUsage = hidebug.getCpuUsage();
  ```
S
shawn_he 已提交
164

S
shawn_he 已提交
165
## hidebug.startProfiling<sup>(deprecated)</sup>
S
shawn_he 已提交
166 167 168

startProfiling(filename : string) : void

S
shawn_he 已提交
169 170 171
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hidebug.startJsCpuProfiling](#hidebugstartjscpuprofiling9) instead.

Starts the profiling method. `startProfiling()` and `stopProfiling()` are called in pairs. `startProfiling()` always occurs before `stopProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.
S
shawn_he 已提交
172

S
shawn_he 已提交
173 174
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
175 176
**Parameters**

S
shawn_he 已提交
177
| Name  | Type  | Mandatory| Description                                                        |
S
shawn_he 已提交
178
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
179
| filename | string | Yes  | User-defined profile name. The `filename.json` file is generated in the `files` directory of the application based on the specified `filename`.|
S
shawn_he 已提交
180 181 182 183 184

**Example**

```js
hidebug.startProfiling("cpuprofiler-20220216");
S
shawn_he 已提交
185
// code block
S
shawn_he 已提交
186
// ...
S
shawn_he 已提交
187
// code block
S
shawn_he 已提交
188 189 190 191
hidebug.stopProfiling();
```


S
shawn_he 已提交
192

S
shawn_he 已提交
193
## hidebug.stopProfiling<sup>(deprecated)</sup>
S
shawn_he 已提交
194 195 196

stopProfiling() : void

S
shawn_he 已提交
197 198 199
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hidebug.stopJsCpuProfiling](#hidebugstopjscpuprofiling9) instead.

Stops the profiling method. `startProfiling()` and `stopProfiling()` are called in pairs. `startProfiling()` always occurs before `stopProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.
S
shawn_he 已提交
200

S
shawn_he 已提交
201 202
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
203 204 205 206
**Example**

```js
hidebug.startProfiling("cpuprofiler-20220216");
S
shawn_he 已提交
207
// code block
S
shawn_he 已提交
208
// ...
S
shawn_he 已提交
209
// code block
S
shawn_he 已提交
210 211 212
hidebug.stopProfiling();
```

S
shawn_he 已提交
213
## hidebug.dumpHeapData<sup>(deprecated)</sup>
S
shawn_he 已提交
214 215 216

dumpHeapData(filename : string) : void

S
shawn_he 已提交
217 218 219
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hidebug.dumpJsHeapData](#hidebugdumpjsheapdata9) instead.

Exports the heap data.
S
shawn_he 已提交
220

S
shawn_he 已提交
221 222
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
223 224
**Parameters**

S
shawn_he 已提交
225
| Name  | Type  | Mandatory| Description                                                        |
S
shawn_he 已提交
226
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
227
| filename | string | Yes  | User-defined heap file name. The `filename.heapsnapshot` file is generated in the `files` directory of the application based on the specified `filename`.|
S
shawn_he 已提交
228 229 230 231 232 233

**Example**

```js
hidebug.dumpHeapData("heap-20220216");
```
S
shawn_he 已提交
234 235 236

## hidebug.getServiceDump<sup>9+<sup>

S
shawn_he 已提交
237
getServiceDump(serviceid : number, fd : number, args : Array\<string>) : void
S
shawn_he 已提交
238

S
shawn_he 已提交
239
Obtains system service information.
S
shawn_he 已提交
240

S
shawn_he 已提交
241
**Required permissions**: ohos.permission.DUMP
S
shawn_he 已提交
242 243 244 245 246 247 248

**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
249 250 251
| serviceid | number | Yes  | Obtains the system service information based on the specified service ID.|
| fd | number | Yes  | File descriptor to which data is written by the API.|
| args | Array\<string> | Yes  | Parameter list corresponding to the **Dump** API of the system service.|
S
shawn_he 已提交
252 253 254 255 256


**Example**

```js
S
shawn_he 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
import fileio from '@ohos.fileio'
import hidebug from '@ohos.hidebug'
import featureAbility from '@ohos.ability.featureAbility'

let context = featureAbility.getContext();
context.getFilesDir().then((data) => {
  var path = data + "/serviceInfo.txt"
  console.info("output path: " + path)
  let fd = fileio.openSync(path, 0o102, 0o666)
  var serviceId = 10
  var args = new Array("allInfo")
  try {
    hidebug.getServiceDump(serviceId, fd, args)
  } catch (error) {
    console.info(error.code)
    console.info(error.message)
  }
  fileio.closeSync(fd);
})
```

## hidebug.startJsCpuProfiling<sup>9+</sup>

startJsCpuProfiling(filename : string) : void

Starts the profiling method. `startJsCpuProfiling()` and `stopJsCpuProfiling()` are called in pairs. `startJsCpuProfiling()` always occurs before `stopJsCpuProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.

**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filename | string | Yes  | User-defined profile name. The `filename.json` file is generated in the `files` directory of the application based on the specified `filename`.|

**Example**

```js
import hidebug from '@ohos.hidebug'

try {
  hidebug.startJsCpuProfiling("cpu_profiling");
  ...
  hidebug.stopJsCpuProfiling();
} catch (error) {
  console.info(error.code)
  console.info(error.message)
}
```

## hidebug.stopJsCpuProfiling<sup>9+</sup>

stopJsCpuProfiling() : void

Stops the profiling method. `startJsCpuProfiling()` and `stopJsCpuProfiling()` are called in pairs. `startJsCpuProfiling()` always occurs before `stopJsCpuProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.

**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filename | string | Yes  | User-defined profile name. The `filename.json` file is generated in the `files` directory of the application based on the specified `filename`.|

**Example**

```js
import hidebug from '@ohos.hidebug'

try {
  hidebug.startJsCpuProfiling("cpu_profiling");
  ...
  hidebug.stopJsCpuProfiling();
} catch (error) {
  console.info(error.code)
  console.info(error.message)
}
```

## hidebug.dumpJsHeapData<sup>9+</sup>

dumpJsHeapData(filename : string) : void

Exports the heap data.

**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filename | string | Yes  | User-defined heap file name. The `filename.heapsnapshot` file is generated in the `files` directory of the application based on the specified `filename`.|

**Example**

```js
import hidebug from '@ohos.hidebug'

try {
  hidebug.dumpJsHeapData("heapData");
} catch (error) {
  console.info(error.code)
  console.info(error.message)
}
S
shawn_he 已提交
361
```