自定义锁过滤使用场景
用户可以通过在数据竞争检查命令devkit advisor dr-check后,添加-cf/--config-file参数的方式,实现在动态内存一致性检测期间不检查数据竞争的自定义锁。
添加-cf/--config-file参数需指定json配置文件,文件中写入需要过滤的自定义实现的加锁和解锁函数。
自定义锁过滤使用示例
本示例通过指定自定义锁过滤配置文件,执行数据竞争检查后实现自定义锁过滤操作。
- 此处以自定义类添加锁为例,请根据实际情况在-cf参数指定的配置文件中添加加锁、解锁函数。“/home/advisor/test”文件内容:
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... The pid of the memtracer process is 1459201 Scanned time: 2025/05/11 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_20250511070612_124d.json /home/DevKit_Software/dr-check_20250511070612_124d.html /home/DevKit_Software/dr-check_20250511070612_124d.csv
- 对于自定义类锁my_custom_spinlock的过滤,创建配置文件config.json,在配置文件中写入需要过滤的加锁和解锁函数相关内容。
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原理相似)。
加锁和解锁函数共有配置选项字段介绍:
- id_args:代表在加锁/解锁函数的参数列表中能够代表锁对象的索引(通常情况下锁对象是一个指针)。
- name:代表加锁/解锁函数名全称,C++语言中函数名书写方式为“class::function_name(*)”,C语言中函数名书写方式为“function_name”,可用“*”匹配或跳过任意字符。
加锁函数特有配置选项字段介绍:
- shared:代表该函数操作的对象是否为读锁。取值范围为false、true,默认值为false;若为读锁,比如在读写锁上下文中,shared字段值为true。
- success:代表函数的返回值为何值时表示加锁成功。取值范围为always、zero、non_zero,默认为always,若自定义实现try_lock类型加锁函数,则根据函数返回值为0(对应zero)或非0(对应non_zero)配置该字段。
- 再次执行数据竞争检查。
devkit advisor -f /home/advisor/test -cf /home/testcase/config.json
“/home/testcase/config.json”:json配置文件,指定在动态内存一致性检测期间不检查数据竞争的自定义锁,请根据实际情况进行修改。
返回信息如下,从返回信息中的加粗信息可以看出无代码行需要修改,实现自定义锁过滤操作。
Executing data race check task, please wait... The pid of the memtracer process is 1459201 Scanned time: 2025/05/12 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_20250512070815_1256.json /home/DevKit_Software/dr-check_20250512070815_1256.html /home/DevKit_Software/dr-check_20250512070815_1256.csv
父主题: 数据竞争检查