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

S
shawn_he 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
S
shawn_he 已提交
4 5 6 7 8 9
> 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.

You can run the hidebug command 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.

## Modules to Import

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


## hidebug.getNativeHeapSize

getNativeHeapSize(): bigint

Obtains the total size of the native heap memory.

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

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

S
shawn_he 已提交
25 26 27
| Type  | Description                       |
| ------ | --------------------------- |
| bigint | Total size of the native heap memory.|
S
shawn_he 已提交
28

S
shawn_he 已提交
29 30

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


## hidebug.getNativeHeapAllocatedSize

getNativeHeapAllocatedSize(): bigint

Obtains the size of the allocated native heap memory.

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


S
shawn_he 已提交
45
**Return value**
S
shawn_he 已提交
46 47 48
| Type  | Description                             |
| ------ | --------------------------------- |
| bigint | Size of the allocated native heap memory.|
S
shawn_he 已提交
49

S
shawn_he 已提交
50 51

**Example**
P
Peter_1988 已提交
52
  ```js
S
shawn_he 已提交
53 54 55 56 57 58 59 60 61 62
  let nativeHeapAllocatedSize = hidebug.getNativeHeapAllocatedSize();
  ```


## hidebug.getNativeHeapFreeSize

getNativeHeapFreeSize(): bigint

Obtains the size of the free native heap memory.

S
shawn_he 已提交
63
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
64 65


S
shawn_he 已提交
66
**Return value**
S
shawn_he 已提交
67 68 69
| Type  | Description                           |
| ------ | ------------------------------- |
| bigint | Size of the free native heap memory.|
S
shawn_he 已提交
70

S
shawn_he 已提交
71 72

**Example**
P
Peter_1988 已提交
73
  ```js
S
shawn_he 已提交
74 75 76 77 78 79 80 81 82 83
  let nativeHeapFreeSize = hidebug.getNativeHeapFreeSize();
  ```


## hidebug.getPss

getPss(): bigint

Obtains the PSS of this process.

S
shawn_he 已提交
84
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
85 86


S
shawn_he 已提交
87
**Return value**
S
shawn_he 已提交
88 89 90
| Type  | Description                     |
| ------ | ------------------------- |
| bigint | PSS of the process.|
S
shawn_he 已提交
91

S
shawn_he 已提交
92 93

**Example**
P
Peter_1988 已提交
94
  ```js
S
shawn_he 已提交
95 96 97 98 99 100 101 102 103 104
  let pss = hidebug.getPss();
  ```


## hidebug.getSharedDirty

getSharedDirty(): bigint

Obtains the size of the shared dirty memory of this process.

S
shawn_he 已提交
105
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
106 107


S
shawn_he 已提交
108
**Return value**
S
shawn_he 已提交
109 110 111
| Type  | Description                      |
| ------ | -------------------------- |
| bigint | Size of the shared dirty memory of the process.|
S
shawn_he 已提交
112

S
shawn_he 已提交
113 114

**Example**
P
Peter_1988 已提交
115
  ```js
S
shawn_he 已提交
116
  let sharedDirty = hidebug.getSharedDirty();
S
shawn_he 已提交
117 118
  ```

S
shawn_he 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
## hidebug.getPrivateDirty<sup>9+<sup>

getPrivateDirty(): bigint

Obtains the size of the private dirty memory of this process.

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


**Return value**
| Type  | Description                      |
| ------ | -------------------------- |
| bigint | Size of the private dirty memory of the process.|


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

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

getCpuUsage(): number

Obtains the CPU usage of this process.

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

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


**Return value**
| Type  | Description                      |
| ------ | -------------------------- |
| number | CPU usage of the process.|


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

## hidebug.startProfiling

startProfiling(filename : string) : void

Starts the profiling method. `startProfiling()` and `stopProfiling()` are called in pairs. `startProfiling()` always occurs before `stopProfiling()`; that is, calling the functions in the following sequences is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.

S
shawn_he 已提交
167 168
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
169 170
**Parameters**

S
shawn_he 已提交
171
| Name  | Type  | Mandatory| Description                                                        |
S
shawn_he 已提交
172
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
173
| 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 已提交
174 175 176 177 178

**Example**

```js
hidebug.startProfiling("cpuprofiler-20220216");
S
shawn_he 已提交
179
// code block
S
shawn_he 已提交
180
// ...
S
shawn_he 已提交
181
// code block
S
shawn_he 已提交
182 183 184 185 186 187 188 189 190
hidebug.stopProfiling();
```



## hidebug.stopProfiling

stopProfiling() : void

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

S
shawn_he 已提交
193 194
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
195 196 197 198
**Example**

```js
hidebug.startProfiling("cpuprofiler-20220216");
S
shawn_he 已提交
199
// code block
S
shawn_he 已提交
200
// ...
S
shawn_he 已提交
201
// code block
S
shawn_he 已提交
202 203 204 205 206 207 208
hidebug.stopProfiling();
```

## hidebug.dumpHeapData

dumpHeapData(filename : string) : void

S
shawn_he 已提交
209
Exports data from the specified heap file.
S
shawn_he 已提交
210

S
shawn_he 已提交
211 212
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
213 214
**Parameters**

S
shawn_he 已提交
215
| Name  | Type  | Mandatory | Description                                                        |
S
shawn_he 已提交
216
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
217
| filename | string | Yes  | User-defined heap file name. The `filename.heapsnapshot` file is generated in the `files` directory of the app based on the specified `filename`.|
S
shawn_he 已提交
218 219 220 221 222 223

**Example**

```js
hidebug.dumpHeapData("heap-20220216");
```
S
shawn_he 已提交
224 225 226 227 228

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

getServiceDump(serviceid : number) : string

S
shawn_he 已提交
229
Obtains information on the specified system service.
S
shawn_he 已提交
230 231 232 233 234 235 236 237 238

This is a system API and cannot be called by third-party applications.

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

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
239
| serviceid | number | Yes  | ID of the system service. |
S
shawn_he 已提交
240 241 242 243

**Return value**
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
244
| string | Absolute path of the file that contains the service information to dump. |
S
shawn_he 已提交
245 246 247 248 249 250 251

**Example**

```js
let serviceId = 10;
let pathName = hidebug.getServiceDump(serviceId);
```