Com a VM rápida, um render 3D normal passa de 500 mil comandos dentro
da fatia de 12 ms, e o limite de passos por quadro abortava o programa
("limite de passos atingido") em parte das vezes, conforme onde o
quadro cortava. O guarda agora é por tempo: um laço que fica 3 s sem
pausa (corpo de Meus Blocos) é parado; laços comuns pausam a cada volta.
vm_push zerava o TinyBlocks_Flow inteiro (dois textos de erro) a cada
"se" executado; agora só os campos. shape_of só para comandos comuns.
vm_bench: diagnóstico (variáveis e pilhas) quando um render não termina.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
7.2 KiB
C
179 lines
7.2 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
/* vm_bench.c - mede a VM do editor rodando um projeto sem janela.
|
|
* Uso: build/vm_bench projeto.json [renders]
|
|
* Clica a bandeira e, a cada render, liga a variavel "mudou" (o Minecraft so
|
|
* redesenha quando a camera mexe) e mede quanto o render leva.
|
|
* BENCH_STEP=n gira a camera n graus por render (padrao 3)
|
|
* BENCH_CAM=x,y,z,p camera fixa (cx, cy, cz, pit)
|
|
* BENCH_PROFILE=1 so renderiza, sem medir (para o callgrind)
|
|
* TINYBLOCKS_NO_JIT=1 desliga o compilador; TINYBLOCKS_VM_MS troca a fatia. */
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <tinyblocks/tinyblocks.h>
|
|
#include <tinyblocks/tb_renderable_block.h>
|
|
#include <tinyblocks/tb_vars.h>
|
|
#include <tinyblocks/tb_image.h>
|
|
#include <tinyblocks/tb_value_class.h>
|
|
#include <tb_editor.h>
|
|
#include <tb_editor_internal.h>
|
|
#include <tb_graphics.h>
|
|
#include <tb_events.h>
|
|
#include <tb_control.h>
|
|
#include <tb_errors.h>
|
|
#include <tb_operators.h>
|
|
#include <tb_variables.h>
|
|
#include <tb_appearance.h>
|
|
#include <tb_pen.h>
|
|
#include <tb_input_lib.h>
|
|
#include <tb_imglib.h>
|
|
#include <tb_number.h>
|
|
#include <tb_string.h>
|
|
#include <tb_vector.h>
|
|
|
|
static TinyBlocks* rt;
|
|
static TinyBlocks_Block *ev, *scene;
|
|
static double t_eval, t_draw;
|
|
|
|
static double now(void) {
|
|
struct timespec t;
|
|
clock_gettime(CLOCK_MONOTONIC, &t);
|
|
return t.tv_sec + t.tv_nsec / 1e9;
|
|
}
|
|
|
|
/* Um quadro do editor: entrada + VM (eval) e desenho */
|
|
static void frame(void) {
|
|
tb_block_set_output(ev, GFX_IN_WIN_W, tb_value_integer(1280));
|
|
tb_block_set_output(ev, GFX_IN_WIN_H, tb_value_integer(720));
|
|
tb_block_set_output(ev, GFX_IN_TEXT, tb_value_text(""));
|
|
tb_block_set_output(ev, GFX_IN_DROP_FILE, tb_value_text(""));
|
|
double a = now();
|
|
tb_block_eval(scene, rt);
|
|
double b = now();
|
|
TinyBlocks_RenderableBlock* rb = NULL;
|
|
scene->type->on_draw(scene, rt, &rb);
|
|
t_eval += b - a;
|
|
t_draw += now() - b;
|
|
tb_renderable_release_all();
|
|
}
|
|
|
|
static double var_num(const char* name) {
|
|
const TinyBlocks_Value* v = tb_vars_get(name);
|
|
return v ? tb_value_as_number(*v, 0) : 0;
|
|
}
|
|
|
|
/* Diagnostico: a VM nao terminou o que devia (variaveis e pilhas) */
|
|
static void diag(Ed* ed) {
|
|
const char* show[] = { "i", "j", "nf", "f", "mudou", "k", "tt", "pyy", "pxx", "y0", "y1", "x0", "x1" };
|
|
for (int q = 0; q < (int)(sizeof(show) / sizeof(show[0])); q++) {
|
|
const TinyBlocks_Value* v = tb_vars_get(show[q]);
|
|
char buf[64] = "-";
|
|
if (v) tb_value_to_text(*v, buf, sizeof(buf));
|
|
fprintf(stderr, " %s=%s", show[q], buf);
|
|
}
|
|
fprintf(stderr, "\nvm_running=%d vm_n=%d\n", ed->vm_running, ed->vm_n);
|
|
for (int t = 0; t < ed->vm_n; t++)
|
|
for (int k = 0; k < ed->vm[t].sp; k++) {
|
|
VmFrame* f = &ed->vm[t].st[k];
|
|
int bi = f->bi;
|
|
const char* ty = bi >= 0 && f->T->ws[bi].type ? f->T->ws[bi].type->type_name : "?";
|
|
fprintf(stderr, " thread %d nivel %d: %s bi=%d %s count=%lld\n", t, k,
|
|
f->kind == VF_FLOW ? "C" : "pilha", bi, ty, (long long)f->flow.count);
|
|
}
|
|
}
|
|
|
|
/* Camera fixa (BENCH_CAM) */
|
|
static void set_cam(int yaw) {
|
|
tb_vars_set("yaw", tb_value_integer(yaw));
|
|
const char* cam = getenv("BENCH_CAM");
|
|
if (!cam) return;
|
|
double c[4] = {0};
|
|
sscanf(cam, "%lf,%lf,%lf,%lf", &c[0], &c[1], &c[2], &c[3]);
|
|
const char* nm[4] = { "cx", "cy", "cz", "pit" };
|
|
for (int q = 0; q < 4; q++) tb_vars_set(nm[q], tb_value_real(c[q]));
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) { fprintf(stderr, "uso: %s projeto.json [renders]\n", argv[0]); return 2; }
|
|
int frames = argc > 2 ? atoi(argv[2]) : 20;
|
|
int step = getenv("BENCH_STEP") ? atoi(getenv("BENCH_STEP")) : 3;
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
setenv("XDG_CONFIG_HOME", "/tmp/tb_bench_cfg", 1);
|
|
setenv("TINYBLOCKS_MCP_PORT", "0", 1);
|
|
tinyblocks_init(&rt, NULL);
|
|
tb_graphics_set_backend(tb_graphics_null_backend());
|
|
tb_library_register(rt, tb_events_get_library());
|
|
tb_library_register(rt, tb_control_get_library());
|
|
tb_library_register(rt, tb_errors_get_library());
|
|
tb_library_register(rt, tb_operators_get_library());
|
|
tb_library_register(rt, tb_variables_get_library());
|
|
tb_library_register(rt, tb_appearance_get_library());
|
|
tb_library_register(rt, tb_pen_get_library());
|
|
tb_library_register(rt, tb_input_get_library());
|
|
tb_library_register(rt, tb_imglib_get_library());
|
|
tb_library_register(rt, tb_number_get_library());
|
|
tb_library_register(rt, tb_string_get_library());
|
|
tb_library_register(rt, tb_vector_get_library());
|
|
tb_library_register(rt, tb_graphics_get_library());
|
|
tb_library_register(rt, tb_editor_get_library());
|
|
ev = tb_block_create(rt, tb_type_block_find(rt, "Graphics.Input"));
|
|
scene = tb_block_create(rt, tb_type_block_find(rt, "Editor.Scene"));
|
|
tb_block_set_default(scene, 0, tb_value_block(ev));
|
|
frame();
|
|
Ed* ed = *(Ed**)scene->userdata;
|
|
if (!project_load(ed, argv[1])) { fprintf(stderr, "nao abriu: %s\n", ed->hot_status); return 1; }
|
|
tab_select(ed, ed->tab_n - 1); /* a ultima aba (o programa) */
|
|
frame();
|
|
run_flag(ed);
|
|
|
|
if (getenv("BENCH_PROFILE")) { /* so render: "mudou" sempre ligado */
|
|
for (int k = 0; k < frames; k++) {
|
|
tb_vars_set("mudou", tb_value_integer(1));
|
|
set_cam(270);
|
|
frame();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* preparacao + 1o render: ate "f" chegar em "nf" com "mudou" desligado */
|
|
double t0 = now();
|
|
int n = 0;
|
|
for (; ed->vm_running && n < 3000; n++) {
|
|
frame();
|
|
if (n > 2 && var_num("mudou") == 0 && var_num("nf") > 0 && var_num("f") >= var_num("nf")) break;
|
|
}
|
|
if (n >= 3000) { fprintf(stderr, "preparacao nao terminou\n"); diag(ed); return 1; }
|
|
printf("preparacao + 1o render: %.3f s (%d quadros do editor)\n", now() - t0, n);
|
|
|
|
double best = 1e9, sum = 0;
|
|
for (int k = 0; k < frames; k++) {
|
|
tb_vars_set("mudou", tb_value_integer(1));
|
|
set_cam(k * step);
|
|
double a = now();
|
|
t_eval = t_draw = 0;
|
|
int m = 0;
|
|
/* o render comeca quando o programa ve "mudou" e termina quando f chega em nf */
|
|
do {
|
|
frame();
|
|
if (++m == 3000) { fprintf(stderr, "render %d nao terminou\n", k); diag(ed); return 1; }
|
|
} while (var_num("mudou") == 1 || var_num("f") < var_num("nf"));
|
|
double dt = now() - a;
|
|
sum += dt;
|
|
if (dt < best) best = dt;
|
|
printf("render %2d: %.1f ms (%d quadros do editor; VM+entrada %.1f ms, desenho %.1f ms)\n",
|
|
k, dt * 1000, m, t_eval * 1000, t_draw * 1000);
|
|
}
|
|
const TinyBlocks_Value* tv = tb_vars_get("tela");
|
|
if (tv && tv->kind == TB_VALUE_IMAGE) { /* checksum da tela (FNV-1a) */
|
|
const TinyBlocks_Image* im = tv->as.image;
|
|
const uint8_t* d = tb_image_data_const(im);
|
|
size_t len = (size_t)tb_image_width(im) * tb_image_height(im) * tb_image_channels(im);
|
|
uint32_t h = 2166136261u;
|
|
for (size_t i = 0; i < len; i++) { h ^= d[i]; h *= 16777619u; }
|
|
printf(" checksum da tela: %08x\n", h);
|
|
}
|
|
printf("media %.1f ms, melhor %.1f ms\n", sum / frames * 1000, best * 1000);
|
|
return 0;
|
|
}
|