make profiling togglable

This commit is contained in:
John Alanbrook 2024-08-04 15:20:11 -05:00
parent 570c12e3db
commit 33d4ebf14a
10 changed files with 111 additions and 45 deletions

View file

@ -66,9 +66,10 @@ actor.delay = function(fn, seconds) {
stop.pct = function() { return 1-(stop.remain / stop.seconds); }; stop.pct = function() { return 1-(stop.remain / stop.seconds); };
function update(dt) { function update(dt) {
profile.frame("timer");
stop.remain -= dt; stop.remain -= dt;
if (stop.remain <= 0) if (stop.remain <= 0) execute();
execute(); profile.endframe();
} }
var rm = Register.appupdate.register(update); var rm = Register.appupdate.register(update);

View file

@ -3,7 +3,6 @@ globalThis.entityreport = {};
var timer_update = function(dt) var timer_update = function(dt)
{ {
this.fn(); this.fn();
} }
function obj_unique_name(name, obj) { function obj_unique_name(name, obj) {
@ -92,8 +91,10 @@ var entity = {
stop.pct = function() { return 1 - (stop.remain/stop.seconds); }; stop.pct = function() { return 1 - (stop.remain/stop.seconds); };
function update(dt) { function update(dt) {
profile.frame("timer");
stop.remain -= dt; stop.remain -= dt;
if (stop.remain <= 0) execute(); if (stop.remain <= 0) execute();
profile.endframe();
} }
var rm = Register.update.register(update); var rm = Register.update.register(update);

View file

@ -53,8 +53,12 @@ var hittar = 500;
var hitpct = 0.2; var hitpct = 0.2;
var start_gather = profile.now(); var start_gather = profile.now();
function start_prof_gather() var gathering_cpu = false;
profile.start_cpu_gather = function()
{ {
if (gathering_cpu) return;
gathering_cpu = true;
profile.gather(hittar, function() { profile.gather(hittar, function() {
var time = profile.now()-st; var time = profile.now()-st;
@ -77,8 +81,15 @@ function start_prof_gather()
}); });
} }
if (profile.enabled) profile.cpu_frame = function()
start_prof_gather(); {
if (gathering_cpu) return;
profile.gather(Math.random_range(300,600), function() {
console.stack(2);
profile.gather_stop();
});
}
var filecache = {}; var filecache = {};
function get_line(file, line) { function get_line(file, line) {
@ -99,8 +110,10 @@ function get_line(file, line) {
return text.trim(); return text.trim();
} }
profile.print_cpu_instr = function() profile.stop_cpu_instr = function()
{ {
if (!gathering_cpu) return;
say("===CPU INSTRUMENTATION===\n"); say("===CPU INSTRUMENTATION===\n");
var gather_time = profile.now()-start_gather; var gather_time = profile.now()-start_gather;
var e = Object.values(callgraph); var e = Object.values(callgraph);
@ -130,6 +143,33 @@ profile.best_t = function (t) {
profile.report = function (start, msg = "[undefined report]") { console.info(`${msg} in ${profile.best_t(profile.now() - start)}`); }; profile.report = function (start, msg = "[undefined report]") { console.info(`${msg} in ${profile.best_t(profile.now() - start)}`); };
var frame_avg = false;
profile.start_frame_avg = function()
{
if (frame_avg) return;
say("===STARTING FRAME AVERAGE MEASUREMENTS===");
profile_frames = {};
profile_frame_ts = [];
profile_cframe = profile_frames;
pframe = 0;
frame_avg = true;
}
profile.stop_frame_avg = function()
{
if (!frame_avg) return;
frame_avg = false;
profile.print_frame_avg();
}
profile.toggle_frame_avg = function()
{
if (frame_avg) profile.stop_frame_avg();
else profile.start_frame_avg();
}
var profile_frames = {}; var profile_frames = {};
var profile_frame_ts = []; var profile_frame_ts = [];
var profile_cframe = profile_frames; var profile_cframe = profile_frames;
@ -137,6 +177,8 @@ var pframe = 0;
profile.frame = function profile_frame(title) profile.frame = function profile_frame(title)
{ {
if (!frame_avg) return;
profile_frame_ts.push(profile_cframe); profile_frame_ts.push(profile_cframe);
profile_cframe[title] ??= {}; profile_cframe[title] ??= {};
profile_cframe = profile_cframe[title]; profile_cframe = profile_cframe[title];
@ -147,6 +189,8 @@ profile.frame = function profile_frame(title)
profile.endframe = function profile_endframe() profile.endframe = function profile_endframe()
{ {
if (!frame_avg) return;
if (profile_cframe === profile_frames) return; if (profile_cframe === profile_frames) return;
profile_cframe._times[pframe] = profile.now() - profile_cframe._times[pframe]; profile_cframe._times[pframe] = profile.now() - profile_cframe._times[pframe];
profile_cframe = profile_frame_ts.pop(); profile_cframe = profile_frame_ts.pop();
@ -165,9 +209,13 @@ var print_frame = function(frame, indent, title)
profile.print_frame_avg = function() profile.print_frame_avg = function()
{ {
say("===FRAME AVERAGES===\n");
var indent = ""; var indent = "";
for (var i in profile_frames) for (var i in profile_frames)
print_frame(profile_frames[i], "", 'frame'); print_frame(profile_frames[i], "", 'frame');
say("\n");
} }
var report_cache = {}; var report_cache = {};
@ -193,7 +241,7 @@ profile.print_cache_report = function()
for (var i in report_cache) for (var i in report_cache)
str += printreport(report_cache[i], i) + "\n"; str += printreport(report_cache[i], i) + "\n";
return str; say(str);
} }
function addreport(group, line, start) { function addreport(group, line, start) {

View file

@ -1,3 +1,5 @@
globalThis.gamestate = {};
global.check_registers = function (obj) { global.check_registers = function (obj) {
for (var reg in Register.registries) { for (var reg in Register.registries) {
if (typeof obj[reg] === 'function') { if (typeof obj[reg] === 'function') {
@ -247,15 +249,9 @@ prosperon.touchrelease = function (touches) {};
prosperon.touchmove = function (touches) {}; prosperon.touchmove = function (touches) {};
prosperon.clipboardpaste = function (str) {}; prosperon.clipboardpaste = function (str) {};
prosperon.quit = function () { prosperon.quit = function () {
if (!profile.enabled) return; profile.print_cache_report();
profile.stop_frame_avg()
say(profile.print_cache_report()); profile.stop_cpu_instr();
say("===FRAME AVERAGES===\n");
say(profile.print_frame_avg());
say("\n");
profile.print_cpu_instr();
}; };
window.size = [640, 480]; window.size = [640, 480];
@ -277,33 +273,6 @@ global.mixin("scripts/gui");
global.mixin("scripts/tween"); global.mixin("scripts/tween");
global.mixin("scripts/ai"); global.mixin("scripts/ai");
global.mixin("scripts/particle"); global.mixin("scripts/particle");
var timer = {
update(dt) {
this.remain -= dt;
if (this.remain <= 0) {
this.fn();
this.kill();
}
},
kill() {
this.end();
delete this.fn;
},
delay(fn, secs) {
var t = Object.create(this);
t.time = secs;
t.remain = secs;
t.fn = fn;
t.end = Register.update.register(timer.update.bind(t));
var returnfn = timer.kill.bind(t);
returnfn.remain = secs;
return returnfn;
},
};
global.mixin("scripts/physics"); global.mixin("scripts/physics");
global.mixin("scripts/geometry"); global.mixin("scripts/geometry");

View file

@ -459,6 +459,8 @@ render.init = function() {
poly_ssbo = render.make_textssbo(); poly_ssbo = render.make_textssbo();
sprite_ssbo = render.make_textssbo(); sprite_ssbo = render.make_textssbo();
globalThis.imgui = render.imgui_init();
render.textshader = textshader; render.textshader = textshader;
os.make_circle2d().draw = function() { os.make_circle2d().draw = function() {

View file

@ -14,8 +14,10 @@ if (os.sys() === 'macos') {
appy.inputs['S-g'] = os.gc; appy.inputs['S-g'] = os.gc;
} }
appy.inputs.f12 = function() { mum.debug = !mum.debug; } //appy.inputs.f12 = function() { mum.debug = !mum.debug; }
appy.inputs.f12 = function() { profile.cpu_frame(); }
appy.inputs.f11 = window.toggle_fullscreen; appy.inputs.f11 = window.toggle_fullscreen;
appy.inputs.f10 = function() { profile.toggle_frame_avg(); }
appy.inputs['M-f4'] = prosperon.quit; appy.inputs['M-f4'] = prosperon.quit;
player[0].control(appy); player[0].control(appy);

View file

@ -107,6 +107,7 @@ var tween = function(from, to, time, fn)
{ {
var start = profile.secs(profile.now()); var start = profile.secs(profile.now());
var update = function(dt) { var update = function(dt) {
profile.frame("tween");
var elapsed = profile.secs(profile.now()) - start; var elapsed = profile.secs(profile.now()) - start;
fn(from.lerp(to,elapsed/time)); fn(from.lerp(to,elapsed/time));
if (elapsed >= time) { if (elapsed >= time) {
@ -114,6 +115,7 @@ var tween = function(from, to, time, fn)
if (stop.then) stop.then(); if (stop.then) stop.then();
stop(); stop();
} }
profile.endframe();
}; };
var stop = Register.update.register(update); var stop = Register.update.register(update);
return stop; return stop;

View file

@ -34,6 +34,7 @@
#include "sokol_glue.h" #include "sokol_glue.h"
#include <chipmunk/chipmunk_unsafe.h> #include <chipmunk/chipmunk_unsafe.h>
#include "gui.h" #include "gui.h"
#include "timer.h"
#if (defined(_WIN32) || defined(__WIN32__)) #if (defined(_WIN32) || defined(__WIN32__))
#include <direct.h> #include <direct.h>
@ -105,6 +106,7 @@ QJSCLASS(sg_buffer)
QJSCLASS(datastream) QJSCLASS(datastream)
QJSCLASS(cpShape) QJSCLASS(cpShape)
QJSCLASS(cpConstraint) QJSCLASS(cpConstraint)
QJSCLASS(timer)
static JSValue js_circle2d; static JSValue js_circle2d;
static JSValue js_poly2d; static JSValue js_poly2d;
@ -2397,6 +2399,9 @@ static const JSCFunctionListEntry js_texture_funcs[] = {
MIST_FUNC_DEF(texture, blit, 5) MIST_FUNC_DEF(texture, blit, 5)
}; };
static const JSCFunctionListEntry js_timer_funcs[] = {
};
JSC_GETSET(font, linegap, number) JSC_GETSET(font, linegap, number)
JSC_GET(font, height, number) JSC_GET(font, height, number)
@ -2961,6 +2966,7 @@ void ffi_load() {
QJSCLASSPREP_FUNCS(window); QJSCLASSPREP_FUNCS(window);
QJSCLASSPREP_FUNCS(datastream); QJSCLASSPREP_FUNCS(datastream);
QJSCLASSPREP_FUNCS(cpShape); QJSCLASSPREP_FUNCS(cpShape);
QJSCLASSPREP_FUNCS(timer);
QJSGLOBALCLASS(nota); QJSGLOBALCLASS(nota);
QJSGLOBALCLASS(input); QJSGLOBALCLASS(input);

22
source/engine/timer.c Normal file
View file

@ -0,0 +1,22 @@
#include "timer.h"
#include "stb_ds.h"
timer *timers;
timer *timer_make()
{
return NULL;
}
void timer_free(timer *t)
{
}
void timer_update(double dt)
{
for (int i = 0; i < arrlen(timers); i++) {
timers[i].remain -= dt;
}
}

13
source/engine/timer.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef TIMER_H
typedef struct timer {
double start;
double remain;
} timer;
timer *timer_make();
void timer_free(timer *t);
void timer_update(double dt);
#endif