Skip to content

Commit

Permalink
bootstrap changes to core
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbackrack committed Jan 6, 2025
1 parent bade4c0 commit 5a1c652
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 99 deletions.
2 changes: 1 addition & 1 deletion ci/build-stanza-version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# like 1.23.45
#
# Use version 0.17.56 to compile 0.18.0
0.18.96
0.18.97
232 changes: 134 additions & 98 deletions core/core.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -218,32 +218,62 @@ protected lostanza deftype HeapStatistic :
;are used only in compiled mode.
;Permanent state changes in-between each code load.
;Variable state changes in-between each boundary change.
protected lostanza deftype VMState :
;Compiled and Interpreted Mode
global-offsets: ptr<long> ;(Permanent State)
global-mem: ptr<byte> ;(Permanent State)
var sig-handler: long ;(Permanent State)
var current-coroutine-ptr: ptr<long> ;[TODO] Change to long to represent reference.
var stepping-coroutine-ptr: ptr<long> ;[TODO] Change to long to represent reference.
const-table: ptr<long> ;(Permanent State)
const-mem: ptr<byte> ;(Permanent State)
data-offsets: ptr<int> ;(Permanent State)
data-mem: ptr<byte> ;(Permanent State)
code-offsets: ptr<int> ;(Permanent State)
registers: ptr<long> ;(Permanent State)
system-registers: ptr<long> ;(Permanent State)
var heap: Heap ;(Variable State)
safepoint-table: ptr<?> ;(Variable State)
debug-table: ptr<?> ;(Variable State)
local-var-table: ptr<?> ;(Variable State)
heap-statistics: ptr<HeapStatistic> ;(Variable State)
;Compiled Mode Tables
class-table: ptr<ClassDescriptor>
global-root-table: ptr<GlobalRoots>
stackmap-table: ptr<ptr<StackMap>>
stack-trace-table: ptr<StackTraceTable>
extern-table: ptr<ExternTable>
extern-defn-table: ptr<ExternDefnTable>
#if-defined(BOOTSTRAP) :

protected lostanza deftype VMState :
;Compiled and Interpreted Mode
global-offsets: ptr<long> ;(Permanent State)
global-mem: ptr<byte> ;(Permanent State)
var sig-handler: long ;(Permanent State)
var current-coroutine-ptr: ptr<long> ;[TODO] Change to long to represent reference.
var stepping-coroutine-ptr: ptr<long> ;[TODO] Change to long to represent reference.
const-table: ptr<long> ;(Permanent State)
const-mem: ptr<byte> ;(Permanent State)
data-offsets: ptr<int> ;(Permanent State)
data-mem: ptr<byte> ;(Permanent State)
code-offsets: ptr<int> ;(Permanent State)
registers: ptr<long> ;(Permanent State)
system-registers: ptr<long> ;(Permanent State)
var heap: Heap ;(Variable State)
safepoint-table: ptr<?> ;(Variable State)
debug-table: ptr<?> ;(Variable State)
local-var-table: ptr<?> ;(Variable State)
;Compiled Mode Tables
class-table: ptr<ClassDescriptor>
global-root-table: ptr<GlobalRoots>
stackmap-table: ptr<ptr<StackMap>>
stack-trace-table: ptr<StackTraceTable>
extern-table: ptr<ExternTable>
extern-defn-table: ptr<ExternDefnTable>

#else:

protected lostanza deftype VMState :
;Compiled and Interpreted Mode
global-offsets: ptr<long> ;(Permanent State)
global-mem: ptr<byte> ;(Permanent State)
var sig-handler: long ;(Permanent State)
var current-coroutine-ptr: ptr<long> ;[TODO] Change to long to represent reference.
var stepping-coroutine-ptr: ptr<long> ;[TODO] Change to long to represent reference.
const-table: ptr<long> ;(Permanent State)
const-mem: ptr<byte> ;(Permanent State)
data-offsets: ptr<int> ;(Permanent State)
data-mem: ptr<byte> ;(Permanent State)
code-offsets: ptr<int> ;(Permanent State)
registers: ptr<long> ;(Permanent State)
system-registers: ptr<long> ;(Permanent State)
var heap: Heap ;(Variable State)
safepoint-table: ptr<?> ;(Variable State)
debug-table: ptr<?> ;(Variable State)
local-var-table: ptr<?> ;(Variable State)
heap-statistics: ptr<HeapStatistic> ;(Variable State)
;Compiled Mode Tables
class-table: ptr<ClassDescriptor>
global-root-table: ptr<GlobalRoots>
stackmap-table: ptr<ptr<StackMap>>
stack-trace-table: ptr<StackTraceTable>
extern-table: ptr<ExternTable>
extern-defn-table: ptr<ExternDefnTable>

