Skip to content

Commit 4aa7162

Browse files
committed
some decorator attempt and stuff
1 parent b4edf82 commit 4aa7162

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/benchmark/Sieve.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
App,
1010
TimeValue,
1111
Origin,
12-
Log
12+
Log,
13+
GraphDebugLogger
1314
} from "../core/internal";
1415

1516
Log.global.level = Log.levels.INFO;
@@ -97,6 +98,7 @@ class Filter extends Reactor {
9798
// console.log("CREATED: " + x._getFullyQualifiedName())
9899
// FIXME: weird hack. Maybe just accept writable ports as well?
99100
const port = (out as unknown as WritablePort<number>).getPort();
101+
console.log("connecting......");
100102
this.connect(port, n.inp);
101103
// FIXME: this updates the dependency graph, but it doesn't redo the topological sort
102104
// For a pipeline like this one, it is not necessary, but in general it is.
@@ -133,5 +135,10 @@ class Sieve extends App {
133135
}
134136
}
135137

138+
136139
const sieve = new Sieve("Sieve");
140+
141+
globalThis.graphDebugLogger = new GraphDebugLogger(sieve);
142+
globalThis.recording = false;
143+
137144
sieve._start();

src/core/graph.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,43 @@
33
* @author Marten Lohstroh <[email protected]>
44
*/
55

6+
import { DebugLogger } from "util";
67
import {Reaction} from "./reaction";
78
import type {Sortable, Variable} from "./types";
8-
import {Log} from "./util";
9+
import {GraphDebugLogger, Log} from "./util";
10+
11+
12+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/no-explicit-any
13+
const debugLoggerDecorator = (target: any, context: ClassMethodDecoratorContext) => {
14+
if (context.kind === "method") {
15+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16+
return function (this: any, ...args: unknown[]): any {
17+
console.log(`${context.name.toString()} is called.`);
18+
console.log(`Tracestack: ${(new Error()).stack}`);
19+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
20+
return target.call(this, ...args);
21+
}
22+
}
23+
}
24+
25+
declare global {
26+
var graphDebugLogger: GraphDebugLogger | undefined;
27+
var recording: boolean;
28+
}
29+
30+
const debugHelper = (): void => {
31+
if (globalThis.recording) { return; }
32+
globalThis.recording = true;
33+
if (globalThis.graphDebugLogger == null) {
34+
return
35+
}
36+
37+
const err = new Error();
38+
console.log(err.stack);
39+
const debuglogger = globalThis.graphDebugLogger;
40+
debuglogger.capture(err);
41+
globalThis.recording = false;
42+
};
943

1044
/**
1145
* A generic precedence graph.
@@ -53,6 +87,7 @@ export class PrecedenceGraph<T> {
5387
* @param node
5488
*/
5589
addNode(node: T): void {
90+
debugHelper();
5691
if (!this.adjacencyMap.has(node)) {
5792
this.adjacencyMap.set(node, new Set());
5893
}
@@ -143,6 +178,8 @@ export class PrecedenceGraph<T> {
143178
* @param downstream The node at which the directed edge ends.
144179
*/
145180
addEdge(upstream: T, downstream: T): void {
181+
debugHelper();
182+
console.log("!");
146183
const deps = this.adjacencyMap.get(downstream);
147184
if (deps == null) {
148185
this.adjacencyMap.set(downstream, new Set([upstream]));

src/core/util.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import ULog from "ulog";
2-
import { readFileSync } from 'fs';
2+
import { TimeValue } from "./time";
3+
import { Component } from "./component";
4+
import { Reactor } from "./reactor";
5+
import fs from 'fs';
36

47
/**
58
* Utilities for the reactor runtime.
@@ -185,4 +188,36 @@ export class Log {
185188
}
186189
}
187190
}
191+
interface runtimeLog {
192+
time: TimeValue,
193+
graphMermaidString: string,
194+
stacktrace: string,
195+
}
196+
197+
export class GraphDebugLogger {
198+
logs: runtimeLog[];
199+
rootReactor: Reactor;
200+
201+
constructor(rootReactor: Reactor) {
202+
this.logs = [];
203+
this.rootReactor = rootReactor;
204+
}
188205

206+
public capture(stacktrace?: Error): void {
207+
const hierarchy = this.rootReactor._getNodeHierarchyLevels();
208+
// _getPrecedenceGraph is private
209+
const graph = this.rootReactor["_getPrecedenceGraph"]();
210+
const str = graph.toMermaidString(undefined, hierarchy);
211+
const time = this.rootReactor["util"].getElapsedLogicalTime();
212+
this.logs.push({
213+
time,
214+
graphMermaidString: str,
215+
stacktrace: stacktrace?.stack ?? "unknown"
216+
});
217+
}
218+
219+
public write(filepath: string): void {
220+
// Blocking
221+
fs.appendFileSync(filepath, JSON.stringify(this.logs), {flag: "a+"});
222+
}
223+
}

0 commit comments

Comments
 (0)