• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用HiDebug获取调试信息(ArkTS)
2
3为应用提供多种以供调试、调优的方法。包括但不限于内存、CPU、GPU、GC等相关数据的获取,进程trace、profiler采集,VM堆快照转储等。由于该模块的接口大多比较耗费性能,接口调用较为耗时,且基于HiDebug模块定义,该模块内的接口仅建议在应用调试,调优阶段使用。若需要在其他场景使用时,请认真评估所需调用的接口对应用性能的影响。
4
5## 接口说明
6
7| 接口名                             | 描述                                                         |
8| ---------------------------------- | ------------------------------------------------------------ |
9| hidebug.getNativeHeapSize          | 获取内存分配器统计的进程持有的堆内存大小(含分配器元数据)。 |
10| hidebug.getNativeHeapAllocatedSize | 获取内存分配器统计的进程业务分配的堆内存大小。               |
11| hidebug.getNativeHeapFreeSize      | 获取内存分配器持有的缓存内存大小。                           |
12| hidebug.getPss                     | 获取应用进程实际使用的物理内存大小。                         |
13| hidebug.getVss                     | 获取应用进程虚拟耗用内存大小。                               |
14| hidebug.getSharedDirty             | 获取进程的共享脏内存大小。                                   |
15| hidebug.getPrivateDirty            | 获取进程的私有脏内存大小。                                   |
16| hidebug.getCpuUsage                | 获取进程的CPU使用率。                                        |
17| hidebug.getServiceDump             | 获取系统服务信息。                                           |
18| hidebug.dumpJsHeapData             | 虚拟机堆导出。                                               |
19| hidebug.startJsCpuProfiling        | 启动虚拟机Profiling方法跟踪。                                |
20| hidebug.stopJsCpuProfiling         | 停止虚拟机Profiling方法跟踪。                                |
21| hidebug.getAppVMMemoryInfo         | 获取VM内存相关信息。                                         |
22| hidebug.getAppThreadCpuUsage       | 获取应用线程CPU使用情况。                                    |
23| hidebug.startAppTraceCapture       | 启动应用trace采集。                                          |
24| hidebug.stopAppTraceCapture        | 停止应用trace采集。                                          |
25| hidebug.getAppMemoryLimit          | 获取应用程序进程内存限制。                                   |
26| hidebug.getSystemCpuUsage          | 获取系统的CPU资源占用情况。                                  |
27| hidebug.setAppResourceLimit        | 设置应用的fd数量、线程数量、js内存或者native内存资源限制。   |
28| hidebug.getAppNativeMemInfo        | 获取应用进程内存信息。                                       |
29| hidebug.getSystemMemInfo           | 获取系统内存信息。                                           |
30| hidebug.getVMRuntimeStats          | 获取系统gc全部统计信息。                                     |
31| hidebug.getVMRuntimeStat           | 根据参数获取指定的系统gc统计信息。                           |
32| hidebug.isDebugState               | 获取应用进程被调试状态。                                     |
33| hidebug.getGraphicsMemory          | 使用异步方式获取应用显存大小。                    |
34| hidebug.getGraphicsMemorySync      | 使用同步方式获取应用显存大小。                    |
35| hidebug.dumpJsRawHeapData          | 为当前线程转储虚拟机的原始堆快照。                  |
36
37HiDebug的具体用法可查看API参考[API参考文档](../reference/apis-performance-analysis-kit/js-apis-hidebug.md)。
38
39## 开发示例
40
41下文将展示如何在应用内增加一个按钮,并单击该按钮以调用hidebug接口。
42
431. 新建一个工程,选择“Empty Ability”。
44
452. 工程配置界面中,**Model**选择“Stage”。
46
473. 在**Project**窗口单击entry > src > main > ets > pages,打开工程中的Index.ets文件。
48
49   新增一个方法调用hidebug接口,本文以hidebug.getSystemCpuUsage()为例,其他接口可参考[API参考文档](../reference/apis-performance-analysis-kit/js-apis-hidebug.md)。
50
51   ```ts
52   import { hidebug } from '@kit.PerformanceAnalysisKit';
53   import { BusinessError } from '@kit.BasicServicesKit';
54   function testHiDebug(event?: ClickEvent) {
55     try {
56       console.info(`getSystemCpuUsage: ${hidebug.getSystemCpuUsage()}`)
57     } catch (error) {
58       console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
59     }
60   }
61   ```
62
63   给文本Text组件添加一个点击事件,示例代码如下:
64
65   ```ts
66   @Entry
67   @Component
68   struct Index {
69     @State message: string = 'Hello World'
70
71     build() {
72       Row() {
73         Column() {
74           Text(this.message)
75             .fontSize(50)
76             .fontWeight(FontWeight.Bold)
77             .onClick(testHiDebug);//添加点击事件
78         }
79         .width('100%')
80       }
81       .height('100%')
82     }
83   }
84   ```
85
864. 在真机上运行该工程,单击应用/服务界面上的“Hello World”文本。
87
885. 在DevEco Studio的底部,切换到“Log”窗口,设置日志的过滤条件为“testTag”。
89
90   此时窗口将显示通过hidebug.getSystemCpuUsage()接口获取的CPU使用率的相关日志。
91   ```Text
92	08-20 11:06:01.891   1948-1948     A03d00/JSAPP                    com.examp...lication  I     getSystemCpuUsage: 0.4722222222222222
93   ```
94
95<!--RP1-->
96<!--RP1End-->