Description:[feature] add hitrace

Signed-off-by: stesen <stesen.ma@huawei.com>
Change-Id: I7832f0230feb5a6c4276b72dabace81bc4b31dec
This commit is contained in:
stesen 2021-08-18 22:02:18 +08:00
parent c58f5972f4
commit 5ecdd3127e
22 changed files with 2792 additions and 63 deletions

177
LICENSE Normal file
View File

@ -0,0 +1,177 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@ -1,36 +0,0 @@
# hiviewdfx_hitrace
#### Description
{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}
#### Software Architecture
Software architecture description
#### Installation
1. xxxx
2. xxxx
3. xxxx
#### Instructions
1. xxxx
2. xxxx
3. xxxx
#### Contribution
1. Fork the repository
2. Create Feat_xxx branch
3. Commit your code
4. Create Pull Request
#### Gitee Feature
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
4. The most valuable open source project [GVP](https://gitee.com/gvp)
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)

152
README.md
View File

@ -1,39 +1,137 @@
# hiviewdfx_hitrace
# HiTrace<a name="EN-US_TOPIC_0000001078081802"></a>
#### 介绍
{**以下是 Gitee 平台说明,您可以替换此简介**
Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN。专为开发者提供稳定、高效、安全的云端软件开发协作平台
无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)}
- [Overview](#section11660541593)
- [Architecture](#section16334748141112)
- [Directory Structure](#section161941989596)
- [Constraints](#section119744591305)
- [Usage](#section1312121216216)
- [Available APIs](#section1551164914237)
- [Usage Guidelines](#section129654513264)
#### 软件架构
软件架构说明
- [Repositories Involved](#section1371113476307)
## Overview<a name="section11660541593"></a>
HiTrace provides APIs to implement call chain tracing throughout a service process. With HiTrace, you can quickly obtain the run log for the call chain of a specified service process and locate faults in cross-device, cross-process, or cross-thread communications.
## Architecture<a name="section16334748141112"></a>
**Figure 1** Architecture of HiTrace <a name="fig4460722185514"></a>
#### 安装教程
![](figures/en-us_image_0000001123644797.png)
1. xxxx
2. xxxx
3. xxxx
HiTrace is the lightweight implementation based on the distributed call chain of cloud computing. HiTrace implements call chain tracing as follows:
#### 使用说明
- Transfers **traceid** in cross-device, cross-process, and cross-thread communications.
- Stores **traceid** in the thread local storage \(TLS\) at the Native layer of the process.
- Automatically adds **traceid** to existing events and run logs.
1. xxxx
2. xxxx
3. xxxx
## Directory Structure<a name="section161941989596"></a>
#### 参与贡献
```
/base/hiviewdfx/hitrace
├── frameworks # Framework code
│ ├── java # HiTrace Java implementation code
│ ├── jni # HiTrace JNI implementation code
│ └── native # HiTrace Native implementation code
└── interfaces # APIs
├── java # Java APIs
│ ├── innerkits # JAR packages opened to internal subsystems
│ └── kits # JAR packages opened to applications
└── native # C/C++ APIs
└── innerkits # Header files opened to internal subsystems
```
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
## Constraints<a name="section119744591305"></a>
HiTrace is already supported by the IPC and EventHandler communication mechanisms. If you are using a custom communication mechanism, adaptation is required to use HiTrace.
#### 特技
## Usage<a name="section1312121216216"></a>
### Available APIs<a name="section1551164914237"></a>
Major APIs of HiTrace
<a name="table1764215412123"></a>
<table><tbody><tr id="row1370464111219"><td class="cellrowborder" valign="top" width="8.98%"><p id="p1670474115124"><a name="p1670474115124"></a><a name="p1670474115124"></a><strong id="b13126192811515"><a name="b13126192811515"></a><a name="b13126192811515"></a>Class</strong></p>
</td>
<td class="cellrowborder" valign="top" width="27.47%"><p id="p167041041191214"><a name="p167041041191214"></a><a name="p167041041191214"></a><strong id="b93135237153"><a name="b93135237153"></a><a name="b93135237153"></a>API</strong></p>
</td>
<td class="cellrowborder" valign="top" width="63.55%"><p id="p970484112122"><a name="p970484112122"></a><a name="p970484112122"></a><strong id="b4370358151913"><a name="b4370358151913"></a><a name="b4370358151913"></a>Description</strong></p>
</td>
</tr>
<tr id="row970417418126"><td class="cellrowborder" rowspan="2" valign="top" width="8.98%"><p id="p16704184111220"><a name="p16704184111220"></a><a name="p16704184111220"></a>HiTrace</p>
</td>
<td class="cellrowborder" valign="top" width="27.47%"><p id="p1270444181220"><a name="p1270444181220"></a><a name="p1270444181220"></a>HiTraceId begin(String name, int flags)</p>
</td>
<td class="cellrowborder" valign="top" width="63.55%"><p id="p20704144114123"><a name="p20704144114123"></a><a name="p20704144114123"></a>Starts call chain tracing, generates a <strong id="b52151451154412"><a name="b52151451154412"></a><a name="b52151451154412"></a>HiTraceId</strong> object, and sets it in the TLS of the calling thread.</p>
<p id="p1270494114128"><a name="p1270494114128"></a><a name="p1270494114128"></a>Input parameters:</p>
<p id="p370434151220"><a name="p370434151220"></a><a name="p370434151220"></a><strong id="b1586330121717"><a name="b1586330121717"></a><a name="b1586330121717"></a>name</strong>: Indicates the name of the service process.</p>
<p id="p9704104181212"><a name="p9704104181212"></a><a name="p9704104181212"></a><strong id="b10973143151713"><a name="b10973143151713"></a><a name="b10973143151713"></a>flags</strong>: Indicates call chain flags, which can be used in combination. </p>
<p id="p187046416125"><a name="p187046416125"></a><a name="p187046416125"></a><strong id="b3332144316176"><a name="b3332144316176"></a><a name="b3332144316176"></a>HITRACE_FLAG_INCLUDE_ASYNC</strong>: Traces both synchronous and asynchronous calls. By default, only synchronous calls are traced.</p>
<p id="p12704104121212"><a name="p12704104121212"></a><a name="p12704104121212"></a><strong id="b17906163911715"><a name="b17906163911715"></a><a name="b17906163911715"></a>HITRACE_FLAG_DONOT_CREATE_SPAN</strong>: Do note create a span. By default, a span is created.</p>
<p id="p17704104171210"><a name="p17704104171210"></a><a name="p17704104171210"></a><strong id="b16819174510193"><a name="b16819174510193"></a><a name="b16819174510193"></a>HITRACE_FLAG_TP_INFO</strong>: Outputs the tracepoint information. By default, the tracepoint information is not output.</p>
<p id="p97041241181215"><a name="p97041241181215"></a><a name="p97041241181215"></a><strong id="b15542450201714"><a name="b15542450201714"></a><a name="b15542450201714"></a>HITRACE_FLAG_NO_BE_INFO</strong>: Do not output the start and end information. By default, the information is output.</p>
<p id="p47041241151211"><a name="p47041241151211"></a><a name="p47041241151211"></a><strong id="b68075771720"><a name="b68075771720"></a><a name="b68075771720"></a>HITRACE_FLAG_DONOT_ENABLE_LOG</strong>: Do not associate logs for output. By default, logs are associated for output.</p>
<p id="p147041741141215"><a name="p147041741141215"></a><a name="p147041741141215"></a><strong id="b11871181131812"><a name="b11871181131812"></a><a name="b11871181131812"></a>HITRACE_FLAG_FAULT_TRIGGER</strong>: Triggers call chain tracing by fault. By default, call chain tracing is triggered normally.</p>
<p id="p2704164181214"><a name="p2704164181214"></a><a name="p2704164181214"></a><strong id="b59161113131816"><a name="b59161113131816"></a><a name="b59161113131816"></a>HITRACE_FLAG_D2D_TP_INFO</strong>: Outputs inter-device tracepoint information. By default, the tracepoint information is not output.</p>
<p id="p17704941121210"><a name="p17704941121210"></a><a name="p17704941121210"></a><strong id="b20659623501"><a name="b20659623501"></a><a name="b20659623501"></a>HITRACE_FLAG_DEFAULT</strong>: Indicates the default flag.</p>
<p id="p17704174141216"><a name="p17704174141216"></a><a name="p17704174141216"></a>Output parameters: none</p>
<p id="p07045418125"><a name="p07045418125"></a><a name="p07045418125"></a>Return value: Returns a valid <strong id="b149053347524"><a name="b149053347524"></a><a name="b149053347524"></a>HiTraceId</strong> object if call chain tracing is triggered successfully; returns an invalid object otherwise.</p>
<p id="p17041941151217"><a name="p17041941151217"></a><a name="p17041941151217"></a>Note: In nested tracing mode, an invalid object will be returned if tracing is started at the nested layer.</p>
</td>
</tr>
<tr id="row18704194111211"><td class="cellrowborder" valign="top"><p id="p11704641131213"><a name="p11704641131213"></a><a name="p11704641131213"></a>void end(HiTraceId id)</p>
</td>
<td class="cellrowborder" valign="top"><p id="p20704144141212"><a name="p20704144141212"></a><a name="p20704144141212"></a>Stops call chain tracing based on the <strong id="b109681242310"><a name="b109681242310"></a><a name="b109681242310"></a>HiTraceId</strong> object returned by the <strong id="b171995411436"><a name="b171995411436"></a><a name="b171995411436"></a>Begin</strong> API, and clears the <strong id="b87181818247"><a name="b87181818247"></a><a name="b87181818247"></a>HiTraceId</strong> object in the TLS of the calling thread.</p>
<p id="p7704174181215"><a name="p7704174181215"></a><a name="p7704174181215"></a>Input parameters:</p>
<p id="p14704134111216"><a name="p14704134111216"></a><a name="p14704134111216"></a><strong id="b15542231182"><a name="b15542231182"></a><a name="b15542231182"></a>id</strong>: Indicates the <strong id="b046517501252"><a name="b046517501252"></a><a name="b046517501252"></a>HiTraceId</strong> object.</p>
<p id="p15704104111215"><a name="p15704104111215"></a><a name="p15704104111215"></a>Output parameters: none</p>
<p id="p16704741181213"><a name="p16704741181213"></a><a name="p16704741181213"></a>Return value: none</p>
</td>
</tr>
</tbody>
</table>
### Usage Guidelines<a name="section129654513264"></a>
\(1\) Enable call chain tracing for the service process.
Import the **HiTrace** and **HiTraceId** classes.
```
import ohos.hiviewdfx.HiTrace;
import ohos.hiviewdfx.HiTraceId;
```
Add the code to start or stop call chain tracing.
```
HiTraceId traceId = HiTrace.begin("MyServiceFlow", HiTrace.HITRACE_FLAG_DEFAULT);
... (service process for call chain tracing)
HiTrace.end(traceId);
```
\(2\) Run the application.
\(3\) Use the hilog tool in the shell to filter logs of the service invoking process.
Filter logs based on the **name** parameter in the tracing task and find **ChainId**.
hilog | grep "MyServiceFlow"
**Note**: The log format for traceid is as follows:
Time PID TID Level Domain/Tag: \[**ChainId**, SpanId, ParentSpanId\] Content
Filter the logs of the service invoking process based on **ChainId**.
hilog | grep "ChainId"
## Repositories Involved<a name="section1371113476307"></a>
[Hivew](https://gitee.com/openharmony)
[HiDumper](https://gitee.com/openharmony)
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)

138
README_zh.md Normal file
View File

@ -0,0 +1,138 @@
# HiTrace组件<a name="ZH-CN_TOPIC_0000001078081802"></a>
- [简介](#section11660541593)
- [组件框架](#section16334748141112)
- [目录](#section161941989596)
- [约束](#section119744591305)
- [说明](#section1312121216216)
- [接口说明](#section1551164914237)
- [使用说明](#section129654513264)
- [相关仓](#section1371113476307)
## 简介<a name="section11660541593"></a>
HiTrace在OpenHarmony中为开发者提供业务流程调用链跟踪的维测接口。通过使用该接口所提供的功能可以帮助开发者迅速获取指定业务流程调用链的运行日志、定位跨设备/跨进程/跨线程的故障问题。
## 组件框架<a name="section16334748141112"></a>
**图 1** 组件框架图<a name="fig4460722185514"></a>
![](figures/zh-cn_image_0000001123644797.png)
HiTrace实现机制
- 基于云计算分布式调用链思想的轻量级实现。
- 在跨设备/跨进程/跨线程的通信机制中传递traceid。
- 在进程Native层TLSThread Local Storage中存储traceid。
- 在事件、运行日志中自动附加traceid。
## 目录<a name="section161941989596"></a>
```
/base/hiviewdfx/hitrace
├── frameworks # 框架代码
│ ├── java # HiTrace java实现代码
│ ├── jni # HiTrace jni代码
│ └── native # HiTrace native实现代码
└── interfaces # 接口
├── java # java接口
│ ├── innerkits # 对内部子系统暴露的jar包定义
│ └── kits # 对应用暴露的jar包定义
└── native # C/C++接口
└── innerkits # 对内部子系统暴露的头文件
```
## 约束<a name="section119744591305"></a>
系统通用的通信机制\(IPC, EventHandler\)已支持HiTrace机制 对业务自定义通信机制需要适配HiTrace机制。
## 说明<a name="section1312121216216"></a>
### 接口说明<a name="section1551164914237"></a>
主要接口:
<a name="table1764215412123"></a>
<table><tbody><tr id="row1370464111219"><td class="cellrowborder" valign="top" width="8.98%"><p id="p1670474115124"><a name="p1670474115124"></a><a name="p1670474115124"></a><strong id="b1970404151213"><a name="b1970404151213"></a><a name="b1970404151213"></a></strong></p>
</td>
<td class="cellrowborder" valign="top" width="27.47%"><p id="p167041041191214"><a name="p167041041191214"></a><a name="p167041041191214"></a><strong id="b5925204893112"><a name="b5925204893112"></a><a name="b5925204893112"></a>方法</strong></p>
</td>
<td class="cellrowborder" valign="top" width="63.55%"><p id="p970484112122"><a name="p970484112122"></a><a name="p970484112122"></a><strong id="b2926194820311"><a name="b2926194820311"></a><a name="b2926194820311"></a>描述</strong></p>
</td>
</tr>
<tr id="row970417418126"><td class="cellrowborder" rowspan="2" valign="top" width="8.98%"><p id="p16704184111220"><a name="p16704184111220"></a><a name="p16704184111220"></a>HiTrace</p>
</td>
<td class="cellrowborder" valign="top" width="27.47%"><p id="p1270444181220"><a name="p1270444181220"></a><a name="p1270444181220"></a>HiTraceId begin(String name, int flags)</p>
</td>
<td class="cellrowborder" valign="top" width="63.55%"><p id="p20704144114123"><a name="p20704144114123"></a><a name="p20704144114123"></a>功能启动Hitrace跟踪生成HiTraceId对象并设置到当前线程TLS中。</p>
<p id="p1270494114128"><a name="p1270494114128"></a><a name="p1270494114128"></a>输入参数:</p>
<p id="p370434151220"><a name="p370434151220"></a><a name="p370434151220"></a>name业务流程名称。</p>
<p id="p9704104181212"><a name="p9704104181212"></a><a name="p9704104181212"></a>flags跟踪指示位可以组合使用具体含义为</p>
<p id="p187046416125"><a name="p187046416125"></a><a name="p187046416125"></a>HITRACE_FLAG_INCLUDE_ASYNC同时跟踪同步调用和异步调用缺省只跟踪同步调用。</p>
<p id="p12704104121212"><a name="p12704104121212"></a><a name="p12704104121212"></a>HITRACE_FLAG_DONOT_CREATE_SPAN不创建子分支缺省创建子分支。</p>
<p id="p17704104171210"><a name="p17704104171210"></a><a name="p17704104171210"></a>HITRACE_FLAG_TP_INFO输出tracepoint信息缺省不输出。</p>
<p id="p97041241181215"><a name="p97041241181215"></a><a name="p97041241181215"></a>HITRACE_FLAG_NO_BE_INFO不输出起始、结束信息缺省输出。</p>
<p id="p47041241151211"><a name="p47041241151211"></a><a name="p47041241151211"></a>HITRACE_FLAG_DONOT_ENABLE_LOG不与日志关联输出缺省关联。</p>
<p id="p147041741141215"><a name="p147041741141215"></a><a name="p147041741141215"></a>HITRACE_FLAG_FAULT_TRIGGER故障触发的跟踪缺省为正常启动的。</p>
<p id="p2704164181214"><a name="p2704164181214"></a><a name="p2704164181214"></a>HITRACE_FLAG_D2D_TP_INFO输出设备间tracepoint信息缺省不输出。</p>
<p id="p17704941121210"><a name="p17704941121210"></a><a name="p17704941121210"></a>HITRACE_FLAG_DEFAULT: 缺省标志。</p>
<p id="p17704174141216"><a name="p17704174141216"></a><a name="p17704174141216"></a>输出参数:无</p>
<p id="p07045418125"><a name="p07045418125"></a><a name="p07045418125"></a>返回值启动跟踪超过返回有效HiTraceId对象否则返回无效对象。</p>
<p id="p17041941151217"><a name="p17041941151217"></a><a name="p17041941151217"></a>注意:嵌套启动跟踪时,内层启动调用返回无效对象。</p>
</td>
</tr>
<tr id="row18704194111211"><td class="cellrowborder" valign="top"><p id="p11704641131213"><a name="p11704641131213"></a><a name="p11704641131213"></a>void end(HiTraceId id)</p>
</td>
<td class="cellrowborder" valign="top"><p id="p20704144141212"><a name="p20704144141212"></a><a name="p20704144141212"></a>功能根据begin返回的HiTraceId停止HiTrace跟踪清除当前线程TLS中HiTraceId内容。</p>
<p id="p7704174181215"><a name="p7704174181215"></a><a name="p7704174181215"></a>输入参数:</p>
<p id="p14704134111216"><a name="p14704134111216"></a><a name="p14704134111216"></a>idHiTraceId对象</p>
<p id="p15704104111215"><a name="p15704104111215"></a><a name="p15704104111215"></a>输出参数:无</p>
<p id="p16704741181213"><a name="p16704741181213"></a><a name="p16704741181213"></a>返回值:无</p>
</td>
</tr>
</tbody>
</table>
### 使用说明<a name="section129654513264"></a>
1在待跟踪的业务调用流程中插入跟踪控制代码。
引入类名。
```
import ohos.hiviewdfx.HiTrace;
import ohos.hiviewdfx.HiTraceId;
```
在业务代码中使用(启动/结束跟踪):
```
HiTraceId traceId = HiTrace.begin("MyServiceFlow", HiTrace.HITRACE_FLAG_DEFAULT);
...... (待跟踪的业务调用流程)
HiTrace.end(traceId);
```
2运行应用程序
3shell下使用hilog工具过滤业务调用流程的日志。
按启动跟踪里的name参数过滤日志找出ChainId
hilog | grep “MyServiceFlow”
注意traceid的日志格式如下
Time PID TID Level Domain/Tag: \[**ChainId**, SpanId, ParentSpanId\] Content
按ChainId过滤出业务调用流程的日志
hilog | grep “ChainId”
## 相关仓<a name="section1371113476307"></a>
[HivewDfx子系统](https://gitee.com/openharmony)
[HiDumper组件](https://gitee.com/openharmony)

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

View File

@ -0,0 +1,32 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
ohos_source_set("libhitrace_source") {
include_dirs = [
"//base/hiviewdfx/hitrace/interfaces/native/innerkits/include",
"//base/hiviewdfx/hilog/frameworks/native/include",
"//utils/native/base/include",
]
sources = [
"hitrace.cpp",
"hitracec.c",
"hitraceid.cpp",
]
external_deps = [ "hilog_native:libhilog" ]
deps = [ "//utils/native/base:utils" ]
}

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hitrace/hitrace.h"
#include "hitrace_inner.h"
using namespace std;
namespace OHOS {
namespace HiviewDFX {
HiTraceId HiTrace::Begin(const string& name, int flags)
{
return HiTraceId(::HiTraceBegin(name.c_str(), flags));
}
void HiTrace::End(const HiTraceId& id)
{
::HiTraceEnd(&(id.id_));
return;
}
HiTraceId HiTrace::GetId()
{
return HiTraceId(::HiTraceGetId());
}
void HiTrace::SetId(const HiTraceId& id)
{
::HiTraceSetId(&(id.id_));
return;
}
void HiTrace::ClearId()
{
::HiTraceClearId();
return;
}
HiTraceId HiTrace::CreateSpan()
{
return HiTraceId(::HiTraceCreateSpan());
}
void HiTrace::Tracepoint(HiTraceTracepointType type, const HiTraceId& id, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
::HiTraceTracepointInner(HITRACE_CM_DEFAULT, type, &(id.id_), fmt, args);
va_end(args);
return;
}
void HiTrace::Tracepoint(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceId& id,
const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
::HiTraceTracepointInner(mode, type, &(id.id_), fmt, args);
va_end(args);
return;
}
} // namespace HiviewDFX
} // namespace OHOS

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HIVIEWDFX_HITRACE_INNER_H
#define HIVIEWDFX_HITRACE_INNER_H
#include "hitrace/hitracec.h"
#ifdef __cplusplus
extern "C" {
#endif
void HiTraceTracepointInner(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
const char* fmt, va_list args);
#ifdef __cplusplus
}
#endif
#endif // HIVIEWDFX_HITRACE_INNER_H

View File

@ -0,0 +1,365 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hitrace/hitracec.h"
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "securec.h"
#include "hilog/log.h"
#include "hilog_trace.h"
#include "hitrace_inner.h"
#undef LOG_DOMAIN
#undef LOG_TAG
static const unsigned int LOG_DOMAIN = 0xD002D03;
static const char* LOG_TAG = "HiTraceC";
typedef struct HiTraceChainIdStruct {
union {
#if __BYTE_ORDER == __LITTLE_ENDIAN
struct {
uint64_t reserved : 4;
uint64_t usecond : 20;
uint64_t second : 16;
uint64_t cpuId : 4;
uint64_t deviceId : 20;
};
struct {
uint64_t padding : 4;
uint64_t chainId : 60;
};
#elif __BYTE_ORDER == __BIG_ENDIAN
struct {
uint64_t deviceId : 20;
uint64_t cpuId : 4;
uint64_t second : 16;
uint64_t usecond : 20;
uint64_t reserved : 4;
};
struct {
uint64_t chainId : 60;
uint64_t padding : 4;
};
#else
#error "ERROR: No BIG_LITTLE_ENDIAN defines."
#endif
};
} HiTraceChainIdStruct;
typedef struct HiTraceIdStructExtra {
uint32_t setTls : 1;
uint32_t reserved : 31;
} HiTraceIdStructExtra;
typedef struct HiTraceIdStructInner {
HiTraceIdStruct id;
HiTraceIdStructExtra extra;
} HiTraceIdStructInner;
static __thread HiTraceIdStructInner g_hiTraceId = {{0, 0, 0, 0, 0, 0}, {0, 0}};
static inline HiTraceIdStructInner* GetThreadIdInner()
{
return &g_hiTraceId;
}
HiTraceIdStruct HiTraceGetId()
{
HiTraceIdStructInner* pThreadId = GetThreadIdInner();
return pThreadId->id;
}
void HiTraceSetId(const HiTraceIdStruct* pId)
{
if (!HiTraceIsValid(pId)) {
return;
}
HiTraceIdStructInner* pThreadId = GetThreadIdInner();
pThreadId->id = *pId;
return;
}
void HiTraceClearId()
{
HiTraceIdStructInner* pThreadId = GetThreadIdInner();
HiTraceInitId(&(pThreadId->id));
return;
}
static inline int HiTraceGetDeviceId()
{
// save device id and use it later
static int deviceId = 0;
if (deviceId == 0) {
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_sec);
deviceId = random();
}
return deviceId;
}
static inline unsigned int HiTraceGetCpuId()
{
// Using vdso call will make get_cpu_id faster: sched_getcpu()
static unsigned int cpuId = 0;
cpuId++;
return cpuId;
}
static inline uint64_t HiTraceCreateChainId()
{
// get timestamp. Using vdso call(no system call)
struct timeval tv;
gettimeofday(&tv, NULL);
HiTraceChainIdStruct chainId = {
.padding = 0,
.chainId = 0
};
chainId.deviceId = HiTraceGetDeviceId();
chainId.cpuId = HiTraceGetCpuId();
chainId.second = tv.tv_sec;
chainId.usecond = tv.tv_usec;
return chainId.chainId;
}
HiTraceIdStruct HiTraceBegin(const char* name, int flags)
{
HiTraceIdStruct id;
HiTraceInitId(&id);
if ((flags < HITRACE_FLAG_MIN) || (flags > HITRACE_FLAG_MAX)) {
return id;
}
HiTraceIdStructInner* pThreadId = GetThreadIdInner();
if (HiTraceIsValid(&(pThreadId->id))) {
return id;
}
id.valid = HITRACE_ID_VALID;
id.ver = HITRACE_VER_1;
id.chainId = HiTraceCreateChainId();
id.flags = flags;
id.spanId = 0;
id.parentSpanId = 0;
pThreadId->id = id;
if (!HiTraceIsFlagEnabled(&id, HITRACE_FLAG_NO_BE_INFO)) {
HILOG_INFO(LOG_CORE, "HiTraceBegin name:%{public}s flags:%{public}x.", name ? name : "", (int)id.flags);
}
return id;
}
void HiTraceEnd(const HiTraceIdStruct* pId)
{
if (!HiTraceIsValid(pId)) {
HILOG_ERROR(LOG_CORE, "HiTraceEnd error: invalid end id.");
return;
}
HiTraceIdStructInner* pThreadId = GetThreadIdInner();
if (!HiTraceIsValid(&(pThreadId->id))) {
HILOG_ERROR(LOG_CORE, "HiTraceEnd error: invalid thread id.");
return;
}
if (HiTraceGetChainId(pId) != HiTraceGetChainId(&(pThreadId->id))) {
HILOG_ERROR(LOG_CORE, "HiTraceEnd error: end id(%{public}llx) != thread id(%{public}llx).",
(unsigned long long)pId->chainId, (unsigned long long)pThreadId->id.chainId);
return;
}
if (!HiTraceIsFlagEnabled(&(pThreadId->id), HITRACE_FLAG_NO_BE_INFO)) {
HILOG_INFO(LOG_CORE, "HiTraceEnd.");
}
HiTraceInitId(&(pThreadId->id));
return;
}
// BKDRHash
static uint32_t HashFunc(const void* pData, uint32_t dataLen)
{
const uint32_t seed = 131;
if ((!pData) || dataLen == 0) {
return 0;
}
uint32_t hash = 0;
uint32_t len = dataLen;
char* p = (char*)pData;
for (; len > 0; --len) {
hash = (hash * seed) + (*p++);
}
return hash;
}
HiTraceIdStruct HiTraceCreateSpan()
{
static const uint32_t hashDataNum = 5;
HiTraceIdStruct id = HiTraceGetId();
if (!HiTraceIsValid(&id)) {
return id;
}
if (HiTraceIsFlagEnabled(&id, HITRACE_FLAG_DONOT_CREATE_SPAN)) {
return id;
}
// create child span id
struct timeval tv;
gettimeofday(&tv, NULL);
uint32_t hashData[hashDataNum];
hashData[0] = HiTraceGetDeviceId(); // 0: device id
hashData[1] = id.parentSpanId; // 1: parent span id
hashData[2] = id.spanId; // 2: span id
hashData[3] = tv.tv_sec; // 3: second
hashData[4] = tv.tv_usec; // 4: usecond
uint32_t hash = HashFunc(hashData, hashDataNum * sizeof(uint32_t));
id.parentSpanId = id.spanId;
id.spanId = hash;
return id;
}
void HiTraceTracepointInner(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
const char* fmt, va_list args)
{
static const int tpBufferSize = 2048;
static const char* hiTraceTypeStr[] = { "CS", "CR", "SS", "SR", "GENERAL", };
static const char* hiTraceModeStr[] = { "DEFAULT", "THREAD", "PROCESS", "DEVICE", };
if (mode < HITRACE_CM_MIN || mode > HITRACE_CM_MAX) {
return;
}
if (type < HITRACE_TP_MIN || type > HITRACE_TP_MAX) {
return;
}
if (!HiTraceIsValid(pId)) {
return;
}
if (!HiTraceIsFlagEnabled(pId, HITRACE_FLAG_TP_INFO) && !HiTraceIsFlagEnabled(pId, HITRACE_FLAG_D2D_TP_INFO)) {
// Both tp and d2d-tp flags are disabled.
return;
} else if (!HiTraceIsFlagEnabled(pId, HITRACE_FLAG_TP_INFO) && (mode != HITRACE_CM_DEVICE)) {
// Only d2d-tp flag is enabled. But the communication mode is not device-to-device.
return;
}
char buff[tpBufferSize];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
// if using privacy parameter: vsnprintf => hilog_vsnprintf
int ret = vsnprintf_s(buff, tpBufferSize, tpBufferSize - 1, fmt, args);
#pragma clang diagnostic pop
if (ret == -1) { // -1: vsnprintf_s copy string fail
return;
}
buff[tpBufferSize - 1] = 0;
HILOG_INFO(LOG_CORE, "<%{public}s,%{public}s,[%{public}llx,%{public}llx,%{public}llx]> %{public}s",
hiTraceModeStr[mode], hiTraceTypeStr[type], (unsigned long long)pId->chainId,
(unsigned long long)pId->spanId, (unsigned long long)pId->parentSpanId, buff);
return;
}
void HiTraceTracepointWithArgs(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, va_list args)
{
HiTraceTracepointInner(HITRACE_CM_DEFAULT, type, pId, fmt, args);
}
void HiTraceTracepointExWithArgs(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
const char* fmt, va_list args)
{
HiTraceTracepointInner(mode, type, pId, fmt, args);
}
void HiTraceTracepoint(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
HiTraceTracepointInner(HITRACE_CM_DEFAULT, type, pId, fmt, args);
va_end(args);
return;
}
void HiTraceTracepointEx(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
HiTraceTracepointInner(mode, type, pId, fmt, args);
va_end(args);
return;
}
// return: -1 -- fail; 0 -- all valid; 1 -- all valid except span
int HiTraceGetInfo(uint64_t* pChainId, uint32_t* pFlags, uint64_t* pSpanId, uint64_t* pParentSpanId)
{
if (!pChainId || !pFlags || !pSpanId || !pParentSpanId) {
return -1;
}
HiTraceIdStruct id = HiTraceGetId();
if (!HiTraceIsValid(&id)) {
return -1;
}
if (HiTraceIsFlagEnabled(&id, HITRACE_FLAG_DONOT_ENABLE_LOG)) {
return -1;
}
*pChainId = HiTraceGetChainId(&id);
*pFlags = HiTraceGetFlags(&id);
if (HiTraceIsFlagEnabled(&id, HITRACE_FLAG_DONOT_CREATE_SPAN)) {
*pSpanId = 0;
*pParentSpanId = 0;
return 1;
}
*pSpanId = HiTraceGetSpanId(&id);
*pParentSpanId = HiTraceGetParentSpanId(&id);
return 0;
}
static void __attribute__((constructor)) HiTraceInit()
{
// Call HiLog Register Interface
HiLogRegisterGetIdFun(HiTraceGetInfo);
}
static void __attribute__((destructor)) HiTraceFini()
{
HiLogUnregisterGetIdFun(HiTraceGetInfo);
}

View File

@ -0,0 +1,105 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hitrace/hitraceid.h"
namespace OHOS {
namespace HiviewDFX {
HiTraceId::HiTraceId()
{
id_.valid = HITRACE_ID_INVALID;
id_.ver = 0;
id_.chainId = 0;
id_.flags = 0;
id_.spanId = 0;
id_.parentSpanId = 0;
}
HiTraceId::HiTraceId(const HiTraceIdStruct& id) : id_(id)
{}
HiTraceId::HiTraceId(const uint8_t* pIdArray, int len)
{
id_ = HiTraceBytesToId(pIdArray, len);
}
bool HiTraceId::IsValid() const
{
return HiTraceIsValid(&id_);
}
bool HiTraceId::IsFlagEnabled(HiTraceFlag flag) const
{
return HiTraceIsFlagEnabled(&id_, flag);
}
void HiTraceId::EnableFlag(HiTraceFlag flag)
{
HiTraceEnableFlag(&id_, flag);
return;
}
int HiTraceId::GetFlags() const
{
return HiTraceGetFlags(&id_);
}
void HiTraceId::SetFlags(int flags)
{
HiTraceSetFlags(&id_, flags);
return;
}
uint64_t HiTraceId::GetChainId() const
{
return HiTraceGetChainId(&id_);
}
void HiTraceId::SetChainId(uint64_t chainId)
{
HiTraceSetChainId(&id_, chainId);
return;
}
uint64_t HiTraceId::GetSpanId() const
{
return HiTraceGetSpanId(&id_);
}
void HiTraceId::SetSpanId(uint64_t spanId)
{
HiTraceSetSpanId(&id_, spanId);
return;
}
uint64_t HiTraceId::GetParentSpanId() const
{
return HiTraceGetParentSpanId(&id_);
}
void HiTraceId::SetParentSpanId(uint64_t parentSpanId)
{
HiTraceSetParentSpanId(&id_, parentSpanId);
return;
}
int HiTraceId::ToBytes(uint8_t* pIdArray, int len) const
{
return HiTraceIdToBytes(&id_, pIdArray, len);
}
} // namespace HiviewDFX
} // namespace OHOS

View File

@ -0,0 +1,71 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/test.gni")
module_output_path = "hiviewdfx/hitrace"
config("module_private_config") {
visibility = [ ":*" ]
include_dirs = [
"//base/hiviewdfx/hitrace/interfaces/native/innerkits/include",
"//base/hiviewdfx/hilog/frameworks/native/include",
"//utils/native/base/include",
]
}
ohos_unittest("HitraceCTest") {
module_out_path = module_output_path
sources = [
"../hitracec.c",
"unittest/common/hitracec_test.cpp",
]
configs = [ ":module_private_config" ]
deps = [
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [ "hilog_native:libhilog" ]
}
ohos_unittest("HitraceCppTest") {
module_out_path = module_output_path
sources = [
"../hitrace.cpp",
"../hitracec.c",
"../hitraceid.cpp",
"unittest/common/hitracecpp_test.cpp",
]
configs = [ ":module_private_config" ]
deps = [
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
external_deps = [ "hilog_native:libhilog" ]
}
group("unittest") {
testonly = true
deps = [
":HitraceCTest",
":HitraceCppTest",
]
}

View File

@ -0,0 +1,562 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hitrace/hitracec.h"
#include <gtest/gtest.h>
namespace OHOS {
namespace HiviewDFX {
using namespace testing::ext;
#define HITRACE_DEBUG
#ifndef HITRACE_DEBUG
#define PRINT_ID(p)
#else
#define PRINT_ID(p) \
printf(#p " valid:%d, ver:%d, chain:0x%llx, flags:%x, span:0x%x, pspan:0x%x.\n", static_cast<int>((p)->valid), \
static_cast<int>((p)->ver), static_cast<long long>((p)->chainId), static_cast<int>((p)->flags), \
static_cast<int>((p)->spanId), static_cast<int>((p)->parentSpanId))
#endif
class HiTraceCTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
};
void HiTraceCTest::SetUpTestCase()
{}
void HiTraceCTest::TearDownTestCase()
{}
void HiTraceCTest::SetUp()
{
HiTraceClearId();
}
void HiTraceCTest::TearDown()
{}
/**
* @tc.name: Dfx_HiTraceCTest_IdTest_001
* @tc.desc: Get, set and clear trace id
* @tc.type: FUNC
* @tc.require: AR000CQVA0
*/
HWTEST_F(HiTraceCTest, IdTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. get and validate trace id.
* @tc.expected: step1. trace id is invalid.
* @tc.steps: step2. construct trace id with chain id, span id, parent span id
* and flags and set it into context, then get and validate it.
* @tc.expected: step2. trace id is valid with same chain id, span id, parent
* span id and flags.
* @tc.steps: step3. construct invalid trace id and set into context, then get
* and validate it.
* @tc.expected: step3. trace id is the same with step2.
* @tc.steps: step4. clear trace id, then get and validate it.
* @tc.expected: step4. trace id is invalid.
*/
HiTraceIdStruct initId = HiTraceGetId();
EXPECT_EQ(0, HiTraceIsValid(&initId));
PRINT_ID(&initId);
// set thread id
constexpr uint64_t chainId = 0xABCDEF;
constexpr uint64_t spanId = 0x12345;
constexpr uint64_t parentSpanId = 0x67890;
constexpr int flags = HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN;
HiTraceIdStruct setId;
HiTraceInitId(&setId);
HiTraceSetChainId(&setId, chainId);
HiTraceSetFlags(&setId, flags);
HiTraceSetSpanId(&setId, spanId);
HiTraceSetParentSpanId(&setId, parentSpanId);
PRINT_ID(&setId);
HiTraceSetId(&setId);
HiTraceIdStruct getId = HiTraceGetId();
EXPECT_EQ(1, HiTraceIsValid(&getId));
EXPECT_EQ(chainId, HiTraceGetChainId(&getId));
EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceGetFlags(&getId));
EXPECT_EQ(spanId, HiTraceGetSpanId(&getId));
EXPECT_EQ(parentSpanId, HiTraceGetParentSpanId(&getId));
PRINT_ID(&getId);
// set invalid id
HiTraceIdStruct invalidId;
HiTraceInitId(&invalidId);
HiTraceSetId(&invalidId);
getId = HiTraceGetId();
EXPECT_EQ(1, HiTraceIsValid(&getId));
EXPECT_EQ(chainId, HiTraceGetChainId(&getId));
EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceGetFlags(&getId));
EXPECT_EQ(spanId, HiTraceGetSpanId(&getId));
EXPECT_EQ(parentSpanId, HiTraceGetParentSpanId(&getId));
PRINT_ID(&getId);
// clear thread id
HiTraceClearId();
HiTraceIdStruct clearId = HiTraceGetId();
EXPECT_EQ(0, HiTraceIsValid(&clearId));
PRINT_ID(&clearId);
}
/**
* @tc.name: Dfx_HiTraceCTest_IntfTest_001
* @tc.desc: Interconversion between trace id and bytes array.
* @tc.type: FUNC
* @tc.require: AR000CQV9T
*/
HWTEST_F(HiTraceCTest, IntfTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. construct trace id and validate it.
* @tc.expected: step1. trace id is valid.
* @tc.steps: step2. convert trace id to bytes array.
* @tc.expected: step2. convert success when array size >= id length.
* @tc.steps: step3. convert bytes array to trace id.
* @tc.expected: step3. convert success only when array size == id length.
* @tc.steps: step4. convert invalid id to bytes array.
* @tc.expected: step4. convert fail.
* @tc.steps: step5. convert invalid bytes array to id.
* @tc.expected: step5. convert fail.
*/
// id to bytes
constexpr uint64_t chainId = 0xABCDEF;
constexpr uint64_t spanId = 0x12345;
constexpr uint64_t parentSpanId = 0x67890;
constexpr int flags = HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN;
HiTraceIdStruct id = {HITRACE_ID_VALID, HITRACE_VER_1, chainId, flags, spanId, parentSpanId};
EXPECT_EQ(1, HiTraceIsValid(&id));
PRINT_ID(&id);
constexpr int idLen = sizeof(HiTraceIdStruct);
uint8_t bytes[idLen + 1];
int len = HiTraceIdToBytes(&id, bytes, idLen - 1);
EXPECT_EQ(0, len);
len = HiTraceIdToBytes(&id, bytes, idLen + 1);
EXPECT_EQ(idLen, len);
len = HiTraceIdToBytes(&id, bytes, idLen);
EXPECT_EQ(idLen, len);
PRINT_ID(reinterpret_cast<HiTraceIdStruct*>(bytes));
// bytes to id
HiTraceIdStruct bytesToId = HiTraceBytesToId(bytes, idLen - 1);
EXPECT_EQ(0, HiTraceIsValid(&bytesToId));
bytesToId = HiTraceBytesToId(bytes, idLen + 1);
EXPECT_EQ(0, HiTraceIsValid(&bytesToId));
bytesToId = HiTraceBytesToId(bytes, idLen);
EXPECT_EQ(1, HiTraceIsValid(&bytesToId));
EXPECT_EQ(chainId, HiTraceGetChainId(&bytesToId));
EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceGetFlags(&bytesToId));
EXPECT_EQ(spanId, HiTraceGetSpanId(&bytesToId));
EXPECT_EQ(parentSpanId, HiTraceGetParentSpanId(&bytesToId));
PRINT_ID(&bytesToId);
// set invalid id
HiTraceIdStruct invalidId;
HiTraceInitId(&invalidId);
EXPECT_EQ(0, HiTraceIdToBytes(&invalidId, bytes, idLen));
invalidId = HiTraceBytesToId(nullptr, idLen);
EXPECT_EQ(0, HiTraceIsValid(&invalidId));
}
/**
* @tc.name: Dfx_HiTraceCTest_IntfTest_002
* @tc.desc: Start and stop trace.
* @tc.type: FUNC
* @tc.require: AR000CQV9T
*/
HWTEST_F(HiTraceCTest, IntfTest_002, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with flags, get trace id and validit it.
* @tc.expected: step1. trace id and flags is valid.
* @tc.steps: step2. stop trace, get trace id and validit it.
* @tc.expected: step2. trace id is invalid.
*/
// begin
HiTraceIdStruct beginId = HiTraceBegin("test", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_NO_BE_INFO);
EXPECT_EQ(1, HiTraceIsValid(&beginId));
EXPECT_EQ(1, HiTraceIsFlagEnabled(&beginId, HITRACE_FLAG_INCLUDE_ASYNC));
EXPECT_EQ(1, HiTraceIsFlagEnabled(&beginId, HITRACE_FLAG_NO_BE_INFO));
PRINT_ID(&beginId);
// end
HiTraceEnd(&beginId);
HiTraceIdStruct endId = HiTraceGetId();
EXPECT_EQ(0, HiTraceIsValid(&endId));
PRINT_ID(&endId);
}
/**
* @tc.name: Dfx_HiTraceCTest_IntfTest_003
* @tc.desc: Start and stop trace with reentered.
* @tc.type: FUNC
* @tc.require: AR000CQV9T
*/
HWTEST_F(HiTraceCTest, IntfTest_003, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace twice and get 2nd trace id.
* @tc.expected: step1. 2nd trace is invalid.
* @tc.steps: step2. get trace id and check.
* @tc.expected: step2. trace id is valid and same with 1st id.
* @tc.steps: step3. set chain id with wrong id and get trace id.
* @tc.expected: step3. trace id is valid and same with 1st id.
* @tc.steps: step4. stop trace twice and get trace id.
* @tc.expected: step4. trace id is invalid.
*/
HiTraceIdStruct beginId = HiTraceBegin("begin", HITRACE_FLAG_INCLUDE_ASYNC);
PRINT_ID(&beginId);
// reenter begin
HiTraceIdStruct reBeginId = HiTraceBegin("reenter begin", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(0, HiTraceIsValid(&reBeginId));
EXPECT_NE(HiTraceGetChainId(&reBeginId), HiTraceGetChainId(&beginId));
EXPECT_EQ(0, HiTraceIsFlagEnabled(&reBeginId, HITRACE_FLAG_INCLUDE_ASYNC));
EXPECT_EQ(0, HiTraceIsFlagEnabled(&reBeginId, HITRACE_FLAG_TP_INFO));
PRINT_ID(&reBeginId);
// reenter end
HiTraceEnd(&reBeginId);
HiTraceIdStruct endId = HiTraceGetId();
EXPECT_EQ(1, HiTraceIsValid(&endId));
EXPECT_EQ(HiTraceGetChainId(&endId), HiTraceGetChainId(&beginId));
EXPECT_EQ(1, HiTraceIsFlagEnabled(&endId, HITRACE_FLAG_INCLUDE_ASYNC));
EXPECT_EQ(0, HiTraceIsFlagEnabled(&endId, HITRACE_FLAG_TP_INFO));
PRINT_ID(&endId);
// end with wrong chainId
HiTraceIdStruct wrongBeginId = beginId;
HiTraceSetChainId(&wrongBeginId, HiTraceGetChainId(&beginId) + 1);
HiTraceEnd(&wrongBeginId);
HiTraceIdStruct wrongEndId = HiTraceGetId();
EXPECT_EQ(1, HiTraceIsValid(&wrongEndId));
EXPECT_EQ(HiTraceGetChainId(&wrongEndId), HiTraceGetChainId(&beginId));
EXPECT_EQ(1, HiTraceIsFlagEnabled(&wrongEndId, HITRACE_FLAG_INCLUDE_ASYNC));
PRINT_ID(&wrongEndId);
// end
HiTraceEnd(&beginId);
HiTraceIdStruct reEndId = HiTraceGetId();
EXPECT_EQ(0, HiTraceIsValid(&reEndId));
PRINT_ID(&reEndId);
// end with invalid thread id
HiTraceEnd(&beginId);
HiTraceIdStruct endInvalidId = HiTraceGetId();
EXPECT_EQ(0, HiTraceIsValid(&endInvalidId));
PRINT_ID(&endInvalidId);
}
/**
* @tc.name: Dfx_HiTraceCTest_SpanTest_001
* @tc.desc: Create child and grand child span.
* @tc.type: FUNC
* @tc.require: AR000CQVA2
*/
HWTEST_F(HiTraceCTest, SpanTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_DONOT_CREATE_SPAN,
* get and check flags.
* @tc.expected: step1. flags is same with set and span id is 0.
* @tc.steps: step2. create child id.
* @tc.expected: step2. child id has same span id with parent.
* @tc.steps: step3. set child id into context.
* @tc.steps: step4. create grand child id.
* @tc.expected: step4. grand child id has same span id with parent and child.
*/
HiTraceIdStruct id = HiTraceBegin("test", 0);
EXPECT_EQ(0, HiTraceGetFlags(&id));
EXPECT_EQ(0UL, HiTraceGetSpanId(&id));
EXPECT_EQ(0UL, HiTraceGetParentSpanId(&id));
PRINT_ID(&id);
// create child span
HiTraceIdStruct childId = HiTraceCreateSpan();
EXPECT_EQ(1, HiTraceIsValid(&childId));
EXPECT_EQ(HiTraceGetFlags(&childId), HiTraceGetFlags(&id));
EXPECT_EQ(HiTraceGetChainId(&childId), HiTraceGetChainId(&id));
EXPECT_EQ(HiTraceGetParentSpanId(&childId), HiTraceGetSpanId(&id));
PRINT_ID(&childId);
// set child id to thread id
HiTraceSetId(&childId);
// continue to create child span
HiTraceIdStruct grandChildId = HiTraceCreateSpan();
EXPECT_EQ(1, HiTraceIsValid(&grandChildId));
EXPECT_EQ(HiTraceGetFlags(&grandChildId), HiTraceGetFlags(&id));
EXPECT_EQ(HiTraceGetChainId(&grandChildId), HiTraceGetChainId(&id));
EXPECT_EQ(HiTraceGetParentSpanId(&grandChildId), HiTraceGetSpanId(&childId));
PRINT_ID(&grandChildId);
// end
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_SpanTest_002
* @tc.desc: Start and stop trace with reentered.
* @tc.type: FUNC
* @tc.require: AR000CQVA2
*/
HWTEST_F(HiTraceCTest, SpanTest_002, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_DONOT_CREATE_SPAN,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_DONOT_CREATE_SPAN is enabled.
* @tc.steps: step2. create child id.
* @tc.expected: step2. child id is same with parent id.
*/
// begin with "donot create span" flag
HiTraceIdStruct id = HiTraceBegin("test", HITRACE_FLAG_DONOT_CREATE_SPAN);
EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_DONOT_CREATE_SPAN));
PRINT_ID(&id);
// create child span
HiTraceIdStruct childId = HiTraceCreateSpan();
EXPECT_EQ(1, HiTraceIsValid(&childId));
EXPECT_EQ(HiTraceGetFlags(&childId), HiTraceGetFlags(&id));
EXPECT_EQ(HiTraceGetChainId(&childId), HiTraceGetChainId(&id));
EXPECT_EQ(HiTraceGetSpanId(&childId), HiTraceGetSpanId(&id));
EXPECT_EQ(HiTraceGetParentSpanId(&childId), HiTraceGetParentSpanId(&id));
PRINT_ID(&childId);
// end
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_TracepointTest_001
* @tc.desc: Start trace with HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCTest, TracepointTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
* @tc.steps: step2. add trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step2. add trace point info with null and check logs.
* @tc.expected: step2. trace point cannot be found in logs.
*/
HiTraceIdStruct id = HiTraceBegin("test tp flag", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 12);
HiTraceTracepoint(HITRACE_TP_CS, nullptr, "client send msg content %d", 12);
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_TracepointTest_002
* @tc.desc: Start trace without HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCTest, TracepointTest_002, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_TP_INFO flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_TP_INFO is not enabled.
* @tc.steps: step2. add trace point info with id and check logs.
* @tc.expected: step2. trace point cannot be found in logs.
*/
// begin with tp flag
HiTraceIdStruct id = HiTraceBegin("test no tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
EXPECT_EQ(0, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 12);
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_TracepointTest_003
* @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCTest, TracepointTest_003, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step2. add D2D trace point info with null and check logs.
* @tc.expected: step2. trace point cannot be found in logs.
* @tc.steps: step3. add trace point info with id and check logs.
* @tc.expected: step3. trace point cannot be found in logs.
*/
HiTraceIdStruct id = HiTraceBegin("test D2D tp flag", HITRACE_FLAG_D2D_TP_INFO);
EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "cannot be found %d", 22);
HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "cannot be found %d", 32);
HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "cannot be found %d", 42);
HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, nullptr, "cannot be found %d", 13);
HiTraceTracepoint(HITRACE_TP_CS, &id, "cannot be found %d", 14);
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_TracepointTest_004
* @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCTest, TracepointTest_004, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_D2D_TP_INFO flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point cannot be found in logs.
*/
HiTraceIdStruct id = HiTraceBegin("test no D2D tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
EXPECT_EQ(0, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "cannot be found %d", 12);
HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "cannot be found %d", 22);
HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "cannot be found %d", 32);
HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "cannot be found %d", 42);
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_TracepointTest_005
* @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO and HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCTest, TracepointTest_005, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
* @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step3. add trace point info with id and check logs.
* @tc.expected: step3. trace point can be found in logs.
*/
HiTraceIdStruct id = HiTraceBegin("test D2D | TP tp flag", HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "client send msg content %d", 22);
HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "client send msg content %d", 32);
HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "client send msg content %d", 42);
HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 13);
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_TracepointTest_006
* @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO, but with HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCTest, TracepointTest_006, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
* * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step2. add trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
*/
HiTraceIdStruct id = HiTraceBegin("test no D2D, but tp flag", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(0, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "client send msg content %d", 22);
HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "client send msg content %d", 32);
HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "client send msg content %d", 42);
HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 13);
HiTraceEnd(&id);
}
/**
* @tc.name: Dfx_HiTraceCTest_SyncAsyncTest_001
* @tc.desc: Start trace with SYNC or ASYNC.
* @tc.type: FUNC
* @tc.require: AR000CQ0G7
*/
HWTEST_F(HiTraceCTest, SyncAsyncTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_INCLUDE_ASYNC flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_INCLUDE_ASYNC is not enabled.
* @tc.steps: step2. start trace with HITRACE_FLAG_INCLUDE_ASYNC flag.
* get and check flags.
* @tc.expected: step2. HITRACE_FLAG_INCLUDE_ASYNC is enabled.
*/
// begin with sync flag
HiTraceIdStruct syncId = HiTraceBegin("test sync only", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(0, HiTraceIsFlagEnabled(&syncId, HITRACE_FLAG_INCLUDE_ASYNC));
HiTraceTracepoint(HITRACE_TP_CS, &syncId, "client send msg: %s", "sync");
HiTraceEnd(&syncId);
// begin with async flag
HiTraceIdStruct asyncId = HiTraceBegin("test sync+async", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, HiTraceIsFlagEnabled(&asyncId, HITRACE_FLAG_INCLUDE_ASYNC));
HiTraceTracepoint(HITRACE_TP_CS, &asyncId, "client send msg: %s", "async");
HiTraceEnd(&asyncId);
}
} // namespace HiviewDFX
} // namespace OHOS

View File

@ -0,0 +1,561 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hitrace/hitrace.h"
#include <gtest/gtest.h>
namespace OHOS {
namespace HiviewDFX {
using namespace testing::ext;
class HiTraceCppTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
};
void HiTraceCppTest::SetUpTestCase()
{}
void HiTraceCppTest::TearDownTestCase()
{}
void HiTraceCppTest::SetUp()
{
HiTrace::ClearId();
}
void HiTraceCppTest::TearDown()
{}
/**
* @tc.name: Dfx_HiTraceCppTest_IdTest_001
* @tc.desc: Get, set and clear trace id
* @tc.type: FUNC
* @tc.require: AR000CQV9U
*/
HWTEST_F(HiTraceCppTest, IdTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. get and validate trace id.
* @tc.expected: step1. trace id is invalid.
* @tc.steps: step2. construct trace id with chain id, span id, parent span id
* and flags and set it into context, then get and validate it.
* @tc.expected: step2. trace id is valid with same chain id, span id, parent
* span id and flags.
* @tc.steps: step3. construct invalid trace id and set into context, then get
* and validate it.
* @tc.expected: step3. trace id is the same with step2.
* @tc.steps: step4. clear trace id, then get and validate it.
* @tc.expected: step4. trace id is invalid.
*/
HiTraceId initId = HiTrace::GetId();
EXPECT_EQ(0, initId.IsValid());
/* set thread id */
constexpr uint64_t CHAIN_ID = 0xABCDEF;
constexpr uint64_t SPAN_ID = 0x12345;
constexpr uint64_t PARENT_SPAN_ID = 0x67890;
initId.SetChainId(CHAIN_ID);
initId.EnableFlag(HITRACE_FLAG_INCLUDE_ASYNC);
initId.EnableFlag(HITRACE_FLAG_DONOT_CREATE_SPAN);
initId.SetSpanId(SPAN_ID);
initId.SetParentSpanId(PARENT_SPAN_ID);
EXPECT_EQ(1, initId.IsValid());
EXPECT_EQ(CHAIN_ID, initId.GetChainId());
EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, initId.GetFlags());
EXPECT_EQ(SPAN_ID, initId.GetSpanId());
EXPECT_EQ(PARENT_SPAN_ID, initId.GetParentSpanId());
HiTrace::SetId(initId);
HiTraceId getId = HiTrace::GetId();
EXPECT_EQ(1, getId.IsValid());
EXPECT_EQ(CHAIN_ID, getId.GetChainId());
EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, getId.GetFlags());
EXPECT_EQ(SPAN_ID, getId.GetSpanId());
EXPECT_EQ(PARENT_SPAN_ID, getId.GetParentSpanId());
HiTraceId invalidId;
HiTrace::SetId(invalidId);
getId = HiTrace::GetId();
EXPECT_EQ(1, getId.IsValid());
EXPECT_EQ(CHAIN_ID, getId.GetChainId());
EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, getId.GetFlags());
EXPECT_EQ(SPAN_ID, getId.GetSpanId());
EXPECT_EQ(PARENT_SPAN_ID, getId.GetParentSpanId());
HiTrace::ClearId();
HiTraceId clearId = HiTrace::GetId();
EXPECT_EQ(0, clearId.IsValid());
}
/**
* @tc.name: Dfx_HiTraceCppTest_IntfTest_001
* @tc.desc: Interconversion between trace id and bytes array.
* @tc.type: FUNC
* @tc.require: AR000CQV9U
*/
HWTEST_F(HiTraceCppTest, IntfTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. get trace id and validate it.
* @tc.expected: step1. trace id is invalid.
* @tc.steps: step2. construct trace id and validate it.
* @tc.expected: step2. trace id is valid.
* @tc.steps: step3. convert trace id to bytes array.
* @tc.expected: step3. convert success when array size >= id length.
* @tc.steps: step4. convert bytes array to trace id.
* @tc.expected: step4. convert success only when array size == id length.
* @tc.steps: step5. convert invalid id to bytes array.
* @tc.expected: step5. convert fail.
* @tc.steps: step6. convert invalid bytes array to id.
* @tc.expected: step6. convert fail.
*/
HiTraceId initId = HiTrace::GetId();
EXPECT_EQ(0, initId.IsValid());
constexpr uint64_t CHAIN_ID = 0xABCDEF;
constexpr uint64_t SPAN_ID = 0x12345;
constexpr uint64_t PARENT_SPAN_ID = 0x67890;
constexpr int FLAGS = HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN;
initId.SetChainId(CHAIN_ID);
initId.SetFlags(FLAGS);
initId.SetSpanId(SPAN_ID);
initId.SetParentSpanId(PARENT_SPAN_ID);
EXPECT_EQ(1, initId.IsValid());
constexpr int ID_LEN = sizeof(HiTraceIdStruct);
uint8_t bytes[ID_LEN + 1];
int len = initId.ToBytes(bytes, ID_LEN - 1);
EXPECT_EQ(0, len);
len = initId.ToBytes(bytes, ID_LEN + 1);
EXPECT_EQ(ID_LEN, len);
len = initId.ToBytes(bytes, ID_LEN);
EXPECT_EQ(ID_LEN, len);
/* bytes to id */
HiTraceId bytesToId = HiTraceId(bytes, ID_LEN - 1);
EXPECT_EQ(0, bytesToId.IsValid());
bytesToId = HiTraceId(bytes, ID_LEN + 1);
EXPECT_EQ(0, bytesToId.IsValid());
bytesToId = HiTraceId(bytes, ID_LEN);
EXPECT_EQ(1, bytesToId.IsValid());
EXPECT_EQ(CHAIN_ID, bytesToId.GetChainId());
EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, bytesToId.GetFlags());
EXPECT_EQ(SPAN_ID, bytesToId.GetSpanId());
EXPECT_EQ(PARENT_SPAN_ID, bytesToId.GetParentSpanId());
/* set invalid id */
HiTraceId invalidId;
EXPECT_EQ(0, invalidId.ToBytes(bytes, ID_LEN));
invalidId = HiTraceId(nullptr, ID_LEN);
EXPECT_EQ(0, invalidId.IsValid());
}
/**
* @tc.name: Dfx_HiTraceCppTest_IntfTest_002
* @tc.desc: Start and stop trace.
* @tc.type: FUNC
* @tc.require: AR000CQV9U
*/
HWTEST_F(HiTraceCppTest, IntfTest_002, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with flags, get trace id and validit it.
* @tc.expected: step1. trace id and flags is valid.
* @tc.steps: step2. stop trace, get trace id and validit it.
* @tc.expected: step2. trace id is invalid.
*/
HiTraceId beginId = HiTrace::Begin("test", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_NO_BE_INFO);
EXPECT_EQ(1, beginId.IsValid());
EXPECT_EQ(1, beginId.IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC));
EXPECT_EQ(1, beginId.IsFlagEnabled(HITRACE_FLAG_NO_BE_INFO));
HiTrace::End(beginId);
HiTraceId endId = HiTrace::GetId();
EXPECT_EQ(0, endId.IsValid());
}
/**
* @tc.name: Dfx_HiTraceCppTest_IntfTest_003
* @tc.desc: Start and stop trace with reentered.
* @tc.type: FUNC
* @tc.require: AR000CQV9U
*/
HWTEST_F(HiTraceCppTest, IntfTest_003, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace twice and get 2nd trace id.
* @tc.expected: step1. 2nd trace is invalid.
* @tc.steps: step2. get trace id and check.
* @tc.expected: step2. trace id is valid and same with 1st id.
* @tc.steps: step3. set chain id with wrong id and get trace id.
* @tc.expected: step3. trace id is valid and same with 1st id.
* @tc.steps: step4. stop trace twice and get trace id.
* @tc.expected: step4. trace id is invalid.
*/
/* begin */
HiTraceId beginId = HiTrace::Begin("begin", HITRACE_FLAG_INCLUDE_ASYNC);
/* reenter begin */
HiTraceId reBeginId = HiTrace::Begin("reenter begin", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(0, reBeginId.IsValid());
EXPECT_NE(reBeginId.GetChainId(), beginId.GetChainId());
EXPECT_EQ(0, reBeginId.IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC));
EXPECT_EQ(0, reBeginId.IsFlagEnabled(HITRACE_FLAG_TP_INFO));
/* reenter end */
HiTrace::End(reBeginId);
HiTraceId endId = HiTrace::GetId();
EXPECT_EQ(1, endId.IsValid());
EXPECT_EQ(endId.GetChainId(), beginId.GetChainId());
EXPECT_EQ(1, endId.IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC));
EXPECT_EQ(0, endId.IsFlagEnabled(HITRACE_FLAG_TP_INFO));
/* end with wrong chainId */
HiTraceId wrongBeginId = beginId;
wrongBeginId.SetChainId(beginId.GetChainId() + 1);
HiTrace::End(wrongBeginId);
HiTraceId wrongEndId = HiTrace::GetId();
EXPECT_EQ(1, wrongEndId.IsValid());
EXPECT_EQ(wrongEndId.GetChainId(), beginId.GetChainId());
EXPECT_EQ(1, wrongEndId.IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC));
/* end */
HiTrace::End(beginId);
HiTraceId reEndId = HiTrace::GetId();
EXPECT_EQ(0, reEndId.IsValid());
/* end with invalid thread id */
HiTrace::End(beginId);
HiTraceId endInvalidId = HiTrace::GetId();
EXPECT_EQ(0, endInvalidId.IsValid());
}
/**
* @tc.name: Dfx_HiTraceCppTest_SpanTest_001
* @tc.desc: Create child and grand child span.
* @tc.type: FUNC
* @tc.require: AR000CQV9U
*/
HWTEST_F(HiTraceCppTest, SpanTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_DONOT_CREATE_SPAN,
* get and check flags.
* @tc.expected: step1. flags is same with set and span id is 0.
* @tc.steps: step2. create child id.
* @tc.expected: step2. child id has same span id with parent.
* @tc.steps: step3. set child id into context.
* @tc.steps: step4. create grand child id.
* @tc.expected: step4. grand child id has same span id with parent and child.
*/
/* begin with span flag */
HiTraceId id = HiTrace::Begin("test", 0);
EXPECT_EQ(0, id.GetFlags());
EXPECT_EQ(0UL, id.GetSpanId());
EXPECT_EQ(0UL, id.GetParentSpanId());
/* create child span */
HiTraceId childId = HiTrace::CreateSpan();
EXPECT_EQ(1, childId.IsValid());
EXPECT_EQ(childId.GetFlags(), id.GetFlags());
EXPECT_EQ(childId.GetChainId(), id.GetChainId());
EXPECT_EQ(childId.GetParentSpanId(), id.GetSpanId());
/* set child id to thread id */
HiTrace::SetId(childId);
/* continue to create child span */
HiTraceId grandChildId = HiTrace::CreateSpan();
EXPECT_EQ(1, grandChildId.IsValid());
EXPECT_EQ(grandChildId.GetFlags(), id.GetFlags());
EXPECT_EQ(grandChildId.GetChainId(), id.GetChainId());
EXPECT_EQ(grandChildId.GetParentSpanId(), childId.GetSpanId());
/* end */
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_SpanTest_002
* @tc.desc: Start and stop trace with reentered.
* @tc.type: FUNC
* @tc.require: AR000CQV9U
*/
HWTEST_F(HiTraceCppTest, SpanTest_002, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_DONOT_CREATE_SPAN,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_DONOT_CREATE_SPAN is enabled.
* @tc.steps: step2. create child id.
* @tc.expected: step2. child id is same with parent id.
*/
/* begin with "donot create span" flag */
HiTraceId id = HiTrace::Begin("test", HITRACE_FLAG_DONOT_CREATE_SPAN);
EXPECT_EQ(1, id.IsFlagEnabled(HITRACE_FLAG_DONOT_CREATE_SPAN));
/* create child span */
HiTraceId childId = HiTrace::CreateSpan();
EXPECT_EQ(1, childId.IsValid());
EXPECT_EQ(childId.GetFlags(), id.GetFlags());
EXPECT_EQ(childId.GetChainId(), id.GetChainId());
EXPECT_EQ(childId.GetSpanId(), id.GetSpanId());
EXPECT_EQ(childId.GetParentSpanId(), id.GetParentSpanId());
/* end */
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_TracepointTest_001
* @tc.desc: Start trace with HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCppTest, TracepointTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
* @tc.steps: step2. add trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step3. add trace point info with null and check logs.
* @tc.expected: step3. trace point cannot be found in logs.
*/
/* begin with tp flag */
HiTraceId invalidId;
HiTraceId id = HiTrace::Begin("test tp flag", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, id.IsFlagEnabled(HITRACE_FLAG_TP_INFO));
HiTrace::Tracepoint(HITRACE_TP_CS, id, "client send msg content %d", 12);
HiTrace::Tracepoint(HITRACE_TP_CS, invalidId, "client send msg content %d", 12);
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_TracepointTest_002
* @tc.desc: Start trace without HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCppTest, TracepointTest_002, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_TP_INFO flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_TP_INFO is not enabled.
* @tc.steps: step2. add trace point info with id and check logs.
* @tc.expected: step2. trace point cannot be found in logs.
*/
/* begin with tp flag */
HiTraceId id = HiTrace::Begin("test no tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
EXPECT_EQ(0, id.IsFlagEnabled(HITRACE_FLAG_TP_INFO));
HiTrace::Tracepoint(HITRACE_TP_CS, id, "client send msg content %d", 12);
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_TracepointTest_003
* @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCppTest, TracepointTest_003, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step2. add D2D trace point info with null and check logs.
* @tc.expected: step2. trace point cannot be found in logs.
* @tc.steps: step3. add trace point info with id and check logs.
* @tc.expected: step3. trace point cannot be found in logs.
*/
HiTraceId id = HiTrace::Begin("test D2D tp flag", HITRACE_FLAG_D2D_TP_INFO);
EXPECT_EQ(1, id.IsFlagEnabled(HITRACE_FLAG_D2D_TP_INFO));
HiTrace::Tracepoint(HITRACE_CM_DEVICE, HITRACE_TP_CS, id, "client send msg content %d", 12);
HiTrace::Tracepoint(HITRACE_CM_PROCESS, HITRACE_TP_CS, id, "cannot be found %d", 22);
HiTrace::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_CS, id, "cannot be found %d", 32);
HiTrace::Tracepoint(HITRACE_CM_DEFAULT, HITRACE_TP_CS, id, "cannot be found %d", 42);
HiTraceId invalidId;
HiTrace::Tracepoint(HITRACE_CM_DEVICE, HITRACE_TP_CS, invalidId, "cannot be found %d", 13);
HiTrace::Tracepoint(HITRACE_TP_CS, id, "cannot be found %d", 14);
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_TracepointTest_004
* @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCppTest, TracepointTest_004, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_D2D_TP_INFO flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point cannot be found in logs.
*/
HiTraceId id = HiTrace::Begin("test no D2D tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
EXPECT_EQ(0, id.IsFlagEnabled(HITRACE_FLAG_D2D_TP_INFO));
HiTrace::Tracepoint(HITRACE_CM_DEVICE, HITRACE_TP_CS, id, "cannot be found %d", 12);
HiTrace::Tracepoint(HITRACE_CM_PROCESS, HITRACE_TP_CS, id, "cannot be found %d", 22);
HiTrace::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_CS, id, "cannot be found %d", 32);
HiTrace::Tracepoint(HITRACE_CM_DEFAULT, HITRACE_TP_CS, id, "cannot be found %d", 42);
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_TracepointTest_005
* @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO and HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCppTest, TracepointTest_005, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO,
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
* @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step3. add trace point info with id and check logs.
* @tc.expected: step3. trace point can be found in logs.
*/
HiTraceId id = HiTrace::Begin("test D2D | TP tp flag", HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, id.IsFlagEnabled(HITRACE_FLAG_D2D_TP_INFO));
EXPECT_EQ(1, id.IsFlagEnabled(HITRACE_FLAG_TP_INFO));
HiTrace::Tracepoint(HITRACE_CM_DEVICE, HITRACE_TP_CS, id, "client send msg content %d", 12);
HiTrace::Tracepoint(HITRACE_CM_PROCESS, HITRACE_TP_CS, id, "client send msg content %d", 22);
HiTrace::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_CS, id, "client send msg content %d", 32);
HiTrace::Tracepoint(HITRACE_CM_DEFAULT, HITRACE_TP_CS, id, "client send msg content %d", 42);
HiTrace::Tracepoint(HITRACE_TP_CS, id, "client send msg content %d", 13);
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_TracepointTest_006
* @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO, but with HITRACE_FLAG_TP_INFO flag.
* @tc.type: FUNC
* @tc.require: AR000CQVA3
*/
HWTEST_F(HiTraceCppTest, TracepointTest_006, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
* * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
* @tc.steps: step2. add D2D trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
* @tc.steps: step2. add trace point info with id and check logs.
* @tc.expected: step2. trace point can be found in logs.
*/
HiTraceId id = HiTrace::Begin("test no D2D, but tp flag", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(0, id.IsFlagEnabled(HITRACE_FLAG_D2D_TP_INFO));
EXPECT_EQ(1, id.IsFlagEnabled(HITRACE_FLAG_TP_INFO));
HiTrace::Tracepoint(HITRACE_CM_DEVICE, HITRACE_TP_CS, id, "client send msg content %d", 12);
HiTrace::Tracepoint(HITRACE_CM_PROCESS, HITRACE_TP_CS, id, "client send msg content %d", 22);
HiTrace::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_CS, id, "client send msg content %d", 32);
HiTrace::Tracepoint(HITRACE_CM_DEFAULT, HITRACE_TP_CS, id, "client send msg content %d", 42);
HiTrace::Tracepoint(HITRACE_TP_CS, id, "client send msg content %d", 13);
HiTrace::End(id);
}
/**
* @tc.name: Dfx_HiTraceCppTest_SyncAsyncTest_001
* @tc.desc: Start trace with SYNC or ASYNC.
* @tc.type: FUNC
* @tc.require: AR000CQ0G7
*/
HWTEST_F(HiTraceCppTest, SyncAsyncTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace without HITRACE_FLAG_INCLUDE_ASYNC flag.
* get and check flags.
* @tc.expected: step1. HITRACE_FLAG_INCLUDE_ASYNC is not enabled.
* @tc.steps: step2. start trace with HITRACE_FLAG_INCLUDE_ASYNC flag.
* get and check flags.
* @tc.expected: step2. HITRACE_FLAG_INCLUDE_ASYNC is enabled.
*/
/* begin with sync flag */
HiTraceId syncId = HiTrace::Begin("test sync only", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(0, syncId.IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC));
HiTrace::Tracepoint(HITRACE_TP_CS, syncId, "client send msg: %s", "sync");
HiTrace::End(syncId);
/* begin with async flag */
HiTraceId asyncId = HiTrace::Begin("test sync+async", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, asyncId.IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC));
HiTrace::Tracepoint(HITRACE_TP_CS, asyncId, "client send msg: %s", "async");
HiTrace::End(asyncId);
}
/**
* @tc.name: Dfx_HiTraceCppTest_InvalidParamTest_001
* @tc.desc: Start trace with SYNC or ASYNC.
* @tc.type: FUNC
* @tc.require: AR000CQV9U
*/
HWTEST_F(HiTraceCppTest, InvalidParamTest_001, TestSize.Level1)
{
/**
* @tc.steps: step1. start trace with invalid flag and validate trace id.
* @tc.expected: step1. trace id is invalid.
* @tc.steps: step2. start trace with invalid name and validate trace id.
* @tc.expected: step2. trace id is valid.
*/
/* begin with invalid flag */
HiTraceId invalidFlagId = HiTrace::Begin("invalid param", HITRACE_FLAG_MAX+1);
EXPECT_EQ(0, invalidFlagId.IsValid());
invalidFlagId = HiTrace::Begin("invalid param", -1);
EXPECT_EQ(0, invalidFlagId.IsValid());
HiTrace::End(invalidFlagId);
/* begin with invalid name */
HiTraceId invalidNameId = HiTrace::Begin("", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, invalidNameId.IsValid());
HiTrace::End(invalidNameId);
}
} // namespace HiviewDFX
} // namespace OHOS

