• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hidebug (Debug调试)
2
3> **说明:**
4>
5> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
6
7使用hidebug,可以获取应用内存的使用情况,包括应用进程的静态堆内存(native heap)信息、应用进程内存占用PSS(Proportional Set Size)信息等;可以完成虚拟机内存切片导出,虚拟机CPU Profiling采集等操作。
8
9## 导入模块
10
11```ts
12import hidebug from '@ohos.hidebug';
13```
14
15
16## hidebug.getNativeHeapSize
17
18getNativeHeapSize(): bigint
19
20获取本应用堆内存的总大小。
21
22**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
23
24**返回值:**
25
26| 类型   | 说明                        |
27| ------ | --------------------------- |
28| bigint | 返回本应用堆内存总大小,单位为Byte。 |
29
30**示例:**
31  ```ts
32  let nativeHeapSize: bigint = hidebug.getNativeHeapSize();
33  ```
34
35## hidebug.getNativeHeapAllocatedSize
36
37getNativeHeapAllocatedSize(): bigint
38
39获取本应用堆内存的已分配内存大小。
40
41**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
42
43**返回值:**
44
45| 类型   | 说明                              |
46| ------ | --------------------------------- |
47| bigint | 返回本应用堆内存的已分配内存,单位为Byte。 |
48
49
50**示例:**
51  ```ts
52  let nativeHeapAllocatedSize: bigint = hidebug.getNativeHeapAllocatedSize();
53  ```
54
55## hidebug.getNativeHeapFreeSize
56
57getNativeHeapFreeSize(): bigint
58
59获取本应用堆内存的空闲内存大小。
60
61**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
62
63**返回值:**
64
65| 类型   | 说明                            |
66| ------ | ------------------------------- |
67| bigint | 返回本应用堆内存的空闲内存,单位为Byte。 |
68
69**示例:**
70  ```ts
71  let nativeHeapFreeSize: bigint = hidebug.getNativeHeapFreeSize();
72  ```
73
74## hidebug.getPss
75
76getPss(): bigint
77
78获取应用进程实际使用的物理内存大小。
79
80**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
81
82**返回值:**
83
84| 类型   | 说明                      |
85| ------ | ------------------------- |
86| bigint | 返回应用进程实际使用的物理内存大小,单位为kB。 |
87
88**示例:**
89  ```ts
90  let pss: bigint = hidebug.getPss();
91  ```
92
93## hidebug.getSharedDirty
94
95getSharedDirty(): bigint
96
97获取进程的共享脏内存大小。
98
99**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
100
101**返回值:**
102
103| 类型   | 说明                       |
104| ------ | -------------------------- |
105| bigint | 返回进程的共享脏内存大小,单位为kB。 |
106
107
108**示例:**
109  ```ts
110  let sharedDirty: bigint = hidebug.getSharedDirty();
111  ```
112
113## hidebug.getPrivateDirty<sup>9+<sup>
114
115getPrivateDirty(): bigint
116
117获取进程的私有脏内存大小。
118
119**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
120
121**返回值:**
122
123| 类型   | 说明                       |
124| ------ | -------------------------- |
125| bigint | 返回进程的私有脏内存大小,单位为kB。 |
126
127**示例:**
128  ```ts
129  let privateDirty: bigint = hidebug.getPrivateDirty();
130  ```
131
132## hidebug.getCpuUsage<sup>9+<sup>
133
134getCpuUsage(): number
135
136获取进程的CPU使用率。
137
138如占用率为50%,则返回0.5。
139
140**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
141
142**返回值:**
143
144| 类型   | 说明                       |
145| ------ | -------------------------- |
146| number | 获取进程的CPU使用率。 |
147
148
149**示例:**
150  ```ts
151  let cpuUsage: number = hidebug.getCpuUsage();
152  ```
153
154## hidebug.getServiceDump<sup>9+<sup>
155
156getServiceDump(serviceid : number, fd : number, args : Array\<string>) : void
157
158获取系统服务信息。
159
160**需要权限**: ohos.permission.DUMP
161
162**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
163
164**参数:**
165
166| 参数名   | 类型   | 必填 | 说明                                                         |
167| -------- | ------ | ---- | ------------------------------------------------------------ |
168| serviceid | number | 是   | 基于该用户输入的service id获取系统服务信息。|
169| fd | number | 是   | 文件描述符,该接口会往该fd中写入数据。|
170| args | Array\<string> | 是   | 系统服务的Dump接口所对应的参数列表。|
171
172**错误码:**
173
174以下错误码的详细介绍请参见[系统事件错误码](../errorcodes/errorcode-hiviewdfx-hidebug.md)。
175
176| 错误码ID | 错误信息 |
177| ------- | ----------------------------------------------------------------- |
178| 11400101 | the service id is invalid                                           |
179| 401 | the parameter check failed                                            |
180
181**示例:**
182
183```ts
184import fs from '@ohos.file.fs'
185import hidebug from '@ohos.hidebug'
186import common from '@ohos.app.ability.common'
187import { BusinessError } from '@ohos.base'
188
189let applicationContext: common.Context | null = null;
190try {
191  applicationContext = this.context.getApplicationContext();
192} catch (error) {
193  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
194}
195
196let filesDir: string = applicationContext!.filesDir;
197let path: string = filesDir + "/serviceInfo.txt";
198console.info("output path: " + path);
199let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
200let serviceId: number = 10;
201let args: Array<string> = new Array("allInfo");
202
203try {
204  hidebug.getServiceDump(serviceId, file.fd, args);
205} catch (error) {
206  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
207}
208fs.closeSync(file);
209```
210
211## hidebug.startJsCpuProfiling<sup>9+</sup>
212
213startJsCpuProfiling(filename : string) : void
214
215启动虚拟机Profiling方法跟踪,`startJsCpuProfiling()`方法的调用需要与`stopJsCpuProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
216
217**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
218
219**参数:**
220
221| 参数名   | 类型   | 必填 | 说明                                                         |
222| -------- | ------ | ---- | ------------------------------------------------------------ |
223| filename | string | 是   | 用户自定义的profiling文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.json`文件。 |
224
225**错误码:**
226
227以下错误码的详细介绍请参见[系统事件错误码](../errorcodes/errorcode-universal.md)。
228
229| 错误码ID | 错误信息 |
230| ------- | ----------------------------------------------------------------- |
231| 401 | the parameter check failed                                            |
232
233**示例:**
234
235```ts
236import hidebug from '@ohos.hidebug'
237import { BusinessError } from '@ohos.base'
238
239try {
240  hidebug.startJsCpuProfiling("cpu_profiling");
241  // ...
242  hidebug.stopJsCpuProfiling();
243} catch (error) {
244  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
245}
246```
247
248## hidebug.stopJsCpuProfiling<sup>9+</sup>
249
250stopJsCpuProfiling() : void
251
252停止虚拟机Profiling方法跟踪,`startJsCpuProfiling()`方法的调用需要与`stopJsCpuProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
253
254**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
255
256**参数:**
257
258| 参数名   | 类型   | 必填 | 说明                                                         |
259| -------- | ------ | ---- | ------------------------------------------------------------ |
260| filename | string | 是   | 用户自定义的profiling文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.json`文件。 |
261
262**示例:**
263
264```ts
265import hidebug from '@ohos.hidebug'
266import { BusinessError } from '@ohos.base'
267
268try {
269  hidebug.startJsCpuProfiling("cpu_profiling");
270  // ...
271  hidebug.stopJsCpuProfiling();
272} catch (error) {
273  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
274}
275```
276
277## hidebug.dumpJsHeapData<sup>9+</sup>
278
279dumpJsHeapData(filename : string) : void
280
281虚拟机堆导出。
282
283**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
284
285**参数:**
286
287| 参数名   | 类型   | 必填 | 说明                                                         |
288| -------- | ------ | ---- | ------------------------------------------------------------ |
289| filename | string | 是   | 用户自定义的虚拟机堆文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.heapsnapshot`文件。 |
290
291**错误码:**
292
293以下错误码的详细介绍请参见[系统事件错误码](../errorcodes/errorcode-universal.md)。
294
295| 错误码ID | 错误信息 |
296| ------- | ----------------------------------------------------------------- |
297| 401 | the parameter check failed                                            |
298
299**示例:**
300
301```ts
302import hidebug from '@ohos.hidebug'
303import { BusinessError } from '@ohos.base'
304
305try {
306  hidebug.dumpJsHeapData("heapData");
307} catch (error) {
308  console.info(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
309}
310```
311
312## hidebug.startProfiling<sup>(deprecated)</sup>
313
314startProfiling(filename : string) : void
315
316> **说明:** 从 API Version 9 开始废弃,建议使用[hidebug.startJsCpuProfiling](#hidebugstartjscpuprofiling9)替代。
317
318启动虚拟机Profiling方法跟踪,`startProfiling()`方法的调用需要与`stopProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
319
320**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
321
322**参数:**
323
324| 参数名   | 类型   | 必填 | 说明                                                         |
325| -------- | ------ | ---- | ------------------------------------------------------------ |
326| filename | string | 是   | 用户自定义的profiling文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.json`文件。 |
327
328**示例:**
329
330```ts
331hidebug.startProfiling("cpuprofiler-20220216");
332// code block
333// ...
334// code block
335hidebug.stopProfiling();
336```
337
338## hidebug.stopProfiling<sup>(deprecated)</sup>
339
340stopProfiling() : void
341
342> **说明:** 从 API Version 9 开始废弃,建议使用[hidebug.stopJsCpuProfiling](#hidebugstopjscpuprofiling9)替代。
343
344停止虚拟机Profiling方法跟踪,`stopProfiling()`方法的调用需要与`startProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
345
346**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
347
348**示例:**
349
350```ts
351hidebug.startProfiling("cpuprofiler-20220216");
352// code block
353// ...
354// code block
355hidebug.stopProfiling();
356```
357
358## hidebug.dumpHeapData<sup>(deprecated)</sup>
359
360dumpHeapData(filename : string) : void
361
362> **说明:** 从 API Version 9 开始废弃,建议使用[hidebug.dumpJsHeapData](#hidebugdumpjsheapdata9)替代。
363
364虚拟机堆导出。
365
366**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
367
368**参数:**
369
370| 参数名   | 类型   | 必填 | 说明                                                         |
371| -------- | ------ | ---- | ------------------------------------------------------------ |
372| filename | string | 是   | 用户自定义的虚拟机堆文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.heapsnapshot`文件。 |
373
374**示例:**
375
376```ts
377hidebug.dumpHeapData("heap-20220216");
378```
379