]>
Commit | Line | Data |
---|---|---|
3e67e5c9 | 1 | #!/usr/bin/env python3 |
35df7443 | 2 | # SPDX-License-Identifier: LGPL-2.1+ |
2ea8c083 MS |
3 | |
4 | import gdb | |
5 | ||
6 | class sd_dump_hashmaps(gdb.Command): | |
c544fc31 ZJS |
7 | "dump systemd's hashmaps" |
8 | ||
9 | def __init__(self): | |
31ca609f | 10 | super().__init__("sd_dump_hashmaps", gdb.COMMAND_DATA, gdb.COMPLETE_NONE) |
c544fc31 ZJS |
11 | |
12 | def invoke(self, arg, from_tty): | |
13 | d = gdb.parse_and_eval("hashmap_debug_list") | |
3aff6c79 | 14 | hashmap_type_info = gdb.parse_and_eval("hashmap_type_info") |
c544fc31 ZJS |
15 | uchar_t = gdb.lookup_type("unsigned char") |
16 | ulong_t = gdb.lookup_type("unsigned long") | |
17 | debug_offset = gdb.parse_and_eval("(unsigned long)&((HashmapBase*)0)->debug") | |
18 | ||
19 | print("type, hash, indirect, entries, max_entries, buckets, creator") | |
20 | while d: | |
31ca609f | 21 | h = gdb.parse_and_eval(f"(HashmapBase*)((char*){int(d.cast(ulong_t))} - {debug_offset})") |
c544fc31 ZJS |
22 | |
23 | if h["has_indirect"]: | |
24 | storage_ptr = h["indirect"]["storage"].cast(uchar_t.pointer()) | |
25 | n_entries = h["indirect"]["n_entries"] | |
26 | n_buckets = h["indirect"]["n_buckets"] | |
27 | else: | |
28 | storage_ptr = h["direct"]["storage"].cast(uchar_t.pointer()) | |
29 | n_entries = h["n_direct_entries"] | |
3aff6c79 | 30 | n_buckets = hashmap_type_info[h["type"]]["n_direct_buckets"] |
c544fc31 ZJS |
31 | |
32 | t = ["plain", "ordered", "set"][int(h["type"])] | |
33 | ||
ea806175 | 34 | print(f'{t}, {h["hash_ops"]}, {bool(h["has_indirect"])}, {n_entries}, {d["max_entries"]}, {n_buckets}, {d["func"].string()}, {d["file"].string()}:{d["line"]}') |
c544fc31 ZJS |
35 | |
36 | if arg != "" and n_entries > 0: | |
3aff6c79 | 37 | dib_raw_addr = storage_ptr + hashmap_type_info[h["type"]]["entry_size"] * n_buckets |
c544fc31 ZJS |
38 | |
39 | histogram = {} | |
31ca609f | 40 | for i in range(0, n_buckets): |
c544fc31 ZJS |
41 | dib = int(dib_raw_addr[i]) |
42 | histogram[dib] = histogram.get(dib, 0) + 1 | |
43 | ||
31ca609f | 44 | for dib in sorted(histogram): |
c544fc31 | 45 | if dib != 255: |
31ca609f | 46 | print(f"{dib:>3} {histogram[dib]:>8} {float(histogram[dib]/n_entries):.0%} of entries") |
c544fc31 | 47 | else: |
31ca609f ZJS |
48 | print(f"{dib:>3} {histogram[dib]:>8} {float(histogram[dib]/n_buckets):.0%} of slots") |
49 | s = sum(dib*count for (dib, count) in histogram.items() if dib != 255) / n_entries | |
50 | print(f"mean DIB of entries: {s}") | |
c544fc31 ZJS |
51 | |
52 | blocks = [] | |
53 | current_len = 1 | |
54 | prev = int(dib_raw_addr[0]) | |
31ca609f | 55 | for i in range(1, n_buckets): |
c544fc31 ZJS |
56 | dib = int(dib_raw_addr[i]) |
57 | if (dib == 255) != (prev == 255): | |
58 | if prev != 255: | |
59 | blocks += [[i, current_len]] | |
60 | current_len = 1 | |
61 | else: | |
62 | current_len += 1 | |
63 | ||
64 | prev = dib | |
65 | if prev != 255: | |
66 | blocks += [[i, current_len]] | |
67 | # a block may be wrapped around | |
68 | if len(blocks) > 1 and blocks[0][0] == blocks[0][1] and blocks[-1][0] == n_buckets - 1: | |
69 | blocks[0][1] += blocks[-1][1] | |
70 | blocks = blocks[0:-1] | |
71 | print("max block: {}".format(max(blocks, key=lambda a: a[1]))) | |
72 | print("sum block lens: {}".format(sum(b[1] for b in blocks))) | |
31ca609f | 73 | print("mean block len: {}".format(sum(b[1] for b in blocks) / len(blocks))) |
c544fc31 ZJS |
74 | |
75 | d = d["debug_list_next"] | |
2ea8c083 MS |
76 | |
77 | sd_dump_hashmaps() |