/usr/bin/mono-heapviz is in mono-devel 3.2.8+dfsg-4ubuntu1.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | #!/usr/bin/env python
# Generate a heap visualization for SGen from the heap dump written by
# mono if the MONO_GC_DEBUG is set to something like
# "heap-dump=/path/to/file". This script accepts the file as stdin
# and generates HTML and PNG files.
from __future__ import print_function
import sys, os
import Image, ImageDraw
from xml.sax import ContentHandler, make_parser
from xml.sax.handler import feature_namespaces
from optparse import OptionParser
chunk_size = 1024 # number of bytes in a chunk
chunk_pixel_size = 2 # a chunk is a square with this side length
large_sections = False
def mark_chunk (img_draw, i, color, section_width):
row = i / section_width
col = i % section_width
pixel_col = col * chunk_pixel_size
pixel_row = row * chunk_pixel_size
img_draw.rectangle ([(pixel_col, pixel_row), (pixel_col + chunk_pixel_size - 1, pixel_row + chunk_pixel_size - 1)], fill = color)
class Range:
pass
class OccupiedRange (Range):
def __init__ (self, offset, size):
self.offset = offset
self.size = size
def mark (self, img_draw, color, section_width):
start = self.offset / chunk_size
end = (self.offset + self.size - 1) / chunk_size
for i in range (start, end + 1):
mark_chunk (img_draw, i, color, section_width)
class ObjectRange (OccupiedRange):
def __init__ (self, klass, offset, size):
OccupiedRange.__init__ (self, offset, size)
self.klass = klass;
class SectionHandler:
def __init__ (self, width):
self.width = width
self.ranges = []
self.size = 0
self.used = 0
def add_object (self, klass, offset, size):
self.ranges.append (ObjectRange (klass, offset, size))
self.used += size
def add_occupied (self, offset, size):
self.ranges.append (OccupiedRange (offset, size))
self.used += size
def draw (self):
height = (((self.size / chunk_size) + (self.width - 1)) / self.width) * chunk_pixel_size
color_background = (255, 255, 255)
color_free = (0, 0, 0)
color_occupied = (0, 255, 0)
img = Image.new ('RGB', (self.width * chunk_pixel_size, height), color_free)
img_draw = ImageDraw.Draw (img)
#FIXME: remove filling after end of heap
for r in self.ranges:
r.mark (img_draw, color_occupied, self.width)
return img
def emit (self, collection_file, collection_kind, collection_num, section_num):
print ('<h2>%s</h2>' % self.header (), file = collection_file)
print ('<p>Size %d kB - ' % (self.size / 1024), file = collection_file)
print ('used %d kB</p>' % (self.used / 1024), file = collection_file)
filename = '%s_%d_%d.png' % (collection_kind, collection_num, section_num)
print ('<p><img src="%s"></img></p>' % filename, file = collection_file)
img = self.draw ()
img.save (filename)
class SmallSectionHandler (SectionHandler):
def __init__ (self):
SectionHandler.__init__ (self, -1)
self.offset = 0
def start_section (self, kind, size):
assert kind == 'old'
if self.width <= 0:
self.width = (size + chunk_size - 1) / chunk_size
if self.width < 128:
self.width = 512
self.current_section_size = size
else:
self.current_section_size = self.width * chunk_size
self.size += size
def add_object (self, klass, offset, size):
SectionHandler.add_object (self, klass, self.offset + offset, size)
def add_occupied (self, offset, size):
SectionHandler.add_occupied (self, self.offset + offset, size)
def end_section (self):
self.offset += self.current_section_size
def header (self):
return 'old sections'
class LargeSectionHandler (SectionHandler):
def __init__ (self):
SectionHandler.__init__ (self, 512)
def start_section (self, kind, size):
self.kind = kind
self.ranges = []
self.size = size
self.used = 0
def end_section (self):
pass
def header (self):
return self.kind + ' section'
class DocHandler (ContentHandler):
def start (self):
self.collection_index = 0
self.index_file = open ('index.html', 'w')
print ('<html><body>', file = self.index_file)
def end (self):
print ('</body></html>', file = self.index_file)
self.index_file.close ()
def startElement (self, name, attrs):
if name == 'collection':
self.collection_kind = attrs.get('type', None)
self.collection_num = int(attrs.get('num', None))
reason = attrs.get('reason', None)
if reason:
reason = ' (%s)' % reason
else:
reason = ''
self.section_num = 0
filename = 'collection_%d.html' % self.collection_index
print ('<a href="%s">%s%s collection %d</a>' % (filename, self.collection_kind, reason, self.collection_num), file = self.index_file)
self.collection_file = open (filename, 'w')
print ('<html><body>', file = self.collection_file)
print ('<p><a href="collection_%d.html">Prev</a> <a href="collection_%d.html">Next</a> <a href="index.html">Index</a></p>' % (self.collection_index - 1, self.collection_index + 1), file = self.collection_file)
print ('<h1>%s collection %d</h1>' % (self.collection_kind, self.collection_num), file = self.collection_file)
self.usage = {}
self.los_usage = {}
self.pinned_usage = {}
self.occupancies = {}
self.in_los = False
self.in_pinned = False
self.heap_used = 0
self.heap_size = 0
self.los_size = 0
if large_sections:
self.section_handler = LargeSectionHandler ()
else:
self.section_handler = self.small_section_handler = SmallSectionHandler ()
elif name == 'pinned':
kind = attrs.get('type', None)
bytes = int(attrs.get('bytes', None))
print ('Pinned from %s: %d kB<br>' % (kind, bytes / 1024), file = self.collection_file)
elif name == 'occupancy':
size = int (attrs.get ('size', None))
available = int (attrs.get ('available', None))
used = int (attrs.get ('used', None))
unused = available - used
print ('Occupancy of %d byte slots: %d / %d (%d kB / %d%% wasted)<br>' % (size, used, available, unused * size / 1024, unused * 100 / available), file = self.collection_file)
elif name == 'section':
kind = attrs.get('type', None)
size = int(attrs.get('size', None))
self.heap_size += size
if not large_sections:
if kind == 'nursery':
self.section_handler = LargeSectionHandler ()
else:
self.section_handler = self.small_section_handler
self.section_handler.start_section (kind, size)
elif name == 'object':
klass = attrs.get('class', None)
size = int(attrs.get('size', None))
if self.in_los:
usage_dict = self.los_usage
self.los_size += size
elif self.in_pinned:
location = attrs.get('location', None)
if location not in self.pinned_usage:
self.pinned_usage[location] = {}
usage_dict = self.pinned_usage[location]
else:
usage_dict = self.usage
offset = int(attrs.get('offset', None))
self.section_handler.add_object (klass, offset, size)
self.heap_used += size
if not (klass in usage_dict):
usage_dict [klass] = (0, 0)
usage = usage_dict [klass]
usage_dict [klass] = (usage [0] + 1, usage [1] + size)
elif name == 'occupied':
offset = int(attrs.get('offset', None))
size = int(attrs.get('size', None))
self.section_handler.add_occupied (offset, size)
self.heap_used += size
elif name == 'los':
self.in_los = True
elif name == 'pinned-objects':
self.in_pinned = True
def dump_usage (self, usage_dict, limit):
klasses = sorted (usage_dict.keys (), lambda x, y: usage_dict [y][1] - usage_dict [x][1])
if limit:
klasses = klasses [0:limit]
for klass in klasses:
usage = usage_dict [klass]
if usage [1] < 100000:
print ('%s %d bytes' % (klass, usage [1]), file = self.collection_file)
else:
print ('%s %d kB' % (klass, usage [1] / 1024), file = self.collection_file)
print (' (%d)<br>' % usage [0], file = self.collection_file)
def endElement (self, name):
if name == 'section':
self.section_handler.end_section ()
if large_sections or self.section_handler != self.small_section_handler:
self.section_handler.emit (self.collection_file, self.collection_kind, self.collection_num, self.section_num)
self.section_num += 1
elif name == 'collection':
if not large_sections:
self.small_section_handler.emit (self.collection_file, self.collection_kind, self.collection_num, self.section_num)
self.dump_usage (self.usage, 10)
print ('<h3>LOS</h3>', file = self.collection_file)
self.dump_usage (self.los_usage, None)
print ('<h3>Pinned</h3>', file = self.collection_file)
for location in sorted (self.pinned_usage.keys ()):
print ('<h4>%s</h4>' % location, file = self.collection_file)
self.dump_usage (self.pinned_usage[location], None)
print ('</body></html>', file = self.collection_file)
print (' - %d kB / %d kB (%d%%) - %d kB LOS</a><br>' % (self.heap_used / 1024, self.heap_size / 1024, int(100.0 * self.heap_used / self.heap_size), self.los_size / 1024), file = self.index_file)
self.collection_file.close ()
self.collection_index += 1
elif name == 'los':
self.in_los = False
elif name == 'pinned-objects':
self.in_pinned = False
def main ():
usage = "usage: %prog [options]"
parser = OptionParser (usage)
parser.add_option ("-l", "--large-sections", action = "store_true", dest = "large_sections")
parser.add_option ("-s", "--small-sections", action = "store_false", dest = "large_sections")
(options, args) = parser.parse_args ()
if options.large_sections:
large_sections = True
dh = DocHandler ()
parser = make_parser ()
parser.setFeature (feature_namespaces, 0)
parser.setContentHandler (dh)
dh.start ()
parser.parse (sys.stdin)
dh.end ()
if __name__ == "__main__":
main ()
|