Skip to content

Commit

Permalink
track stack and heap memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
caipng committed Apr 22, 2024
1 parent ad60a86 commit bdec49a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/interpreter/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export type HeapEntry = { size: number };
export class Heap {
private readonly allocations: Record<number, HeapEntry>;

private _memUsage: number;
private heapStart: number;
private heapEnd: number;

constructor(heapStart: number, heapEnd: number) {
this.allocations = {};
this.heapStart = heapStart;
this.heapEnd = heapEnd;
this._memUsage = 0;
}

public allocate(size: number): number {
Expand All @@ -33,6 +35,7 @@ export class Heap {
if (!ok) return 0;
curr = roundUpM(curr, MAX_ALIGN);
this.allocations[curr] = { size };
this._memUsage += size;
return curr;
}

Expand All @@ -41,9 +44,14 @@ export class Heap {
throw new Error(
"undefined behaviour: free on address not returned by malloc",
);
this._memUsage -= this.allocations[addr].size;
delete this.allocations[addr];
}

public get memUsage() {
return this._memUsage;
}

public getBlockSize(addr: number): number {
if (!(addr in this.allocations)) return 0;
return this.allocations[addr].size;
Expand Down
10 changes: 7 additions & 3 deletions src/interpreter/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ export class RuntimeView {
stash: StashItem[];
textAndData: TextAndData;
stack: StackFrame[];
stackMemUsage: number;
blockScopes: Record<Identifier, number>[];
fileScope: Record<Identifier, number>;
memory: Memory;
functions: [string, TypedCompoundStatement, FunctionType][];
functionCalls: number[];
rbpArr: number[];
heap: Record<number, HeapEntry>;
heapMemUsage: number;
effectiveTypeTable: Record<number, EffectiveTypeTableEntry>;
initTable: InitializedTable;

Expand All @@ -52,6 +54,7 @@ export class RuntimeView {
this.stash = rt.stash.getArr();
this.textAndData = cloneDeep(rt.textAndData);
this.stack = rt.stack.getArr();
this.stackMemUsage = rt.stack.memUsage;
this.blockScopes = rt.symbolTable.getBlockScopes();
this.fileScope = rt.symbolTable.getFileScope();
this.memory = cloneDeep(rt.memory);
Expand All @@ -60,8 +63,9 @@ export class RuntimeView {
this.rbpArr = rt.stack.getRbpArr();
this.rbpArr.push(rt.stack.rbp);
this.heap = rt.heap.getAllocations();
this.heapMemUsage = rt.heap.memUsage;
this.effectiveTypeTable = rt.effectiveTypeTable.getTable();
this.initTable = cloneDeep(rt.initTable)
this.initTable = cloneDeep(rt.initTable);
}
}

Expand Down Expand Up @@ -98,7 +102,7 @@ export class Runtime {
config.memory.heap.baseAddress,
config.memory.heap.baseAddress + config.memory.heap.size,
);
this.initTable = new InitializedTable()
this.initTable = new InitializedTable();

this.functions = [];
this.builtinFunctions = [];
Expand All @@ -109,7 +113,7 @@ export class Runtime {
for (const [identifier, f] of Object.entries(BUILTIN_FUNCTIONS)) {
const address = this.allocateText(shortInt());
const idx = this.addBuiltinFunction(f);
this.effectiveTypeTable.add(address, shortInt())
this.effectiveTypeTable.add(address, shortInt());
this.memory.setScalar(
address,
BigInt(-idx - 1),
Expand Down
14 changes: 12 additions & 2 deletions src/interpreter/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ export class RuntimeStack extends Stack<StackFrame> {
private _rsp: number;
private _rbpStack: Stack<number>;
private _rspStack: Stack<number>;
private _memUsage: number;

constructor(stackBaseAddress: number) {
super();
this._rbp = stackBaseAddress;
this._rsp = stackBaseAddress;
this._rbpStack = new Stack();
this._rspStack = new Stack();
this._memUsage = 0;
}

getRbpArr(): number[] {
Expand All @@ -42,12 +44,16 @@ export class RuntimeStack extends Stack<StackFrame> {
const [size, alignment] = RuntimeStack.getStackFrameSizeAndAlignment(x);
this._rbp = roundUpM(this._rsp + 1, alignment);
this._rsp = this._rbp + size - 1;
this._memUsage += size;
}

override pop(): StackFrame {
this._rsp = this._rspStack.pop();
this._rbp = this._rbpStack.pop();
return super.pop();
const sf = super.pop();
const size = RuntimeStack.getStackFrameSizeAndAlignment(sf)[0];
this._memUsage -= size;
return sf;
}

get rbp(): number {
Expand All @@ -58,6 +64,10 @@ export class RuntimeStack extends Stack<StackFrame> {
return this._rsp;
}

get memUsage(): number {
return this._memUsage;
}

static calculateStackFrame(
params: ParameterTypeAndIdentifier[],
body: TypedCompoundStatement,
Expand Down Expand Up @@ -86,7 +96,7 @@ export class RuntimeStack extends Stack<StackFrame> {
identifierPrefix.join("::") +
(identifierPrefix.length ? "::" : "") +
j.identifier;
j.qualifiedIdentifier = qid;
j.qualifiedIdentifier = qid;
ptr = roundUpM(ptr, typeInfo.alignment);
res[qid] = { address: ptr, typeInfo };
ptr += typeInfo.size;
Expand Down

0 comments on commit bdec49a

Please sign in to comment.