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