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