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