lostanza deftype ExternTable :
length: long
Expand Down Expand Up @@ -2785,80 +2815,86 @@ public lostanza defn min (x:long, y:long) -> long :
;===================== Heap Analyzer ========================
;============================================================

public defstruct HeapStat :
tag : Int ; type
name : String ; name of type
num-uses : Long ; num references in heap
num-bytes : Long ; num bytes occupied in heap
with:
printer => true
#if-defined(BOOTSTRAP) :

; clear out statistics for each concrete class
lostanza defn clear-heap-statistics (vms:ptr<VMState>) -> int :
labels:
begin: goto loop(0)
loop (i:int) :
val stat = vms.heap-statistics[i]
if stat.num-bytes < 0L : ; sentinel
return stat.num-uses as int
else :
vms.heap-statistics[i].num-bytes = 0
vms.heap-statistics[i].num-uses = 0
goto loop(i + 1)
public defn analyze-heap () -> Long : 0L

; run gc, collect heap stats while walking each object in nursery and heap
lostanza defn do-analyze-heap (stats:ref<Vector<HeapStat>>) -> ref<Long> :
val vms:ptr<VMState> = call-prim flush-vm()
run-garbage-collector()
val num-classes = clear-heap-statistics(vms)
val hsize = do-analyze-heap(vms.heap.start, vms.heap.old-objects-end, vms)
val nursery = nursery-start(addr(vms.heap))
val nsize = do-analyze-heap(nursery, vms.heap.top, vms)
for (var i:int = 0, i < num-classes, i = i + 1) :
val heap-stat = vms.heap-statistics[i]
val num-uses = heap-stat.num-uses
if num-uses > 0L :
add(stats, HeapStat(new Int{i as int}, String(class-name(i)), new Long{num-uses}, new Long{heap-stat.num-bytes}))
return new Long{ hsize + nsize }

; collect heap stats while walking each object in consecutive range of memory
lostanza defn do-analyze-heap (pstart:ptr<long>, pend:ptr<long>, vms:ptr<VMState>) -> long :
var p:ptr<long> = pstart
while p < pend :
val tag = [p] as int
val class = vms.class-table[tag].record
var size:long = 0L
if class.item-size == 0 :
size = object-size-on-heap(class.size)
else :
val class = class as ptr<ArrayRecord>
val array = p as ptr<ObjectLayout>
val len = array.slots[0]
val base-size = class.base-size
val item-size = class.item-size
val my-size = base-size + item-size * len
size = object-size-on-heap(my-size)
p = p + size
vms.heap-statistics[tag].num-uses = vms.heap-statistics[tag].num-uses + 1L
vms.heap-statistics[tag].num-bytes = vms.heap-statistics[tag].num-bytes + size
return (pend as long) - (pstart as long)

; public interface to heap analyzer collecting and printing out stats
public defn analyze-heap () -> Long :
val stats = Vector<HeapStat>()
val size = do-analyze-heap(stats)
val res = reverse(to-list(lazy-qsort(num-bytes, stats)))
println("Heap size %_" % [size])
var max-bytes-size = reduce(max, length("Size"), seq({ length(to-string(num-bytes(_))) }, res))
var max-perc-size = 3
var max-uses-size = reduce(max, length("Uses"), seq({ length(to-string(num-uses(_))) }, res))
defn pad (s:String, n:Int) -> String : append-all(cat(repeatedly({ " " }, (n - length(s))), [s]))
println(" %_ %_ %_: %_" % [pad("Size", max-bytes-size), "Perc", pad("Use", max-uses-size), "Type"])
for hc in res do :
val p = to-int(to-double(num-bytes(hc)) * 100.0 / to-double(size))
println(" %_ %_%% %_: %_" % [
pad(to-string(num-bytes(hc)), max-bytes-size), pad(to-string(p), max-perc-size), pad(to-string(num-uses(hc)), max-uses-size), name(hc)])
size
#else :