View File

@ -0,0 +1,38 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
config("libhitrace_pub_config") {
visibility = [ ":*" ]
include_dirs = [ "include" ]
}
ohos_shared_library("libhitrace") {
public_configs = [ ":libhitrace_pub_config" ]
deps = [ "//base/hiviewdfx/hitrace/frameworks/native:libhitrace_source" ]
external_deps = [ "hilog_native:libhilog" ]
output_extension = "so"
if (build_public_version) {
install_enable = true
} else {
install_enable = false
}
part_name = "hitrace_native"
subsystem_name = "hiviewdfx"
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HIVIEWDFX_HITRACE_CPP_H
#define HIVIEWDFX_HITRACE_CPP_H
#include "hitrace/hitrace.h"
#include "hitrace/hitraceid.h"
#ifdef __cplusplus
#include <string>
namespace OHOS {
namespace HiviewDFX {
class HiTrace final {
public:
static HiTraceId Begin(const std::string& name, int flags);
static void End(const HiTraceId& id);
static HiTraceId GetId();
static void SetId(const HiTraceId& id);
static void ClearId();
static HiTraceId CreateSpan();
static void Tracepoint(HiTraceTracepointType type, const HiTraceId& id, const char* fmt, ...)
__attribute__((__format__(os_log, 3, 4)));
static void Tracepoint(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceId& id,
const char* fmt, ...) __attribute__((__format__(os_log, 4, 5)));
private:
HiTrace() = default;
~HiTrace() = default;
};
} // namespace HiviewDFX
} // namespace OHOS
#endif // __cplusplus
#endif // HIVIEWDFX_HITRACE_CPP_H

View File

@ -0,0 +1,245 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HIVIEWDFX_HITRACE_C_H
#define HIVIEWDFX_HITRACE_C_H
#include <endian.h>
#include <stdarg.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum HiTraceIdValid {
HITRACE_ID_INVALID = 0,
HITRACE_ID_VALID = 1,
} HiTraceIdValid;
typedef enum HiTraceVersion {
HITRACE_VER_1 = 0,
} HiTraceVersion;
typedef enum HiTraceFlag {
// MIN: valid.
HITRACE_FLAG_MIN = 0,
// DEFAULT: default value.
HITRACE_FLAG_DEFAULT = 0,
// trace sync and async call. default: trace sync call only.
HITRACE_FLAG_INCLUDE_ASYNC = 1 << 0,
// do not create child span. default: create child span.
HITRACE_FLAG_DONOT_CREATE_SPAN = 1 << 1,
// output tracepoint info in span. default: do not output tracepoint info.
HITRACE_FLAG_TP_INFO = 1 << 2,
// do not output begin and end info. default: output begin and end info.
HITRACE_FLAG_NO_BE_INFO = 1 << 3,
// do not add id to log. default: add id to log.
HITRACE_FLAG_DONOT_ENABLE_LOG = 1 << 4,
// the trace is triggered by fault.
HITRACE_FLAG_FAULT_TRIGGER = 1 << 5,
// output device-to-device tracepoint info in span only. default: do not output device-to-device tracepoint info.
HITRACE_FLAG_D2D_TP_INFO = 1 << 6,
// MAX: valid.
HITRACE_FLAG_MAX = (1 << 7) - 1,
} HiTraceFlag;
// HiTrace tracepoint type
typedef enum HiTraceTracepointType {
HITRACE_TP_MIN = 0, // MIN: valid
HITRACE_TP_CS = 0, // client send
HITRACE_TP_CR = 1, // client receive
HITRACE_TP_SS = 2, // server send
HITRACE_TP_SR = 3, // server receive
HITRACE_TP_GENERAL = 4, // general info
HITRACE_TP_MAX = 4, // MAX: valid
} HiTraceTracepointType;
// HiTrace communication mode
typedef enum HiTraceCommunicationMode {
HITRACE_CM_MIN = 0, // MIN: valid
HITRACE_CM_DEFAULT = 0, // unspecified communication mode
HITRACE_CM_THREAD = 1, // thread-to-thread communication mode
HITRACE_CM_PROCESS = 2, // process-to-process communication mode
HITRACE_CM_DEVICE = 3, // device-to-device communication mode
HITRACE_CM_MAX = 3, // MAX: valid
} HiTraceCommunicationMode;
typedef struct HiTraceIdStruct {
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint64_t valid : 1;
uint64_t ver : 3;
uint64_t chainId : 60;
uint64_t flags : 12;
uint64_t spanId : 26;
uint64_t parentSpanId : 26;
#elif __BYTE_ORDER == __BIG_ENDIAN
uint64_t chainId : 60;
uint64_t ver : 3;
uint64_t valid : 1;
uint64_t parentSpanId : 26;
uint64_t spanId : 26;
uint64_t flags : 12;
#else
#error "ERROR: No BIG_LITTLE_ENDIAN defines."
#endif
} HiTraceIdStruct;
#define HITRACE_ID_LEN sizeof(HiTraceIdStruct)
HiTraceIdStruct HiTraceBegin(const char* name, int flags);
void HiTraceEnd(const HiTraceIdStruct* pId);
HiTraceIdStruct HiTraceGetId();
void HiTraceSetId(const HiTraceIdStruct* pId);
void HiTraceClearId();
HiTraceIdStruct HiTraceCreateSpan();
void HiTraceTracepoint(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...)
__attribute__((__format__(os_log, 3, 4)));
void HiTraceTracepointWithArgs(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, va_list args);
void HiTraceTracepointEx(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
const char* fmt, ...) __attribute__((__format__(os_log, 4, 5)));
void HiTraceTracepointExWithArgs(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
const char* fmt, va_list args);
static inline void HiTraceInitId(HiTraceIdStruct* pId)
{
pId->valid = HITRACE_ID_INVALID;
pId->ver = 0;
pId->chainId = 0;
pId->flags = 0;
pId->spanId = 0;
pId->parentSpanId = 0;
}
static inline int HiTraceIsValid(const HiTraceIdStruct* pId)
{
return (pId) && (pId->valid == HITRACE_ID_VALID);
}
static inline int HiTraceIsFlagEnabled(const HiTraceIdStruct* pId, HiTraceFlag flag)
{
return HiTraceIsValid(pId) && ((pId->flags & (uint64_t)flag) != 0);
}
static inline void HiTraceEnableFlag(HiTraceIdStruct* pId, HiTraceFlag flag)
{
if (HiTraceIsValid(pId)) {
pId->flags |= (uint64_t)flag;
}
return;
}
static inline int HiTraceGetFlags(const HiTraceIdStruct* pId)
{
if (!HiTraceIsValid(pId)) {
return 0;
}
return pId->flags;
}
static inline void HiTraceSetFlags(HiTraceIdStruct* pId, int flags)
{
if (HiTraceIsValid(pId) && (flags >= HITRACE_FLAG_MIN) && (flags < HITRACE_FLAG_MAX)) {
pId->flags = flags;
}
return;
}
static inline uint64_t HiTraceGetChainId(const HiTraceIdStruct* pId)
{
if (!HiTraceIsValid(pId)) {
return 0;
}
return pId->chainId;
}
static inline void HiTraceSetChainId(HiTraceIdStruct* pId, uint64_t chainId)
{
if (!pId) {
return;
}
if (!HiTraceIsValid(pId)) {
pId->valid = HITRACE_ID_VALID;
pId->ver = HITRACE_VER_1;
pId->flags = pId->spanId = pId->parentSpanId = 0;
}
pId->chainId = chainId;
}
static inline uint64_t HiTraceGetSpanId(const HiTraceIdStruct* pId)
{
if (!HiTraceIsValid(pId)) {
return 0;
}
return pId->spanId;
}
static inline void HiTraceSetSpanId(HiTraceIdStruct* pId, uint64_t spanId)
{
if (HiTraceIsValid(pId)) {
pId->spanId = spanId;
}
return;
}
static inline uint64_t HiTraceGetParentSpanId(const HiTraceIdStruct* pId)
{
if (!HiTraceIsValid(pId)) {
return 0;
}
return pId->parentSpanId;
}
static inline void HiTraceSetParentSpanId(HiTraceIdStruct* pId, uint64_t parentSpanId)
{
if (HiTraceIsValid(pId)) {
pId->parentSpanId = parentSpanId;
}
return;
}
static inline int HiTraceIdToBytes(const HiTraceIdStruct* pId, uint8_t* pIdArray, int len)
{
if (!HiTraceIsValid(pId) || (len < (int)HITRACE_ID_LEN)) {
return 0;
}
*((uint64_t*)pIdArray) = htobe64(*((uint64_t*)pId));
*((uint64_t*)pIdArray + 1) = htobe64(*((uint64_t*)pId + 1));
return sizeof(HiTraceIdStruct);
}
static inline HiTraceIdStruct HiTraceBytesToId(const uint8_t* pIdArray, int len)
{
HiTraceIdStruct id = {0, 0, 0, 0, 0, 0};
HiTraceInitId(&id);
if ((!pIdArray) || (len != (int)HITRACE_ID_LEN)) {
return id;
}
*((uint64_t*)(&id)) = be64toh(*((uint64_t*)pIdArray));
*((uint64_t*)(&id) + 1) = be64toh(*((uint64_t*)pIdArray + 1));
return id;
}
#ifdef __cplusplus
}
#endif
#endif // HIVIEWDFX_HITRACE_C_H

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HIVIEWDFX_HITRACE_ID_H
#define HIVIEWDFX_HITRACE_ID_H
#include "hitrace/hitracec.h"
#ifdef __cplusplus
namespace OHOS {
namespace HiviewDFX {
class HiTraceId final {
public:
HiTraceId();
HiTraceId(const HiTraceIdStruct& id);
HiTraceId(const uint8_t* pIdArray, int len);
~HiTraceId() = default;
bool IsValid() const;
bool IsFlagEnabled(HiTraceFlag flag) const;
void EnableFlag(HiTraceFlag flag);
int GetFlags() const;
void SetFlags(int flags);
uint64_t GetChainId() const;
void SetChainId(uint64_t chainId);
uint64_t GetSpanId() const;
void SetSpanId(uint64_t spanId);
uint64_t GetParentSpanId() const;
void SetParentSpanId(uint64_t parentSpanId);
int ToBytes(uint8_t* pIdArray, int len) const;
private:
HiTraceIdStruct id_;
friend class HiTrace;
};
} // namespace HiviewDFX
} // namespace OHOS
#endif // __cplusplus
#endif // HIVIEWDFX_HITRACE_ID_H

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HIVIEWDFX_HITRACE_H
#define HIVIEWDFX_HITRACE_H
#include "hitrace/hitrace.h"
#include "hitrace/hitracec.h"
#include "hitrace/hitraceid.h"
#endif // HIVIEWDFX_HITRACE_H

