js-apis-hidebug.md 10.4 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.hidebug (HiDebug)
S
shawn_he 已提交
2

S
shawn_he 已提交
3 4
> **NOTE**
>
S
shawn_he 已提交
5 6
> 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 已提交
7
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 已提交
8 9 10

## Modules to Import

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


## hidebug.getNativeHeapSize

getNativeHeapSize(): bigint

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

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

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

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

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

## hidebug.getNativeHeapAllocatedSize

getNativeHeapAllocatedSize(): bigint

S
shawn_he 已提交
39
Obtains the allocated heap memory size of this application.
S
shawn_he 已提交
40

S
shawn_he 已提交
41
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
42

S
shawn_he 已提交
43
**Return value**
S
shawn_he 已提交
44

S
shawn_he 已提交
45 46
| Type  | Description                             |
| ------ | --------------------------------- |
S
shawn_he 已提交
47
| bigint | Allocated heap memory of the application, in KB.|
S
shawn_he 已提交
48

S
shawn_he 已提交
49 50

**Example**
S
shawn_he 已提交
51 52 53
  ```js
  let nativeHeapAllocatedSize = hidebug.getNativeHeapAllocatedSize();
  ```
S
shawn_he 已提交
54 55 56 57 58

## hidebug.getNativeHeapFreeSize

getNativeHeapFreeSize(): bigint

S
shawn_he 已提交
59
Obtains the free heap memory size of this application.
S
shawn_he 已提交
60

S
shawn_he 已提交
61
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
62

S
shawn_he 已提交
63
**Return value**
S
shawn_he 已提交
64

S
shawn_he 已提交
65 66
| Type  | Description                           |
| ------ | ------------------------------- |
S
shawn_he 已提交
67
| bigint | Free heap memory size of the application, in KB.|
S
shawn_he 已提交
68 69

**Example**
S
shawn_he 已提交
70 71 72
  ```js
  let nativeHeapFreeSize = hidebug.getNativeHeapFreeSize();
  ```
S
shawn_he 已提交
73 74 75 76 77

## hidebug.getPss

getPss(): bigint

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

S
shawn_he 已提交
80
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
81

S
shawn_he 已提交
82
**Return value**
S
shawn_he 已提交
83

S
shawn_he 已提交
84 85
| Type  | Description                     |
| ------ | ------------------------- |
S
shawn_he 已提交
86
| bigint | Size of the physical memory actually used by the application process, in KB.|
S
shawn_he 已提交
87 88

**Example**
S
shawn_he 已提交
89 90 91
  ```js
  let pss = hidebug.getPss();
  ```
S
shawn_he 已提交
92 93 94 95 96

## hidebug.getSharedDirty

getSharedDirty(): bigint

S
shawn_he 已提交
97
Obtains the size of the shared dirty memory of a process.
S
shawn_he 已提交
98

S
shawn_he 已提交
99
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
100

S
shawn_he 已提交
101
**Return value**
S
shawn_he 已提交
102

S
shawn_he 已提交
103 104
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
105
| bigint | Size of the shared dirty memory of the process, in KB.|
S
shawn_he 已提交
106

S
shawn_he 已提交
107 108

**Example**
S
shawn_he 已提交
109 110 111
  ```js
  let sharedDirty = hidebug.getSharedDirty();
  ```
S
shawn_he 已提交
112

S
shawn_he 已提交
113 114 115 116
## hidebug.getPrivateDirty<sup>9+<sup>

getPrivateDirty(): bigint

S
shawn_he 已提交
117
Obtains the size of the private dirty memory of a process.
S
shawn_he 已提交
118 119 120

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

S
shawn_he 已提交
121
**Return value**
S
shawn_he 已提交
122

S
shawn_he 已提交
123 124
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
125
| bigint | Size of the private dirty memory of the process, in KB.|
S
shawn_he 已提交
126 127 128 129 130 131 132 133 134 135

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

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

getCpuUsage(): number

S
shawn_he 已提交
136
Obtains the CPU usage of a process.
S
shawn_he 已提交
137 138 139 140 141

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

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

S
shawn_he 已提交
142
**Return value**
S
shawn_he 已提交
143

S
shawn_he 已提交
144 145
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
146
| number | CPU usage of the process.|
S
shawn_he 已提交
147 148 149


**Example**
S
shawn_he 已提交
150 151 152
  ```js
  let cpuUsage = hidebug.getCpuUsage();
  ```
S
shawn_he 已提交
153

S
shawn_he 已提交
154 155
## hidebug.getServiceDump<sup>9+<sup>

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

S
shawn_he 已提交
158
Obtains system service information.
S
shawn_he 已提交
159

S
shawn_he 已提交
160
**Required permissions**: ohos.permission.DUMP
S
shawn_he 已提交
161 162 163 164 165 166 167

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

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
168 169 170
| 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 已提交
171 172 173 174 175


**Example**

```js
S
shawn_he 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
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");
S
shawn_he 已提交
218
  // ...
S
shawn_he 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  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");
S
shawn_he 已提交
247
  // ...
S
shawn_he 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  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 已提交
280
```
S
shawn_he 已提交
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

## hidebug.startProfiling<sup>(deprecated)</sup>

startProfiling(filename : string) : void

> **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`.

**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
hidebug.startProfiling("cpuprofiler-20220216");
// code block
// ...
// code block
hidebug.stopProfiling();
```

## hidebug.stopProfiling<sup>(deprecated)</sup>

stopProfiling() : void

> **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`.

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

**Example**

```js
hidebug.startProfiling("cpuprofiler-20220216");
// code block
// ...
// code block
hidebug.stopProfiling();
```

## hidebug.dumpHeapData<sup>(deprecated)</sup>

dumpHeapData(filename : string) : void

> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hidebug.dumpJsHeapData](#hidebugdumpjsheapdata9) instead.

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
hidebug.dumpHeapData("heap-20220216");
```