public defstruct HeapStat :
tag : Int ; type
name : String ; name of type
num-uses : Long ; num references in heap
num-bytes : Long ; num bytes occupied in heap
with:
printer => true

; clear out statistics for each concrete class
lostanza defn clear-heap-statistics (vms:ptr<VMState>) -> int :
labels:
begin: goto loop(0)
loop (i:int) :
val stat = vms.heap-statistics[i]
if stat.num-bytes < 0L : ; sentinel
return stat.num-uses as int
else :
vms.heap-statistics[i].num-bytes = 0
vms.heap-statistics[i].num-uses = 0
goto loop(i + 1)

; run gc, collect heap stats while walking each object in nursery and heap
lostanza defn do-analyze-heap (stats:ref<Vector<HeapStat>>) -> ref<Long> :
val vms:ptr<VMState> = call-prim flush-vm()
run-garbage-collector()
val num-classes = clear-heap-statistics(vms)
val hsize = do-analyze-heap(vms.heap.start, vms.heap.old-objects-end, vms)
val nursery = nursery-start(addr(vms.heap))
val nsize = do-analyze-heap(nursery, vms.heap.top, vms)
for (var i:int = 0, i < num-classes, i = i + 1) :
val heap-stat = vms.heap-statistics[i]
val num-uses = heap-stat.num-uses
if num-uses > 0L :
add(stats, HeapStat(new Int{i as int}, String(class-name(i)), new Long{num-uses}, new Long{heap-stat.num-bytes}))
return new Long{ hsize + nsize }

; collect heap stats while walking each object in consecutive range of memory
lostanza defn do-analyze-heap (pstart:ptr<long>, pend:ptr<long>, vms:ptr<VMState>) -> long :
var p:ptr<long> = pstart
while p < pend :
val tag = [p] as int
val class = vms.class-table[tag].record
var size:long = 0L
if class.item-size == 0 :
size = object-size-on-heap(class.size)
else :
val class = class as ptr<ArrayRecord>
val array = p as ptr<ObjectLayout>
val len = array.slots[0]
val base-size = class.base-size
val item-size = class.item-size
val my-size = base-size + item-size * len
size = object-size-on-heap(my-size)
p = p + size
vms.heap-statistics[tag].num-uses = vms.heap-statistics[tag].num-uses + 1L
vms.heap-statistics[tag].num-bytes = vms.heap-statistics[tag].num-bytes + size
return (pend as long) - (pstart as long)

; public interface to heap analyzer collecting and printing out stats
public defn analyze-heap () -> Long :
val stats = Vector<HeapStat>()
val size = do-analyze-heap(stats)
val res = reverse(to-list(lazy-qsort(num-bytes, stats)))
println("Heap size %_" % [size])
var max-bytes-size = reduce(max, length("Size"), seq({ length(to-string(num-bytes(_))) }, res))
var max-perc-size = 3
var max-uses-size = reduce(max, length("Uses"), seq({ length(to-string(num-uses(_))) }, res))
defn pad (s:String, n:Int) -> String : append-all(cat(repeatedly({ " " }, (n - length(s))), [s]))
println(" %_ %_ %_: %_" % [pad("Size", max-bytes-size), "Perc", pad("Use", max-uses-size), "Type"])
for hc in res do :
val p = to-int(to-double(num-bytes(hc)) * 100.0 / to-double(size))
println(" %_ %_%% %_: %_" % [
pad(to-string(num-bytes(hc)), max-bytes-size), pad(to-string(p), max-perc-size), pad(to-string(num-uses(hc)), max-uses-size), name(hc)])
size

;============================================================
;===================== Debugging ============================
Expand Down

0 comments on commit 5a1c652

Please sign in to comment.