gdb-sd_dump_hashmaps: apply "ruff format" and "ruff check --fix"

This commit is contained in:
Yu Watanabe
2026-05-18 02:44:47 +09:00
parent 30c87278d4
commit 86d3b07fba
+29 -27
View File
@@ -4,38 +4,39 @@
import gdb import gdb
class sd_dump_hashmaps(gdb.Command): class sd_dump_hashmaps(gdb.Command):
"dump systemd's hashmaps" "dump systemd's hashmaps"
def __init__(self): def __init__(self):
super().__init__("sd_dump_hashmaps", gdb.COMMAND_DATA, gdb.COMPLETE_NONE) super().__init__('sd_dump_hashmaps', gdb.COMMAND_DATA, gdb.COMPLETE_NONE)
def invoke(self, arg, _from_tty): def invoke(self, arg, _from_tty):
d = gdb.parse_and_eval("hashmap_debug_list") d = gdb.parse_and_eval('hashmap_debug_list')
hashmap_type_info = gdb.parse_and_eval("hashmap_type_info") hashmap_type_info = gdb.parse_and_eval('hashmap_type_info')
uchar_t = gdb.lookup_type("unsigned char") uchar_t = gdb.lookup_type('unsigned char')
ulong_t = gdb.lookup_type("unsigned long") ulong_t = gdb.lookup_type('unsigned long')
debug_offset = gdb.parse_and_eval("(unsigned long)&((HashmapBase*)0)->debug") debug_offset = gdb.parse_and_eval('(unsigned long)&((HashmapBase*)0)->debug')
print("type, hash, indirect, entries, max_entries, buckets, creator") print('type, hash, indirect, entries, max_entries, buckets, creator')
while d: while d:
h = gdb.parse_and_eval(f"(HashmapBase*)((char*){int(d.cast(ulong_t))} - {debug_offset})") h = gdb.parse_and_eval(f'(HashmapBase*)((char*){int(d.cast(ulong_t))} - {debug_offset})')
if h["has_indirect"]: if h['has_indirect']:
storage_ptr = h["indirect"]["storage"].cast(uchar_t.pointer()) storage_ptr = h['indirect']['storage'].cast(uchar_t.pointer())
n_entries = h["indirect"]["n_entries"] n_entries = h['indirect']['n_entries']
n_buckets = h["indirect"]["n_buckets"] n_buckets = h['indirect']['n_buckets']
else: else:
storage_ptr = h["direct"]["storage"].cast(uchar_t.pointer()) storage_ptr = h['direct']['storage'].cast(uchar_t.pointer())
n_entries = h["n_direct_entries"] n_entries = h['n_direct_entries']
n_buckets = hashmap_type_info[h["type"]]["n_direct_buckets"] n_buckets = hashmap_type_info[h['type']]['n_direct_buckets']
t = ["plain", "ordered", "set"][int(h["type"])] t = ['plain', 'ordered', 'set'][int(h['type'])]
print(f'{t}, {h["hash_ops"]}, {bool(h["has_indirect"])}, {n_entries}, {d["max_entries"]}, {n_buckets}') print(f'{t}, {h["hash_ops"]}, {bool(h["has_indirect"])}, {n_entries}, {d["max_entries"]}, {n_buckets}') # fmt: skip
if arg != "" and n_entries > 0: if arg != '' and n_entries > 0:
dib_raw_addr = storage_ptr + hashmap_type_info[h["type"]]["entry_size"] * n_buckets dib_raw_addr = storage_ptr + hashmap_type_info[h['type']]['entry_size'] * n_buckets
histogram = {} histogram = {}
for i in range(0, n_buckets): for i in range(0, n_buckets):
@@ -44,11 +45,11 @@ class sd_dump_hashmaps(gdb.Command):
for dib in sorted(histogram): for dib in sorted(histogram):
if dib != 255: if dib != 255:
print(f"{dib:>3} {histogram[dib]:>8} {float(histogram[dib]/n_entries):.0%} of entries") print(f'{dib:>3} {histogram[dib]:>8} {float(histogram[dib] / n_entries):.0%} of entries') # fmt: skip
else: else:
print(f"{dib:>3} {histogram[dib]:>8} {float(histogram[dib]/n_buckets):.0%} of slots") print(f'{dib:>3} {histogram[dib]:>8} {float(histogram[dib] / n_buckets):.0%} of slots') # fmt: skip
s = sum(dib*count for (dib, count) in histogram.items() if dib != 255) / n_entries s = sum(dib * count for (dib, count) in histogram.items() if dib != 255) / n_entries
print(f"mean DIB of entries: {s}") print(f'mean DIB of entries: {s}')
blocks = [] blocks = []
current_len = 1 current_len = 1
@@ -69,10 +70,11 @@ class sd_dump_hashmaps(gdb.Command):
if len(blocks) > 1 and blocks[0][0] == blocks[0][1] and blocks[-1][0] == n_buckets - 1: if len(blocks) > 1 and blocks[0][0] == blocks[0][1] and blocks[-1][0] == n_buckets - 1:
blocks[0][1] += blocks[-1][1] blocks[0][1] += blocks[-1][1]
blocks = blocks[0:-1] blocks = blocks[0:-1]
print("max block: {}".format(max(blocks, key=lambda a: a[1]))) print(f'max block: {max(blocks, key=lambda a: a[1])}')
print("sum block lens: {}".format(sum(b[1] for b in blocks))) print(f'sum block lens: {sum(b[1] for b in blocks)}')
print("mean block len: {}".format(sum(b[1] for b in blocks) / len(blocks))) print(f'mean block len: {sum(b[1] for b in blocks) / len(blocks)}')
d = d['debug_list_next']
d = d["debug_list_next"]
sd_dump_hashmaps() sd_dump_hashmaps()