2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# This file is part of systemd.
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# Copyright 2014 Michal Schmidt
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# systemd is free software; you can redistribute it and/or modify it
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# under the terms of the GNU Lesser General Public License as published by
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# the Free Software Foundation; either version 2.1 of the License, or
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# (at your option) any later version.
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# systemd is distributed in the hope that it will be useful, but
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# WITHOUT ANY WARRANTY; without even the implied warranty of
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# Lesser General Public License for more details.
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# You should have received a copy of the GNU Lesser General Public License
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt# along with systemd; If not, see <http://www.gnu.org/licenses/>.
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt "dump systemd's hashmaps"
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt super(sd_dump_hashmaps, self).__init__("sd_dump_hashmaps", gdb.COMMAND_DATA, gdb.COMPLETE_NONE)
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt all_entry_sizes = gdb.parse_and_eval("all_entry_sizes")
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt all_direct_buckets = gdb.parse_and_eval("all_direct_buckets")
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt hashmap_base_t = gdb.lookup_type("HashmapBase")
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt debug_offset = gdb.parse_and_eval("(unsigned long)&((HashmapBase*)0)->debug")
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt print "type, hash, indirect, entries, max_entries, buckets, creator"
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt h = gdb.parse_and_eval("(HashmapBase*)((char*)%d - %d)" % (int(d.cast(ulong_t)), debug_offset))
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt if h["has_indirect"]:
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt storage_ptr = h["indirect"]["storage"].cast(uchar_t.pointer())
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt storage_ptr = h["direct"]["storage"].cast(uchar_t.pointer())
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt n_buckets = all_direct_buckets[int(h["type"])];
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt t = ["plain", "ordered", "set"][int(h["type"])]
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt 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"])
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt dib_raw_addr = storage_ptr + (all_entry_sizes[h["type"]] * n_buckets)
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt print "%3d %8d %f%% of entries" % (dib, histogram[dib], 100.0*histogram[dib]/n_entries)
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt print "%3d %8d %f%% of slots" % (dib, histogram[dib], 100.0*histogram[dib]/n_buckets)
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt print "mean DIB of entries: %f" % (sum([dib*histogram[dib] for dib in iter(histogram) if dib != 255])*1.0/n_entries)
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt # a block may be wrapped around
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt if len(blocks) > 1 and blocks[0][0] == blocks[0][1] and blocks[-1][0] == n_buckets - 1:
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt print "max block: %s" % max(blocks, key=lambda a: a[1])
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt print "sum block lens: %d" % sum(b[1] for b in blocks)
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt print "mean block len: %f" % (1.0 * sum(b[1] for b in blocks) / len(blocks))
2ea8c08306c7e33f8217a878cf990fc491c9432cMichal Schmidt d = d["debug_list_next"]