#debugging #locks #rw-lock #mutex #symbols #wrapper #context

rust-debugging-locks

用于调试 Rust 中锁的库

1 个不稳定版本

0.8.9 2024 年 2 月 20 日

#331并发

MIT 许可证

27KB
395

此实用程序在 RwLock(以及未来的 Mutex)周围提供了一个轻量级包装,用于调试锁等待。

包装器跟踪调用者和创建者的堆栈帧,以提供调试上下文。

用法

  • 参考 debugging_locks_run.rs
  • 您需要包含调试符号才能看到堆栈跟踪

为发布版启用调试符号

[profile.release]
debug = true

(这将使二进制文件大小增加 20 倍;机器码的性能/优化不受影响)

启用日志记录器

有两条信息级别可用

  1. 关于锁的基本信息(例如,谁被阻塞以及如何记录)
  2. 调用者、锁创建者和锁持有者的堆栈跟踪
 env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
RUST_LOG=rust_debugging_locks::debugging_locks=info the_binary

缺少什么?

  • 检测调试符号是否可用,并在不可用时警告/失败
  • 定义回调接口
  • 从方法名中移除十六进制 "debugging_locks_run.rs:rust_basics::debugging_locks_run::runit::hbcf42217d721148f"
  • 为每行日志添加字符串(例如,散列)以允许分组(使用 grep)
  • 增强 rwlock 包装器的基准测试
  • 懒惰地符号化堆栈跟踪;仅保留指令指针/符号地址
  • 检查 last_returned_lock_from 是否昂贵;如果是,将其作为可选功能
  • 使阈值可配置(例如,通过环境变量)

启动信息(如何确定其是否正常工作)

[2023-05-02T18:17:53Z INFO  rust_debugging_locks::debugging_locks] NEW WRAPPED RWLOCK (v0.0.0)

示例输出

[2023-05-03T09:33:26Z INFO  rust_debugging_locks::debugging_locks] READER BLOCKED on thread main:ThreadId(1) for 7.597502ms (locktag xFxiD)
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     blocking call:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:60
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     current lock holder:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::{{closure}}::h56d46ee0d6ad82da:51
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     rwlock constructed here:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:47
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z INFO  rust_debugging_locks::debugging_locks] READER BLOCKED on thread main:ThreadId(1) for 9.658057ms (locktag xFxiD)
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     blocking call:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:60
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     current lock holder:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::{{closure}}::h56d46ee0d6ad82da:51
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     rwlock constructed here:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:47
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z INFO  rust_debugging_locks::debugging_locks] READER BLOCKED on thread main:ThreadId(1) for 20.825162ms (locktag xFxiD)
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     blocking call:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:60
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     current lock holder:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::{{closure}}::h56d46ee0d6ad82da:51
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     rwlock constructed here:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:47
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z INFO  rust_debugging_locks::debugging_locks] READER BLOCKED on thread main:ThreadId(1) for 24.683994ms (locktag xFxiD)
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     blocking call:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:60
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     current lock holder:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::{{closure}}::h56d46ee0d6ad82da:51
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     rwlock constructed here:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:47
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z INFO  rust_debugging_locks::debugging_locks] READER BLOCKED on thread main:ThreadId(1) for 38.673215ms (locktag xFxiD)
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     blocking call:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:60
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     current lock holder:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::{{closure}}::h56d46ee0d6ad82da:51
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>     rwlock constructed here:
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::writer_blocks_reader::h90b32e8be4ee69f9:47
[2023-05-03T09:33:26Z DEBUG rust_debugging_locks::debugging_locks]  |xFxiD>       simple.rs!simple::main::h51d8a2c7c463da66:12

locktag

当创建 locktag 时,将其分配给 RwLock 实例。使用 locktag 将日志行分组在一起。该 locktagRwLock::new() 方法调用者的堆栈跟踪的散列。

依赖项

~3–4.5MB
~97K SLoC