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