2023-01-14 04:36:28 +08:00
|
|
|
#!/usr/bin/python3
|
2021-10-30 04:25:12 +08:00
|
|
|
# This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
# Given a heap snapshot, this tool gathers basic statistics about the allocated objects
|
|
|
|
# To generate a snapshot, use luaC_dump, ideally preceded by luaC_fullgc
|
|
|
|
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
def updatesize(d, k, s):
|
|
|
|
oc, os = d.get(k, (0, 0))
|
|
|
|
d[k] = (oc + 1, os + s)
|
|
|
|
|
|
|
|
def sortedsize(p):
|
|
|
|
return sorted(p, key = lambda s: s[1][1], reverse = True)
|
|
|
|
|
2022-08-12 04:42:54 +08:00
|
|
|
def getkey(heap, obj, key):
|
|
|
|
pairs = obj.get("pairs", [])
|
|
|
|
for i in range(0, len(pairs), 2):
|
|
|
|
if pairs[i] and heap[pairs[i]]["type"] == "string" and heap[pairs[i]]["data"] == key:
|
|
|
|
if pairs[i + 1] and heap[pairs[i + 1]]["type"] == "string":
|
|
|
|
return heap[pairs[i + 1]]["data"]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
with open(sys.argv[1]) as f:
|
|
|
|
dump = json.load(f)
|
|
|
|
heap = dump["objects"]
|
|
|
|
|
|
|
|
size_type = {}
|
|
|
|
size_udata = {}
|
|
|
|
size_category = {}
|
|
|
|
|
|
|
|
for addr, obj in heap.items():
|
|
|
|
updatesize(size_type, obj["type"], obj["size"])
|
|
|
|
|
|
|
|
if obj.get("cat") != None:
|
|
|
|
updatesize(size_category, str(obj["cat"]), obj["size"])
|
|
|
|
|
|
|
|
if obj["type"] == "userdata" and "metatable" in obj:
|
|
|
|
metatable = heap[obj["metatable"]]
|
2022-08-12 04:42:54 +08:00
|
|
|
typemt = getkey(heap, metatable, "__type") or "unknown"
|
2021-10-30 04:25:12 +08:00
|
|
|
updatesize(size_udata, typemt, obj["size"])
|
|
|
|
|
|
|
|
print("objects by type:")
|
|
|
|
for type, (count, size) in sortedsize(size_type.items()):
|
|
|
|
print(type.ljust(10), str(size).rjust(8), "bytes", str(count).rjust(5), "objects")
|
|
|
|
|
|
|
|
print()
|
|
|
|
|
|
|
|
print("userdata by __type:")
|
|
|
|
for type, (count, size) in sortedsize(size_udata.items()):
|
|
|
|
print(type.ljust(20), str(size).rjust(8), "bytes", str(count).rjust(5), "objects")
|
|
|
|
|
|
|
|
if len(size_category) != 0:
|
|
|
|
print()
|
|
|
|
|
|
|
|
print("objects by category:")
|
|
|
|
for type, (count, size) in sortedsize(size_category.items()):
|
2022-08-19 05:04:33 +08:00
|
|
|
cat = dump["stats"]["categories"][type]
|
|
|
|
name = cat["name"] if "name" in cat else str(type)
|
2021-10-30 04:25:12 +08:00
|
|
|
print(name.ljust(30), str(size).rjust(8), "bytes", str(count).rjust(5), "objects")
|