Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
c107eabf
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c107eabf
编写于
3月 21, 2023
作者:
S
shanghuanni
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
release3.2 hitracemeter-guidelines.md 文档格式修改
Signed-off-by:
N
shanghuanni
<
shanghuanni1@huawei.com
>
上级
29f72f11
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
171 addition
and
162 deletion
+171
-162
zh-cn/device-dev/subsystems/subsys-dfx-hitracemeter.md
zh-cn/device-dev/subsystems/subsys-dfx-hitracemeter.md
+171
-162
未找到文件。
zh-cn/device-dev/subsystems/subsys-dfx-hitracemeter.md
浏览文件 @
c107eabf
...
...
@@ -7,6 +7,7 @@ HiTraceMeter在OpenHarmony中,为开发者提供业务流程调用链跟踪的
## 基本概念
HiTraceMeter系统主要分为三部分:
-
JS/C++应用打点API;
-
Trace数据采集命令行工具;
-
Trace数据图形分析工具。
...
...
@@ -80,14 +81,13 @@ HiTraceMeter主要提供抓取用户态和内核态Trace数据的命令行工具
## 约束与限制
-
HiTraceMeter所有功能与接口的实现都依赖于内核提供的ftrace功能,ftrace 是内核提供的一个 framework,采用 plugin 的方式支持开发人员添加更多种类的 trace 功能,因此使用HiTraceMeter之前要使能 ftrace,否则HiTraceMeter的功能无法使用(目前大部分Linux内核默认使能了ftrace,关于ftrace的详细介绍可查看内核ftrace相关资料
[
ftrace相关资料
](
https://blog.csdn.net/Luckiers/article/details/124646205
)
)。
[
ftrace相关资料
](
https://blog.csdn.net/Luckiers/article/details/124646205
)
)。
-
HiTraceMeter仅限小型系统、标准系统下使用。
# HiTraceMeter开发指导
...
...
@@ -95,7 +95,6 @@ HiTraceMeter分为JS/C++应用打点API与数据采集命令行工具hitrace,
## 场景介绍
在实际开发过程中,开发者可能会遇到app卡顿或者在代码调试过程中需要查看代码调用流程,HiTraceMeter接口提供了相应的接口来跟踪程序延时和代码调用流程,分析性能问题。
...
...
@@ -106,20 +105,19 @@ C++接口仅系统开发者使用,JS(目前暂未开放js接口)应用开
**表 1**
同步接口
| Sync trace | 功能描述 |参数说明 |
| :----------------------------------------------------------- | ------------- |------------- |
| void StartTrace(uint64_t label, const std::string& value, float limit = -1); | 启动同步trace |label: Trace category。
<br
/>
value: Trace携带的信息,表明当前的某种状态,例如内存大小,队列长短等。 |
| void FinishTrace(uint64_t label); | 关闭同步trace |label: Trace category。 |
| Sync trace | 功能描述 | 参数说明 |
|:---------------------------------------------------------------------------- | --------- | --------------------------------------------------------------------- |
| void StartTrace(uint64_t label, const std::string& value, float limit = -1); | 启动同步trace | label: Trace category。
<br
/>
value: Trace携带的信息,表明当前的某种状态,例如内存大小,队列长短等。 |
| void FinishTrace(uint64_t label); | 关闭同步trace | label: Trace category。 |
同步接口StartTrace和FinishTrace必须配对使用,FinishTrace和前面最近的StartTrace进行匹配。StartTrace和FinishTrace函数对可以嵌套模式使用,跟踪数据解析时使用栈式数据结构进行匹配。接口中的limit参数用于限流,使用默认值即可。
**表 2**
异步接口
| Async trace
| 功能描述 |参数说明
|
| ------------------------------------------------------------
| ------------- |
------------- |
| Async trace
| 功能描述 | 参数说明
|
| ------------------------------------------------------------
------------------------------------- | --------- | ---------------------------------------------------------------------------------------
------------- |
| void StartAsyncTrace(uint64_t label, const std::string& value, int32_t taskId, float limit = -1); | 开启异步trace | label: Trace category。
<br
/>
value: Trace携带的信息,表明当前的某种状态,例如内存大小,队列长短等。
<br
/>
taskId:异步Trace中用来表示关联的ID。 |
| void FinishAsyncTrace(uint64_t label, const std::string& value, int32_t taskId); | 关闭异步trace | label: Trace category。
<br
/>
value: Trace携带的信息,表明当前的某种状态,例如内存大小,队列长短等。
<br
/>
taskId:异步Trace中用来表示关联的ID。 |
| void FinishAsyncTrace(uint64_t label, const std::string& value, int32_t taskId);
| 关闭异步trace | label: Trace category。
<br
/>
value: Trace携带的信息,表明当前的某种状态,例如内存大小,队列长短等。
<br
/>
taskId:异步Trace中用来表示关联的ID。 |
...
...
@@ -127,145 +125,144 @@ C++接口仅系统开发者使用,JS(目前暂未开放js接口)应用开
**表 3**
计数器接口
| Counter Trace | 功能描述 |参数说明 |
| ------------------------------------------------------------ | --------- |--------- |
| void CountTrace(uint64_t label, const std::string& name, int64_t); | 计数trace |label: Trace category。
<br
/>
name: Trace的名称,IDE中会以此字段展示这段Trace。 |
| Counter Trace | 功能描述 | 参数说明 |
| ------------------------------------------------------------------ | ------- | -------------------------------------------------------------- |
| void CountTrace(uint64_t label, const std::string& name, int64_t); | 计数trace | label: Trace category。
<br
/>
name: Trace的名称,IDE中会以此字段展示这段Trace。 |
## 开发步骤
1.
编译依赖添加,需要修改的编译配置文件base
\h
iviewdfx
\h
itrace
\c
md
\B
UILD.gn 。
```
external_deps = [ "hitrace_native:hitrace_meter"]
```
```
external_deps = [ "hitrace_native:hitrace_meter"]
```
2.
头文件依赖添加。
```
cpp
#include "hitrace_meter.h"//接口函数定义头文件
```
```
cpp
#include "hitrace_meter.h"//接口函数定义头文件
```
3.
接口调用,将需要跟踪的Trace value传入参数,目前HiTraceMeter支持的Trace Tag在基本概念hitrace_meter.h中都已列出,我们以OHOS这个Tag为例,假设我们需要获取func1,func2函数的Trace数据,参考下面实例,在shell中执行hitrace命令后会自动抓取Trace数据,抓到的Trace数据中包括了函数调用过程以及调用过程消耗的内存和时间,可用于分析代码调用流程,代码性能问题。
```
cpp
#include "hitrace_meter.h" // 包含hitrace_meter.h
using
namespace
std
;
int
main
()
{
uint64_t
label
=
BYTRACE_TAG_OHOS
;
sleep
(
1
);
CountTrace
(
label
,
"count number"
,
2000
);
// 整数跟踪
StartTrace
(
label
,
"func1Trace"
,
-
1
);
// func1Start的跟踪起始点
sleep
(
1
);
StartTrace
(
label
,
"func2Trace"
,
-
1
);
// func2Start的跟踪起始点
sleep
(
2
);
FinishTrace
(
label
);
// func2Trace的结束点
sleep
(
1
);
FinishTrace
(
label
);
// func1Trace的结束点
StartAsyncTrace
(
label
,
"asyncTrace1"
,
1234
);
// 异步asyncTrace1的开始点
FinishAsyncTrace
(
label
,
"asyncTrace1"
,
1234
);
// 异步asyncTrace2的结束点
return
0
;
}
```
```
cpp
#include "hitrace_meter.h" // 包含hitrace_meter.h
using
namespace
std
;
int
main
()
{
uint64_t
label
=
BYTRACE_TAG_OHOS
;
sleep
(
1
);
CountTrace
(
label
,
"count number"
,
2000
);
// 整数跟踪
StartTrace
(
label
,
"func1Trace"
,
-
1
);
// func1Start的跟踪起始点
sleep
(
1
);
StartTrace
(
label
,
"func2Trace"
,
-
1
);
// func2Start的跟踪起始点
sleep
(
2
);
FinishTrace
(
label
);
// func2Trace的结束点
sleep
(
1
);
FinishTrace
(
label
);
// func1Trace的结束点
StartAsyncTrace
(
label
,
"asyncTrace1"
,
1234
);
// 异步asyncTrace1的开始点
FinishAsyncTrace
(
label
,
"asyncTrace1"
,
1234
);
// 异步asyncTrace1的结束点
return
0
;
}
```
4.
使用方法,打点编译部署完成后,运行下面命令行来抓取Trace。然后在端侧shell里运行应用,可以抓取到Trace数据。
```
hdc_std shell hitrace -t 10 ohos > .\myapp_demo.ftrace
```
抓取之后的数据可以在smartperf中"Open trace file"或者直接拖入图形区打开,关于smartperf的详细介绍可查看
[
smartperf
](
https://toscode.gitee.com/openharmony-sig/smartperf
)
。
```
hdc_std shell hitrace -t 10 ohos > .\myapp_demo.ftrace
```
抓取之后的数据可以在smartperf中"Open trace file"或者直接拖入图形区打开,关于smartperf的详细介绍可查看
[
smartperf
](
https://toscode.gitee.com/openharmony-sig/smartperf
)
。
## 调测验证
以下为一个demo调试过程,该demo使用了同步接口中的StartTrace和FinishTrace。
1.
编写测试代码hitrace_example.cpp(
[
hitrace_example.cpp
](
https://gitee.com/openharmony/hiviewdfx_hitrace/blob/master/cmd/example/hitrace_example.cpp
)
),将使用到的接口加入代码:
```
cpp
int
main
()
{
thread
t1
(
ThreadFunc1
);
t1
.
join
();
StartTrace
(
LABEL
,
"testStart"
);
sleep
(
SLEEP_ONE_SECOND
);
StartTrace
(
LABEL
,
"funcAStart"
,
SLEEP_ONE_SECOND
);
// 打印起始点
FuncA
();
FinishTrace
(
LABEL
);
sleep
(
SLEEP_TWO_SECOND
);
thread
t2
(
ThreadFunc2
);
t2
.
join
();
StartTrace
(
LABEL
,
"funcBStart"
,
SLEEP_TWO_SECOND
);
FuncB
();
FinishTrace
(
LABEL
);
// 打印结束点
sleep
(
SLEEP_TWO_SECOND
);
sleep
(
SLEEP_ONE_SECOND
);
FinishTrace
(
LABEL
);
FuncC
();
return
0
;
}
```
```
cpp
int
main
()
{
thread
t1
(
ThreadFunc1
);
t1
.
join
();
StartTrace
(
LABEL
,
"testStart"
);
sleep
(
SLEEP_ONE_SECOND
);
StartTrace
(
LABEL
,
"funcAStart"
,
SLEEP_ONE_SECOND
);
// 打印起始点
FuncA
();
FinishTrace
(
LABEL
);
sleep
(
SLEEP_TWO_SECOND
);
thread
t2
(
ThreadFunc2
);
t2
.
join
();
StartTrace
(
LABEL
,
"funcBStart"
,
SLEEP_TWO_SECOND
);
FuncB
();
FinishTrace
(
LABEL
);
// 打印结束点
sleep
(
SLEEP_TWO_SECOND
);
sleep
(
SLEEP_ONE_SECOND
);
FinishTrace
(
LABEL
);
FuncC
();
return
0
;
}
```
2.
修改gn编译文件并编译,编译配置文件路径base
\h
iviewdfx
\h
itrace
\c
md
\B
UILD.gn 。
```
ohos_executable("hitrace_example") {
sources = [ "example/hitrace_example.cpp" ]
external_deps = [ "hitrace_native:hitrace_meter" ]
subsystem_name = "hiviewdfx"
part_name = "hitrace_native"
}
group("hitrace_target") {
deps = [
":hitrace",
":hitrace_example",
]
}
```
3.
将编译出来的hitrace_example可执行文件放到设备中的/system/bin目录下
,
在shell中执行依次执行如下命令:
```
shell
hitrace
--trace_begin
ohos
hitrace_exampe
hitrace
--trace_dump
```
```
ohos_executable("hitrace_example") {
sources = [ "example/hitrace_example.cpp" ]
external_deps = [ "hitrace_native:hitrace_meter" ]
subsystem_name = "hiviewdfx"
part_name = "hitrace_native"
}
group("hitrace_target") {
deps = [
":hitrace",
":hitrace_example",
]
}
```
3.
将编译出来的hitrace_example可执行文件放到设备中的/system/bin目录下
,
在shell中执行依次执行如下命令:
```
shell
hitrace
--trace_begin
ohos
hitrace_exampe
hitrace
--trace_dump
```
当我们看到Trace数据中有我们需要的Trace value时,说明成功抓取Trace,成功的数据如下所示:
```
<...>-1651 (-------) [002] .... 327.194136: tracing_mark_write: S|1650|H:testAsync 111
<...>-1650 (-------) [001] .... 332.197640: tracing_mark_write: B|1650|H:testStart
<...>-1650 (-------) [001] .... 333.198018: tracing_mark_write: B|1650|H:funcAStart
<...>-1650 (-------) [001] .... 334.198507: tracing_mark_write: E|1650|
<...>-1654 (-------) [003] .... 341.201673: tracing_mark_write: F|1650|H:testAsync 111
<...>-1650 (-------) [001] .... 341.202168: tracing_mark_write: B|1650|H:funcBStart
<...>-1650 (-------) [001] .... 343.202557: tracing_mark_write: E|1650|
<...>-1650 (-------) [001] .... 346.203178: tracing_mark_write: E|1650|
<...>-1650 (-------) [001] .... 346.203457: tracing_mark_write: C|1650|H:count number 1
<...>-1650 (-------) [001] .... 347.203818: tracing_mark_write: C|1650|H:count number 2
<...>-1650 (-------) [001] .... 348.204207: tracing_mark_write: C|1650|H:count number 3
<...>-1650 (-------) [001] .... 349.204473: tracing_mark_write: C|1650|H:count number 4
<...>-1650 (-------) [001] .... 350.204851: tracing_mark_write: C|1650|H:count number 5
<...>-1655 (-------) [001] .... 365.944658: tracing_mark_write: trace_event_clock_sync: realtime_ts=1502021460925
<...>-1655 (-------) [001] .... 365.944686: tracing_mark_write: trace_event_clock_sync: parent_ts=365.944641
```
```
<...>-1651 (-------) [002] .... 327.194136: tracing_mark_write: S|1650|H:testAsync 111
<...>-1650 (-------) [001] .... 332.197640: tracing_mark_write: B|1650|H:testStart
<...>-1650 (-------) [001] .... 333.198018: tracing_mark_write: B|1650|H:funcAStart
<...>-1650 (-------) [001] .... 334.198507: tracing_mark_write: E|1650|
<...>-1654 (-------) [003] .... 341.201673: tracing_mark_write: F|1650|H:testAsync 111
<...>-1650 (-------) [001] .... 341.202168: tracing_mark_write: B|1650|H:funcBStart
<...>-1650 (-------) [001] .... 343.202557: tracing_mark_write: E|1650|
<...>-1650 (-------) [001] .... 346.203178: tracing_mark_write: E|1650|
<...>-1650 (-------) [001] .... 346.203457: tracing_mark_write: C|1650|H:count number 1
<...>-1650 (-------) [001] .... 347.203818: tracing_mark_write: C|1650|H:count number 2
<...>-1650 (-------) [001] .... 348.204207: tracing_mark_write: C|1650|H:count number 3
<...>-1650 (-------) [001] .... 349.204473: tracing_mark_write: C|1650|H:count number 4
<...>-1650 (-------) [001] .... 350.204851: tracing_mark_write: C|1650|H:count number 5
<...>-1655 (-------) [001] .... 365.944658: tracing_mark_write: trace_event_clock_sync: realtime_ts=1502021460925
<...>-1655 (-------) [001] .... 365.944686: tracing_mark_write: trace_event_clock_sync: parent_ts=365.944641
```
# HiTraceMeter命令行工具使用指导
...
...
@@ -273,72 +270,84 @@ HiTraceMeter提供了可执行的二进制程序hitrace,设备刷openharmony
**表 4**
命令行列表
| Option
| Description
|
| -----------------------------
- | ----
-------------------------------------------------------- |
| Option
| Description
|
| -----------------------------
|
-------------------------------------------------------- |
| -h,--help | 查看option帮助 |
| -b n,--buffer_size n | 指定n(KB)内存大小用于存取trace日志,默认2048KB |
| -t n,--time n | 用来指定trace运行的时间(单位:s),取决于需要分析过程的时间 |
| --trace_clock clock
| trace输出的时钟类型,一般设备支持boot、global、mono、uptime、perf等,默认为boot |
| --trace_begin
| 启动抓trace
|
| --trace_dump
| 将数据输出到指定位置(默认控制台)
|
| --trace_finish
| 停止抓trace,并将数据输出到指定位置(默认控制台)
|
| -l,--list_categories | 输出手机能支持的trace模块 |
| --overwrite
| 当缓冲区满的时候,将丢弃最新的信息。(默认丢弃最老的日志)
|
| -o filename,--output filename | 指定输出的目标文件名称 |
| -z
| 抓取trace后进行压缩
|
| -b n,--buffer_size n | 指定n(KB)内存大小用于存取trace日志,默认2048KB
|
| -t n,--time n | 用来指定trace运行的时间(单位:s),取决于需要分析过程的时间
|
| --trace_clock clock | trace输出的时钟类型,一般设备支持boot、global、mono、uptime、perf等,默认为boot |
| --trace_begin
| 启动抓trace
|
| --trace_dump
| 将数据输出到指定位置(默认控制台)
|
| --trace_finish
| 停止抓trace,并将数据输出到指定位置(默认控制台)
|
| -l,--list_categories | 输出手机能支持的trace模块
|
| --overwrite
| 当缓冲区满的时候,将丢弃最新的信息。(默认丢弃最老的日志)
|
| -o filename,--output filename | 指定输出的目标文件名称
|
| -z
| 抓取trace后进行压缩
|
以下是常用hitrace命令示例,供开发者参考:
-
查询支持的label。
```
hitrace -l
```
或者
```
hitrace --list_categories
```
-
设置4M缓存,抓取10秒,抓取label为ability的trace信息。
```
hitrace -b 4096 -t 10 --overwrite ability > /data/log/mytrace.ftrace
```
-
设置trace的输出时钟为mono。
```
hitrace --trace_clock mono -b 4096 -t 10 --overwrite ability > /data/log/mytrace.ftrace
```
-
抓取trace后进行压缩。
```
hitrace -z -b 4096 -t 10 --overwrite ability > /data/log/mytrace.ftrace
```
# 常见问题
### hitrace抓数据不全或者没抓到数据
#### 现象描述
#### 现象描述
执行hitrace命令抓数据不全或者没抓到数据。
执行hitrace命令抓数据不全或者没抓到数据。
#### 根因分析
#### 解决方法
参数-t 时间设置过小或者-b缓冲区buffer设置过小导致数据丢失。
参数-t 时间设置过小或者-b缓冲区buffer设置过小导致数据丢失,可设置-t 60,-b 204800扩大抓trace时间和缓冲区buffer解决。
#### 解决方法
可设置-t 60,-b 204800扩大抓trace时间和缓冲区buffer解决。
# 参考
更多关于HiTraceMeter的详细内容请参考:
[
hiviewdfx_hitrace: A Lightweight Distributed Tracing | 轻量级的分布式调用链跟踪 (gitee.com)
](
https://gitee.com/openharmony/hiviewdfx_hitrace
)
。
更多关于HiTraceMeter的详细内容请参考:
[
轻量级的分布式调用链跟踪
](
https://gitee.com/openharmony/hiviewdfx_hitrace
)
。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录