| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
|
| 4 |
import re, os, sys, operator |
| 5 |
|
| 6 |
""" |
| 7 |
This script parses a log produced by rstrategies_logger.py into a graph and converts it to various outputs. |
| 8 |
The most useful outputs are the dot* commands producing a visualization of the log using the dot-command of graphviz. |
| 9 |
Every strategy is a node in the graph, and the edges are collections or objects that transition between |
| 10 |
two strategies at some point during the log. |
| 11 |
Artificial nodes are created for log entries without an explicit source node. These are the events when a |
| 12 |
collection is created. |
| 13 |
The input to this script is a logfile, a command and optional flags. |
| 14 |
If the name of the logfile includes one of the AVAILABLE_VMS as a substring, the first three global variables |
| 15 |
are automatically configured. |
| 16 |
The script should work without these configurations, but the output will probably not be that pretty. |
| 17 |
To avoid errors, the -a flag is implied when running without proper configuration. |
| 18 |
""" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
STORAGE_NODES = [] |
| 23 |
|
| 24 |
|
| 25 |
NODE_RENAMINGS = {} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
STORAGE_SOURCES = {} |
| 30 |
|
| 31 |
def SET_VM(vm_name): |
| 32 |
global STORAGE_NODES |
| 33 |
global NODE_RENAMINGS |
| 34 |
global STORAGE_SOURCES |
| 35 |
if vm_name == 'RSqueak': |
| 36 |
STORAGE_NODES = ['List', 'WeakList', 'SmallIntegerOrNil', 'FloatOrNil', 'AllNil'] |
| 37 |
NODE_RENAMINGS = dict((x+'Strategy', x) for x in STORAGE_NODES) |
| 38 |
STORAGE_SOURCES = {'Filledin': 'Image Loading', 'Initialized': 'Object Creation'} |
| 39 |
elif vm_name == 'Pycket': |
| 40 |
STORAGE_SOURCES = {'Created': 'Array Creation'} |
| 41 |
|
| 42 |
elif vm_name == 'Topaz': |
| 43 |
|
| 44 |
pass |
| 45 |
else: |
| 46 |
raise Exception("Unhandled vm name %s" % vm_name) |
| 47 |
|
| 48 |
AVAILABLE_VMS = ['RSqueak', 'Pycket', 'Topaz'] |
| 49 |
|
| 50 |
def configure_vm(logfile, flags): |
| 51 |
vm_config_name = None |
| 52 |
for vm_name in AVAILABLE_VMS: |
| 53 |
if vm_name in logfile: |
| 54 |
vm_config_name = vm_name |
| 55 |
break |
| 56 |
if vm_config_name is not None: |
| 57 |
print("Using VM configuration %s" % vm_name) |
| 58 |
SET_VM(vm_name) |
| 59 |
else: |
| 60 |
print("No VM configuration found in filename '%s'. Available configurations: %s" % |
| 61 |
(logfile, AVAILABLE_VMS)) |
| 62 |
print("Please add new VM configuration or rename logfile. Turning on -a flag to avoid errors.") |
| 63 |
flags.allstorage = True |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
def percent(part, total): |
| 70 |
if total == 0: |
| 71 |
return 0 |
| 72 |
return float(part)*100 / total |
| 73 |
|
| 74 |
def parse(filename, flags, callback): |
| 75 |
parsed_entries = 0 |
| 76 |
if filename == "-": |
| 77 |
opener = lambda: sys.stdin |
| 78 |
else: |
| 79 |
opener = lambda: open(filename, 'r', 1) |
| 80 |
with opener() as file: |
| 81 |
while True: |
| 82 |
line = file.readline() |
| 83 |
if len(line) == 0: |
| 84 |
break |
| 85 |
entry = parse_line(line, flags) |
| 86 |
if entry: |
| 87 |
parsed_entries += 1 |
| 88 |
callback(entry) |
| 89 |
return parsed_entries |
| 90 |
|
| 91 |
line_pattern = re.compile("^(?P<operation>\w+) \(((?P<old>\w+) -> )?(?P<new>\w+)\)( of (?P<classname>.+))? size (?P<size>[0-9]+)( objects (?P<objects>[0-9]+))?( elements: (?P<classnames>.+( .+)*))?$") |
| 92 |
|
| 93 |
def parse_line(line, flags): |
| 94 |
result = line_pattern.match(line) |
| 95 |
if result is None: |
| 96 |
if flags.verbose: |
| 97 |
print("Could not parse line: %s" % line[:-1]) |
| 98 |
return None |
| 99 |
operation = str(result.group('operation')) |
| 100 |
old_storage = result.group('old') |
| 101 |
new_storage = str(result.group('new')) |
| 102 |
classname = str(result.group('classname')) |
| 103 |
size = int(result.group('size')) |
| 104 |
objects = result.group('objects') |
| 105 |
objects = int(objects) if objects else 1 |
| 106 |
classnames = result.group('classnames') |
| 107 |
if classnames is not None: |
| 108 |
classnames = classnames.split(' ') |
| 109 |
classnames = set(classnames) |
| 110 |
else: |
| 111 |
classnames = set() |
| 112 |
|
| 113 |
is_storage_source = old_storage is None |
| 114 |
if is_storage_source: |
| 115 |
if operation in STORAGE_SOURCES: |
| 116 |
old_storage = STORAGE_SOURCES[operation] |
| 117 |
else: |
| 118 |
print("Using operation %s as storage source." % operation) |
| 119 |
old_storage = str(old_storage) |
| 120 |
|
| 121 |
if new_storage in NODE_RENAMINGS: |
| 122 |
new_storage = NODE_RENAMINGS[new_storage] |
| 123 |
if old_storage in NODE_RENAMINGS: |
| 124 |
old_storage = NODE_RENAMINGS[old_storage] |
| 125 |
|
| 126 |
return LogEntry(operation, old_storage, new_storage, classname, size, objects, classnames, is_storage_source) |
| 127 |
|
| 128 |
class LogEntry(object): |
| 129 |
|
| 130 |
def __init__(self, operation, old_storage, new_storage, classname, size, objects, classnames, is_storage_source): |
| 131 |
self.operation = operation |
| 132 |
self.old_storage = old_storage |
| 133 |
self.new_storage = new_storage |
| 134 |
self.classname = classname |
| 135 |
self.size = size |
| 136 |
self.objects = objects |
| 137 |
self.classnames = classnames |
| 138 |
self.is_storage_source = is_storage_source |
| 139 |
assert old_storage != new_storage, "old and new storage identical in log entry: %s" % self |
| 140 |
|
| 141 |
def full_key(self): |
| 142 |
return (self.operation, self.old_storage, self.new_storage) |
| 143 |
|
| 144 |
def __lt__(self, other): |
| 145 |
return self.classname < other.classname |
| 146 |
|
| 147 |
def __repr__(self): |
| 148 |
return "%s(%s)" % (self.__str__(), object.__repr__(self)) |
| 149 |
|
| 150 |
def __str__(self): |
| 151 |
old_storage_string = "%s -> " % self.old_storage if self.old_storage else "" |
| 152 |
classname_string = " of %s" % self.classname if self.classname else "" |
| 153 |
objects_string = " objects %d" % self.objects if self.objects > 1 else "" |
| 154 |
return "%s (%s%s)%s size %d%s" % (self.operation, old_storage_string, self.new_storage, classname_string, self.size, objects_string) |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
class Operations(object): |
| 161 |
|
| 162 |
def __init__(self, objects=0, slots=0, element_classnames=[]): |
| 163 |
self.objects = objects |
| 164 |
self.slots = slots |
| 165 |
self.element_classnames = set(element_classnames) |
| 166 |
|
| 167 |
def __str__(self, total=None): |
| 168 |
if self.objects == 0: |
| 169 |
avg_slots = 0 |
| 170 |
else: |
| 171 |
avg_slots = float(self.slots) / self.objects |
| 172 |
if total is not None and total.slots != 0: |
| 173 |
percent_slots = " (%.1f%%)" % percent(self.slots, total.slots) |
| 174 |
else: |
| 175 |
percent_slots = "" |
| 176 |
if total is not None and total.objects != 0: |
| 177 |
if percent(self.objects, total.objects) < 0.1: |
| 178 |
percent_objects = " (< 0.1%)" |
| 179 |
else: |
| 180 |
percent_objects = " (%.1f%%)" % percent(self.objects, total.objects) |
| 181 |
else: |
| 182 |
percent_objects = "" |
| 183 |
slots = format(self.slots, ",d") |
| 184 |
objects = format(self.objects, ",d") |
| 185 |
classnames = (" [ elements: %s ]" % ' '.join([str(x) for x in self.element_classnames])) \ |
| 186 |
if len(self.element_classnames) else "" |
| 187 |
return "%s%s slots in %s%s objects (avg size: %.1f)%s" % (slots, percent_slots, objects, percent_objects, avg_slots, classnames) |
| 188 |
|
| 189 |
def __repr__(self): |
| 190 |
return "%s(%s)" % (self.__str__(), object.__repr__(self)) |
| 191 |
|
| 192 |
def add_log_entry(self, entry): |
| 193 |
self.slots = self.slots + entry.size |
| 194 |
self.objects = self.objects + entry.objects |
| 195 |
self.element_classnames |= entry.classnames |
| 196 |
|
| 197 |
def __sub__(self, other): |
| 198 |
return Operations(self.objects - other.objects, self.slots - other.slots) |
| 199 |
|
| 200 |
def __add__(self, other): |
| 201 |
return Operations(self.objects + other.objects, self.slots + other.slots) |
| 202 |
|
| 203 |
def __lt__(self, other): |
| 204 |
return self.slots < other.slots |
| 205 |
|
| 206 |
def empty(self): |
| 207 |
return self.objects == 0 and self.slots == 0 |
| 208 |
|
| 209 |
def prefixprint(self, key="", total=None): |
| 210 |
if not self.empty(): |
| 211 |
print("%s%s" % (key, self.__str__(total))) |
| 212 |
|
| 213 |
class ClassOperations(object): |
| 214 |
|
| 215 |
def __init__(self): |
| 216 |
self.classes = {} |
| 217 |
|
| 218 |
def cls(self, name): |
| 219 |
if name not in self.classes: |
| 220 |
self.classes[name] = Operations() |
| 221 |
return self.classes[name] |
| 222 |
|
| 223 |
def total(self): |
| 224 |
return reduce(operator.add, self.classes.values(), Operations()) |
| 225 |
|
| 226 |
def __str__(self): |
| 227 |
return "ClassOperations(%s)" % self.classes |
| 228 |
|
| 229 |
def __repr__(self): |
| 230 |
return "%s(%s)" % (self.__str__(), object.__repr__(self)) |
| 231 |
|
| 232 |
def __add__(self, other): |
| 233 |
result = ClassOperations() |
| 234 |
result.classes = dict(self.classes) |
| 235 |
for classname, other_class in other.classes.items(): |
| 236 |
result.cls(classname) |
| 237 |
result.classes[classname] += other_class |
| 238 |
return result |
| 239 |
|
| 240 |
def __sub__(self, other): |
| 241 |
result = ClassOperations() |
| 242 |
result.classes = dict(self.classes) |
| 243 |
for classname, other_class in other.classes.items(): |
| 244 |
result.cls(classname) |
| 245 |
result.classes[classname] -= other_class |
| 246 |
return result |
| 247 |
|
| 248 |
class StorageEdge(object): |
| 249 |
|
| 250 |
def __init__(self, operation="None", origin=None, target=None): |
| 251 |
self.operation = operation |
| 252 |
self.classes = ClassOperations() |
| 253 |
self.origin = origin |
| 254 |
self.target = target |
| 255 |
self.is_storage_source = False |
| 256 |
|
| 257 |
def full_key(self): |
| 258 |
return (self.operation, self.origin.name, self.target.name) |
| 259 |
|
| 260 |
def cls(self, classname): |
| 261 |
return self.classes.cls(classname) |
| 262 |
|
| 263 |
def total(self): |
| 264 |
return self.classes.total() |
| 265 |
|
| 266 |
def notify_nodes(self): |
| 267 |
self.origin.note_outgoing(self) |
| 268 |
self.target.note_incoming(self) |
| 269 |
|
| 270 |
def add_log_entry(self, entry): |
| 271 |
self.cls(entry.classname).add_log_entry(entry) |
| 272 |
if entry.is_storage_source: |
| 273 |
self.is_storage_source = True |
| 274 |
|
| 275 |
def as_log_entries(self): |
| 276 |
entries = [] |
| 277 |
for classname, ops in self.classes.classes.items(): |
| 278 |
origin = None if self.is_storage_source else self.origin.name |
| 279 |
entry = LogEntry(self.operation, origin, self.target.name, classname, |
| 280 |
ops.slots, ops.objects, ops.element_classnames, self.is_storage_source) |
| 281 |
entries.append(entry) |
| 282 |
return entries |
| 283 |
|
| 284 |
def __lt__(self, other): |
| 285 |
return self.full_key() < other.full_key() |
| 286 |
|
| 287 |
def __str__(self): |
| 288 |
return "[%s %s -> %s]" % (self.operation, self.origin, self.target) |
| 289 |
|
| 290 |
def __repr__(self): |
| 291 |
return "%s(%s)" % (self.__str__(), object.__repr__(self)) |
| 292 |
|
| 293 |
def __add__(self, other): |
| 294 |
origin = self.origin if self.origin is not None else other.origin |
| 295 |
target = self.target if self.target is not None else other.target |
| 296 |
result = StorageEdge(self.operation, origin, target) |
| 297 |
result.classes += self.classes + other.classes |
| 298 |
return result |
| 299 |
|
| 300 |
def __sub__(self, other): |
| 301 |
origin = self.origin if self.origin is not None else other.origin |
| 302 |
target = self.target if self.target is not None else other.target |
| 303 |
result = StorageEdge(self.operation, origin, target) |
| 304 |
result.classes += self.classes - other.classes |
| 305 |
return result |
| 306 |
|
| 307 |
class StorageNode(object): |
| 308 |
|
| 309 |
def __init__(self, name): |
| 310 |
self.name = name |
| 311 |
self.incoming = set() |
| 312 |
self.outgoing = set() |
| 313 |
|
| 314 |
def note_incoming(self, edge): |
| 315 |
assert edge.target is self |
| 316 |
if edge not in self.incoming: |
| 317 |
self.incoming.add(edge) |
| 318 |
|
| 319 |
def note_outgoing(self, edge): |
| 320 |
assert edge.origin is self |
| 321 |
if edge not in self.outgoing: |
| 322 |
self.outgoing.add(edge) |
| 323 |
|
| 324 |
def incoming_edges(self, operation): |
| 325 |
return filter(lambda x: x.operation == operation, self.incoming) |
| 326 |
|
| 327 |
def outgoing_edges(self, operation): |
| 328 |
return filter(lambda x: x.operation == operation, self.outgoing) |
| 329 |
|
| 330 |
def sum_incoming(self, operation): |
| 331 |
return reduce(operator.add, self.incoming_edges(operation), StorageEdge(operation)) |
| 332 |
|
| 333 |
def sum_outgoing(self, operation): |
| 334 |
return reduce(operator.add, self.outgoing_edges(operation), StorageEdge(operation)) |
| 335 |
|
| 336 |
def sum_all_incoming(self): |
| 337 |
return reduce(operator.add, self.incoming, StorageEdge()) |
| 338 |
|
| 339 |
def sum_all_outgoing(self): |
| 340 |
return reduce(operator.add, self.outgoing, StorageEdge()) |
| 341 |
|
| 342 |
def __str__(self): |
| 343 |
return self.name |
| 344 |
|
| 345 |
def __repr__(self): |
| 346 |
return "%s(%s)" % (self.__str__(), object.__repr__(self)) |
| 347 |
|
| 348 |
def merge_edge_sets(self, set1, set2, key_slot): |
| 349 |
getter = lambda edge: edge.__dict__[key_slot] |
| 350 |
set_dict = dict([(getter(edge), edge) for edge in set1]) |
| 351 |
for edge in set2: |
| 352 |
key = getter(edge) |
| 353 |
if key not in set_dict: |
| 354 |
set_dict[key] = edge |
| 355 |
else: |
| 356 |
set_dict[key] += edge |
| 357 |
return set(set_dict.values()) |
| 358 |
|
| 359 |
def __add__(self, other): |
| 360 |
result = StorageNode("%s %s" % (self.name, other.name)) |
| 361 |
result.incoming = self.merge_edge_sets(self.incoming, other.incoming, "origin") |
| 362 |
|
| 363 |
for edge in result.incoming: |
| 364 |
edge.target = result |
| 365 |
result.outgoing = self.merge_edge_sets(self.outgoing, other.outgoing, "target") |
| 366 |
for edge in result.outgoing: |
| 367 |
edge.origin = result |
| 368 |
return result |
| 369 |
|
| 370 |
def __lt__(self, other): |
| 371 |
return self.name < other.name |
| 372 |
|
| 373 |
def is_artificial(self): |
| 374 |
for outgoing in self.outgoing: |
| 375 |
if outgoing.is_storage_source: |
| 376 |
return True |
| 377 |
return False |
| 378 |
|
| 379 |
def is_storage_node(self): |
| 380 |
return self.is_artificial() or self.name in STORAGE_NODES |
| 381 |
|
| 382 |
def dot_name(self): |
| 383 |
return self.name.replace(" ", "_") |
| 384 |
|
| 385 |
class StorageGraph(object): |
| 386 |
|
| 387 |
def __init__(self): |
| 388 |
self.nodes = {} |
| 389 |
self.edges = {} |
| 390 |
self.operations = set() |
| 391 |
|
| 392 |
def node(self, name): |
| 393 |
if name not in self.nodes: |
| 394 |
self.nodes[name] = StorageNode(name) |
| 395 |
return self.nodes[name] |
| 396 |
|
| 397 |
def assert_sanity(self): |
| 398 |
visited_edges = set() |
| 399 |
for node in self.nodes.values(): |
| 400 |
for edge in node.incoming: |
| 401 |
assert edge in self.edges.values(), "Edge not in graph's edges: %s" % edge |
| 402 |
visited_edges.add(edge) |
| 403 |
if not edge.target is node: |
| 404 |
print("Wrong edge target: %s\nIncoming edge: %s\nIn node: %s" % (edge.target, edge, node)) |
| 405 |
assert False |
| 406 |
if not edge in edge.origin.outgoing: |
| 407 |
print("Edge not in origin's outgoing: %s\nIncoming edge: %s\nIn node: %s" % (edge.origin.outgoing, edge, node)) |
| 408 |
assert False |
| 409 |
for edge in node.outgoing: |
| 410 |
assert edge in self.edges.values(), "Edge not in graph's edges: %s" % edge |
| 411 |
visited_edges.add(edge) |
| 412 |
if not edge.origin is node: |
| 413 |
print("Wrong edge origin: %s\nOutgoing edge: %s\nIn node: %s" % (edge.origin, edge, node)) |
| 414 |
assert False |
| 415 |
if not edge in edge.target.incoming: |
| 416 |
print("Edge not in origin's incoming: %s\nOutgoing edge: %s\nIn node: %s" % (edge.target.incoming, edge, node)) |
| 417 |
assert False |
| 418 |
assert len(visited_edges) == len(self.edges.values()), "Not all of graph's edges visited." |
| 419 |
|
| 420 |
def add_log_entry(self, log_entry): |
| 421 |
self.operations.add(log_entry.operation) |
| 422 |
key = log_entry.full_key() |
| 423 |
if key not in self.edges: |
| 424 |
edge = StorageEdge(log_entry.operation, self.node(log_entry.old_storage), self.node(log_entry.new_storage)) |
| 425 |
self.edges[key] = edge |
| 426 |
edge.notify_nodes() |
| 427 |
self.edges[key].add_log_entry(log_entry) |
| 428 |
|
| 429 |
def collapse_nodes(self, collapsed_nodes, new_name=None): |
| 430 |
if len(collapsed_nodes) == 0: |
| 431 |
return |
| 432 |
for node in collapsed_nodes: |
| 433 |
del self.nodes[node.name] |
| 434 |
for edge in node.incoming: |
| 435 |
del self.edges[edge.full_key()] |
| 436 |
for edge in node.outgoing: |
| 437 |
del self.edges[edge.full_key()] |
| 438 |
new_node = reduce(operator.add, collapsed_nodes) |
| 439 |
if new_name is not None: |
| 440 |
new_node.name = new_name |
| 441 |
self.nodes[new_node.name] = new_node |
| 442 |
|
| 443 |
for node in collapsed_nodes: |
| 444 |
for edge in node.incoming: |
| 445 |
edge.origin.outgoing.remove(edge) |
| 446 |
new_edges = filter(lambda filtered: filtered.origin == edge.origin, new_node.incoming) |
| 447 |
assert len(new_edges) == 1 |
| 448 |
edge.origin.outgoing.add(new_edges[0]) |
| 449 |
for edge in node.outgoing: |
| 450 |
edge.target.incoming.remove(edge) |
| 451 |
new_edges = filter(lambda filtered: filtered.target == edge.target, new_node.outgoing) |
| 452 |
assert len(new_edges) == 1 |
| 453 |
edge.target.incoming.add(new_edges[0]) |
| 454 |
for edge in new_node.incoming: |
| 455 |
self.edges[edge.full_key()] = edge |
| 456 |
for edge in new_node.outgoing: |
| 457 |
self.edges[edge.full_key()] = edge |
| 458 |
self.assert_sanity() |
| 459 |
|
| 460 |
def collapse_nonstorage_nodes(self, new_name=None): |
| 461 |
nodes = filter(lambda x: not x.is_storage_node(), self.nodes.values()) |
| 462 |
self.collapse_nodes(nodes, new_name) |
| 463 |
|
| 464 |
def sorted_nodes(self): |
| 465 |
nodes = self.nodes.values() |
| 466 |
nodes.sort() |
| 467 |
return nodes |
| 468 |
|
| 469 |
def make_graph(logfile, flags): |
| 470 |
graph = StorageGraph() |
| 471 |
def callback(entry): |
| 472 |
graph.add_log_entry(entry) |
| 473 |
parse(logfile, flags, callback) |
| 474 |
graph.assert_sanity() |
| 475 |
return graph |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
def command_summarize(logfile, flags): |
| 482 |
graph = make_graph(logfile, flags) |
| 483 |
if not flags.allstorage: |
| 484 |
graph.collapse_nonstorage_nodes() |
| 485 |
for node in graph.sorted_nodes(): |
| 486 |
node.print_summary(flags, graph.operations) |
| 487 |
|
| 488 |
def StorageNode_print_summary(self, flags, all_operations): |
| 489 |
print("\n%s:" % self.name) |
| 490 |
sum = StorageEdge() |
| 491 |
total_incoming = self.sum_all_incoming().total() if flags.percent else None |
| 492 |
|
| 493 |
print("\tIncoming:") |
| 494 |
for operation in all_operations: |
| 495 |
if flags.detailed: |
| 496 |
edges = [ (edge.origin.name, edge) for edge in self.incoming_edges(operation) ] |
| 497 |
else: |
| 498 |
edges = [ (operation, self.sum_incoming(operation)) ] |
| 499 |
for edgename, edge in edges: |
| 500 |
edge.print_with_name("\t\t\t", edgename, total_incoming, flags) |
| 501 |
sum += edge |
| 502 |
|
| 503 |
print("\tOutgoing:") |
| 504 |
for operation in all_operations: |
| 505 |
if flags.detailed: |
| 506 |
edges = [ (edge.target.name, edge) for edge in self.outgoing_edges(operation) ] |
| 507 |
else: |
| 508 |
edges = [ (operation, self.sum_outgoing(operation)) ] |
| 509 |
for edgename, edge in edges: |
| 510 |
edge.print_with_name("\t\t\t", edgename, total_incoming, flags) |
| 511 |
sum -= edge |
| 512 |
|
| 513 |
sum.print_with_name("\t", "Remaining", total_incoming, flags) |
| 514 |
|
| 515 |
StorageNode.print_summary = StorageNode_print_summary |
| 516 |
|
| 517 |
def StorageEdge_print_with_name(self, prefix, edgename, total_reference, flags): |
| 518 |
if flags.classes: |
| 519 |
print("%s%s:" % (prefix, edgename)) |
| 520 |
prefix += "\t\t" |
| 521 |
operations = self.classes.classes.items() |
| 522 |
operations.sort(reverse=True, key=operator.itemgetter(1)) |
| 523 |
else: |
| 524 |
operations = [ (edgename, self.total()) ] |
| 525 |
for classname, classops in operations: |
| 526 |
classops.prefixprint("%s%s: " % (prefix, classname), total_reference) |
| 527 |
|
| 528 |
StorageEdge.print_with_name = StorageEdge_print_with_name |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
def command_print_dot(logfile, flags): |
| 536 |
graph = make_graph(logfile, flags) |
| 537 |
print("/*") |
| 538 |
print("Storage Statistics (dot format):") |
| 539 |
print("================================") |
| 540 |
print("*/") |
| 541 |
print(dot_string(graph, flags)) |
| 542 |
|
| 543 |
def run_dot(logfile, flags, output_type): |
| 544 |
import subprocess |
| 545 |
dot = dot_string(make_graph(logfile, flags), flags) |
| 546 |
command = ["dot", "-T%s" % output_type, "-o%s.%s" % (flags.logfile, output_type)] |
| 547 |
print("Running:\n%s" % " ".join(command)) |
| 548 |
p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 549 |
output = p.communicate(input=dot)[0] |
| 550 |
print(output) |
| 551 |
|
| 552 |
def command_dot(logfile, flags): |
| 553 |
run_dot(logfile, flags, "jpg") |
| 554 |
def command_dot_ps(logfile, flags): |
| 555 |
run_dot(logfile, flags, "ps") |
| 556 |
def command_dot_pdf(logfile, flags): |
| 557 |
run_dot(logfile, flags, "pdf") |
| 558 |
def command_dot_svg(logfile, flags): |
| 559 |
run_dot(logfile, flags, "svg") |
| 560 |
|
| 561 |
def dot_string(graph, flags): |
| 562 |
result = "digraph G {" |
| 563 |
incoming_cache = {} |
| 564 |
if not flags.allstorage: |
| 565 |
graph.collapse_nonstorage_nodes("Other") |
| 566 |
|
| 567 |
def make_label(edge, prefix="", total_edge=None, slots_per_object=False): |
| 568 |
object_suffix = " objects" |
| 569 |
slots_suffix = " slots" |
| 570 |
if not flags.objects or not flags.slots: |
| 571 |
object_suffix = slots_suffix = "" |
| 572 |
if total_edge and flags.percent and total_edge.objects != 0: |
| 573 |
if percent(edge.objects, total_edge.objects) < 0.1: |
| 574 |
percent_objects = " (< 0.1%)" |
| 575 |
else: |
| 576 |
percent_objects = " (%.1f%%)" % percent(edge.objects, total_edge.objects) |
| 577 |
if percent(edge.slots, total_edge.slots) < 0.1: |
| 578 |
percent_slots = " (< 0.1%)" |
| 579 |
else: |
| 580 |
percent_slots = " (%.1f%%)" % percent(edge.slots, total_edge.slots) |
| 581 |
else: |
| 582 |
percent_objects = percent_slots = "" |
| 583 |
label = "" |
| 584 |
if flags.objects: |
| 585 |
label += "%s%s%s%s<BR/>" % (prefix, format(edge.objects, ",.0f"), object_suffix, percent_objects) |
| 586 |
if flags.slots: |
| 587 |
label += "%s%s%s%s<BR/>" % (prefix, format(edge.slots, ",.0f"), slots_suffix, percent_slots) |
| 588 |
if slots_per_object and flags.slotsPerObject: |
| 589 |
label += "%.1f slots/object<BR/>" % (float(total.slots) / total.objects) |
| 590 |
return label |
| 591 |
|
| 592 |
for node in graph.nodes.values(): |
| 593 |
incoming = node.sum_all_incoming().total() |
| 594 |
outgoing = node.sum_all_outgoing().total() |
| 595 |
remaining = incoming - outgoing |
| 596 |
if node.is_artificial(): |
| 597 |
incoming_cache[node.name] = outgoing |
| 598 |
shape = ",shape=box" |
| 599 |
label = make_label(outgoing) |
| 600 |
else: |
| 601 |
incoming_cache[node.name] = incoming |
| 602 |
shape = "" |
| 603 |
label = make_label(incoming, "Incoming: ") |
| 604 |
if remaining.objects != incoming.objects: |
| 605 |
label += make_label(remaining, "Remaining: ", incoming) |
| 606 |
result += "%s [label=<<B><U>%s</U></B><BR/>%s>%s];" % (node.dot_name(), node.name, label, shape) |
| 607 |
|
| 608 |
for edge in graph.edges.values(): |
| 609 |
total = edge.total() |
| 610 |
incoming = incoming_cache[edge.origin.name] |
| 611 |
label = make_label(total, "", incoming, slots_per_object=True) |
| 612 |
target_node = edge.target.dot_name() |
| 613 |
source_node = edge.origin.dot_name() |
| 614 |
result += "%s -> %s [label=<%s>];" % (source_node, target_node, label) |
| 615 |
|
| 616 |
result += "}" |
| 617 |
return result |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
def command_aggregate(logfile, flags): |
| 624 |
graph = make_graph(logfile, flags) |
| 625 |
edges = graph.edges.values() |
| 626 |
edges.sort() |
| 627 |
for edge in edges: |
| 628 |
logentries = edge.as_log_entries() |
| 629 |
logentries.sort() |
| 630 |
for entry in logentries: |
| 631 |
print(entry) |
| 632 |
|
| 633 |
def command_print_entries(logfile, flags): |
| 634 |
def callback(entry): |
| 635 |
print(entry) |
| 636 |
parse(logfile, flags, callback) |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
class Flags(object): |
| 643 |
|
| 644 |
def __init__(self, flags): |
| 645 |
self.flags = {} |
| 646 |
for name, short in flags: |
| 647 |
self.__dict__[name] = False |
| 648 |
self.flags[short] = name |
| 649 |
|
| 650 |
def handle(self, arg): |
| 651 |
if arg in self.flags: |
| 652 |
self.__dict__[self.flags[arg]] = True |
| 653 |
return True |
| 654 |
else: |
| 655 |
return False |
| 656 |
|
| 657 |
def __str__(self): |
| 658 |
descriptions = [ ("%s (%s)" % description) for description in self.flags.items() ] |
| 659 |
return "[%s]" % " | ".join(descriptions) |
| 660 |
|
| 661 |
def usage(flags, commands): |
| 662 |
print("Arguments: logfile command %s" % flags) |
| 663 |
print("Available commands: %s" % commands) |
| 664 |
exit(1) |
| 665 |
|
| 666 |
def main(argv): |
| 667 |
flags = Flags([ |
| 668 |
|
| 669 |
('verbose', '-v'), |
| 670 |
|
| 671 |
|
| 672 |
('percent', '-p'), |
| 673 |
('allstorage', '-a'), |
| 674 |
|
| 675 |
|
| 676 |
('detailed', '-d'), |
| 677 |
('classes', '-c'), |
| 678 |
|
| 679 |
|
| 680 |
('slots', '-s'), |
| 681 |
('objects', '-o'), |
| 682 |
('slotsPerObject', '-S'), |
| 683 |
]) |
| 684 |
|
| 685 |
command_prefix = "command_" |
| 686 |
module = sys.modules[__name__].__dict__ |
| 687 |
commands = [ a[len(command_prefix):] for a in module.keys() if a.startswith(command_prefix) ] |
| 688 |
|
| 689 |
if len(argv) < 2: |
| 690 |
usage(flags, commands) |
| 691 |
logfile = argv[0] |
| 692 |
flags.logfile = logfile |
| 693 |
configure_vm(logfile, flags) |
| 694 |
command = argv[1] |
| 695 |
for flag in argv[2:]: |
| 696 |
if not flags.handle(flag): |
| 697 |
usage(flags, commands) |
| 698 |
if command not in commands: |
| 699 |
usage(flags, commands) |
| 700 |
|
| 701 |
func = module[command_prefix + command] |
| 702 |
func(logfile, flags) |
| 703 |
|
| 704 |
if __name__ == "__main__": |
| 705 |
main(sys.argv[1:]) |