43
lite/BUILD.gn Normal file
View File

@ -0,0 +1,43 @@
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/ndk/ndk.gni")
hitrace_sources = [
"//base/hiviewdfx/hitrace/frameworks/native/hitrace.cpp",
"//base/hiviewdfx/hitrace/frameworks/native/hitracec.c",
"//base/hiviewdfx/hitrace/frameworks/native/hitraceid.cpp"
]
config("hitrace_config") {
include_dirs = [
"//base/hiviewdfx/hitrace/interfaces/native/innerkits",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
"//base/hiviewdfx/hitrace/interfaces/native/innerkits/include",
"//third_party/bounds_checking_function/include",
]
}
lite_library("hitrace") {
target_type = "shared_library"
sources = hitrace_sources
public_configs = [":hitrace_config"]
public_deps = [
"//third_party/bounds_checking_function:libsec_shared",
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared"
]
cflags = [ "-Wall" ]
}

68
lite/test/BUILD.gn Normal file
View File

@ -0,0 +1,68 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/test.gni")
hitrace_root_dir="//base/hiviewdfx/hitrace"
hitrace_code_dir="${hitrace_root_dir}/frameworks/native"
hitrace_test_dir="${hitrace_root_dir}/frameworks/native/test/unittest"
config("module_private_config") {
visibility = [ ":*" ]
include_dirs = [
"//base/hiviewdfx/hitrace/interfaces/native/innerkits/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
"//third_party/bounds_checking_function/include",
]
}
unittest("HitraceCTest") {
sources = [
"${hitrace_code_dir}/hitracec.c",
"${hitrace_test_dir}/common/hitracec_test.cpp",
]
configs = [ ":module_private_config" ]
public_deps = [
"//third_party/bounds_checking_function:libsec_shared",
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared"
]
}
unittest("HitraceCppTest") {
sources = [
"${hitrace_code_dir}/hitrace.cpp",
"${hitrace_code_dir}/hitracec.c",
"${hitrace_code_dir}/hitraceid.cpp",
"${hitrace_test_dir}/common/hitracecpp_test.cpp",
]
configs = [ ":module_private_config" ]
public_deps = [
"//third_party/bounds_checking_function:libsec_shared",
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared"
]
}
group("unittest") {
if (ohos_build_type == "debug") {
deps = [
":HitraceCTest",
":HitraceCppTest",
]
}
}

24
ohos.build Normal file
View File

@ -0,0 +1,24 @@
{
"subsystem": "hiviewdfx",
"parts": {
"hitrace_native": {
"module_list": [
"//base/hiviewdfx/hitrace/interfaces/native/innerkits:libhitrace"
],
"inner_kits": [
{
"name": "//base/hiviewdfx/hitrace/interfaces/native/innerkits:libhitrace",
"header": {
"header_base": "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/",
"header_files": [
"hitrace/trace.h",
"hitrace/hitracec.h",
"hitrace/hitraceid.h",
"hitrace/hitrace.h"
]
}
}
]
}
}
}