用户可以通过在数据竞争检查命令devkit advisor dr-check后,添加-cf/--config-file参数的方式,实现在动态内存一致性检测期间不检查数据竞争的自定义锁。
添加-cf/--config-file参数需指定json配置文件,文件中写入需要过滤的自定义实现的加锁和解锁函数。
本示例通过指定自定义锁过滤配置文件,执行数据竞争检查后实现自定义锁过滤操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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; } |
“/home/advisor/test”:ELF文件路径,请根据实际情况进行修改。
devkit advisor -f /home/advisor/test
返回信息如下,从返回信息中的加粗信息可以看出有2行代码推荐修改。
Executing data race check task, please wait... Scanned time: 2024/12/10 07:06:12 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 2 recommendable code lines to modify. For the details information, please check: /home/DevKit_Software/dr-check_20241210070612_124d.json /home/DevKit_Software/dr-check_20241210070612_124d.html /home/DevKit_Software/dr-check_20241210070612_124d.csv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { "receive": [ { "id_arg": 0, "name":"my_custom_spinlock::lock(*)", "shared": false, "success": "always" } ], "send": [ { "id_arg": 0, "name": "my_custom_spinlock::unlock(*)" } ] } |
receive列表为自定义实现的加锁函数,表示等待资源的函数(和pthread_mutex_lock原理相似);send列表为自定义实现的解锁函数,表示释放资源的函数(和pthread_mutex_unlock原理相似)。
加锁和解锁函数共有配置选项字段介绍:
加锁函数特有配置选项字段介绍:
devkit advisor -f /home/advisor/test -cf /home/testcase/config.json
“/home/testcase/config.json”:json配置文件,指定在动态内存一致性检测期间不检查数据竞争的自定义锁,请根据实际情况进行修改。
返回信息如下,从返回信息中的加粗信息可以看出无代码行需要修改,实现自定义锁过滤操作。
Executing data race check task, please wait... Scanned time: 2024/12/10 07:08: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_20241210070815_1256.json /home/DevKit_Software/dr-check_20241210070815_1256.html /home/DevKit_Software/dr-check_20241210070815_1256.csv