Skip to content

Commit

Permalink
clean up more
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbackrack committed Jan 28, 2025
1 parent 6bee7f8 commit f3e6f82
Showing 1 changed file with 17 additions and 119 deletions.
136 changes: 17 additions & 119 deletions core/heap-analysis.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ lostanza defn roots (dom:ptr<core/HeapDominator>) -> ptr<LSLongVector> :
lostanza defn offs (dom:ptr<core/HeapDominator>) -> ptr<LSLongVector> :
return dom.offs as ptr<LSLongVector>

lostanza defn collect-object-address-and-size (p:ptr<long>, tag:int, size:long, vms:ptr<core/VMState>) -> ref<False> :
lostanza defn collect-object-address-and-size
(p:ptr<long>, tag:int, size:long, vms:ptr<core/VMState>) -> ref<False> :
add(addrs(vms.dom), p as long)
add(sizes(vms.dom), size as long)
return false

lostanza defn collect-object-contents (p:ptr<long>, tag:int, size:long, vms:ptr<core/VMState>) -> ref<False> :
lostanza defn collect-object-contents
(p:ptr<long>, tag:int, size:long, vms:ptr<core/VMState>) -> ref<False> :
add(offs(vms.dom), heap(vms.dom).length)
add(heap(vms.dom), tag as long)
val idx = heap(vms.dom).length
Expand Down Expand Up @@ -81,7 +83,8 @@ public lostanza defn register-root-reference (ref:ptr<long>, vms:ptr<core/VMStat
return false

lostanza defn iterate-objects
(pstart:ptr<long>, pend:ptr<long>, vms:ptr<core/VMState>, f:ptr<((ptr<long>, int, long, ptr<core/VMState>) -> ref<False>)>) -> ref<False> :
(pstart:ptr<long>, pend:ptr<long>, vms:ptr<core/VMState>,
f:ptr<((ptr<long>, int, long, ptr<core/VMState>) -> ref<False>)>) -> ref<False> :
var p:ptr<long> = pstart
while p < pend :
val tag = [p] as int
Expand Down Expand Up @@ -122,7 +125,8 @@ lostanza deftype LowFlatObjects :
lostanza deftype FlatObjects <: IndexedCollection&Lengthable :
value : ptr<LowFlatObjects>

lostanza defn FlatObjects (sizes:ptr<LSLongVector>, offs:ptr<LSLongVector>, heap:ptr<LSLongVector>) -> ref<FlatObjects> :
lostanza defn FlatObjects
(sizes:ptr<LSLongVector>, offs:ptr<LSLongVector>, heap:ptr<LSLongVector>) -> ref<FlatObjects> :
val lfo = call-c clib/stz_malloc(sizeof(LowFlatObjects)) as ptr<LowFlatObjects>
lfo.sizes = sizes
lfo.offs = offs
Expand Down Expand Up @@ -243,7 +247,6 @@ defn print-id-object-stats (objs:FlatObjects, tot-sizes:Tuple<Int>) -> False :
println("")

defn objects-to-id-objects (objs:FlatObjects) -> FlatIdObjects :
; print-id-object-guts(objs)
FlatIdObjects(to-tuple $ (0 to length(objs)), to-tuple $ (0 to length(objs)), objs)

;;; DOMINATORS
Expand All @@ -260,12 +263,10 @@ defn depth-first (ios:FlatIdObjects) -> FlatIdObjects :
add(order0, idx)
val missing = filter({ not visited?[_] }, 0 to length(visited?))
val order = to-tuple $ cat(missing, order0)
; println("DFS %_ %_" % [length(order), order])
FlatIdObjects(to-tuple $ order, scatter(0 to length(order), to-tuple(order)), objs(ios))

; fast dominators algorithm assuming depth-first order
defn idom (num:Int, prevs:Tuple<List<Int>>) -> Tuple<Int> :
; println("IDOM NUM %_ PREVS %_" % [num, prevs])
val doms = Array<Int>(num, -1)
val start-id = num - 1
doms[start-id] = start-id
Expand Down Expand Up @@ -304,15 +305,15 @@ defn idom (num:Int, prevs:Tuple<List<Int>>) -> Tuple<Int> :

defn calc-sizes (ios:FlatIdObjects, doms:Tuple<Int>) -> Array<Int> :
val tot-sizes = to-array<Int> $ sizes(ios)
; println("%_: %_" % [0, tot-sizes])
val len = length(ios)
for i in 0 to (len - 1) do :
if doms[i] >= 0 :
tot-sizes[doms[i]] = tot-sizes[doms[i]] + tot-sizes[i]
; println("%_: %_" % [i + 1, tot-sizes])
tot-sizes

defn print-xml (s:FileOutputStream, id-objs:FlatIdObjects, sizes:Array<Int>, nexts:Tuple<List<Int>>, doms:Tuple<Int>, threshold:Int = 0) :
defn print-xml
(s:FileOutputStream, id-objs:FlatIdObjects, sizes:Array<Int>,
nexts:Tuple<List<Int>>, doms:Tuple<Int>, threshold:Int = 0) :
val objs = objs(id-objs)
defn children (doms:Tuple<Int>) -> Tuple<Tuple<Int>> :
val children = to-tuple $ repeatedly({ Vector<Int>() }, length(nexts))
Expand All @@ -321,133 +322,30 @@ defn print-xml (s:FileOutputStream, id-objs:FlatIdObjects, sizes:Array<Int>, nex
map(to-tuple, children)
defn stringify (s:String) -> String :
replace(s, "&", "A")
defn indent (n:Int) :
for i in 0 to n do : print(s, " ")
defn P (n:Int, str:Printable) :
for i in 0 to (n * 2) do : print(s, " ")
println(s, str)
val kiddies = children(doms)
let walk (idx:Int = length(doms) - 1, depth:Int = 0) :
val id = order(id-objs)[idx]
val name = stringify(class-name(tag-of(objs, id)))
indent(depth * 2) println(s, "<%_ RETAINED=\"%_\" STATIC=\"%_\">" % [name, sizes[idx], size-of(objs, id)])
P(depth, "<%_ RETAINED=\"%_\" STATIC=\"%_\">" % [name, sizes[idx], size-of(objs, id)])
val childs = reverse $ to-list $ qsort({ sizes[_] }, filter({ sizes[_] > threshold }, kiddies[idx]))
for child in childs do :
walk(child, depth + 1)
indent(depth * 2) println(s, "</%_>" % [name])
P(depth, "</%_>" % [name])

public defn heap-dominator-tree (filename:String) -> FlatIdObjects :
val objs = do-dominator-tree()
; val objs = tst-dominator-tree()
; dump-heap(objs)
val id-objs0 = objects-to-id-objects(objs)
; val nxts0 = nexts(id-objs0)
; val prvs0 = prevs(nxts0)
; for (id in order(id-objs0), i in 0 to false) do :
; id-print-guts(i, id, tag-of(objs, id), refs(objs, id))
; print(" NEXTS %_ PREVS %_" % [nxts0[i], prvs0[i]])
; println("")
val id-objs = depth-first(id-objs0)
; val nxts = nexts(id-objs)
; val prvs = prevs(nxts)
; for (id in order(id-objs), i in 0 to false) do :
; id-print-guts(i, id, tag-of(objs, id), refs(objs, id))
; print(" NEXTS %_ PREVS %_" % [nxts[i], prvs[i]])
; println("")
val nxts = nexts(id-objs)
val doms = idom(length(id-objs), prevs(nxts))
; println("IDOM DONE %_" % [doms])
val sizes = calc-sizes(id-objs, doms)
; println("SIZES %_" % [sizes])
print-id-object-stats(objs, to-tuple $ gather(sizes, reorder(id-objs)))
val s = FileOutputStream(filename)
print-xml(s, id-objs, sizes, nxts, doms)
close(s)
id-objs

; defstruct Tup :
; value : Tuple
;
; val tup3 = Tup([ 0 1 2 3 4 5 6 7 8 9 ])
; val tup2 = Tup([ tup3 tup3 ])
; val tup1 = Tup([ tup2 tup2 ])
; val tup0 = Tup([ tup1 tup1 ])

defstruct BinTree :
left : BinTree|Int
right : BinTree|Int

defn bin-tree (n:Int) -> BinTree :
if n <= 0 :
BinTree(0, 1)
else :
BinTree(bin-tree(n - 1), bin-tree(n - 1))

val tup = bin-tree(6)

heap-dominator-tree("sizes.xml")

; val tst-2 = [ 1 ]
; val tst-1 = [ tst-2 ]
; val tst-0 = [ tst-1, tst-2 ]
;
; lostanza defn object-type-id (x:ref<?>) -> int :
; val ref = x as long
; val p = (ref - 1L) as ptr<long>
; return [p] as int
;
; lostanza defn tst-dominator-tree () -> ref<FlatObjects> :
; val vms:ptr<core/VMState> = call-prim flush-vm()
; val dom = vms.dom
; clear(offs(dom))
; clear(sizes(dom))
; clear(heap(dom))
; ;0
; add(offs(dom), 0L)
; add(heap(dom), -1L)
; add(heap(dom), 1L)
; add(heap(dom), 2L)
; add(sizes(dom), 2L * 8L)
; ;1
; add(offs(dom), heap(dom).length)
; add(heap(dom), object-type-id(tst-1))
; add(heap(dom), 1L)
; add(heap(dom), 3L)
; add(sizes(dom), 8L + 1L * 8L)
; ;2
; add(offs(dom), heap(dom).length)
; add(heap(dom), object-type-id(tst-0))
; add(heap(dom), 2L)
; add(heap(dom), 1L)
; add(heap(dom), 3L)
; add(sizes(dom), 8L + 2L * 8L)
; ;3
; add(offs(dom), heap(dom).length)
; add(heap(dom), object-type-id(tst-1))
; add(heap(dom), 0L)
; add(sizes(dom), 8L)
; return FlatObjects(sizes(dom), offs(dom), heap(dom))

lostanza defn dump-heap (objs:ref<FlatObjects>) -> ref<False> :
call-c clib/printf("OFFS:\n")
for (var i:int = 0, i < objs.value.offs.length, i = i + 1) :
call-c clib/printf("%d : %ld\n", i, objs.value.offs.items[i])
call-c clib/printf("HEAP:\n")
for (var i:int = 0, i < objs.value.heap.length, i = i + 1) :
call-c clib/printf("%d : %ld\n", i, objs.value.heap.items[i])
return false

defstruct IntArrayPow2 :
sizes : IntArray
offs : IntArray
items : IntArray

defn accum (xs:Seqable<Int>, init:Int) -> Seq<Int> :
var accum:Int = init
for x in xs seq : (val r = accum, accum = accum + x, r)

defn IntArrayPow2 (sizes:IntArray) -> IntArrayPow2 :
val len = length(sizes)
val offs = to-intarray $ accum(sizes, 0)
val items = IntArray(offs[len - 1] + sizes[len - 1])
IntArrayPow2(sizes, offs, items)

defn get (vv:IntArrayPow2, idx:Int) -> Collection<Int> :
items(vv)[offs(vv)[idx] to (offs(vv)[idx] + sizes(vv)[idx])]
; heap-dominator-tree("sizes.xml")

0 comments on commit f3e6f82

Please sign in to comment.