prosperon/scripts/profile.js

397 lines
9.3 KiB
JavaScript
Raw Normal View History

2024-09-22 23:10:36 -05:00
/*
TYPES OF PROFILING
report - can see specific events that happened. Includes inclusive vs noninclusive times. When used on top of each other, also generates a callstack.
2024-10-02 09:55:32 -05:00
snapshot - See the amount of something every amount of time
memory - can see how much memory is allocated and from where [not implemented yet]
2024-09-22 23:10:36 -05:00
*/
2024-09-26 11:36:09 -05:00
function calc_cpu(fn, times, diff = 0) {
2024-09-25 20:51:20 -05:00
var series = [];
for (var i = 0; i < times; i++) {
var st = profile.now();
2024-09-26 11:36:09 -05:00
fn(i);
2024-09-25 20:51:20 -05:00
series.push(profile.now() - st - diff);
}
2024-09-25 20:51:20 -05:00
return series;
}
2024-09-26 11:36:09 -05:00
function empty_fn() {}
// Measure how long a fn takes to run, ignoring the overhead of a function call
profile.cpu = function profile_cpu(fn, times = 1, q = fn) {
var empty = calc_cpu(empty_fn, 100000);
var mean = Math.mean(empty);
2024-09-26 11:36:09 -05:00
var series = calc_cpu(fn, times, mean);
var elapsed = Math.sum(series);
2024-09-26 11:36:09 -05:00
var avgt = profile.best_t(elapsed / series.length);
2024-07-24 14:17:32 -05:00
var totalt = profile.best_t(elapsed);
2024-09-26 11:36:09 -05:00
say(`profile [${q}]: ${avgt} ± ${profile.best_t(Math.ci(series))} [${totalt} for ${times} loops]`);
2024-08-05 15:26:18 -05:00
say(`result of function is ${fn()}`);
2024-09-26 11:36:09 -05:00
};
profile.ms = function (t) {
return profile.secs(t) * 1000;
};
2024-07-24 14:17:32 -05:00
var callgraph = {};
2024-09-23 18:17:46 -05:00
profile.rawstacks = {};
2024-07-24 14:17:32 -05:00
2024-10-02 09:55:32 -05:00
function add_callgraph_from_stack(err, time)
{
var stack = err.stack.split("\n").slice(1);
var rawstack = stack.join("\n");
profile.rawstacks[rawstack] ??= {
time: 0,
hits: 0,
};
profile.rawstacks[rawstack].hits++;
profile.rawstacks[rawstack].time += time;
stack = stack.map(x => x.slice(7).split(" "));
var fns = stack.map(x => x[0]).filter(x => x);
var lines = stack.map(x => x[1]).filter(x => x);
lines = lines.map(x => x.slice(1, x.length - 1));
add_callgraph(fns[0], lines[0], time, true);
for (var i = 1; i < fns.length; i++) add_callgraph(fns[i], lines[i], time, false);
}
2024-09-26 19:28:54 -05:00
function add_callgraph(fn, line, time, alone) {
2024-07-24 14:17:32 -05:00
var cc = callgraph[line];
if (!cc) {
var cc = {};
callgraph[line] = cc;
cc.time = 0;
cc.hits = 0;
cc.fn = fn;
cc.line = line;
2024-09-26 19:28:54 -05:00
cc.alone = {
time: 0,
2024-09-26 23:12:30 -05:00
hits: 0,
};
2024-07-24 14:17:32 -05:00
}
cc.time += time;
cc.hits++;
2024-09-26 19:28:54 -05:00
if (alone) {
cc.alone.time += time;
cc.alone.hits++;
}
2024-07-24 14:17:32 -05:00
}
2024-09-22 10:45:31 -05:00
var hittar = 500; // number of call instructions before getting a new frame
var hitpct = 0.2; // amount to randomize it
2024-07-24 14:17:32 -05:00
var start_gather = profile.now();
2024-09-23 18:17:46 -05:00
profile.cpu_start = undefined;
2024-09-26 11:36:09 -05:00
profile.clear_cpu = function () {
2024-09-23 18:17:46 -05:00
callgraph = {};
2024-09-26 19:28:54 -05:00
profile.cpu_instr = undefined;
2024-10-02 09:55:32 -05:00
profile.gather_stop();
profile.cpu_start = undefined;
2024-09-26 11:36:09 -05:00
};
2024-08-04 15:20:11 -05:00
2024-10-02 09:55:32 -05:00
function cpu_record_frame()
{
}
// These values are set to get the most amount of frames without causing a stack overflow
var hittar = 450;
var hitpct = 0.2;
profile.start_cpu_gather_fn = function()
{
2024-09-23 18:17:46 -05:00
if (profile.cpu_start) return;
2024-10-02 09:55:32 -05:00
profile.clear_cpu();
2024-09-23 18:17:46 -05:00
profile.cpu_start = profile.now();
var st = profile.cpu_start;
2024-09-26 11:36:09 -05:00
2024-10-02 09:55:32 -05:00
profile.gather(Math.variate(hittar,hitpct), function() {
2024-09-26 11:36:09 -05:00
var time = profile.now() - st;
var err = new Error();
2024-10-02 09:55:32 -05:00
add_callgraph_from_stack(err, time);
st = profile.now();
profile.gather_rate(Math.variate(hittar,hitpct));
});
}
2024-09-26 11:36:09 -05:00
2024-10-02 09:55:32 -05:00
profile.start_cpu_gather = function (gathertime = 5) {
if (profile.cpu_start) return;
profile.clear_cpu();
// gather cpu frames for 'gathertime' seconds
profile.cpu_start = profile.now();
var st = profile.cpu_start;
2024-09-26 11:36:09 -05:00
2024-10-02 09:55:32 -05:00
profile.gather(hittar, function () {
var time = profile.now() - st;
2024-09-26 11:36:09 -05:00
2024-10-02 09:55:32 -05:00
var err = new Error();
add_callgraph_from_stack(err, time);
st = profile.now();
2024-10-02 09:55:32 -05:00
if (profile.secs(st - profile.cpu_start) < gathertime)
profile.gather_rate(Math.variate(hittar, hitpct));
else
profile.stop_cpu_measure();
});
2024-09-26 11:36:09 -05:00
};
2024-10-02 09:55:32 -05:00
profile.stop_cpu_measure = function()
{
if (!profile.cpu_start) return;
profile.gather_stop();
var gathertime = profile.now()-profile.cpu_start;
console.info(`gathered for ${profile.best_t(gathertime)}`);
profile.cpu_start = undefined;
var e = Object.values(callgraph);
e = e.filter(x => x.line);
for (var x of e) {
var ffs = x.line.split(":");
x.timeper = x.time / x.hits;
x.pct = x.time/gathertime * 100;
x.alone.timeper = x.alone.time / x.alone.hits;
x.alone.pct = x.alone.time / gathertime * 100;
x.fncall = get_line(ffs[0], ffs[1]);
x.log = x.line + " " + x.fn + " " + x.fncall;
x.incl = {
time: x.time,
timeper: x.timeper,
hits: x.hits,
pct: x.pct,
};
}
profile.cpu_instr = e;
}
2024-07-24 14:17:32 -05:00
var filecache = {};
2024-09-26 11:36:09 -05:00
function get_line(file, line) {
2024-07-24 14:17:32 -05:00
var text = filecache[file];
if (!text) {
var f = io.slurp(file);
if (!f) {
filecache[file] = "undefined";
return filecache[file];
}
2024-09-26 11:36:09 -05:00
filecache[file] = io.slurp(file).split("\n");
2024-07-24 14:17:32 -05:00
text = filecache[file];
}
2024-09-25 20:51:20 -05:00
2024-09-26 11:36:09 -05:00
if (typeof text === "string") return text;
text = text[Number(line) - 1];
2024-07-24 14:17:32 -05:00
if (!text) return "NULL";
return text.trim();
}
2024-09-26 11:36:09 -05:00
profile.stop_cpu_instr = function () {
return;
};
2024-07-24 14:17:32 -05:00
2024-09-22 10:45:31 -05:00
/*
2024-09-25 20:51:20 -05:00
Frame averages are an instrumented profiling technique. Place frame() calls
in your code to get a call graph for things you are interested in.
2024-09-22 10:45:31 -05:00
*/
2024-08-04 15:20:11 -05:00
var frame_avg = false;
2024-09-26 11:36:09 -05:00
profile.start_frame_avg = function () {
2024-08-04 15:20:11 -05:00
if (frame_avg) return;
profile_frames = {};
profile_frame_ts = [];
profile_cframe = profile_frames;
pframe = 0;
frame_avg = true;
2024-09-26 11:36:09 -05:00
};
2024-08-04 15:20:11 -05:00
2024-09-26 11:36:09 -05:00
profile.stop_frame_avg = function () {
2024-08-04 15:20:11 -05:00
frame_avg = false;
2024-09-26 11:36:09 -05:00
};
2024-08-04 15:20:11 -05:00
2024-09-26 11:36:09 -05:00
profile.toggle_frame_avg = function () {
2024-08-04 15:20:11 -05:00
if (frame_avg) profile.stop_frame_avg();
else profile.start_frame_avg();
2024-09-26 11:36:09 -05:00
};
2024-08-04 15:20:11 -05:00
2024-09-04 14:34:45 -05:00
var profile_framer = {
series: [],
avg: {},
frame: 72000,
};
var profile_cframe = undefined;
var pframe = 0;
2024-09-04 14:34:45 -05:00
var profile_stack = [];
2024-09-26 11:36:09 -05:00
profile.frame = function profile_frame(title) {
2024-09-26 19:28:54 -05:00
return;
2024-09-23 18:17:46 -05:00
if (profile.cpu_start) return;
2024-08-04 15:20:11 -05:00
if (!frame_avg) return;
2024-09-26 11:36:09 -05:00
2024-09-04 14:34:45 -05:00
if (!profile_cframe) {
profile_cframe = {};
profile_framer.series.push({
2024-09-26 11:36:09 -05:00
time: profile.now(),
data: profile_cframe,
2024-09-04 14:34:45 -05:00
});
2024-09-26 11:36:09 -05:00
} else profile_stack.push(profile_cframe);
2024-07-24 14:17:32 -05:00
profile_cframe[title] ??= {};
profile_cframe = profile_cframe[title];
2024-09-04 14:34:45 -05:00
profile_cframe.time = profile.now();
2024-09-26 11:36:09 -05:00
};
2024-07-24 14:17:32 -05:00
2024-09-26 11:36:09 -05:00
profile.endframe = function profile_endframe() {
2024-09-26 19:28:54 -05:00
return;
2024-08-04 15:20:11 -05:00
if (!frame_avg) return;
2024-09-04 14:34:45 -05:00
profile_cframe.time = profile.now() - profile_cframe.time;
2024-07-24 14:17:32 -05:00
profile_cframe = profile_frame_ts.pop();
2024-09-26 11:36:09 -05:00
};
2024-07-24 14:17:32 -05:00
2024-09-24 14:12:59 -05:00
profile.data = {};
2024-09-25 12:46:59 -05:00
profile.curframe = 0;
2024-09-26 19:28:54 -05:00
profile.snapshot = {};
2024-09-30 04:36:53 -05:00
2024-09-29 06:10:42 -05:00
var classes = ["gameobject", "transform", "dsp_node", "texture", "font", "warp_gravity", "warp_damp", "sg_buffer", "datastream", "cpShape", "cpConstraint", "timer", "skin"];
var get_snapshot = function()
{
var snap = profile.snapshot;
2024-10-02 09:55:32 -05:00
for (var monitor of monitors) {
var stat = monitor.fn();
monitor.hook?.(stat);
snap[monitor.path] = stat;
}
snap.actors = actor.__stats();
2024-09-29 06:10:42 -05:00
snap.memory.textures = game.texture.total_size();
snap.memory.texture_vram = game.texture.total_vram();
snap.particles = stat_emitters();
2024-09-30 11:26:56 -05:00
2024-09-29 06:10:42 -05:00
snap.obj ??= {};
for (var i of classes) {
var proto = globalThis[`${i}_proto`];
if (!proto) continue;
snap.obj[i] = proto._count();
snap.obj[i + "_mem"] = proto._count() * proto.memsize();
2024-09-29 06:10:42 -05:00
}
}
2024-09-26 19:28:54 -05:00
2024-10-02 09:55:32 -05:00
var monitors = [];
profile.add_custom_monitor = function(path, fn, hook)
{
monitors.push({
path:path,
fn:fn,
hook:hook
});
}
profile.add_custom_monitor('rusage', os.rusage, stat => stat.ru_maxrss *= 1024);
profile.add_custom_monitor('mallinfo', os.mallinfo);
profile.add_custom_monitor('memory', os.mem);
2024-09-30 04:36:53 -05:00
var fps = [];
var frame_lead = 1;
var fps_t = 0;
2024-09-26 23:12:30 -05:00
profile.report_frame = function (t) {
2024-09-26 19:28:54 -05:00
fps.push(t);
2024-09-30 04:36:53 -05:00
if (profile.secs(profile.now() - fps_t) > frame_lead) {
2024-09-26 19:28:54 -05:00
profile.snapshot.fps = Math.mean(fps);
2024-09-29 06:10:42 -05:00
fps.length = 0;
2024-09-30 04:36:53 -05:00
fps_t = profile.now();
2024-09-29 06:10:42 -05:00
get_snapshot();
2024-09-26 19:28:54 -05:00
}
2024-10-02 09:55:32 -05:00
}
profile.reports = {};
profile.report = function(path)
{
profile.reports[path] ??= {
report: path,
time: 0,
hits: 0,
avg: 0
};
if (profile.reports[path].st) return;
profile.reports[path].st = profile.now();
}
profile.endreport = function(path)
{
var rep = profile.reports[path];
if (!rep || !rep.st) return;
rep.hits++;
rep.time += profile.now()-rep.st;
delete rep.st;
rep.avg = rep.time/rep.hits;
profile.report_cache = Object.values(profile.reports);
}
2024-09-26 19:28:54 -05:00
2024-09-26 11:36:09 -05:00
function prof_add_stats(obj, stat) {
2024-09-25 12:46:59 -05:00
for (var i in stat) {
obj[i] ??= [];
2024-09-25 20:51:20 -05:00
if (obj[i].last() !== stat[i]) obj[i][profile.curframe] = stat[i];
2024-09-25 12:46:59 -05:00
}
}
2024-09-24 14:12:59 -05:00
2024-09-26 11:36:09 -05:00
profile.pushdata = function (arr, val) {
if (arr.last() !== val) arr[profile.curframe] = val;
};
2024-09-24 17:07:32 -05:00
2024-09-26 19:28:54 -05:00
profile.capturing = false;
2024-09-26 11:36:09 -05:00
profile.capture_data = function () {
2024-09-26 19:28:54 -05:00
if (!profile.capturing && profile.data.memory.malloc_size) return;
2024-09-25 12:46:59 -05:00
prof_add_stats(profile.data.memory, os.mem());
prof_add_stats(profile.data.gfx, imgui.framestats());
prof_add_stats(profile.data.actors, actor.__stats());
profile.curframe++;
2024-09-26 11:36:09 -05:00
};
2024-09-24 14:12:59 -05:00
2024-09-26 11:36:09 -05:00
profile.best_mem = function (bytes) {
var sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes == 0) return "0 Bytes";
2024-08-05 15:26:18 -05:00
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
2024-09-26 11:36:09 -05:00
return (bytes / Math.pow(1024, i)).toPrecision(3) + " " + sizes[i];
};
2024-08-05 15:26:18 -05:00
2024-09-26 11:36:09 -05:00
profile.cleardata = function () {
2024-09-25 15:27:26 -05:00
profile.data.gpu = {};
profile.data.physics = {};
profile.data.script = {};
profile.data.memory = {};
profile.data.gfx = {};
profile.data.actors = {};
profile.data.cpu = {
scripts: [],
render: [],
physics: [],
};
2024-09-26 11:36:09 -05:00
};
2024-09-25 15:27:26 -05:00
profile.cleardata();
2024-09-04 13:23:20 -05:00
profile.last_mem = undefined;
profile.mems = [];
profile.gcs = [];
2024-09-26 11:36:09 -05:00
profile.print_gc = function () {
2024-08-06 14:23:21 -05:00
var gc = os.check_gc();
if (!gc) return;
2024-09-24 17:07:32 -05:00
profile.data.gc ??= [];
2024-09-25 12:46:59 -05:00
profile.data.gc[profile.curframe] = gc;
2024-09-26 11:36:09 -05:00
};
2024-08-06 14:23:21 -05:00
2024-09-26 11:36:09 -05:00
return { profile };