|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Diagnose LMDB lock.mdb holders and waiters. |
| 4 | +
|
| 5 | +Usage: ./diagnose_lockmdb.py [path_to_lock.mdb] |
| 6 | +Default: /home/neogoose/.cache/nvim/fff_nvim/lock.mdb |
| 7 | +
|
| 8 | +Shows: |
| 9 | +1. Every process that has the lock.mdb inode in its mmap/fd table (potential holders) |
| 10 | +2. Every process currently blocked on a futex at an address in the lock.mdb mmap |
| 11 | + (actually waiting on the reader/writer mutex) |
| 12 | +3. The mutex state (__lock / __owner / __kind) for both reader and writer mutex |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import struct |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +LOCK_PATH = sys.argv[1] if len(sys.argv) > 1 else "/home/neogoose/.cache/nvim/fff_nvim/lock.mdb" |
| 21 | + |
| 22 | +RMUTEX_OFFSET = 24 # MDB_txbody.mtb_rmutex |
| 23 | +WMUTEX_OFFSET = 64 # MDB_txninfo.mt2.mt2_wmutex |
| 24 | +MUTEX_SIZE = 40 # sizeof(pthread_mutex_t) on glibc x86_64 |
| 25 | + |
| 26 | + |
| 27 | +def mutex_state(data, off): |
| 28 | + """Decode pthread_mutex_t at offset `off`.""" |
| 29 | + if len(data) < off + 20: |
| 30 | + return None |
| 31 | + return { |
| 32 | + "lock": struct.unpack("i", data[off:off + 4])[0], |
| 33 | + "count": struct.unpack("I", data[off + 4:off + 8])[0], |
| 34 | + "owner": struct.unpack("i", data[off + 8:off + 12])[0], |
| 35 | + "nusers": struct.unpack("I", data[off + 12:off + 16])[0], |
| 36 | + "kind": struct.unpack("i", data[off + 16:off + 20])[0], |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | +def fmt_mutex(m): |
| 41 | + if m is None: |
| 42 | + return "(too small)" |
| 43 | + robust = "ROBUST" if m["kind"] & 0x10 else "non-robust" |
| 44 | + pshared = "PSHARED" if m["kind"] & 0x80 else "private" |
| 45 | + state = {0: "unlocked", 1: "locked", 2: "locked+waiters"}.get(m["lock"], f"lock={m['lock']}") |
| 46 | + return (f"state={state} owner_tid={m['owner']} nusers={m['nusers']} " |
| 47 | + f"kind=0x{m['kind']:x} ({robust}, {pshared})") |
| 48 | + |
| 49 | + |
| 50 | +def get_comm(pid): |
| 51 | + try: |
| 52 | + return Path(f"/proc/{pid}/comm").read_text().strip() |
| 53 | + except Exception: |
| 54 | + return "?" |
| 55 | + |
| 56 | + |
| 57 | +def get_cmdline(pid): |
| 58 | + try: |
| 59 | + return Path(f"/proc/{pid}/cmdline").read_text().replace("\0", " ").strip()[:100] |
| 60 | + except Exception: |
| 61 | + return "?" |
| 62 | + |
| 63 | + |
| 64 | +def get_wchan(pid_or_tid, path): |
| 65 | + try: |
| 66 | + return Path(f"{path}/wchan").read_text().strip() |
| 67 | + except Exception: |
| 68 | + return "?" |
| 69 | + |
| 70 | + |
| 71 | +def get_syscall(tid_path): |
| 72 | + """Return (syscall_nr, arg0, arg1, ..., arg5) or None.""" |
| 73 | + try: |
| 74 | + parts = Path(f"{tid_path}/syscall").read_text().split() |
| 75 | + if parts and parts[0] != "running": |
| 76 | + return [int(p, 0) for p in parts[:7]] |
| 77 | + except Exception: |
| 78 | + pass |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def get_mmap_regions(pid, target_inode): |
| 83 | + """Return list of (start, end) virtual addresses where `target_inode` is mapped.""" |
| 84 | + regions = [] |
| 85 | + try: |
| 86 | + for line in Path(f"/proc/{pid}/maps").read_text().splitlines(): |
| 87 | + parts = line.split() |
| 88 | + if len(parts) < 5: |
| 89 | + continue |
| 90 | + # inode is field index 4 |
| 91 | + try: |
| 92 | + inode = int(parts[4]) |
| 93 | + except ValueError: |
| 94 | + continue |
| 95 | + if inode != target_inode: |
| 96 | + continue |
| 97 | + addr_range = parts[0] |
| 98 | + try: |
| 99 | + start_s, end_s = addr_range.split("-") |
| 100 | + regions.append((int(start_s, 16), int(end_s, 16), line)) |
| 101 | + except Exception: |
| 102 | + continue |
| 103 | + except Exception: |
| 104 | + pass |
| 105 | + return regions |
| 106 | + |
| 107 | + |
| 108 | +def main(): |
| 109 | + path = Path(LOCK_PATH) |
| 110 | + if not path.exists(): |
| 111 | + print(f"ERROR: {path} does not exist") |
| 112 | + return 1 |
| 113 | + |
| 114 | + st = path.stat() |
| 115 | + target_inode = st.st_ino |
| 116 | + size = st.st_size |
| 117 | + print(f"=== Target: {path} ===") |
| 118 | + print(f" inode={target_inode} size={size}\n") |
| 119 | + |
| 120 | + # Read mutex state from the on-disk file |
| 121 | + data = path.read_bytes() |
| 122 | + r_mutex = mutex_state(data, RMUTEX_OFFSET) |
| 123 | + w_mutex = mutex_state(data, WMUTEX_OFFSET) |
| 124 | + print("=== Mutex state on disk ===") |
| 125 | + print(f" READER mutex (offset {RMUTEX_OFFSET}): {fmt_mutex(r_mutex)}") |
| 126 | + print(f" WRITER mutex (offset {WMUTEX_OFFSET}): {fmt_mutex(w_mutex)}") |
| 127 | + print() |
| 128 | + |
| 129 | + # Scan all processes for mmap hits on this inode. |
| 130 | + holders = [] # list of (pid, start, end, deleted) |
| 131 | + for pid_dir in Path("/proc").iterdir(): |
| 132 | + if not pid_dir.name.isdigit(): |
| 133 | + continue |
| 134 | + pid = int(pid_dir.name) |
| 135 | + regions = get_mmap_regions(pid, target_inode) |
| 136 | + if not regions: |
| 137 | + # Also check fd table — inode may be open-but-not-mmap'd |
| 138 | + try: |
| 139 | + for fd in (pid_dir / "fd").iterdir(): |
| 140 | + try: |
| 141 | + tgt = os.readlink(fd) |
| 142 | + except OSError: |
| 143 | + continue |
| 144 | + if "lock.mdb" in tgt and f"[{target_inode}]" in os.stat(fd).st_dev.__str__(): |
| 145 | + # crude — we already caught mmap above, skip |
| 146 | + pass |
| 147 | + except OSError: |
| 148 | + pass |
| 149 | + continue |
| 150 | + for (start, end, line) in regions: |
| 151 | + deleted = "(deleted)" in line |
| 152 | + holders.append((pid, start, end, deleted)) |
| 153 | + |
| 154 | + print("=== Processes with lock.mdb mmap'd ===") |
| 155 | + if not holders: |
| 156 | + print(" (none — no live process holds this inode via mmap)") |
| 157 | + for (pid, start, end, deleted) in holders: |
| 158 | + marker = " (DELETED inode — stale mapping from previous file)" if deleted else "" |
| 159 | + print(f" PID {pid} ({get_comm(pid)}) mmap @ 0x{start:x}-0x{end:x}{marker}") |
| 160 | + print(f" cmdline: {get_cmdline(pid)}") |
| 161 | + |
| 162 | + print() |
| 163 | + |
| 164 | + # For each holder mmap, compute the futex addresses of each mutex, |
| 165 | + # then scan all threads in all processes for futex syscalls targeting those addresses. |
| 166 | + futex_addrs = {} # address -> description |
| 167 | + for (pid, start, _, deleted) in holders: |
| 168 | + futex_addrs[start + RMUTEX_OFFSET] = (pid, "READER mutex", deleted) |
| 169 | + futex_addrs[start + WMUTEX_OFFSET] = (pid, "WRITER mutex", deleted) |
| 170 | + |
| 171 | + print("=== Threads currently blocked on mutex futex ===") |
| 172 | + found_any = False |
| 173 | + for pid_dir in Path("/proc").iterdir(): |
| 174 | + if not pid_dir.name.isdigit(): |
| 175 | + continue |
| 176 | + pid = int(pid_dir.name) |
| 177 | + task_dir = pid_dir / "task" |
| 178 | + if not task_dir.exists(): |
| 179 | + continue |
| 180 | + try: |
| 181 | + tids = [t.name for t in task_dir.iterdir() if t.name.isdigit()] |
| 182 | + except OSError: |
| 183 | + continue |
| 184 | + for tid in tids: |
| 185 | + tid_path = task_dir / tid |
| 186 | + sc = get_syscall(tid_path) |
| 187 | + if sc is None: |
| 188 | + continue |
| 189 | + # syscall 202 = futex on x86_64 |
| 190 | + if sc[0] != 202: |
| 191 | + continue |
| 192 | + futex_addr = sc[1] |
| 193 | + # Match: the blocked thread must have this address mapped into its own address space. |
| 194 | + # Since mmap addresses differ per process, compare only against THIS process's own holders. |
| 195 | + my_regions = get_mmap_regions(pid, target_inode) |
| 196 | + for (start, end, line) in my_regions: |
| 197 | + deleted = "(deleted)" in line |
| 198 | + r_addr = start + RMUTEX_OFFSET |
| 199 | + w_addr = start + WMUTEX_OFFSET |
| 200 | + if futex_addr == r_addr: |
| 201 | + wchan = get_wchan(tid, tid_path) |
| 202 | + dmark = " (DELETED)" if deleted else "" |
| 203 | + print(f" PID {pid} TID {tid} ({get_comm(pid)}) wchan={wchan}: " |
| 204 | + f"blocked on READER mutex @ 0x{futex_addr:x}{dmark}") |
| 205 | + found_any = True |
| 206 | + elif futex_addr == w_addr: |
| 207 | + wchan = get_wchan(tid, tid_path) |
| 208 | + dmark = " (DELETED)" if deleted else "" |
| 209 | + print(f" PID {pid} TID {tid} ({get_comm(pid)}) wchan={wchan}: " |
| 210 | + f"blocked on WRITER mutex @ 0x{futex_addr:x}{dmark}") |
| 211 | + found_any = True |
| 212 | + if not found_any: |
| 213 | + print(" (none)") |
| 214 | + |
| 215 | + print() |
| 216 | + print("=== /proc/locks fcntl/flock state ===") |
| 217 | + try: |
| 218 | + for line in Path("/proc/locks").read_text().splitlines(): |
| 219 | + if str(target_inode) in line: |
| 220 | + # format: N: TYPE ACCESS KIND PID MAJ:MIN:INODE START END |
| 221 | + parts = line.split() |
| 222 | + if len(parts) >= 7 and parts[6].endswith(f":{target_inode}"): |
| 223 | + pid = parts[4] |
| 224 | + print(f" {line}") |
| 225 | + print(f" -> PID {pid} ({get_comm(pid)})") |
| 226 | + except Exception as e: |
| 227 | + print(f" error reading /proc/locks: {e}") |
| 228 | + |
| 229 | + return 0 |
| 230 | + |
| 231 | + |
| 232 | +if __name__ == "__main__": |
| 233 | + sys.exit(main()) |
0 commit comments