chiark / gitweb /
Prep v236 : Add missing SPDX-License-Identifier (9/9) tools
[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 #  This file is part of elogind.
6 #
7 #  Copyright 2014 Michal Schmidt
8 #
9 #  elogind is free software; you can redistribute it and/or modify it
10 #  under the terms of the GNU Lesser General Public License as published by
11 #  the Free Software Foundation; either version 2.1 of the License, or
12 #  (at your option) any later version.
13 #
14 #  elogind is distributed in the hope that it will be useful, but
15 #  WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 #  Lesser General Public License for more details.
18 #
19 #  You should have received a copy of the GNU Lesser General Public License
20 #  along with elogind; If not, see <http://www.gnu.org/licenses/>.
21
22 import gdb
23
24 class sd_dump_hashmaps(gdb.Command):
25         "dump elogind's hashmaps"
26
27         def __init__(self):
28                 super(sd_dump_hashmaps, self).__init__("sd_dump_hashmaps", gdb.COMMAND_DATA, gdb.COMPLETE_NONE)
29
30         def invoke(self, arg, from_tty):
31                 d = gdb.parse_and_eval("hashmap_debug_list")
32                 all_entry_sizes = gdb.parse_and_eval("all_entry_sizes")
33                 all_direct_buckets = gdb.parse_and_eval("all_direct_buckets")
34                 hashmap_base_t = gdb.lookup_type("HashmapBase")
35                 uchar_t = gdb.lookup_type("unsigned char")
36                 ulong_t = gdb.lookup_type("unsigned long")
37                 debug_offset = gdb.parse_and_eval("(unsigned long)&((HashmapBase*)0)->debug")
38
39                 print "type, hash, indirect, entries, max_entries, buckets, creator"
40                 while d:
41                         h = gdb.parse_and_eval("(HashmapBase*)((char*)%d - %d)" % (int(d.cast(ulong_t)), debug_offset))
42
43                         if h["has_indirect"]:
44                                 storage_ptr = h["indirect"]["storage"].cast(uchar_t.pointer())
45                                 n_entries = h["indirect"]["n_entries"]
46                                 n_buckets = h["indirect"]["n_buckets"]
47                         else:
48                                 storage_ptr = h["direct"]["storage"].cast(uchar_t.pointer())
49                                 n_entries = h["n_direct_entries"]
50                                 n_buckets = all_direct_buckets[int(h["type"])];
51
52                         t = ["plain", "ordered", "set"][int(h["type"])]
53
54                         print "%s, %s, %s, %d, %d, %d, %s (%s:%d)" % (t, h["hash_ops"], bool(h["has_indirect"]), n_entries, d["max_entries"], n_buckets, d["func"], d["file"], d["line"])
55
56                         if arg != "" and n_entries > 0:
57                                 dib_raw_addr = storage_ptr + (all_entry_sizes[h["type"]] * n_buckets)
58
59                                 histogram = {}
60                                 for i in xrange(0, n_buckets):
61                                         dib = int(dib_raw_addr[i])
62                                         histogram[dib] = histogram.get(dib, 0) + 1
63
64                                 for dib in sorted(iter(histogram)):
65                                         if dib != 255:
66                                                 print "%3d %8d %f%% of entries" % (dib, histogram[dib], 100.0*histogram[dib]/n_entries)
67                                         else:
68                                                 print "%3d %8d %f%% of slots" % (dib, histogram[dib], 100.0*histogram[dib]/n_buckets)
69                                 print "mean DIB of entries: %f" % (sum([dib*histogram[dib] for dib in iter(histogram) if dib != 255])*1.0/n_entries)
70
71                                 blocks = []
72                                 current_len = 1
73                                 prev = int(dib_raw_addr[0])
74                                 for i in xrange(1, n_buckets):
75                                         dib = int(dib_raw_addr[i])
76                                         if (dib == 255) != (prev == 255):
77                                                 if prev != 255:
78                                                         blocks += [[i, current_len]]
79                                                 current_len = 1
80                                         else:
81                                                 current_len += 1
82
83                                         prev = dib
84                                 if prev != 255:
85                                         blocks += [[i, current_len]]
86                                 # a block may be wrapped around
87                                 if len(blocks) > 1 and blocks[0][0] == blocks[0][1] and blocks[-1][0] == n_buckets - 1:
88                                         blocks[0][1] += blocks[-1][1]
89                                         blocks = blocks[0:-1]
90                                 print "max block: %s" % max(blocks, key=lambda a: a[1])
91                                 print "sum block lens: %d" % sum(b[1] for b in blocks)
92                                 print "mean block len: %f" % (1.0 * sum(b[1] for b in blocks) / len(blocks))
93
94                         d = d["debug_list_next"]
95
96 sd_dump_hashmaps()