---
title: 数据竞争检查
description: "数据竞争检查就是检查C/C++源码在鲲鹏平台运行时是否存在动态内存一致性问题（目前仅支持鲲鹏920），并提供数据竞争的检测结果与插入内存屏障的建议。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevkithistory/devkit_24/cliuserguide/KunpengDevKitCli_0073.html
sourcePath: /source/zh/kunpengdevkithistory/devkit_24/cliuserguide/KunpengDevKitCli_0073.html
indexId: 1b5a41bbe95db35d780b20747ce445f67a0e1e022cfbb7c8320cabd22fc3e9b381
---
# 数据竞争检查

数据竞争检查就是检查C/C++源码在鲲鹏平台运行时是否存在动态内存一致性问题（目前仅支持鲲鹏920），并提供数据竞争的检测结果与插入内存屏障的建议。

只支持对单进程应用进行数据竞争检查，且GCC版本在9.4.0以上。


#### 命令功能

动态检查源码中数据竞争的情况。


#### 命令格式

```
devkit advisor dr-check [-h | --help] {-f ELF_FILE | --elf-file ELF_FILE} [-i INPUT_PATH | --input INPUT_PATH] [-s SAFE_FILE | --safe-file SAFE_FILE][-p ELF_PARAMS | --elf-params ELF_PARAMS] [-r {all,json,html,csv} | --report-type {all,json,html,csv}] [-l {0,1,2,3} | --log-level {0,1,2,3}] [--set-timeout TIMEOUT]
```


#### 参数说明


**表1 参数说明**

| 参数 | 参数选项 | 参数说明 |
| --- | --- | --- |
| \-h/\-\-help | \- | 获取帮助信息。 |
| \-i/\-\-input | input\_path | ELF文件对应的源码文件夹路径。 |
| \-f/\-\-elf\-file | elf\_file | 必选参数，ELF文件路径。待测ELF文件必须带有调试信息（编译时增加\-g选项）。 |
| \-s/\-\-safe\-file | safe\_file | cfg配置文件。指定在动态数据竞争检测中不进行检查的安全函数，C++中函数名称需为重载后的函数名称。 |
| \-p/\-\-elf\-params | elf\_parameter | 指定执行ELF文件时所需的输入参数。 |
| \-o/\-\-output | output\_path | 扫描报告的存放路径，默认存放在当前执行路径下，报告名称为模块名称\_时间戳。 |
| \-l/\-\-log\-level | 0/1/2/3 | 设置日志级别，默认为1。 0：日志级别为DEBUG。 1：日志级别为INFO。 2：日志级别为WARNING。 3：日志级别为ERROR。 |
| \-r/\-\-report\-type | all/json/html/csv | 扫描报告的格式，默认为all。 all：生成JSON、HTML、CSV三种格式报告。 json：生成JSON格式报告。 html：生成HTML格式报告。 csv：生成CSV格式报告。 |
| \-\-set\-timeout | time | 任务超时时间，单位为分钟，若执行时间超过超时时间则退出执行。默认无超时时间，任务将持续执行直到结束。 |


#### 使用示例

此处以扫描“/home/testcase/project”路径下源码文件为例，请根据实际情况进行修改。

```
devkit advisor dr-check -i /home/testcase/project -f /home/advisor/test
```

返回信息如下，并输出报告。

```
Scanned time: 2024/07/16 14:01:30
Configuration:
Elf file path: /home/advisor/test
Scan source code path: /home/testcase/project
Generate report path: /home/ DevKit_Software
Generate report type: all
Task Timeout Interval: The timeout period is not set.
Log level: info
Summary:
There are 2 recommendable code lines to modify.
For the details information, please check:
/home/ DevKit_Software /dr-check_20240716140130_6480.json
/home/ DevKit_Software /dr-check_20240716140130_6480.html
/home/ DevKit_Software /dr-check_20240716140130_6480.csv
```


#### 安全函数使用示例

添加-s/--safe-funcs参数需指定cfg配置文件，文件中需要在每行写入安全函数的名称。若为cpp文件，必须写入重载后的安全函数名称。

此处以自定义类添加锁为例，请根据实际情况进行添加安全函数。

```
class my_custom_spinlock{
std::atomic_flag is_locked ={false};
public:
void lock(){
while (is_locked.test_and_set(std::memory_order_acquire)){}
}
void unlock (){
is_locked .clear (std :: memory_order_release);
}
};
int data_race = 0;
my_custom_spinlock spinlock;
void* first_thread(void* arg){
spinlock.lock();
data_race = 5;
spinlock.unlock();
return 0;
}
void* second_thread(void* arg){
spinlock.lock();
data_race = 3;
spinlock.unlock();
return 0;
}
```

通过nm命令可查看first_thread和second_thread函数重载后名称。

```
nm test
0000000000400a84 T _Z12first_threadPv
0000000000400ac4 T _Z13second_threadPv
```

则用户可创建配置文件safe_functions.cfg，写入内容：

```
_Z12first_threadPv
_Z13second_threadPv
```


此时，执行命令为：

```
devkit advisor dr-check -i /home/testcase/project -f /home/advisor/test -s /home/testcase/safe_functions.cfg
```

返回信息如下，并输出报告。

```
Executing data race check task, please wait...
Scanned time: 2024/09/10 03:06:15
Configuration:
Elf filepath: /home/advisor/test
Scan source code path: /home/testcase/project
Generate report path: /home/DevKit_Software
Generate report type: all
Task Timeout Interval: The timeout period is not set.
Log level: info
Summary:
There are 0 recommendable code lines to modify.
For the details information, please check:
/home/DevKit_Software/dr-check_20240910030615_554d.json
/home/DevKit_Software/dr-check_20240910030615_554d.html
/home/DevKit_Software/dr-check_20240910030615_554d.csv
```

#### 输出报告说明


**表2 数据竞争检查输出报告参数说明**

| 参数 | 说明 |
| --- | --- |
| 配置信息 | 显示软件源码文件及可执行文件存放路径。 |
| 需要修改的源码文件 | 显示存在数据竞争问题，并需要进行修改的源码文件路径等相关信息。 |
