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

S
shawn_he 已提交
3 4
> **NOTE**
> 
S
shawn_he 已提交
5 6 7 8 9 10
> 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 已提交
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 size of the heap memory of this application.
S
shawn_he 已提交
21

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

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

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

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

S
shawn_he 已提交
32 33

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


## hidebug.getNativeHeapAllocatedSize

getNativeHeapAllocatedSize(): bigint

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

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

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

S
shawn_he 已提交
49

S
shawn_he 已提交
50
**Return value**
S
shawn_he 已提交
51 52
| Type  | Description                             |
| ------ | --------------------------------- |
S
shawn_he 已提交
53
| bigint | Size of the allocated heap memory of this application, in kB.|
S
shawn_he 已提交
54

S
shawn_he 已提交
55 56

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


## hidebug.getNativeHeapFreeSize

getNativeHeapFreeSize(): bigint

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

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

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

S
shawn_he 已提交
72

S
shawn_he 已提交
73
**Return value**
S
shawn_he 已提交
74 75
| Type  | Description                           |
| ------ | ------------------------------- |
S
shawn_he 已提交
76
| bigint | Size of the free heap memory of this application, in kB.|
S
shawn_he 已提交
77

S
shawn_he 已提交
78 79

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


## hidebug.getPss

getPss(): bigint

S
shawn_he 已提交
89
Obtains the size of the used physical memory of this process.
S
shawn_he 已提交
90

S
shawn_he 已提交
91
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
92

S
shawn_he 已提交
93

S
shawn_he 已提交
94
**Return value**
S
shawn_he 已提交
95 96
| Type  | Description                     |
| ------ | ------------------------- |
S
shawn_he 已提交
97
| bigint | Used physical memory of this process, in kB.|
S
shawn_he 已提交
98

S
shawn_he 已提交
99 100

**Example**
S
shawn_he 已提交
101 102 103
  ```js
  let pss = hidebug.getPss();
  ```
S
shawn_he 已提交
104 105 106 107 108 109 110 111


## hidebug.getSharedDirty

getSharedDirty(): bigint

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

S
shawn_he 已提交
112
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
113

S
shawn_he 已提交
114

S
shawn_he 已提交
115
**Return value**
S
shawn_he 已提交
116 117
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
118
| bigint | Size of the shared dirty memory of this process, in kB.|
S
shawn_he 已提交
119

S
shawn_he 已提交
120 121

**Example**
S
shawn_he 已提交
122 123 124
  ```js
  let sharedDirty = hidebug.getSharedDirty();
  ```
S
shawn_he 已提交
125

S
shawn_he 已提交
126 127 128 129 130 131 132 133
## hidebug.getPrivateDirty<sup>9+<sup>

getPrivateDirty(): bigint

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

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

S
shawn_he 已提交
134

S
shawn_he 已提交
135
**Return value**
S
shawn_he 已提交
136 137
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
138
| bigint | Size of the private dirty memory of this process, in kB.|
S
shawn_he 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155


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

S
shawn_he 已提交
156

S
shawn_he 已提交
157
**Return value**
S
shawn_he 已提交
158 159
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
160
| number | CPU usage of this process.|
S
shawn_he 已提交
161 162 163


**Example**
S
shawn_he 已提交
164 165 166
  ```js
  let cpuUsage = hidebug.getCpuUsage();
  ```
S
shawn_he 已提交
167 168 169 170 171 172 173

## 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 已提交
174 175
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

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

S
shawn_he 已提交
178
| Name  | Type  | Mandatory| Description                                                        |
S
shawn_he 已提交
179
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
180
| 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 已提交
181 182 183 184 185

**Example**

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


S
shawn_he 已提交
193

S
shawn_he 已提交
194 195 196 197
## hidebug.stopProfiling

stopProfiling() : void

S
shawn_he 已提交
198
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 已提交
199

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

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

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

## hidebug.dumpHeapData

dumpHeapData(filename : string) : void

S
shawn_he 已提交
216
Exports data from the specified heap file.
S
shawn_he 已提交
217

S
shawn_he 已提交
218 219
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
220 221
**Parameters**

S
shawn_he 已提交
222
| Name  | Type  | Mandatory | Description                                                        |
S
shawn_he 已提交
223
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
224
| 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 已提交
225 226 227 228 229 230

**Example**

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

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

getServiceDump(serviceid : number) : string

S
shawn_he 已提交
236
Obtains information on the specified system service.
S
shawn_he 已提交
237 238 239 240 241 242 243 244 245

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 已提交
246
| serviceid | number | Yes  | ID of the system service. |
S
shawn_he 已提交
247 248 249 250

**Return value**
| Type  | Description                      |
| ------ | -------------------------- |
S
shawn_he 已提交
251
| string | Absolute path of the file that contains the service information to dump. |
S
shawn_he 已提交
252 253 254 255 256 257 258

**Example**

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