From 1961b4b237083f4cfac9ca6b249c1d6cb665d149 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Tue, 14 Jul 2026 17:29:02 +0200 Subject: Initial (refactor) --- TODO.md | 6 + anc/null/0123.asm | 8 + anc/v1/55a.asm | 82 +++ arch/null/arch.c | 241 ++++++++ arch/null/arch_inst.c | 48 ++ arch/null/arch_spec.h | 19 + arch/null/client_plots.h | 32 + arch/v1/arch.c | 989 ++++++++++++++++++++++++++++++ arch/v1/arch_inst.c | 40 ++ arch/v1/arch_spec.h | 140 +++++ arch/v1/client_plots.h | 83 +++ core/arch.h | 63 ++ core/arch_inst.h | 24 + core/compress.c | 77 +++ core/compress.h | 27 + core/logger.c | 133 ++++ core/logger.h | 24 + core/salis.c | 1033 +++++++++++++++++++++++++++++++ core/salis.h | 142 +++++ core/sql.c | 105 ++++ core/sql.h | 11 + core/timer.c | 43 ++ core/timer.h | 23 + data/client.cpp | 1409 +++++++++++++++++++++++++++++++++++++++++++ data/client.h | 137 +++++ data/server.c | 390 ++++++++++++ docs/flowchart.odg | Bin 0 -> 23654 bytes docs/flowchart.svg | 844 ++++++++++++++++++++++++++ salis.py | 444 ++++++++++++++ test/anc/v1/1rops.asm | 109 ++++ test/anc/v1/2rops.asm | 26 + test/anc/v1/3rops.asm | 70 +++ test/anc/v1/addrs.asm | 75 +++ test/anc/v1/allos.asm | 33 + test/anc/v1/ifnzs.asm | 50 ++ test/anc/v1/ioops.asm | 104 ++++ test/anc/v1/jumps.asm | 56 ++ test/anc/v1/mbops.asm | 17 + test/anc/v1/noops.asm | 42 ++ test/anc/v1/stack.asm | 51 ++ test/test_build.py | 114 ++++ test/test_determinism.py | 50 ++ test/test_general.py | 58 ++ test/test_run.py | 59 ++ test/test_v1.py | 51 ++ test/ui/asa4/links.json | 1 + test/ui/asa4/ui.c | 29 + test/ui/null/links.json | 1 + test/ui/null/ui.c | 24 + test/ui/push/links.json | 1 + test/ui/push/ui.c | 26 + test/ui/v1/1rops/links.json | 1 + test/ui/v1/1rops/ui.c | 74 +++ test/ui/v1/2rops/links.json | 1 + test/ui/v1/2rops/ui.c | 56 ++ test/ui/v1/3rops/links.json | 1 + test/ui/v1/3rops/ui.c | 65 ++ test/ui/v1/addrs/links.json | 1 + test/ui/v1/addrs/ui.c | 58 ++ test/ui/v1/allos/links.json | 1 + test/ui/v1/allos/ui.c | 80 +++ test/ui/v1/ifnzs/links.json | 1 + test/ui/v1/ifnzs/ui.c | 56 ++ test/ui/v1/ioops/links.json | 1 + test/ui/v1/ioops/ui.c | 66 ++ test/ui/v1/jumps/links.json | 1 + test/ui/v1/jumps/ui.c | 31 + test/ui/v1/mbops/links.json | 1 + test/ui/v1/mbops/ui.c | 46 ++ test/ui/v1/noops/links.json | 1 + test/ui/v1/noops/ui.c | 136 +++++ test/ui/v1/stack/links.json | 1 + test/ui/v1/stack/ui.c | 85 +++ ui/curses/links.json | 1 + ui/curses/ui.c | 1378 ++++++++++++++++++++++++++++++++++++++++++ ui/daemon/links.json | 1 + ui/daemon/ui.c | 54 ++ util/doc-check.py | 43 ++ 78 files changed, 9905 insertions(+) create mode 100644 TODO.md create mode 100644 anc/null/0123.asm create mode 100644 anc/v1/55a.asm create mode 100644 arch/null/arch.c create mode 100644 arch/null/arch_inst.c create mode 100644 arch/null/arch_spec.h create mode 100644 arch/null/client_plots.h create mode 100644 arch/v1/arch.c create mode 100644 arch/v1/arch_inst.c create mode 100644 arch/v1/arch_spec.h create mode 100644 arch/v1/client_plots.h create mode 100644 core/arch.h create mode 100644 core/arch_inst.h create mode 100644 core/compress.c create mode 100644 core/compress.h create mode 100644 core/logger.c create mode 100644 core/logger.h create mode 100644 core/salis.c create mode 100644 core/salis.h create mode 100644 core/sql.c create mode 100644 core/sql.h create mode 100644 core/timer.c create mode 100644 core/timer.h create mode 100644 data/client.cpp create mode 100644 data/client.h create mode 100644 data/server.c create mode 100644 docs/flowchart.odg create mode 100644 docs/flowchart.svg create mode 100755 salis.py create mode 100644 test/anc/v1/1rops.asm create mode 100644 test/anc/v1/2rops.asm create mode 100644 test/anc/v1/3rops.asm create mode 100644 test/anc/v1/addrs.asm create mode 100644 test/anc/v1/allos.asm create mode 100644 test/anc/v1/ifnzs.asm create mode 100644 test/anc/v1/ioops.asm create mode 100644 test/anc/v1/jumps.asm create mode 100644 test/anc/v1/mbops.asm create mode 100644 test/anc/v1/noops.asm create mode 100644 test/anc/v1/stack.asm create mode 100644 test/test_build.py create mode 100644 test/test_determinism.py create mode 100644 test/test_general.py create mode 100644 test/test_run.py create mode 100644 test/test_v1.py create mode 100644 test/ui/asa4/links.json create mode 100644 test/ui/asa4/ui.c create mode 100644 test/ui/null/links.json create mode 100644 test/ui/null/ui.c create mode 100644 test/ui/push/links.json create mode 100644 test/ui/push/ui.c create mode 100644 test/ui/v1/1rops/links.json create mode 100644 test/ui/v1/1rops/ui.c create mode 100644 test/ui/v1/2rops/links.json create mode 100644 test/ui/v1/2rops/ui.c create mode 100644 test/ui/v1/3rops/links.json create mode 100644 test/ui/v1/3rops/ui.c create mode 100644 test/ui/v1/addrs/links.json create mode 100644 test/ui/v1/addrs/ui.c create mode 100644 test/ui/v1/allos/links.json create mode 100644 test/ui/v1/allos/ui.c create mode 100644 test/ui/v1/ifnzs/links.json create mode 100644 test/ui/v1/ifnzs/ui.c create mode 100644 test/ui/v1/ioops/links.json create mode 100644 test/ui/v1/ioops/ui.c create mode 100644 test/ui/v1/jumps/links.json create mode 100644 test/ui/v1/jumps/ui.c create mode 100644 test/ui/v1/mbops/links.json create mode 100644 test/ui/v1/mbops/ui.c create mode 100644 test/ui/v1/noops/links.json create mode 100644 test/ui/v1/noops/ui.c create mode 100644 test/ui/v1/stack/links.json create mode 100644 test/ui/v1/stack/ui.c create mode 100644 ui/curses/links.json create mode 100644 ui/curses/ui.c create mode 100644 ui/daemon/links.json create mode 100644 ui/daemon/ui.c create mode 100755 util/doc-check.py diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..bea58de --- /dev/null +++ b/TODO.md @@ -0,0 +1,6 @@ +# Next todos: +- add v1 plots +- add new data test +- re-add README +- test long sim +- done! diff --git a/anc/null/0123.asm b/anc/null/0123.asm new file mode 100644 index 0000000..7ade591 --- /dev/null +++ b/anc/null/0123.asm @@ -0,0 +1,8 @@ +; file : anc/null/0123.asm +; project : Salis-VM +; author : Paul Oliver + +null 0 +null 1 +null 2 +null 3 diff --git a/anc/v1/55a.asm b/anc/v1/55a.asm new file mode 100644 index 0000000..bd70cdb --- /dev/null +++ b/anc/v1/55a.asm @@ -0,0 +1,82 @@ +; file : anc/v1/55a.asm +; project : Salis-VM +; author : Paul Oliver +; +; Based on the original 55a ancestor organism from Salis-V1. +; This organism first measures itself by means of key/lock instructions. +; Then it replicates continuously in both directions. + +; index +; [section] begin gene +; [section] measure gene +; [section] allocation gene +; [section] copy gene +; [section] split gene +; [section] end gene + +; [section] begin gene +loka + +; [section] measure gene +adrb +keya +adrf +keya +nop1 +incn +nop1 +subn +nop1 +nop1 + +; [section] allocation gene +lokb +notn +nop3 +rpsh +nop1 +rpsh +nop3 +ifnz +nop3 +jmpf +keyc +malb +nop1 +nop2 +jmpf +keyd +lokc +malf +nop1 +nop2 + +; [section] copy gene +lokd +rloa +nop0 +nop3 +rwrt +nop2 +nop3 +incn +incn +nop2 +decn +nop1 +ifnz +nop1 +jmpb +keyd + +; [section] split gene +bspl +rpop +nop3 +rpop +nop1 +jmpb +keyb + +; [section] end gene +loka diff --git a/arch/null/arch.c b/arch/null/arch.c new file mode 100644 index 0000000..f9e7ef4 --- /dev/null +++ b/arch/null/arch.c @@ -0,0 +1,241 @@ +// file : arch/null/arch.c +// project : Salis-VM +// author : Paul Oliver +// +// Example of a minimal viable VM architecture. Useful for testing. +// Also works as a placeholder when creating new VM architectures. + +// index: +// [section] includes +// [section] getters +// [section] callbacks +// [section] validation +// [section] data +// [section] main + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arch.h" +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" +#include "sql.h" + +// ---------------------------------------------------------------------------- +// [section] getters +// ---------------------------------------------------------------------------- +uint64_t arch_proc_get_ip_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->ip; +} + +uint64_t arch_proc_get_sp_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->sp; +} + +uint64_t arch_proc_get_mb0_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb0a; +} + +uint64_t arch_proc_get_mb0_size(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb0s; +} + +uint64_t arch_proc_get_mb1_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb1a; +} + +uint64_t arch_proc_get_mb1_size(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb1s; +} + +uint64_t arch_proc_get_slice(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + (void)core; + (void)pix; + return 1; +} + +// ---------------------------------------------------------------------------- +// [section] callbacks +// ---------------------------------------------------------------------------- +void arch_on_proc_step(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + (void)core; + (void)pix; + return; +} + +void arch_on_proc_kill(struct Core *core) { + assert(core); + assert(core->pnum > 1); + (void)core; +} + +// ---------------------------------------------------------------------------- +// [section] validation +// ---------------------------------------------------------------------------- +#if !defined(NDEBUG) +void arch_validate_core(struct Core *core) { + assert(core); + (void)core; +} +#endif + +// ---------------------------------------------------------------------------- +// [section] data +// ---------------------------------------------------------------------------- +void arch_push_data_header(void) { + log_info("Creating arch table in SQLite database"); + sql_exec( + NULL, NULL, + "create table arch (" +#define FOR_CORE(i) \ + "null_0_pop_" #i " int not null, " \ + "null_1_pop_" #i " int not null, " \ + "null_2_pop_" #i " int not null, " \ + "null_3_pop_" #i " int not null, " + FOR_CORES +#undef FOR_CORE + "step int not null" + ");" + ); +} + +static int arch_count_inst_pop(struct Core *core) { + assert(core); + + for (uint64_t i = 0; i < MVEC_SIZE; i++) { + core->ipop[mvec_get_inst(core, i)]++; + } + +#if !defined(NDEBUG) + uint64_t pop_total = 0; + + for (uint64_t i = 0; i < ARCH_INST_COUNT; i++) { + pop_total += core->ipop[i]; + } + + assert(pop_total == MVEC_SIZE); +#endif + + return 0; +} + +void arch_prepare_data_line(void) { + assert(g_sim_db); + + // Measure instruction population in parallel + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_create(&core->ipop_thrd, (thrd_start_t)arch_count_inst_pop, core); + } + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_join(core->ipop_thrd, NULL); + } +} + +void arch_push_data_line(FILE *eva_file) { + assert(eva_file); + (void)eva_file; + log_info("Pushing row to arch table in SQLite database"); + sql_exec( + NULL, NULL, + "insert into arch (" +#define FOR_CORE(i) \ + "null_0_pop_" #i ", " \ + "null_1_pop_" #i ", " \ + "null_2_pop_" #i ", " \ + "null_3_pop_" #i ", " + FOR_CORES +#undef FOR_CORE + "step" + ") values (" +#define FOR_CORE(i) \ + "%ld, %ld, %ld, %ld, " + FOR_CORES +#undef FOR_CORE + "%ld" + ");", +#define FOR_CORE(i) \ + g_cores[i].ipop[0], \ + g_cores[i].ipop[1], \ + g_cores[i].ipop[2], \ + g_cores[i].ipop[3], + FOR_CORES +#undef FOR_CORE + g_step + ); + + // Reset data aggregation fields + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + memset(core->ipop, 0, sizeof(uint64_t) * ARCH_INST_COUNT); + } +} + +// ---------------------------------------------------------------------------- +// [section] main +// ---------------------------------------------------------------------------- +#if defined(COMMAND_NEW) +void arch_core_init(struct Core *core) { + assert(core); + + for (uint64_t i = 0; i < CLONES; i++) { + uint64_t addr_clone = (MVEC_SIZE / CLONES) * i; + struct Proc *panc = proc_fetch(core, i); + panc->mb0a = addr_clone; + panc->mb0s = ANC_SIZE; + panc->ip = addr_clone; + panc->sp = addr_clone; + } +} +#endif + +#if defined(COMMAND_LOAD) +void arch_core_load(struct Core *core, FILE *f) { + assert(core); + assert(f); + (void)core; + (void)f; +} +#endif + +void arch_core_save(const struct Core *core, FILE *f) { + assert(core); + assert(f); + (void)core; + (void)f; +} + +void arch_core_free(struct Core *core) { + assert(core); + (void)core; +} diff --git a/arch/null/arch_inst.c b/arch/null/arch_inst.c new file mode 100644 index 0000000..4744382 --- /dev/null +++ b/arch/null/arch_inst.c @@ -0,0 +1,48 @@ +// file : arch/null/arch_inst.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#define ARCH_EXPORT_INST_COUNT +#include "arch_spec.h" +#include "arch_inst.h" + +wchar_t arch_inst_symbol(uint8_t inst) { + assert(inst < ARCH_INST_COUNT); + + switch (inst) { + case 0: + return L'⢀'; + case 1: + return L'⢁'; + case 2: + return L'⢂'; + case 3: + return L'⢃'; + } + + assert(false); + return L'\0'; +} + +const char *arch_inst_mnemonic(uint8_t inst) { + assert(inst < ARCH_INST_COUNT); + + switch (inst) { + case 0: + return "null 0"; + case 1: + return "null 1"; + case 2: + return "null 2"; + case 3: + return "null 3"; + } + + assert(false); + return NULL; +} diff --git a/arch/null/arch_spec.h b/arch/null/arch_spec.h new file mode 100644 index 0000000..466e69f --- /dev/null +++ b/arch/null/arch_spec.h @@ -0,0 +1,19 @@ +// file : arch/null/arch_spec.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +#define ARCH_SPEC_INST_BITS 2 + +#define ARCH_SPEC_CORE_FIELDS \ + ARCH_SPEC_CORE_FIELD(thrd_t, ipop_thrd) \ + ARCH_SPEC_CORE_FIELD(uint64_t, ipop[ARCH_INST_COUNT]) \ + +#define ARCH_SPEC_PROC_FIELDS \ + ARCH_SPEC_PROC_FIELD(uint64_t, ip) \ + ARCH_SPEC_PROC_FIELD(uint64_t, sp) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb0a) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb0s) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb1a) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb1s) diff --git a/arch/null/client_plots.h b/arch/null/client_plots.h new file mode 100644 index 0000000..72cd052 --- /dev/null +++ b/arch/null/client_plots.h @@ -0,0 +1,32 @@ +// file : arch/null/client_plots.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +static std::array g_arch_traces = std::to_array>({ +#define FOR_CORE(i) \ + {"null_0_pop_" #i, "null_0_pop_" #i}, \ + {"null_1_pop_" #i, "null_1_pop_" #i}, \ + {"null_2_pop_" #i, "null_2_pop_" #i}, \ + {"null_3_pop_" #i, "null_3_pop_" #i}, + FOR_CORES +#undef FOR_CORE +}); + +static std::array, 0> g_arch_traces_heatmaps = {}; +static std::array g_arch_plots = {}; + +static std::array g_arch_plots_stacked = std::to_array({ +#define FOR_CORE(i) \ + {"ipop%_" #i, "population", { \ + "null_0_pop_" #i, \ + "null_1_pop_" #i, \ + "null_2_pop_" #i, \ + "null_3_pop_" #i, \ + }}, + FOR_CORES +#undef FOR_CORE +}); + +static std::array g_arch_plots_heatmaps = {}; diff --git a/arch/v1/arch.c b/arch/v1/arch.c new file mode 100644 index 0000000..ea6ff36 --- /dev/null +++ b/arch/v1/arch.c @@ -0,0 +1,989 @@ +// file : arch/v1/arch.c +// project : Salis-VM +// author : Paul Oliver +// +// Based on the original VM architecture from Salis-V1. +// The only addition are the extra core-fields used for data aggregation. + +// index: +// [section] includes +// [section] getters +// [section] statics +// [section] callbacks +// [section] validation +// [section] data +// [section] main + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arch.h" +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" +#include "sql.h" + +// ---------------------------------------------------------------------------- +// [section] getters +// ---------------------------------------------------------------------------- +uint64_t arch_proc_get_ip_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->ip; +} + +uint64_t arch_proc_get_sp_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->sp; +} + +uint64_t arch_proc_get_mb0_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb0a; +} + +uint64_t arch_proc_get_mb0_size(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb0s; +} + +uint64_t arch_proc_get_mb1_addr(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb1a; +} + +uint64_t arch_proc_get_mb1_size(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return proc_get(core, pix)->mb1s; +} + +uint64_t arch_proc_get_slice(const struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + (void)core; + (void)pix; + return 1; +} + +// ---------------------------------------------------------------------------- +// [section] statics +// ---------------------------------------------------------------------------- +static void increment_ip(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + proc->ip++; + proc->sp = proc->ip; +} + +static bool is_between(uint8_t inst, uint8_t lo, uint8_t hi) { + assert(inst < ARCH_INST_COUNT); + assert(lo < ARCH_INST_COUNT); + assert(hi < ARCH_INST_COUNT); + assert(lo < hi); + return (inst >= lo) && (inst <= hi); +} + +static bool is_key(uint8_t inst) { + assert(inst < ARCH_INST_COUNT); + return is_between(inst, keya, keyp); +} + +#if !defined(NDEBUG) +static bool is_lock(uint8_t inst) { + assert(inst < ARCH_INST_COUNT); + return is_between(inst, loka, lokp); +} +#endif + +static bool is_rmod(uint8_t inst) { + assert(inst < ARCH_INST_COUNT); + return is_between(inst, nop0, nop3); +} + +static bool key_lock_match(uint8_t key, uint8_t lock) { + assert(key < ARCH_INST_COUNT); + assert(lock < ARCH_INST_COUNT); + assert(is_key(key)); + return (key - keya) == (lock - loka); +} + +static bool seek(struct Core *core, uint64_t pix, bool fwrd) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + + if (proc->ip + 1 >= MVEC_SIZE) { + increment_ip(core, pix); + return false; + } + + uint8_t next = mvec_get_inst(core, proc->ip + 1); + + if (!is_key(next)) { + increment_ip(core, pix); + return false; + } + + if (proc->sp >= MVEC_SIZE) { + // HALT: process SP address is invalid + core->hlts++; + return false; + } + + uint8_t spin = mvec_get_inst(core, proc->sp); + + if (key_lock_match(next, spin)) { + return true; + } + + if (fwrd) { + proc->sp++; + } else { + proc->sp--; + } + + return false; +} + +static void jump(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + +#if !defined(NDEBUG) + uint8_t next = mvec_get_inst(core, proc->ip + 1); + uint8_t spin = mvec_get_inst(core, proc->sp); + assert(is_key(next)); + assert(is_lock(spin)); + assert(key_lock_match(next, spin)); +#endif + + proc->ip = proc->sp; +} + +static void get_reg_addr_list(struct Core *core, uint64_t pix, uint64_t **rlist, int rcount, bool offset) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + assert(rlist); + assert(rcount); + assert(rcount < 4); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t madr = proc->ip + (offset ? 2 : 1); + + for (int i = 0; i < rcount; i++) { + rlist[i] = &proc->r0x; + } + + for (int i = 0; i < rcount; i++) { + uint64_t mnxt = madr + i; + + if (mnxt >= MVEC_SIZE) { + break; + } + + uint8_t mins = mvec_get_inst(core, mnxt); + + if (!is_rmod(mins)) { + break; + } + + switch (mins) { + case nop0: + rlist[i] = &proc->r0x; + break; + case nop1: + rlist[i] = &proc->r1x; + break; + case nop2: + rlist[i] = &proc->r2x; + break; + case nop3: + rlist[i] = &proc->r3x; + break; + } + } +} + +static void address(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t *reg; + +#if !defined(NDEBUG) + uint8_t next = mvec_get_inst(core, proc->ip + 1); + uint8_t spin = mvec_get_inst(core, proc->sp); + assert(is_key(next)); + assert(is_lock(spin)); + assert(key_lock_match(next, spin)); +#endif + + get_reg_addr_list(core, pix, ®, 1, true); + *reg = proc->sp; + increment_ip(core, pix); +} + +static void free_memory_block(struct Core *core, uint64_t addr, uint64_t size) { + assert(core); + assert(size); + + for (uint64_t i = 0; i < size; i++) { + mvec_free(core, addr + i); + } +} + +static void free_child_memory_of(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + assert(proc->mb1s); + free_memory_block(core, proc->mb1a, proc->mb1s); + proc->mb1a = 0; + proc->mb1s = 0; +} + +static void alloc(struct Core *core, uint64_t pix, bool fwrd) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t *regs[2]; + get_reg_addr_list(core, pix, regs, 2, false); + uint64_t bsize = *regs[0]; + + // Do nothing if block-size is zero + if (!bsize) { + increment_ip(core, pix); + return; + } + + // Do nothing if seek pointer is not adjacent to allocated memory block + if (proc->mb1s) { + uint64_t exp_addr = proc->mb1a; + + if (fwrd) { + exp_addr += proc->mb1s; + } else { + exp_addr--; + } + + if (proc->sp != exp_addr) { + increment_ip(core, pix); + return; + } + } + + // HALT: process SP address is invalid + // Discard allocated memory if any... + if (proc->sp >= MVEC_SIZE) { + if (proc->mb1s) { + free_child_memory_of(core, pix); + } + + core->hlts++; + return; + } + + // Allocation was successful, store block address on register + if (proc->mb1s == bsize) { + increment_ip(core, pix); + *regs[1] = proc->mb1a; + return; + } + + // Seek pointer collided with another allocated block. + // Discard and keep looking... + if (mvec_is_alloc(core, proc->sp)) { + if (proc->mb1s) { + free_child_memory_of(core, pix); + } + + if (fwrd) { + proc->sp++; + } else { + proc->sp--; + } + + return; + } + + // Free (non-allocated) byte found, enlarge child block 1 byte + mvec_alloc(core, proc->sp); + + if (!proc->mb1s || !fwrd) { + proc->mb1a = proc->sp; + } + + proc->mb1s++; + + // Advance seek pointer + if (fwrd) { + proc->sp++; + } else { + proc->sp--; + } +} + +static void if_not_zero(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t *reg; + get_reg_addr_list(core, pix, ®, 1, false); + uint64_t jmod = ((proc->ip + 1 < MVEC_SIZE) && is_rmod(mvec_get_inst(core, proc->ip + 1))) ? 1 : 0; + uint64_t rmod = *reg ? 1 : 2; + proc->ip += jmod + rmod; + proc->sp = proc->ip; +} + +static void mem_block_swap(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + + if (proc->mb1s) { + uint64_t tmpa = proc->mb0a; + uint64_t tmps = proc->mb0s; + proc->mb0a = proc->mb1a; + proc->mb0s = proc->mb1s; + proc->mb1a = tmpa; + proc->mb1s = tmps; + + // Memory block swap events mark all addresses within both blocks + for (uint64_t i = 0; i < proc->mb0s; i++) { + core->xeva.data[proc->mb0a + i]++; + } + + for (uint64_t i = 0; i < proc->mb1s; i++) { + core->xeva.data[proc->mb1a + i]++; + } + } + + increment_ip(core, pix); +} + +static void mem_block_clear(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + + if (proc->mb1s) { + free_child_memory_of(core, pix); + } + + increment_ip(core, pix); +} + +static void mem_block_split(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + + if (proc->mb1s) { + struct Proc child = { 0 }; + child.ip = proc->mb1a; + child.sp = proc->mb1a; + child.mb0a = proc->mb1a; + child.mb0s = proc->mb1s; + proc->mb1a = 0; + proc->mb1s = 0; + + // A new organism is born :) + proc_new(core, &child); + } else { + assert(!proc->mb1a); + } + + increment_ip(core, pix); +} + +static void three_regs_op(struct Core *core, uint64_t pix, uint8_t inst) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + uint64_t *regs[3]; + get_reg_addr_list(core, pix, regs, 3, false); + + // Organisms can do arithmetic using any sequence of 3 registers + switch (inst) { + case addn: + *regs[0] = *regs[1] + *regs[2]; + break; + case subn: + *regs[0] = *regs[1] - *regs[2]; + break; + case muln: + *regs[0] = *regs[1] * *regs[2]; + break; + case divn: + if (*regs[2]) { + *regs[0] = *regs[1] / *regs[2]; + } else { + // HALT: division by zero + core->hlts++; + return; + } + + break; + default: + assert(false); + } + + increment_ip(core, pix); +} + +static void one_reg_op(struct Core *core, uint64_t pix, uint8_t inst) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + uint64_t *reg; + get_reg_addr_list(core, pix, ®, 1, false); + + switch (inst) { + case incn: + (*reg)++; + break; + case decn: + (*reg)--; + break; + case notn: + *reg = !(*reg); + break; + case shfl: + *reg <<= 1; + break; + case shfr: + *reg >>= 1; + break; + case zero: + *reg = 0; + break; + case unit: + *reg = 1; + break; + default: + assert(false); + } + + increment_ip(core, pix); +} + +static int get_sp_dir(uint64_t src, uint64_t dst) { + if (src == dst) { + return 0; + } else if (src - dst <= dst - src) { + return -1; + } else { + return 1; + } +} + +static void load_reg(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t *regs[2]; + get_reg_addr_list(core, pix, regs, 2, false); + int sp_dir = get_sp_dir(proc->sp, *regs[0]); + + if (*regs[0] >= MVEC_SIZE) { + // Target address is invalid + increment_ip(core, pix); + } else if (sp_dir == 1) { + proc->sp++; + } else if (sp_dir == -1) { + proc->sp--; + } else { + *regs[1] = mvec_get_inst(core, *regs[0]); + increment_ip(core, pix); + } +} + +static bool is_writeable_by(const struct Core *core, uint64_t addr, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return addr < MVEC_SIZE && (!mvec_is_alloc(core, addr) || mvec_is_proc_owner(core, addr, pix)); +} + +static void write_reg(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t *regs[2]; + get_reg_addr_list(core, pix, regs, 2, false); + int sp_dir = get_sp_dir(proc->sp, *regs[0]); + + if (sp_dir == 1) { + proc->sp++; + } else if (sp_dir == -1) { + proc->sp--; + } else { + if (is_writeable_by(core, *regs[0], pix)) { + uint64_t addr = *regs[0]; + uint8_t inst = *regs[1] & ARCH_INST_MASK; + + // Store write event + core->iwrt[inst]++; + core->weva.data[addr]++; + + if (mvec_is_in_mb0_of_proc(core, addr, pix)) { + core->wmb0++; + } else if (mvec_is_in_mb1_of_proc(core, addr, pix)) { + core->wmb1++; + } else { + core->wdea++; + } + + // Write instruction + mvec_set_inst(core, addr, inst); + } + + increment_ip(core, pix); + } +} + +static void two_reg_op(struct Core *core, uint64_t pix, uint8_t inst) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + uint64_t *regs[2]; + get_reg_addr_list(core, pix, regs, 2, false); + + switch (inst) { + case rdup: + *regs[1] = *regs[0]; + break; + case rswp: + { + uint64_t tmp = *regs[0]; + *regs[0] = *regs[1]; + *regs[1] = tmp; + } + + break; + default: + assert(false); + } + + increment_ip(core, pix); +} + +static void push_to_stack(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t *reg; + get_reg_addr_list(core, pix, ®, 1, false); + proc->s7 = proc->s6; + proc->s6 = proc->s5; + proc->s5 = proc->s4; + proc->s4 = proc->s3; + proc->s3 = proc->s2; + proc->s2 = proc->s1; + proc->s1 = proc->s0; + proc->s0 = *reg; + increment_ip(core, pix); +} + +static void pop_from_stack(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + assert(proc->ip < MVEC_SIZE); + uint64_t *reg; + get_reg_addr_list(core, pix, ®, 1, false); + *reg = proc->s0; + proc->s0 = proc->s1; + proc->s1 = proc->s2; + proc->s2 = proc->s3; + proc->s3 = proc->s4; + proc->s4 = proc->s5; + proc->s5 = proc->s6; + proc->s6 = proc->s7; + proc->s7 = 0; + increment_ip(core, pix); +} + +// ---------------------------------------------------------------------------- +// [section] callbacks +// ---------------------------------------------------------------------------- +void arch_on_proc_step(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + struct Proc *proc = proc_fetch(core, pix); + + if (proc->ip >= MVEC_SIZE) { + // HALT: process IP address is invalid + core->hlts++; + return; + } + + // Fetch instruction + uint8_t inst = mvec_get_inst(core, proc->ip); + core->iexe[inst]++; + + // Execute instruction + switch (inst) { + case jmpb: + if (seek(core, pix, false)) { + jump(core, pix); + } + + break; + case jmpf: + if (seek(core, pix, true)) { + jump(core, pix); + } + + break; + case adrb: + if (seek(core, pix, false)) { + address(core, pix); + } + + break; + case adrf: + if (seek(core, pix, true)) { + address(core, pix); + } + + break; + case malb: + alloc(core, pix, false); + break; + case malf: + alloc(core, pix, true); + break; + case ifnz: + if_not_zero(core, pix); + break; + case bswp: + mem_block_swap(core, pix); + break; + case bclr: + mem_block_clear(core, pix); + break; + case bspl: + mem_block_split(core, pix); + break; + case addn: + case subn: + case muln: + case divn: + three_regs_op(core, pix, inst); + break; + case incn: + case decn: + case notn: + case shfl: + case shfr: + case zero: + case unit: + one_reg_op(core, pix, inst); + break; + case rloa: + load_reg(core, pix); + break; + case rwrt: + write_reg(core, pix); + break; + case rdup: + case rswp: + two_reg_op(core, pix, inst); + break; + case rpsh: + push_to_stack(core, pix); + break; + case rpop: + pop_from_stack(core, pix); + break; + default: + increment_ip(core, pix); + break; + } +} + +void arch_on_proc_kill(struct Core *core) { + assert(core); + assert(core->pnum > 1); + struct Proc *pfst = proc_fetch(core, core->pfst); + free_memory_block(core, pfst->mb0a, pfst->mb0s); + + if (pfst->mb1s) { + free_memory_block(core, pfst->mb1a, pfst->mb1s); + } + + memcpy(pfst, &g_null_proc, sizeof(struct Proc)); +} + +// ---------------------------------------------------------------------------- +// [section] validation +// ---------------------------------------------------------------------------- +#if !defined(NDEBUG) +void arch_validate_core(struct Core *core) { + assert(core); + + for (uint64_t pix = core->pfst; pix <= core->plst; pix++) { + const struct Proc *proc = proc_get(core, pix); + assert(proc->mb0s); + + if (proc->mb1a) { + assert(proc->mb1s); + } + + for (uint64_t i = 0; i < proc->mb0s; i++) { + uint64_t addr = proc->mb0a + i; + assert(mvec_is_alloc(core, addr)); + assert(mvec_is_proc_owner(core, addr, pix)); + } + + for (uint64_t i = 0; i < proc->mb1s; i++) { + uint64_t addr = proc->mb1a + i; + assert(mvec_is_alloc(core, addr)); + assert(mvec_is_proc_owner(core, addr, pix)); + } + } +} +#endif + +// ---------------------------------------------------------------------------- +// [section] data +// ---------------------------------------------------------------------------- +void arch_push_data_header(void) { + assert(g_sim_db); + log_info("Creating arch table in SQLite database"); + + // Empty blob columns help the data-server stay agnostic of DB schema. + // These represent EVA data that will be appended by data server. + sql_exec( + NULL, NULL, + "create table arch (" +#define ARCH_SPEC_INST(label, symbol, core) \ + #label "_pop_" #core " int not null, " \ + #label "_exe_" #core " int not null, " \ + #label "_wrt_" #core " int not null, " +#define FOR_CORE(i) \ + ARCH_SPEC_INST_SET(i) \ + "wmb0_" #i " int not null, " \ + "wmb1_" #i " int not null, " \ + "wdea_" #i " int not null, " \ + "hlts_" #i " int not null, " \ + "wev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i " int not null, wev_" #i " blob, " \ + "xev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i " int not null, xev_" #i " blob, " + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST + "step int not null" + ");" + ); +} + +static int arch_count_inst_pop(struct Core *core) { + assert(core); + + for (uint64_t i = 0; i < MVEC_SIZE; i++) { + core->ipop[mvec_get_inst(core, i)]++; + } + +#if !defined(NDEBUG) + uint64_t pop_total = 0; + + for (uint64_t i = 0; i < ARCH_INST_COUNT; i++) { + pop_total += core->ipop[i]; + } + + assert(pop_total == MVEC_SIZE); +#endif + + return 0; +} + +void arch_prepare_data_line(void) { + assert(g_sim_db); + + // Measure instruction population and compress event-arrays in parallel + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_create(&core->ipop_thrd, (thrd_start_t)arch_count_inst_pop, core); + thrd_create(&core->weva.thrd, (thrd_start_t)comp_deflate, &core->weva.params); + thrd_create(&core->xeva.thrd, (thrd_start_t)comp_deflate, &core->xeva.params); + } + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_join(core->ipop_thrd, NULL); + thrd_join(core->weva.thrd, NULL); + thrd_join(core->xeva.thrd, NULL); + core->weva.blob_size = core->weva.params.strm.total_out; + core->xeva.blob_size = core->xeva.params.strm.total_out; + } +} + +void arch_push_data_line(FILE *eva_file) { + assert(g_sim_db); + assert(eva_file); + assert(g_step % DATA_PUSH_INTERVAL == 0); + log_info("Pushing row to arch table in SQLite database"); + sql_exec( + NULL, NULL, + "insert into arch (" +#define ARCH_SPEC_INST(label, symbol, core) \ + #label "_pop_" #core ", " \ + #label "_exe_" #core ", " \ + #label "_wrt_" #core ", " +#define FOR_CORE(i) \ + ARCH_SPEC_INST_SET(i) \ + "wmb0_" #i ", " \ + "wmb1_" #i ", " \ + "wdea_" #i ", " \ + "hlts_" #i ", " \ + "wev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i ", " \ + "xev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i ", " + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST + "step" + ") values (" +#define ARCH_SPEC_INST(label, symbol, core) \ + "%ld, " \ + "%ld, " \ + "%ld, " +#define FOR_CORE(i) \ + ARCH_SPEC_INST_SET(i) \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST + "%ld" + ");", +#define ARCH_SPEC_INST(label, symbol, core) \ + g_cores[core].ipop[label], \ + g_cores[core].iexe[label], \ + g_cores[core].iwrt[label], +#define FOR_CORE(i) \ + ARCH_SPEC_INST_SET(i) \ + g_cores[i].wmb0, \ + g_cores[i].wmb1, \ + g_cores[i].wdea, \ + g_cores[i].hlts, \ + g_cores[i].weva.blob_size, \ + g_cores[i].xeva.blob_size, + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST + g_step + ); + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + fwrite(core->weva.comp, sizeof(Bytef), core->weva.blob_size, eva_file); + fwrite(core->xeva.comp, sizeof(Bytef), core->xeva.blob_size, eva_file); + comp_deflate_end(&core->weva.params); + comp_deflate_end(&core->xeva.params); + } + + // Reset data aggregation fields + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + memset(core->ipop, 0, sizeof(uint64_t) * ARCH_INST_COUNT); + memset(core->iexe, 0, sizeof(uint64_t) * ARCH_INST_COUNT); + memset(core->iwrt, 0, sizeof(uint64_t) * ARCH_INST_COUNT); + core->wmb0 = 0; + core->wmb1 = 0; + core->wdea = 0; + core->hlts = 0; + memset(core->weva.data, 0, sizeof(uint64_t) * MVEC_SIZE); + memset(core->xeva.data, 0, sizeof(uint64_t) * MVEC_SIZE); + } +} + +// ---------------------------------------------------------------------------- +// [section] main +// ---------------------------------------------------------------------------- +#if defined(COMMAND_NEW) +void arch_core_init(struct Core *core) { + assert(core); + + for (uint64_t i = 0; i < CLONES; i++) { + uint64_t addr_clone = (MVEC_SIZE / CLONES) * i; + struct Proc *panc = proc_fetch(core, i); + panc->mb0a = addr_clone; + panc->mb0s = ANC_SIZE; + panc->ip = addr_clone; + panc->sp = addr_clone; + } + + core_event_array_init(&core->weva); + core_event_array_init(&core->xeva); +} +#endif + +#if defined(COMMAND_LOAD) +void arch_core_load(struct Core *core, FILE *f) { + assert(core); + assert(f); + + fread(core->ipop, sizeof(uint64_t), ARCH_INST_COUNT, f); + fread(core->iexe, sizeof(uint64_t), ARCH_INST_COUNT, f); + fread(core->iwrt, sizeof(uint64_t), ARCH_INST_COUNT, f); + fread(&core->wmb0, sizeof(uint64_t), 1, f); + fread(&core->wmb1, sizeof(uint64_t), 1, f); + fread(&core->wdea, sizeof(uint64_t), 1, f); + fread(core->weva.data, sizeof(uint64_t), MVEC_SIZE, f); + fread(core->xeva.data, sizeof(uint64_t), MVEC_SIZE, f); + + core_event_array_init(&core->weva); + core_event_array_init(&core->xeva); +} +#endif + +void arch_core_save(const struct Core *core, FILE *f) { + assert(core); + assert(f); + + fwrite(core->ipop, sizeof(uint64_t), ARCH_INST_COUNT, f); + fwrite(core->iexe, sizeof(uint64_t), ARCH_INST_COUNT, f); + fwrite(core->iwrt, sizeof(uint64_t), ARCH_INST_COUNT, f); + fwrite(&core->wmb0, sizeof(uint64_t), 1, f); + fwrite(&core->wmb1, sizeof(uint64_t), 1, f); + fwrite(&core->wdea, sizeof(uint64_t), 1, f); + fwrite(core->weva.data, sizeof(uint64_t), MVEC_SIZE, f); + fwrite(core->xeva.data, sizeof(uint64_t), MVEC_SIZE, f); +} + +void arch_core_free(struct Core *core) { + assert(core); + (void)core; +} diff --git a/arch/v1/arch_inst.c b/arch/v1/arch_inst.c new file mode 100644 index 0000000..4d75ede --- /dev/null +++ b/arch/v1/arch_inst.c @@ -0,0 +1,40 @@ +// file : arch/v1/arch_inst.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#define ARCH_EXPORT_INST_COUNT +#include "arch_spec.h" +#include "arch_inst.h" + +wchar_t arch_inst_symbol(uint8_t inst) { + assert(inst < ARCH_INST_COUNT); + assert(inst_enum_count == ARCH_INST_COUNT); + + switch (inst) { +#define ARCH_SPEC_INST(label, symbol, core) case label: return symbol; + ARCH_SPEC_INST_SET(core) +#undef ARCH_SPEC_INST + } + + assert(false); + return L'\0'; +} + +const char *arch_inst_mnemonic(uint8_t inst) { + assert(inst < ARCH_INST_COUNT); + assert(inst_enum_count == ARCH_INST_COUNT); + + switch (inst) { +#define ARCH_SPEC_INST(label, symbol, core) case label: return #label; + ARCH_SPEC_INST_SET(core) +#undef ARCH_SPEC_INST + } + + assert(false); + return NULL; +} diff --git a/arch/v1/arch_spec.h b/arch/v1/arch_spec.h new file mode 100644 index 0000000..a904206 --- /dev/null +++ b/arch/v1/arch_spec.h @@ -0,0 +1,140 @@ +// file : arch/v1/arch_spec.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +// index: +// [section] arch-specific core fields +// [section] arch-specific process fields +// [section] instruction set +// [section] instruction enum + +// V1 has 64 instructions - 1 unused bit +#define ARCH_SPEC_INST_BITS 6 + +// ---------------------------------------------------------------------------- +// [section] arch-specific core fields +// ---------------------------------------------------------------------------- +#define ARCH_SPEC_CORE_FIELDS \ + ARCH_SPEC_CORE_FIELD(thrd_t, ipop_thrd) \ + ARCH_SPEC_CORE_FIELD(uint64_t, ipop[ARCH_INST_COUNT]) \ + ARCH_SPEC_CORE_FIELD(uint64_t, iexe[ARCH_INST_COUNT]) \ + ARCH_SPEC_CORE_FIELD(uint64_t, iwrt[ARCH_INST_COUNT]) \ + ARCH_SPEC_CORE_FIELD(uint64_t, wmb0) \ + ARCH_SPEC_CORE_FIELD(uint64_t, wmb1) \ + ARCH_SPEC_CORE_FIELD(uint64_t, wdea) \ + ARCH_SPEC_CORE_FIELD(uint64_t, hlts) \ + ARCH_SPEC_CORE_FIELD(struct EventArray, weva) \ + ARCH_SPEC_CORE_FIELD(struct EventArray, xeva) + +// ---------------------------------------------------------------------------- +// [section] arch-specific process fields +// ---------------------------------------------------------------------------- +#define ARCH_SPEC_PROC_FIELDS \ + ARCH_SPEC_PROC_FIELD(uint64_t, ip) \ + ARCH_SPEC_PROC_FIELD(uint64_t, sp) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb0a) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb0s) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb1a) \ + ARCH_SPEC_PROC_FIELD(uint64_t, mb1s) \ + ARCH_SPEC_PROC_FIELD(uint64_t, r0x) \ + ARCH_SPEC_PROC_FIELD(uint64_t, r1x) \ + ARCH_SPEC_PROC_FIELD(uint64_t, r2x) \ + ARCH_SPEC_PROC_FIELD(uint64_t, r3x) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s0) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s1) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s2) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s3) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s4) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s5) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s6) \ + ARCH_SPEC_PROC_FIELD(uint64_t, s7) + +// ---------------------------------------------------------------------------- +// [section] instruction set +// ---------------------------------------------------------------------------- +// Extra core argument allows calling macro recursively +// within calls to FOR_CORES. See arch_push_data_header in arch/v1/arch.c +// for an example of what I mean. +#define ARCH_SPEC_INST_SET(core) \ + ARCH_SPEC_INST(noop, L'⢀', core) \ + ARCH_SPEC_INST(nop0, L'0', core) \ + ARCH_SPEC_INST(nop1, L'1', core) \ + ARCH_SPEC_INST(nop2, L'2', core) \ + ARCH_SPEC_INST(nop3, L'3', core) \ + \ + ARCH_SPEC_INST(jmpb, L'❨', core) \ + ARCH_SPEC_INST(jmpf, L'❩', core) \ + ARCH_SPEC_INST(adrb, L'⟦', core) \ + ARCH_SPEC_INST(adrf, L'⟧', core) \ + ARCH_SPEC_INST(malb, L'❴', core) \ + ARCH_SPEC_INST(malf, L'❵', core) \ + \ + ARCH_SPEC_INST(ifnz, L'?', core) \ + ARCH_SPEC_INST(bswp, L'%', core) \ + ARCH_SPEC_INST(bclr, L'∅', core) \ + ARCH_SPEC_INST(bspl, L'$', core) \ + \ + ARCH_SPEC_INST(addn, L'+', core) \ + ARCH_SPEC_INST(subn, L'−', core) \ + ARCH_SPEC_INST(muln, L'×', core) \ + ARCH_SPEC_INST(divn, L'/', core) \ + ARCH_SPEC_INST(incn, L'△', core) \ + ARCH_SPEC_INST(decn, L'▽', core) \ + ARCH_SPEC_INST(notn, L'¬', core) \ + ARCH_SPEC_INST(shfl, L'«', core) \ + ARCH_SPEC_INST(shfr, L'»', core) \ + ARCH_SPEC_INST(zero, L'z', core) \ + ARCH_SPEC_INST(unit, L'u', core) \ + \ + ARCH_SPEC_INST(rloa, L'↓', core) \ + ARCH_SPEC_INST(rwrt, L'↑', core) \ + ARCH_SPEC_INST(rdup, L':', core) \ + ARCH_SPEC_INST(rswp, L'↔', core) \ + ARCH_SPEC_INST(rpsh, L'↥', core) \ + ARCH_SPEC_INST(rpop, L'↧', core) \ + \ + ARCH_SPEC_INST(keya, L'a', core) \ + ARCH_SPEC_INST(keyb, L'b', core) \ + ARCH_SPEC_INST(keyc, L'c', core) \ + ARCH_SPEC_INST(keyd, L'd', core) \ + ARCH_SPEC_INST(keye, L'e', core) \ + ARCH_SPEC_INST(keyf, L'f', core) \ + ARCH_SPEC_INST(keyg, L'g', core) \ + ARCH_SPEC_INST(keyh, L'h', core) \ + ARCH_SPEC_INST(keyi, L'i', core) \ + ARCH_SPEC_INST(keyj, L'j', core) \ + ARCH_SPEC_INST(keyk, L'k', core) \ + ARCH_SPEC_INST(keyl, L'l', core) \ + ARCH_SPEC_INST(keym, L'm', core) \ + ARCH_SPEC_INST(keyn, L'n', core) \ + ARCH_SPEC_INST(keyo, L'o', core) \ + ARCH_SPEC_INST(keyp, L'p', core) \ + \ + ARCH_SPEC_INST(loka, L'A', core) \ + ARCH_SPEC_INST(lokb, L'B', core) \ + ARCH_SPEC_INST(lokc, L'C', core) \ + ARCH_SPEC_INST(lokd, L'D', core) \ + ARCH_SPEC_INST(loke, L'E', core) \ + ARCH_SPEC_INST(lokf, L'F', core) \ + ARCH_SPEC_INST(lokg, L'G', core) \ + ARCH_SPEC_INST(lokh, L'H', core) \ + ARCH_SPEC_INST(loki, L'I', core) \ + ARCH_SPEC_INST(lokj, L'J', core) \ + ARCH_SPEC_INST(lokk, L'K', core) \ + ARCH_SPEC_INST(lokl, L'L', core) \ + ARCH_SPEC_INST(lokm, L'M', core) \ + ARCH_SPEC_INST(lokn, L'N', core) \ + ARCH_SPEC_INST(loko, L'O', core) \ + ARCH_SPEC_INST(lokp, L'P', core) + +// ---------------------------------------------------------------------------- +// [section] instruction enum +// ---------------------------------------------------------------------------- +enum { +#define ARCH_SPEC_INST(label, symbol, core) label, + ARCH_SPEC_INST_SET(core) +#undef ARCH_SPEC_INST + inst_enum_count, +}; diff --git a/arch/v1/client_plots.h b/arch/v1/client_plots.h new file mode 100644 index 0000000..4b2c3af --- /dev/null +++ b/arch/v1/client_plots.h @@ -0,0 +1,83 @@ +// file : arch/v1/client_plots.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +// index: + +static std::array g_arch_traces = std::to_array>({ +#define ARCH_SPEC_INST(label, symbol, core) \ + {#label "_pop_" #core, #label}, \ + {#label "_exe_" #core, #label}, \ + {#label "_wrt_" #core, #label}, +#define FOR_CORE(i) \ + {"wmb0_" #i, "wmb0_" #i}, \ + {"wmb1_" #i, "wmb1_" #i}, \ + {"wdea_" #i, "wdea_" #i}, \ + {"hlts_" #i, "hlts_" #i}, \ + ARCH_SPEC_INST_SET(i) + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST +}); + +static std::array g_arch_traces_heatmaps = std::to_array>({ +#define FOR_CORE(i) \ + {"wev_" #i, "wev_" #i}, \ + {"xev_" #i, "xev_" #i}, + FOR_CORES +#undef FOR_CORE +}); + +static std::array g_arch_plots = std::to_array({ + {"wevs", "general", { +#define FOR_CORE(i) "wmb0_" #i, "wmb1_" #i, "wdea_" #i, + FOR_CORES +#undef FOR_CORE + }}, + + {"hlts", "general", { +#define FOR_CORE(i) "hlts_" #i, + FOR_CORES +#undef FOR_CORE + }} +}); + +static std::array g_arch_plots_stacked = std::to_array({ +#define ARCH_SPEC_INST(label, symbol, core) \ + #label "_pop_" #core, +#define FOR_CORE(i) \ + {"ipop%_" #i, "population", { ARCH_SPEC_INST_SET(i) }}, + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST + +#define ARCH_SPEC_INST(label, symbol, core) \ + #label "_exe_" #core, +#define FOR_CORE(i) \ + {"iexe%_" #i, "population", { ARCH_SPEC_INST_SET(i) }}, + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST + +#define ARCH_SPEC_INST(label, symbol, core) \ + #label "_wrt_" #core, +#define FOR_CORE(i) \ + {"iwrt%_" #i, "population", { ARCH_SPEC_INST_SET(i) }}, + FOR_CORES +#undef FOR_CORE +#undef ARCH_SPEC_INST +}); + +static std::array g_arch_plots_heatmaps = std::to_array({ +#define FOR_CORE(i) \ + {"wev_" #i, "heatmaps", "wev_" #i}, + FOR_CORES +#undef FOR_CORE + +#define FOR_CORE(i) \ + {"xev_" #i, "heatmaps", "xev_" #i}, + FOR_CORES +#undef FOR_CORE +}); diff --git a/core/arch.h b/core/arch.h new file mode 100644 index 0000000..06dbfe3 --- /dev/null +++ b/core/arch.h @@ -0,0 +1,63 @@ +// file : core/arch.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +// index: +// [section] structs +// [section] getters +// [section] callbacks +// [section] validation +// [section] data +// [section] main + +// ---------------------------------------------------------------------------- +// [section] structs +// ---------------------------------------------------------------------------- +struct Core; + +// ---------------------------------------------------------------------------- +// [section] getters +// ---------------------------------------------------------------------------- +uint64_t arch_proc_get_ip_addr(const struct Core *core, uint64_t pix); +uint64_t arch_proc_get_sp_addr(const struct Core *core, uint64_t pix); +uint64_t arch_proc_get_mb0_addr(const struct Core *core, uint64_t pix); +uint64_t arch_proc_get_mb0_size(const struct Core *core, uint64_t pix); +uint64_t arch_proc_get_mb1_addr(const struct Core *core, uint64_t pix); +uint64_t arch_proc_get_mb1_size(const struct Core *core, uint64_t pix); +uint64_t arch_proc_get_slice(const struct Core *core, uint64_t pix); + +// ---------------------------------------------------------------------------- +// [section] callbacks +// ---------------------------------------------------------------------------- +void arch_on_proc_step(struct Core *core, uint64_t pix); +void arch_on_proc_kill(struct Core *core); + +// ---------------------------------------------------------------------------- +// [section] validation +// ---------------------------------------------------------------------------- +#if !defined(NDEBUG) +void arch_validate_core(struct Core *core); +#endif + +// ---------------------------------------------------------------------------- +// [section] data +// ---------------------------------------------------------------------------- +void arch_push_data_header(void); +void arch_prepare_data_line(void); +void arch_push_data_line(FILE *eva_file); + +// ---------------------------------------------------------------------------- +// [section] main +// ---------------------------------------------------------------------------- +#if defined(COMMAND_NEW) +void arch_core_init(struct Core *core); +#endif + +#if defined(COMMAND_LOAD) +void arch_core_load(struct Core *core, FILE *f); +#endif + +void arch_core_save(const struct Core *core, FILE *f); +void arch_core_free(struct Core *core); diff --git a/core/arch_inst.h b/core/arch_inst.h new file mode 100644 index 0000000..2f7c7af --- /dev/null +++ b/core/arch_inst.h @@ -0,0 +1,24 @@ +// file : core/arch_inst.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +#define ARCH_INST_COUNT (1 << ARCH_SPEC_INST_BITS) +#define ARCH_INST_IPCM_FLAG 0x80 +#define ARCH_INST_MALL_FLAG 0x80 +#define ARCH_INST_MASK (ARCH_INST_COUNT - 1) +#define ARCH_INST_UNUSED_BITS (~(ARCH_INST_MALL_FLAG | ARCH_INST_MASK)) + +int arch_inst_count(void); +wchar_t arch_inst_symbol(uint8_t inst); +const char *arch_inst_mnemonic(uint8_t inst); + +// ARCH_EXPORT_INST_COUNT must be defined in arch_inst.c source files. +// This allows Python to read back the number of instructions for the selected +// VM architecture. +#if defined(ARCH_EXPORT_INST_COUNT) +int arch_inst_count(void) { + return ARCH_INST_COUNT; +} +#endif diff --git a/core/compress.c b/core/compress.c new file mode 100644 index 0000000..6ca72c6 --- /dev/null +++ b/core/compress.c @@ -0,0 +1,77 @@ +// file : core/compress.c +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] includes +// [section] definitions + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#include + +#include "compress.h" + +// ---------------------------------------------------------------------------- +// [section] definitions +// ---------------------------------------------------------------------------- +int comp_deflate(struct DeflateParams *params) { + assert(params); + assert(params->size); + assert(params->in); + assert(params->out); + + params->strm.zalloc = NULL; + params->strm.zfree = NULL; + params->strm.opaque = NULL; + + deflateInit(¶ms->strm, Z_DEFAULT_COMPRESSION); + + params->strm.avail_in = params->size; + params->strm.avail_out = params->size; + params->strm.next_in = params->in; + params->strm.next_out = params->out; + + deflate(¶ms->strm, Z_FINISH); + + return 0; +} + +int comp_inflate(struct InflateParams *params) { + assert(params); + assert(params->avail_in); + assert(params->size); + assert(params->in); + assert(params->out); + + params->strm.next_in = params->in; + params->strm.avail_in = params->avail_in; + params->strm.zalloc = NULL; + params->strm.zfree = NULL; + params->strm.opaque = NULL; + + inflateInit(¶ms->strm); + + params->strm.avail_out = params->size; + params->strm.next_out = params->out; + +#if defined(NDEBUG) + inflate(¶ms->strm, Z_FINISH); +#else + assert(inflate(¶ms->strm, Z_FINISH)); +#endif + + return 0; +} + +void comp_deflate_end(struct DeflateParams *params) { + assert(params); + deflateEnd(¶ms->strm); +} + +void comp_inflate_end(struct InflateParams *params) { + assert(params); + inflateEnd(¶ms->strm); +} diff --git a/core/compress.h b/core/compress.h new file mode 100644 index 0000000..4431eed --- /dev/null +++ b/core/compress.h @@ -0,0 +1,27 @@ +// file : core/compress.h +// project : Salis-VM +// author : Paul Oliver +// +// Utilities to help with zlib compression. + +#pragma once + +struct DeflateParams { + z_stream strm; + size_t size; + Bytef *in; + Bytef *out; +}; + +struct InflateParams { + z_stream strm; + size_t avail_in; + size_t size; + Bytef *in; + Bytef *out; +}; + +int comp_deflate(struct DeflateParams *params); +int comp_inflate(struct InflateParams *params); +void comp_deflate_end(struct DeflateParams *params); +void comp_inflate_end(struct InflateParams *params); diff --git a/core/logger.c b/core/logger.c new file mode 100644 index 0000000..29f371e --- /dev/null +++ b/core/logger.c @@ -0,0 +1,133 @@ +// file : core/logger.c +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] includes +// [section] macros +// [section] extern globals +// [section] definitions + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include + +#include "logger.h" + +// ---------------------------------------------------------------------------- +// [section] macros +// ---------------------------------------------------------------------------- +#define COLOR_CLEAR "\033[0m" +#define COLOR_TRACE "\033[1;34m" +#define COLOR_INFO "\033[1;32m" +#define COLOR_ATTENTION "\033[1;33m" + +#define LEVEL_TRACE "TRACE" +#define LEVEL_INFO "INFO" +#define LEVEL_ATTENTION "ATTENTION" + +// ---------------------------------------------------------------------------- +// [section] extern globals +// ---------------------------------------------------------------------------- +void (*log_trace)(const char *format, ...) = log_trace_to_stdout; +void (*log_info)(const char *format, ...) = log_info_to_stdout; +void (*log_attention)(const char *format, ...) = log_attention_to_stdout; + +// ---------------------------------------------------------------------------- +// [section] definitions +// ---------------------------------------------------------------------------- +static void log_to_buffer(char buffer[LOG_BUFFER_SIZE], const char *color_code, const char *level, const char *format, va_list args) { + assert(level); + assert(format); + + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + int msec = (int)(ts.tv_nsec / 1000000l); + struct tm tm = *localtime(&ts.tv_sec); + pid_t pid = getpid(); + + int rem = snprintf( + buffer, + LOG_BUFFER_SIZE, + "%s%d-%02d-%02d %02d:%02d:%02d.%03d %07d [%s]%s ", + color_code ? color_code : "", + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec, + msec, + pid, + level, + color_code ? COLOR_CLEAR : "" + ); + + vsnprintf(buffer + rem, LOG_BUFFER_SIZE - rem, format, args); +} + +void log_trace_to_stdout(const char *format, ...) { + assert(format); + (void)format; + +#if defined(LOG_TRACE) + va_list args; + va_start(args, format); + char buffer[LOG_BUFFER_SIZE]; + log_to_buffer(buffer, COLOR_TRACE, LEVEL_TRACE, format, args); + printf("\r%s\n", buffer); + fflush(stdout); + va_end(args); +#endif +} + +void log_info_to_stdout(const char *format, ...) { + assert(format); + va_list args; + va_start(args, format); + char buffer[LOG_BUFFER_SIZE]; + log_to_buffer(buffer, COLOR_INFO, LEVEL_INFO, format, args); + printf("\r%s\n", buffer); + fflush(stdout); + va_end(args); +} + +void log_attention_to_stdout(const char *format, ...) { + assert(format); + va_list args; + va_start(args, format); + char buffer[LOG_BUFFER_SIZE]; + log_to_buffer(buffer, COLOR_ATTENTION, LEVEL_ATTENTION, format, args); + printf("\r%s\n", buffer); + fflush(stdout); + va_end(args); +} + +void vlog_trace_to_buffer(char buffer[LOG_BUFFER_SIZE], const char *format, va_list args) { + assert(format); + assert(args); + (void)buffer; + (void)format; + (void)args; + +#if defined(LOG_TRACE) + log_to_buffer(buffer, NULL, LEVEL_TRACE, format, args); +#endif +} + +void vlog_info_to_buffer(char buffer[LOG_BUFFER_SIZE], const char *format, va_list args) { + assert(format); + assert(args); + log_to_buffer(buffer, NULL, LEVEL_INFO, format, args); +} + +void vlog_attention_to_buffer(char buffer[LOG_BUFFER_SIZE], const char *format, va_list args) { + assert(format); + assert(args); + log_to_buffer(buffer, NULL, LEVEL_ATTENTION, format, args); +} diff --git a/core/logger.h b/core/logger.h new file mode 100644 index 0000000..74b722d --- /dev/null +++ b/core/logger.h @@ -0,0 +1,24 @@ +// file : core/logger.h +// project : Salis-VM +// author : Paul Oliver +// +// Simple logging library. +// Functions are exported and used to log within Python. + +#pragma once + +#define LOG_BUFFER_SIZE 0x800 + +// UIs may install their own custom loggers. +// By default, library logs to stdout. +extern void (*log_trace)(const char *format, ...); +extern void (*log_info)(const char *format, ...); +extern void (*log_attention)(const char *format, ...); + +void log_trace_to_stdout(const char *format, ...); +void log_info_to_stdout(const char *format, ...); +void log_attention_to_stdout(const char *format, ...); + +void vlog_trace_to_buffer(char buffer[LOG_BUFFER_SIZE], const char *format, va_list args); +void vlog_info_to_buffer(char buffer[LOG_BUFFER_SIZE], const char *format, va_list args); +void vlog_attention_to_buffer(char buffer[LOG_BUFFER_SIZE], const char *format, va_list args); diff --git a/core/salis.c b/core/salis.c new file mode 100644 index 0000000..3c9a0e3 --- /dev/null +++ b/core/salis.c @@ -0,0 +1,1033 @@ +// file : core/salis.c +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] includes +// [section] macros +// [section] globals +// [section] memory vector functions +// [section] mutator functions +// [section] process functions +// [section] core functions +// [section] salis functions + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arch.h" +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" +#include "sql.h" +#include "timer.h" + +// ---------------------------------------------------------------------------- +// [section] macros +// ---------------------------------------------------------------------------- +#define STEP_GROUP_TIME_MIN 0.001f +#define STEP_GROUP_TIME_MAX 0.002f + +// ---------------------------------------------------------------------------- +// [section] globals +// ---------------------------------------------------------------------------- +struct Core g_cores[CORES]; +uint64_t g_step; +uint64_t g_sync; +const struct Proc g_null_proc; + +static char g_asav_pbuf[AUTOSAVE_NAME_LEN]; +static char g_evas_pbuf[EVA_SAVE_NAME_LEN]; + +static atomic_bool g_running; +static thrd_t g_thrd; + +// ---------------------------------------------------------------------------- +// [section] memory vector functions +// ---------------------------------------------------------------------------- +bool mvec_is_alloc(const struct Core *core, uint64_t addr) { + assert(core); + assert(addr < MVEC_SIZE); + return core->mvec[addr] & ARCH_INST_MALL_FLAG ? true : false; +} + +void mvec_alloc(struct Core *core, uint64_t addr) { + assert(core); + assert(addr < MVEC_SIZE); + assert(!mvec_is_alloc(core, addr)); + core->mvec[addr] |= ARCH_INST_MALL_FLAG; + core->aeva.data[addr]++; + core->mall++; +} + +void mvec_free(struct Core *core, uint64_t addr) { + assert(core); + assert(addr < MVEC_SIZE); + assert(mvec_is_alloc(core, addr)); + core->mvec[addr] ^= ARCH_INST_MALL_FLAG; + core->aeva.data[addr]++; + core->mall--; +} + +uint8_t mvec_get_inst(const struct Core *core, uint64_t addr) { + assert(core); + assert(addr < MVEC_SIZE); + return core->mvec[addr] & ARCH_INST_MASK; +} + +#if !defined(NDEBUG) +static uint8_t mvec_get_byte(const struct Core *core, uint64_t addr) { + assert(core); + assert(addr < MVEC_SIZE); + return core->mvec[addr]; +} +#endif + +void mvec_set_inst(struct Core *core, uint64_t addr, uint8_t inst) { + assert(core); + assert(addr < MVEC_SIZE); + assert(inst < ARCH_INST_COUNT); + core->mvec[addr] &= ARCH_INST_MALL_FLAG; + core->mvec[addr] |= inst; +} + +bool mvec_proc_is_live(const struct Core *core, uint64_t pix) { + assert(core); + return pix >= core->pfst && pix <= core->plst; +} + +bool mvec_is_in_mb0_of_proc(const struct Core *core, uint64_t addr, uint64_t pix) { + assert(core); + assert(addr < MVEC_SIZE); + assert(mvec_proc_is_live(core, pix)); + uint64_t mb0a = arch_proc_get_mb0_addr(core, pix); + uint64_t mb0s = arch_proc_get_mb0_size(core, pix); + return (addr >= mb0a) && (addr < (mb0a + mb0s)); +} + +bool mvec_is_in_mb1_of_proc(const struct Core *core, uint64_t addr, uint64_t pix) { + assert(core); + assert(addr < MVEC_SIZE); + assert(mvec_proc_is_live(core, pix)); + uint64_t mb1a = arch_proc_get_mb1_addr(core, pix); + uint64_t mb1s = arch_proc_get_mb1_size(core, pix); + return (addr >= mb1a) && (addr < (mb1a + mb1s)); +} + +bool mvec_is_proc_owner(const struct Core *core, uint64_t addr, uint64_t pix) { + assert(core); + assert(addr < MVEC_SIZE); + assert(mvec_proc_is_live(core, pix)); + return mvec_is_in_mb0_of_proc(core, addr, pix) || mvec_is_in_mb1_of_proc(core, addr, pix); +} + +uint64_t mvec_get_owner(const struct Core *core, uint64_t addr) { + assert(core); + assert(addr < MVEC_SIZE); + assert(mvec_is_alloc(core, addr)); + + for (uint64_t pix = core->pfst; pix <= core->plst; pix++) { + if (mvec_is_proc_owner(core, addr, pix)) { + return pix; + } + } + + assert(false); + return 0; +} + +// ---------------------------------------------------------------------------- +// [section] mutator functions +// ---------------------------------------------------------------------------- +// The following is an implementation of the SplitMix64 PRNG. +// It's used to generate cosmic-ray events (mutations): +// https://en.wikipedia.org/wiki/Xorshift +#if SEED != 0 +#if defined(COMMAND_NEW) +static uint64_t muta_smix(uint64_t *seed) { + assert(seed); + uint64_t next = (*seed += 0x9e3779b97f4a7c15); + next = (next ^ (next >> 30)) * 0xbf58476d1ce4e5b9; + next = (next ^ (next >> 27)) * 0x94d049bb133111eb; + return next ^ (next >> 31); +} +#endif + +static uint64_t muta_ro64(uint64_t x, int k) { + return (x << k) | (x >> (64 - k)); +} + +uint64_t muta_next(struct Core *core) { + assert(core); + uint64_t r = muta_ro64(core->muta[1] * 5, 7) * 9; + uint64_t t = core->muta[1] << 17; + core->muta[2] ^= core->muta[0]; + core->muta[3] ^= core->muta[1]; + core->muta[1] ^= core->muta[2]; + core->muta[0] ^= core->muta[3]; + core->muta[2] ^= t; + core->muta[3] = muta_ro64(core->muta[3], 45); + return r; +} + +void muta_cosmic_ray(struct Core *core) { + assert(core); + uint64_t a = muta_next(core) % MUTA_RANGE; + uint64_t b = muta_next(core); + + if (a < MVEC_SIZE) { + mvec_set_inst(core, a, b & ARCH_INST_MASK); + } +} +#endif + +// ---------------------------------------------------------------------------- +// [section] process functions +// ---------------------------------------------------------------------------- +void proc_new(struct Core *core, const struct Proc *proc) { + assert(core); + assert(proc); + + if (core->pnum == core->pcap) { + // Reallocate circular array + uint64_t new_pcap = core->pcap * 2; + struct Proc *new_pvec = calloc(new_pcap, sizeof(struct Proc)); + + for (uint64_t pix = core->pfst; pix <= core->plst; pix++) { + uint64_t iold = pix % core->pcap; + uint64_t inew = pix % new_pcap; + memcpy(&new_pvec[inew], &core->pvec[iold], sizeof(struct Proc)); + } + + free(core->pvec); + core->pcap = new_pcap; + core->pvec = new_pvec; + } + + core->pnum++; + core->plst++; + memcpy(&core->pvec[core->plst % core->pcap], proc, sizeof(struct Proc)); + + // Register process birth event + uint64_t child_addr = arch_proc_get_mb0_addr(core, core->plst); + uint64_t child_size = arch_proc_get_mb0_size(core, core->plst); + + for (uint64_t i = 0; i < child_size; i++) { + core->beva.data[child_addr + i]++; + } +} + +void proc_kill(struct Core *core) { + assert(core); + assert(core->pnum > 1); + arch_on_proc_kill(core); + core->pcur++; + core->pfst++; + core->pnum--; +} + +const struct Proc *proc_get(const struct Core *core, uint64_t pix) { + assert(core); + + if (mvec_proc_is_live(core, pix)) { + return &core->pvec[pix % core->pcap]; + } else { + return &g_null_proc; + } +} + +struct Proc *proc_fetch(struct Core *core, uint64_t pix) { + assert(core); + assert(mvec_proc_is_live(core, pix)); + return &core->pvec[pix % core->pcap]; +} + +// ---------------------------------------------------------------------------- +// [section] core functions +// ---------------------------------------------------------------------------- +static void core_save(const struct Core *core, FILE *f) { + assert(core); + assert(f); + + fwrite(&core->step, sizeof(uint64_t), 1, f); + fwrite(&core->cycl, sizeof(uint64_t), 1, f); + fwrite(&core->mall, sizeof(uint64_t), 1, f); + fwrite(core->muta, sizeof(uint64_t), 4, f); + fwrite(&core->pnum, sizeof(uint64_t), 1, f); + fwrite(&core->pcap, sizeof(uint64_t), 1, f); + fwrite(&core->pfst, sizeof(uint64_t), 1, f); + fwrite(&core->plst, sizeof(uint64_t), 1, f); + fwrite(&core->pcur, sizeof(uint64_t), 1, f); + fwrite(&core->psli, sizeof(uint64_t), 1, f); + fwrite(&core->ivpt, sizeof(uint64_t), 1, f); + fwrite(core->ivav, sizeof(uint64_t), SYNC_INTERVAL, f); + fwrite(core->iviv, sizeof(uint8_t), SYNC_INTERVAL, f); + + fwrite(core->mvec, sizeof(uint8_t), MVEC_SIZE, f); + fwrite(core->pvec, sizeof(struct Proc), core->pcap, f); + + fwrite(&core->amb0, sizeof(double), 1, f); + fwrite(&core->amb1, sizeof(double), 1, f); + fwrite(&core->emb0, sizeof(uint64_t), 1, f); + fwrite(&core->emb1, sizeof(uint64_t), 1, f); + fwrite(&core->eliv, sizeof(uint64_t), 1, f); + fwrite(&core->edea, sizeof(uint64_t), 1, f); + + fwrite(core->aeva.data, sizeof(uint64_t), MVEC_SIZE, f); + fwrite(core->eeva.data, sizeof(uint64_t), MVEC_SIZE, f); + fwrite(core->beva.data, sizeof(uint64_t), MVEC_SIZE, f); + + arch_core_save(core, f); +} + +void core_event_array_init(struct EventArray *eva) { + assert(eva); + eva->params.size = sizeof(uint64_t) * MVEC_SIZE; + eva->params.in = (Bytef *)eva->data; + eva->params.out = (Bytef *)eva->comp; +} + +#if defined(COMMAND_NEW) +static void core_assemble_ancestor(struct Core *core) { + assert(core); + uint64_t addr = 0; + uint8_t anc_bytes[] = ANC_BYTES; + + for (uint64_t i = 0; i < ANC_SIZE; i++, addr++) { + for (uint64_t j = 0; j < CLONES; j++) { + uint64_t addr_clone = addr + (MVEC_SIZE / CLONES) * j; + mvec_alloc(core, addr_clone); + mvec_set_inst(core, addr_clone, anc_bytes[i]); + } + } +} + +static void core_init(struct Core *core, uint64_t *seed) { + assert(core); + assert(seed); + +#if SEED != 0 + assert(*seed); + core->muta[0] = muta_smix(seed); + core->muta[1] = muta_smix(seed); + core->muta[2] = muta_smix(seed); + core->muta[3] = muta_smix(seed); +#else + (void)seed; +#endif + + core->pnum = CLONES; + core->pcap = CLONES; + core->plst = CLONES - 1; + core->psli = arch_proc_get_slice(core, core->pcur); + core->ivav = calloc(SYNC_INTERVAL, sizeof(uint64_t)); + core->iviv = calloc(SYNC_INTERVAL, sizeof(uint8_t)); + core->pvec = calloc(core->pcap, sizeof(struct Proc)); + assert(core->pvec); + + core_event_array_init(&core->aeva); + core_event_array_init(&core->eeva); + core_event_array_init(&core->beva); + + core_assemble_ancestor(core); + arch_core_init(core); +} +#endif + +#if defined(COMMAND_LOAD) +static void core_load(struct Core *core, FILE *f) { + assert(core); + assert(f); + + fread(&core->step, sizeof(uint64_t), 1, f); + fread(&core->cycl, sizeof(uint64_t), 1, f); + fread(&core->mall, sizeof(uint64_t), 1, f); + fread(core->muta, sizeof(uint64_t), 4, f); + fread(&core->pnum, sizeof(uint64_t), 1, f); + fread(&core->pcap, sizeof(uint64_t), 1, f); + fread(&core->pfst, sizeof(uint64_t), 1, f); + fread(&core->plst, sizeof(uint64_t), 1, f); + fread(&core->pcur, sizeof(uint64_t), 1, f); + fread(&core->psli, sizeof(uint64_t), 1, f); + fread(&core->ivpt, sizeof(uint64_t), 1, f); + + core->ivav = calloc(SYNC_INTERVAL, sizeof(uint64_t)); + core->iviv = calloc(SYNC_INTERVAL, sizeof(uint8_t)); + core->pvec = calloc(core->pcap, sizeof(struct Proc)); + assert(core->ivav); + assert(core->iviv); + assert(core->pvec); + + fread(core->ivav, sizeof(uint64_t), SYNC_INTERVAL, f); + fread(core->iviv, sizeof(uint8_t), SYNC_INTERVAL, f); + fread(core->mvec, sizeof(uint8_t), MVEC_SIZE, f); + fread(core->pvec, sizeof(struct Proc), core->pcap, f); + + fread(&core->amb0, sizeof(double), 1, f); + fread(&core->amb1, sizeof(double), 1, f); + fread(&core->emb0, sizeof(uint64_t), 1, f); + fread(&core->emb1, sizeof(uint64_t), 1, f); + fread(&core->eliv, sizeof(uint64_t), 1, f); + fread(&core->edea, sizeof(uint64_t), 1, f); + + fread(core->aeva.data, sizeof(uint64_t), MVEC_SIZE, f); + fread(core->eeva.data, sizeof(uint64_t), MVEC_SIZE, f); + fread(core->beva.data, sizeof(uint64_t), MVEC_SIZE, f); + + core_event_array_init(&core->aeva); + core_event_array_init(&core->eeva); + core_event_array_init(&core->beva); + + arch_core_load(core, f); +} +#endif + +static void core_pull_ipcm(struct Core *core) { + assert(core); + assert(core->ivpt < SYNC_INTERVAL); + uint8_t *iinst = &core->iviv[core->ivpt]; + uint64_t *iaddr = &core->ivav[core->ivpt]; + + if ((*iinst & ARCH_INST_IPCM_FLAG) != 0) { + mvec_set_inst(core, *iaddr, *iinst & ARCH_INST_MASK); + *iinst = 0; + *iaddr = 0; + } + + assert(*iinst == 0); + assert(*iaddr == 0); +} + +void core_push_ipcm(struct Core *core, uint8_t inst, uint64_t addr) { + assert(core); + assert(core->ivpt < SYNC_INTERVAL); + assert((inst & ARCH_INST_IPCM_FLAG) == 0); + uint8_t *iinst = &core->iviv[core->ivpt]; + uint64_t *iaddr = &core->ivav[core->ivpt]; + assert(*iinst == 0); + assert(*iaddr == 0); + *iinst = inst | ARCH_INST_IPCM_FLAG; + *iaddr = addr; +} + +static void core_step(struct Core *core) { + assert(core); + assert(core->psli); + assert(mvec_proc_is_live(core, core->pcur)); + + core_pull_ipcm(core); + core->ivpt++; + core->ivpt %= SYNC_INTERVAL; + + // Register execution event + uint64_t pcur_ip = arch_proc_get_ip_addr(core, core->pcur); + + if (pcur_ip < MVEC_SIZE) { + if (mvec_is_in_mb0_of_proc(core, pcur_ip, core->pcur)) { + core->emb0++; + } else if (mvec_is_in_mb1_of_proc(core, pcur_ip, core->pcur)) { + core->emb1++; + } else if (mvec_is_alloc(core, pcur_ip)) { + core->eliv++; + } else { + assert(!mvec_is_in_mb0_of_proc(core, pcur_ip, core->pcur)); + assert(!mvec_is_in_mb1_of_proc(core, pcur_ip, core->pcur)); + assert(!mvec_is_alloc(core, pcur_ip)); + core->edea++; + } + + core->eeva.data[pcur_ip]++; + } + + // Step process and decrement time slice counter + arch_on_proc_step(core, core->pcur); + core->step++; + core->psli--; + + if (core->psli) { + return; + } + + // Time slice of process has ended; start time slice of next process + bool cycle_ended = core->pcur == core->plst; + core->pcur = cycle_ended ? core->pfst : core->pcur + 1; + core->psli = arch_proc_get_slice(core, core->pcur); + + if (cycle_ended) { + // Run the reaper at the end of every cycle + // Kill old processes until at least 50% of memory is not allocated + while (core->mall > MVEC_SIZE / 2 && core->pnum > 1) { + proc_kill(core); + } + +#if SEED != 0 + muta_cosmic_ray(core); +#endif + + core->cycl++; + } +} + +static int core_thread(struct Core *core) { + assert(core); + assert(core->step == g_step); + struct Timer timer; + uint64_t step_group_size = 1; + + // Minimize calls to atomic_load_explicit by grouping calls + // to core_step. Check state of atomic variable once per period time_delta, + // where STEP_GROUP_TIME_MIN < time_delta < STEP_GROUP_TIME_MAX. + // Do-while ensures each thread calls core_step at least once. + do { + timer_reset(&timer); + + for (uint64_t i = 0; i < step_group_size; i++) { + core_step(core); + + if (core->step % SYNC_INTERVAL == 0) { + log_trace("Exiting core thread at step %#lx: sync-interval reached", core->step); + log_trace("Step group size: %#lx", step_group_size); + return 0; + } + } + + timer_measure(&timer); + + if (timer.time_delta < STEP_GROUP_TIME_MIN) { + step_group_size *= 2; + } else if (timer.time_delta > STEP_GROUP_TIME_MAX && step_group_size > 1) { + step_group_size /= 2; + } + } while (atomic_load_explicit(&g_running, memory_order_relaxed)); + + log_trace("Exiting core thread at step %#lx: simulation paused", core->step); + log_trace("Step group size: %#lx", step_group_size); + return 0; +} + +#if !defined(NDEBUG) +static void core_validate(struct Core *core) { + assert(core); + + // Validate core fields + assert(core->step == g_step); + assert(core->cycl <= g_step); + assert(core->pnum <= core->pcap); + assert(core->pnum == core->plst + 1 - core->pfst); + assert(core->plst >= core->pfst); + assert(core->pcur >= core->pfst && core->pcur <= core->plst); + assert(core->psli != 0); + + uint64_t mall = 0; + + for (uint64_t i = 0; i < MVEC_SIZE; i++) { + mall += mvec_is_alloc(core, i) ? 1 : 0; + assert((mvec_get_byte(core, i) & ARCH_INST_UNUSED_BITS) == 0); + } + + assert(core->mall == mall); + + // Validate IPCM state + for (uint64_t i = 0; i < SYNC_INTERVAL; i++) { + uint8_t iinst = core->iviv[i]; + + if ((iinst & ARCH_INST_IPCM_FLAG) == 0) { + uint64_t iaddr = core->ivav[i]; + assert(iinst == 0); + assert(iaddr == 0); + } + } + + assert(core->ivpt == g_step % SYNC_INTERVAL); + + // Validate VM architecture specifics + arch_validate_core(core); +} +#endif + +static void core_free(struct Core *core) { + assert(core); + assert(core->ivav); + assert(core->iviv); + assert(core->pvec); + + arch_core_free(core); + + free(core->pvec); + free(core->iviv); + free(core->ivav); + + core->pvec = NULL; + core->iviv = NULL; + core->ivav = NULL; +} + +// ---------------------------------------------------------------------------- +// [section] salis functions +// ---------------------------------------------------------------------------- +static void salis_sync(void) { + assert(g_step % SYNC_INTERVAL == 0); + log_trace("Synchronizing cores @step: %#lx; @sync: %#lx", g_step, g_sync); + +#if !defined(NDEBUG) + for (int i = 0; i < CORES; i++) { + assert(g_cores[i].ivpt == 0); + } +#endif + + uint8_t *iviv0 = g_cores[0].iviv; + uint64_t *ivav0 = g_cores[0].ivav; + + for (int i = 1; i < CORES; i++) { + g_cores[i - 1].iviv = g_cores[i].iviv; + g_cores[i - 1].ivav = g_cores[i].ivav; + } + + g_cores[CORES - 1].iviv = iviv0; + g_cores[CORES - 1].ivav = ivav0; + + for (int i = 0; i < CORES; i++) { + g_cores[i].ivpt = 0; + } + + g_sync++; +} + +static void salis_save(const char *path) { + assert(path); + log_info("Saving simulation state to '%s' on step %#lx", path, g_step); + + size_t size = 0; + char *in = NULL; + FILE *f = open_memstream(&in, &size); + assert(f); + + for (int i = 0; i < CORES; i++) { + core_save(&g_cores[i], f); + } + + fwrite(&g_step, sizeof(uint64_t), 1, f); + fwrite(&g_sync, sizeof(uint64_t), 1, f); + fclose(f); + + char *out = malloc(size); + assert(size); + assert(out); + + struct DeflateParams params = { + .size = size, + .in = (Bytef *)in, + .out = (Bytef *)out, + }; + + comp_deflate(¶ms); + FILE *fx = fopen(path, "wb"); + assert(fx); + fwrite(&size, sizeof(size_t), 1, fx); + fwrite(out, sizeof(char), params.strm.total_out, fx); + fclose(fx); + comp_deflate_end(¶ms); + + free(in); + free(out); +} + +static void salis_auto_save(void) { + assert(g_step % AUTOSAVE_INTERVAL == 0); + int rem = snprintf(g_asav_pbuf, AUTOSAVE_NAME_LEN, "%s-%016lx", SIM_PATH, g_step); + assert(rem >= 0); + assert(rem < AUTOSAVE_NAME_LEN); + (void)rem; + salis_save(g_asav_pbuf); +} + +#if defined(COMMAND_NEW) +static void salis_push_data_header(void) { + assert(g_sim_db); + log_info("Creating core table in SQLite database"); + + // Empty blob columns help the data-server stay agnostic of DB schema. + // These represent EVA data that will be appended by data server. + sql_exec( + NULL, NULL, + "create table core (" +#define FOR_CORE(i) \ + "step_" #i " int not null, " \ + "cycl_" #i " int not null, " \ + "mall_" #i " int not null, " \ + "pnum_" #i " int not null, " \ + "pfst_" #i " int not null, " \ + "plst_" #i " int not null, " \ + "amb0_" #i " real not null, " \ + "amb1_" #i " real not null, " \ + "emb0_" #i " int not null, " \ + "emb1_" #i " int not null, " \ + "eliv_" #i " int not null, " \ + "edea_" #i " int not null, " \ + "aev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i " int not null, aev_" #i " blob, " \ + "eev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i " int not null, eev_" #i " blob, " \ + "bev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i " int not null, bev_" #i " blob, " + FOR_CORES +#undef FOR_CORE + "step int not null" + ");" + ); + + arch_push_data_header(); +} +#endif + +static int salis_measure_ambs_thread(struct Core *core) { + assert(core); + assert(core->amb0 == 0.); + assert(core->amb1 == 0.); + + for (uint64_t j = core->pfst; j <= core->plst; j++) { + core->amb0 += (double)arch_proc_get_mb0_size(core, j); + core->amb1 += (double)arch_proc_get_mb1_size(core, j); + } + + core->amb0 /= core->pnum; + core->amb1 /= core->pnum; + return 0; +} + +static void salis_prepare_data_line(void) { + assert(g_sim_db); + + // Measure average memory block sizes and compress event-arrays in parallel + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_create(&core->amb_thrd, (thrd_start_t)salis_measure_ambs_thread, core); + thrd_create(&core->aeva.thrd, (thrd_start_t)comp_deflate, &core->aeva.params); + thrd_create(&core->eeva.thrd, (thrd_start_t)comp_deflate, &core->eeva.params); + thrd_create(&core->beva.thrd, (thrd_start_t)comp_deflate, &core->beva.params); + } + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_join(core->amb_thrd, NULL); + thrd_join(core->aeva.thrd, NULL); + thrd_join(core->eeva.thrd, NULL); + thrd_join(core->beva.thrd, NULL); + core->aeva.blob_size = core->aeva.params.strm.total_out; + core->eeva.blob_size = core->eeva.params.strm.total_out; + core->beva.blob_size = core->beva.params.strm.total_out; + } + + arch_prepare_data_line(); +} + +static void salis_push_data_line(void) { + assert(g_sim_db); + assert(g_step % DATA_PUSH_INTERVAL == 0); + log_info("Pushing row to core table in SQLite database"); + sql_exec( + NULL, NULL, + "insert into core (" +#define FOR_CORE(i) \ + "step_" #i ", " \ + "cycl_" #i ", " \ + "mall_" #i ", " \ + "pnum_" #i ", " \ + "pfst_" #i ", " \ + "plst_" #i ", " \ + "amb0_" #i ", " \ + "amb1_" #i ", " \ + "emb0_" #i ", " \ + "emb1_" #i ", " \ + "eliv_" #i ", " \ + "edea_" #i ", " \ + "aev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i ", " \ + "eev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i ", " \ + "bev" SALIS_EVENT_ARRAY_SIZE_COL_MARKER #i ", " + FOR_CORES +#undef FOR_CORE + "step" + ") values (" +#define FOR_CORE(i) \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%f, " \ + "%f, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " \ + "%ld, " + FOR_CORES +#undef FOR_CORE + "%ld" + ");", +#define FOR_CORE(i) \ + g_cores[i].step, \ + g_cores[i].cycl, \ + g_cores[i].mall, \ + g_cores[i].pnum, \ + g_cores[i].pfst, \ + g_cores[i].plst, \ + g_cores[i].amb0, \ + g_cores[i].amb1, \ + g_cores[i].emb0, \ + g_cores[i].emb1, \ + g_cores[i].eliv, \ + g_cores[i].edea, \ + g_cores[i].aeva.blob_size, \ + g_cores[i].eeva.blob_size, \ + g_cores[i].beva.blob_size, + FOR_CORES +#undef FOR_CORE + g_step + ); + + // Store EVA data + int rem = snprintf(g_evas_pbuf, EVA_SAVE_NAME_LEN, "%s/evas-%016lx", SIM_EVAS, g_step); + assert(rem >= 0); + assert(rem < EVA_SAVE_NAME_LEN); + (void)rem; + + log_info("Saving event-array data to file: %s", g_evas_pbuf); + FILE *eva_file = fopen(g_evas_pbuf, "wb"); + assert(eva_file); + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + fwrite(core->aeva.comp, sizeof(Bytef), core->aeva.blob_size, eva_file); + fwrite(core->eeva.comp, sizeof(Bytef), core->eeva.blob_size, eva_file); + fwrite(core->beva.comp, sizeof(Bytef), core->beva.blob_size, eva_file); + comp_deflate_end(&core->aeva.params); + comp_deflate_end(&core->eeva.params); + comp_deflate_end(&core->beva.params); + } + + // Reset data aggregation fields + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + core->amb0 = 0.; + core->amb1 = 0.; + core->emb0 = 0; + core->emb1 = 0; + core->eliv = 0; + core->edea = 0; + memset(core->aeva.data, 0, sizeof(uint64_t) * MVEC_SIZE); + memset(core->eeva.data, 0, sizeof(uint64_t) * MVEC_SIZE); + memset(core->beva.data, 0, sizeof(uint64_t) * MVEC_SIZE); + } + + arch_push_data_line(eva_file); + fclose(eva_file); +} + +#if defined(COMMAND_NEW) +void salis_init(void) { + log_info("Creating a new simulation"); + + uint64_t seed = SEED; + + for (int i = 0; i < CORES; i++) { + core_init(&g_cores[i], &seed); + } + + sql_open(); + salis_push_data_header(); + salis_prepare_data_line(); + salis_push_data_line(); + salis_auto_save(); +} +#endif + +#if defined(COMMAND_LOAD) +void salis_load(void) { + log_info("Loading simulation from: %s", SIM_PATH); + FILE *fx = fopen(SIM_PATH, "rb"); + assert(fx); + fseek(fx, 0, SEEK_END); + size_t x_size = ftell(fx) - sizeof(size_t); + char *in = malloc(x_size); + rewind(fx); + assert(x_size); + assert(in); + + size_t size = 0; + fread(&size, sizeof(size_t), 1, fx); + fread(in, 1, x_size, fx); + fclose(fx); + assert(size); + char *out = malloc(size); + assert(out); + + struct InflateParams params = { + .avail_in = x_size, + .size = size, + .in = (Bytef *)in, + .out = (Bytef *)out, + }; + + comp_inflate(¶ms); + comp_inflate_end(¶ms); + FILE *f = fmemopen(out, size, "rb"); + assert(f); + + for (int i = 0; i < CORES; i++) { + core_load(&g_cores[i], f); + } + + fread(&g_step, sizeof(uint64_t), 1, f); + fread(&g_sync, sizeof(uint64_t), 1, f); + fclose(f); + free(in); + free(out); + + sql_open(); +} +#endif + +static void salis_check_intervals(void) { + if (g_step % SYNC_INTERVAL == 0) { + salis_sync(); + } + + if (g_step % AUTOSAVE_INTERVAL == 0) { + salis_auto_save(); + } + + if (g_step % DATA_PUSH_INTERVAL == 0) { + salis_prepare_data_line(); + salis_push_data_line(); + } +} + +#if !defined(NDEBUG) +static void salis_validate(void) { + log_trace("Validating simulation state"); + assert(g_step / SYNC_INTERVAL == g_sync); + + for (int i = 0; i < CORES; i++) { + core_validate(&g_cores[i]); + } +} +#endif + +static int salis_thread(void *data) { + assert(!data); + assert(atomic_load_explicit(&g_running, memory_order_relaxed)); + (void)data; + + while (atomic_load_explicit(&g_running, memory_order_relaxed)) { + log_trace("Launching core threads"); + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_create(&core->thrd, (thrd_start_t)core_thread, core); + } + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + thrd_join(core->thrd, NULL); + } + + // Align step-count on all cores + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + + if (core->step > g_step) { + g_step = core->step; + } + } + + for (int i = 0; i < CORES; i++) { + struct Core *core = &g_cores[i]; + + while (core->step < g_step) { + core_step(core); + } + } + + salis_check_intervals(); + +#if !defined(NDEBUG) + salis_validate(); +#endif + } + + return 0; +} + +void salis_start(void) { + assert(!atomic_load_explicit(&g_running, memory_order_relaxed)); + log_trace("Launching main thread"); + atomic_store_explicit(&g_running, true, memory_order_relaxed); + thrd_create(&g_thrd, (thrd_start_t)salis_thread, NULL); +} + +void salis_join(void) { + assert(atomic_load_explicit(&g_running, memory_order_relaxed)); + log_trace("Waiting for main thread"); + thrd_join(g_thrd, NULL); +} + +void salis_wait(void) { + assert(!atomic_load_explicit(&g_running, memory_order_relaxed)); + log_trace("Waiting for main thread"); + thrd_join(g_thrd, NULL); +} + +void salis_signal_stop(void) { + assert(atomic_load_explicit(&g_running, memory_order_relaxed)); + log_trace("Signaling main thread to stop"); + atomic_store_explicit(&g_running, false, memory_order_relaxed); +} + +void salis_signal_stop_no_check(void) { + log_trace("Signaling main thread to stop"); + atomic_store_explicit(&g_running, false, memory_order_relaxed); +} + +void salis_step(uint64_t steps) { + assert(!atomic_load_explicit(&g_running, memory_order_relaxed)); + log_trace("Stepping simulation %lu times", steps); + + for (uint64_t i = 0; i < steps; i++) { + for (int i = 0; i < CORES; i++) { + core_step(&g_cores[i]); + } + + g_step++; + salis_check_intervals(); + } + +#if !defined(NDEBUG) + salis_validate(); +#endif +} + +void salis_save_and_free(void) { + assert(!atomic_load_explicit(&g_running, memory_order_relaxed)); + salis_save(SIM_PATH); + + log_info("Freeing simulation resources"); + sql_close(); + + for (int i = 0; i < CORES; i++) { + core_free(&g_cores[i]); + } +} diff --git a/core/salis.h b/core/salis.h new file mode 100644 index 0000000..af635af --- /dev/null +++ b/core/salis.h @@ -0,0 +1,142 @@ +// file : core/salis.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +// index: +// [section] macros +// [section] structs +// [section] extern globals +// [section] memory vector declarations +// [section] mutator declarations +// [section] process declarations +// [section] core declarations +// [section] salis declarations + +// ---------------------------------------------------------------------------- +// [section] macros +// ---------------------------------------------------------------------------- +#define SALIS_EVENT_ARRAY_SIZE_COL_MARKER "_evasize_" +#define SALIS_UINT64_HALF 0x8000000000000000ul + +// ---------------------------------------------------------------------------- +// [section] structs +// ---------------------------------------------------------------------------- +struct Proc { +#define ARCH_SPEC_PROC_FIELD(type, name) type name; + ARCH_SPEC_PROC_FIELDS +#undef ARCH_SPEC_PROC_FIELD +}; + +struct EventArray { + uint64_t blob_size; + uint64_t data[MVEC_SIZE]; + uint64_t comp[MVEC_SIZE]; + struct DeflateParams params; + thrd_t thrd; +}; + +struct Core { + uint64_t step; + uint64_t cycl; + uint64_t mall; + uint64_t muta[4]; + + uint64_t pnum; + uint64_t pcap; + uint64_t pfst; + uint64_t plst; + uint64_t pcur; + uint64_t psli; + + uint64_t ivpt; + uint64_t *ivav; + uint8_t *iviv; + + uint8_t mvec[MVEC_SIZE]; // the world + struct Proc *pvec; // the organisms + + // Data aggregation fields + thrd_t amb_thrd; + double amb0; + double amb1; + + uint64_t emb0; + uint64_t emb1; + uint64_t eliv; + uint64_t edea; + + struct EventArray aeva; + struct EventArray eeva; + struct EventArray beva; + + // VM architecture specific fields +#define ARCH_SPEC_CORE_FIELD(type, name) type name; + ARCH_SPEC_CORE_FIELDS +#undef ARCH_SPEC_CORE_FIELD + + thrd_t thrd; +}; + +// ---------------------------------------------------------------------------- +// [section] extern globals +// ---------------------------------------------------------------------------- +extern struct Core g_cores[CORES]; +extern uint64_t g_step; +extern uint64_t g_sync; +extern const struct Proc g_null_proc; + +// ---------------------------------------------------------------------------- +// [section] memory vector declarations +// ---------------------------------------------------------------------------- +bool mvec_is_alloc(const struct Core *core, uint64_t addr); +void mvec_alloc(struct Core *core, uint64_t addr); +void mvec_free(struct Core *core, uint64_t addr); + +uint8_t mvec_get_inst(const struct Core *core, uint64_t addr); +void mvec_set_inst(struct Core *core, uint64_t addr, uint8_t inst); +bool mvec_proc_is_live(const struct Core *core, uint64_t pix); +bool mvec_is_in_mb0_of_proc(const struct Core *core, uint64_t addr, uint64_t pix); +bool mvec_is_in_mb1_of_proc(const struct Core *core, uint64_t addr, uint64_t pix); +bool mvec_is_proc_owner(const struct Core *core, uint64_t addr, uint64_t pix); +uint64_t mvec_get_owner(const struct Core *core, uint64_t addr); + +// ---------------------------------------------------------------------------- +// [section] mutator declarations +// ---------------------------------------------------------------------------- +uint64_t muta_next(struct Core *core); +void muta_cosmic_ray(struct Core *core); + +// ---------------------------------------------------------------------------- +// [section] process declarations +// ---------------------------------------------------------------------------- +void proc_new(struct Core *core, const struct Proc *proc); +void proc_kill(struct Core *core); +const struct Proc *proc_get(const struct Core *core, uint64_t pix); +struct Proc *proc_fetch(struct Core *core, uint64_t pix); + +// ---------------------------------------------------------------------------- +// [section] core declarations +// ---------------------------------------------------------------------------- +void core_event_array_init(struct EventArray *eva); +void core_push_ipcm(struct Core *core, uint8_t inst, uint64_t addr); + +// ---------------------------------------------------------------------------- +// [section] salis declarations +// ---------------------------------------------------------------------------- +#if defined(COMMAND_NEW) +void salis_init(void); +#endif + +#if defined(COMMAND_LOAD) +void salis_load(void); +#endif + +void salis_start(void); +void salis_join(void); +void salis_wait(void); +void salis_signal_stop(void); +void salis_signal_stop_no_check(void); +void salis_step(uint64_t steps); +void salis_save_and_free(void); diff --git a/core/sql.c b/core/sql.c new file mode 100644 index 0000000..59fc38e --- /dev/null +++ b/core/sql.c @@ -0,0 +1,105 @@ +// file : core/sql.c +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] includes +// [section] macros +// [section] globals +// [section] definitions + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include + +#include "logger.h" +#include "sql.h" + +// ---------------------------------------------------------------------------- +// [section] macros +// ---------------------------------------------------------------------------- +#define DATA_PUSH_BUSY_TIMEOUT 600000 + +// ---------------------------------------------------------------------------- +// [section] globals +// ---------------------------------------------------------------------------- +sqlite3 *g_sim_db; + +// ---------------------------------------------------------------------------- +// [section] definitions +// ---------------------------------------------------------------------------- +void sql_open(void) { + sqlite3_open(DATA_PUSH_PATH, &g_sim_db); + assert(g_sim_db); + + // Install busy handler to retry transactions if DB is locked + sqlite3_busy_timeout(g_sim_db, DATA_PUSH_BUSY_TIMEOUT); + + // Enable Write-Ahead Logging (WAL) + // This seems to help prevent DB locks when displaying live data. + // See: https://sqlite.org/wal.html + sql_exec(NULL, NULL, "pragma journal_mode=wal;"); +} + +void sql_close(void) { + assert(g_sim_db); + sqlite3_close(g_sim_db); +} + +void sql_exec(void (*callback)(sqlite3_stmt *sql_stmt, void *data), void *data, const char *sql_format, ...) { + assert(sql_format); + + va_list args; + va_start(args, sql_format); + int sql_len = vsnprintf(NULL, 0, sql_format, args) + 1; + char *sql_str = malloc(sql_len); + assert(sql_str); + va_end(args); + + va_start(args, sql_format); + vsprintf(sql_str, sql_format, args); + va_end(args); + + int sql_res; + sqlite3_stmt *sql_stmt; + + sql_res = sqlite3_prepare_v2(g_sim_db, sql_str, -1, &sql_stmt, NULL); + assert(sql_res == SQLITE_OK); + free(sql_str); + + while (true) { + sql_res = sqlite3_step(sql_stmt); + + if (sql_res == SQLITE_ROW) { + if (callback) { + callback(sql_stmt, data); + } + + continue; + } + + if (sql_res == SQLITE_DONE) { + break; + } + + log_attention("SQLite database returned error %d with message:", sql_res); + log_attention(sqlite3_errmsg(g_sim_db)); + + // Only handle SQLITE_BUSY error, in which case we retry the query. + // Setting 'journal_mode=wal;' should help prevent busy database errors. + if (sql_res == SQLITE_BUSY) { + log_info("Will retry query"); + continue; + } + + assert(false); + } + + sqlite3_finalize(sql_stmt); +} diff --git a/core/sql.h b/core/sql.h new file mode 100644 index 0000000..0fc6295 --- /dev/null +++ b/core/sql.h @@ -0,0 +1,11 @@ +// file : core/sql.h +// project : Salis-VM +// author : Paul Oliver + +#pragma once + +extern sqlite3 *g_sim_db; + +void sql_open(void); +void sql_close(void); +void sql_exec(void (*callback)(sqlite3_stmt *sql_stmt, void *data), void *data, const char *sql_format, ...); diff --git a/core/timer.c b/core/timer.c new file mode 100644 index 0000000..a634a9b --- /dev/null +++ b/core/timer.c @@ -0,0 +1,43 @@ +// file : core/timer.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" +#include "timer.h" + +void timer_reset(struct Timer *timer) { + assert(timer); + clock_gettime(CLOCK_MONOTONIC_COARSE, &timer->time_start); + timer->time_delta = 0.f; +} + +void timer_measure(struct Timer *timer) { + assert(timer); + struct timespec time_end; + clock_gettime(CLOCK_MONOTONIC_COARSE, &time_end); + timer->time_delta = (float)(time_end.tv_sec - timer->time_start.tv_sec); + timer->time_delta += (float)(time_end.tv_nsec - timer->time_start.tv_nsec) / 1000000000.f; +} + +void timer_sps_reset(struct TimerSPS *timer_sps) { + assert(timer_sps); + timer_reset(&timer_sps->timer); + timer_sps->step_start = g_step; + timer_sps->steps_per_sec = 0.f; +} + +void timer_sps_measure(struct TimerSPS *timer_sps) { + assert(timer_sps); + timer_measure(&timer_sps->timer); + float step_delta = (float)(g_step - timer_sps->step_start); + timer_sps->steps_per_sec = step_delta / timer_sps->timer.time_delta; +} diff --git a/core/timer.h b/core/timer.h new file mode 100644 index 0000000..3d0a82f --- /dev/null +++ b/core/timer.h @@ -0,0 +1,23 @@ +// file : core/timer.h +// project : Salis-VM +// author : Paul Oliver +// +// Utilities to help measure time intervals and simulation speed. + +#pragma once + +struct Timer { + struct timespec time_start; + float time_delta; +}; + +struct TimerSPS { + struct Timer timer; + uint64_t step_start; + float steps_per_sec; +}; + +void timer_reset(struct Timer *timer); +void timer_measure(struct Timer *timer); +void timer_sps_reset(struct TimerSPS *timer_sps); +void timer_sps_measure(struct TimerSPS *timer_sps); diff --git a/data/client.cpp b/data/client.cpp new file mode 100644 index 0000000..a38307b --- /dev/null +++ b/data/client.cpp @@ -0,0 +1,1409 @@ +// file : data/client.cpp +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] includes +// [section] macros & enums +// [section] globals: plots +// [section] globals: other +// [section] string comparator definition +// [section] Trace (base) definition +// [section] TraceNamed definition +// [section] TraceHeatmap definition +// [section] Plot (base) definition +// [section] PlotLines definition +// [section] PlotStacked definition +// [section] PlotHeatmap definition +// [section] data functions +// [section] gui functions +// [section] main functions + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#define CLIENT_DECLARE_TRACES_AND_PLOTS +#include "arch_spec.h" +#include "client.h" +#include "client_plots.h" + +extern "C" { +#include "logger.h" +} + +// ---------------------------------------------------------------------------- +// [section] macros & enums +// ---------------------------------------------------------------------------- +#define COLOR_BLACK ImVec4(0.f, 0.f, 0.f, 1.f) +#define DATA_FETCH_INTERVAL 10 +#define DATA_FETCH_INTERVAL_SUBDIV 1000 +#define FONT_SIZE 12.f +#define GLSL_VERSION "#version 130" +#define HM_COLORSCALE_WIDTH 80.f +#define HM_SCALE_POW_INTERVAL 1.f +#define HM_SCALE_POW_MAX 64.f +#define HM_SCALE_POW_MIN -1.f +#define PLOT_HEIGHT_INTERVAL 50.f +#define PLOT_MAX_COLS 8 +#define PLOT_MAX_HEIGHT 800.f +#define PLOT_MIN_COLS 1 +#define PLOT_MIN_HEIGHT 100.f +#define PLOT_SCROLL_MARGIN 28.f + +#define IMGUI_WINDOW_FLAGS ( \ + ImGuiWindowFlags_NoBackground | \ + ImGuiWindowFlags_NoDecoration | \ + ImGuiWindowFlags_NoMove | \ + ImGuiWindowFlags_NoSavedSettings \ +) + +enum Status { + STATUS_STOPPED, + STATUS_RUNNING, + STATUS_FETCHING, + STATUS_STOPPING, +}; + +// ---------------------------------------------------------------------------- +// [section] globals: plots +// ---------------------------------------------------------------------------- +static std::array g_core_traces = std::to_array>({ + {"rowid", "rowid"}, + {"step", "step"}, +#define FOR_CORE(i) \ + {"cycl_" #i, "cycl_" #i}, \ + {"mall_" #i, "mall_" #i}, \ + {"pnum_" #i, "pnum_" #i}, \ + {"pfst_" #i, "pfst_" #i}, \ + {"plst_" #i, "plst_" #i}, \ + {"amb0_" #i, "amb0_" #i}, \ + {"amb1_" #i, "amb1_" #i}, \ + {"emb0_" #i, "emb0_" #i}, \ + {"emb1_" #i, "emb1_" #i}, \ + {"eliv_" #i, "eliv_" #i}, \ + {"edea_" #i, "edea_" #i}, + FOR_CORES +#undef FOR_CORE +}); + +static std::array g_core_traces_heatmaps = std::to_array>({ +#define FOR_CORE(i) \ + {"aev_" #i, "aev_" #i}, \ + {"eev_" #i, "eev_" #i}, \ + {"bev_" #i, "bev_" #i}, + FOR_CORES +#undef FOR_CORE +}); + +static std::array g_core_plots = std::to_array({ + {"cycl", "general", { +#define FOR_CORE(i) "cycl_" #i, + FOR_CORES +#undef FOR_CORE + }}, + {"mall", "general", { +#define FOR_CORE(i) "mall_" #i, + FOR_CORES +#undef FOR_CORE + }}, + {"pnum", "general", { +#define FOR_CORE(i) "pnum_" #i, + FOR_CORES +#undef FOR_CORE + }}, + {"ppop", "general", { +#define FOR_CORE(i) "pfst_" #i, "plst_" #i, + FOR_CORES +#undef FOR_CORE + }}, + {"ambs", "general", { +#define FOR_CORE(i) "amb0_" #i, "amb1_" #i, + FOR_CORES +#undef FOR_CORE + }}, + {"eevs", "general", { +#define FOR_CORE(i) "emb0_" #i, "emb1_" #i, "eliv_" #i, "edea_" #i, + FOR_CORES +#undef FOR_CORE + }}, +}); + +static std::array g_core_plots_heatmaps = std::to_array({ +#define FOR_CORE(i) \ + {"aev_" #i, "heatmaps", "aev_" #i}, + FOR_CORES +#undef FOR_CORE +#define FOR_CORE(i) \ + {"eev_" #i, "heatmaps", "eev_" #i}, + FOR_CORES +#undef FOR_CORE +#define FOR_CORE(i) \ + {"bev_" #i, "heatmaps", "bev_" #i}, + FOR_CORES +#undef FOR_CORE +}); + +// ---------------------------------------------------------------------------- +// [section] globals: other +// ---------------------------------------------------------------------------- +static GLFWwindow *g_window; +static ImGuiIO *g_imgui_io; +static ImGuiStyle *g_imgui_style; +static ImPlotStyle *g_implot_style; + +static std::array g_x_axes = std::to_array({ + "rowid", "step", +#define FOR_CORE(i) "cycl_" #i, + FOR_CORES +#undef FOR_CORE +}); + +static int g_status; +static int g_x_axis = CLIENT_DEFVAL_X_AXIS; +static int64_t g_entries = CLIENT_DEFVAL_ENTRIES; +static int64_t g_nth = CLIENT_DEFVAL_NTH; +static int64_t g_x_low = CLIENT_DEFVAL_X_LOW; +static int64_t g_x_high = CLIENT_DEFVAL_X_HIGH; +static int64_t g_hm_left = CLIENT_DEFVAL_HM_LEFT; +static int64_t g_hm_pixel_count = CLIENT_DEFVAL_HM_PIXEL_COUNT; +static int64_t g_hm_pixel_pow; // calculate on init +static int64_t g_x_current = -1l; +static int64_t g_trace_len; +static int64_t g_trace_offset; + +static thrd_t g_fetching_thread; +static mtx_t g_fetching_mutex; + +static bool g_data_col_visible = true; +static bool g_plot_maximized; +static bool g_plot_scroll; +static float g_plot_scroll_current; +static float g_plot_scroll_to; +static float g_data_col_width; +static std::vector g_plot_cells; +static std::vector g_plot_cells_top; +static std::vector g_plot_cells_bottom; +static std::vector g_plots_covered; +static Plot *g_plot_selected; +static Plot *g_plot_hovered; +static int g_plot_stride; +static int g_downsampled_trace_len; +static int g_plot_cols = 2; +static size_t g_plot_col_selected; +static size_t g_plot_row_selected; +static float g_plot_height = 300.f; +static ImPlotColormap g_hm_colormap_id; + +static std::map *, StringComparator> g_trace_map; +static std::vector *> g_traces; +static std::vector g_plots; +static Trace g_x_axis_double; +static Trace g_zero_trace; + +static std::array g_hm_colormap = std::to_array({ + {0.000f, 0.000f, 0.016f, 1.f}, + {0.106f, 0.047f, 0.255f, 1.f}, + {0.290f, 0.047f, 0.420f, 1.f}, + {0.471f, 0.110f, 0.427f, 1.f}, + {0.647f, 0.173f, 0.376f, 1.f}, + {0.812f, 0.267f, 0.275f, 1.f}, + {0.929f, 0.412f, 0.145f, 1.f}, + {0.984f, 0.608f, 0.024f, 1.f}, + {0.969f, 0.820f, 0.239f, 1.f}, + {0.988f, 1.000f, 0.643f, 1.f}, +}); + +// ---------------------------------------------------------------------------- +// [section] string comparator definition +// ---------------------------------------------------------------------------- +bool StringComparator::operator()(const char *a, const char *b) const { + return strcmp(a, b) < 0; +} + +// ---------------------------------------------------------------------------- +// [section] Trace (base) definition +// ---------------------------------------------------------------------------- +template +Trace::Trace() : std::vector() {} + +template +Trace::~Trace() {} + +template +size_t Trace::start_offset() const { + return (size_t)g_trace_offset; +} + +template +void Trace::trim() { + trim_spec(); +} + +#if !defined(NDEBUG) +template +void Trace::validate() const { + assert(this->size() == g_traces[0]->size()); +} +#endif + +template +void Trace::clear() { + std::vector::clear(); +} + +template +void Trace::push_back(T value) { + std::vector::push_back(value); +} + +template +T *Trace::start() { + return this->size() ? &operator[](start_offset()) : nullptr; +} + +template +T &Trace::operator[](size_t n) { +#if !defined(NDEBUG) + return this->at(n); +#else + return std::vector::operator[](n); +#endif +} + +template +T Trace::operator[](size_t n) const { +#if !defined(NDEBUG) + return this->at(n); +#else + return std::vector::operator[](n); +#endif +} + +template +void Trace::trim_spec(int64_t multiplier) { + assert((int64_t)this->size() >= g_entries * multiplier * 2); + this->erase(this->begin(), this->end() - (g_entries * multiplier)); +} + +// ---------------------------------------------------------------------------- +// [section] TraceNamed definition +// ---------------------------------------------------------------------------- +template +TraceNamed::TraceNamed(const char *name, const char *label) : m_name(name), m_label(label) {} + +template +const char *TraceNamed::get_name() const { + return m_name; +} + +template +const char *TraceNamed::get_label() const { + return m_label; +} + +// ---------------------------------------------------------------------------- +// [section] TraceHeatmap definition +// ---------------------------------------------------------------------------- +template +TraceHeatmap::TraceHeatmap(const char *name, const char *name_fmt) : TraceNamed(name, name_fmt) {} + +template +size_t TraceHeatmap::start_offset() const { + return (size_t)(g_trace_offset * g_hm_pixel_count); +} + +template +void TraceHeatmap::trim() { + Trace::trim_spec(g_hm_pixel_count); +} + +#if !defined(NDEBUG) +template +void TraceHeatmap::validate() const { + assert(this->size() == g_traces[0]->size() * g_hm_pixel_count); +} +#endif + +// ---------------------------------------------------------------------------- +// [section] Plot (base) definition +// ---------------------------------------------------------------------------- +Plot::Plot(const char *name, const char *section) : m_visible(true), m_name(name), m_section(section) {} + +Plot::~Plot() {} + +void Plot::handle_input(int key, int mods) { + (void)key; + (void)mods; +} + +void Plot::flag_for_reset() {} + +const char *Plot::get_name() const { + return m_name; +} + +const char *Plot::get_section() const { + return m_section; +} + +void Plot::render(const ImVec2 &frame_size) { + if (ImPlot::BeginPlot(m_name, ImVec2(frame_size.x - right_margin(), frame_size.y), flags())) { + int axis_flags = ImPlotAxisFlags_Foreground | (g_status != STATUS_STOPPED ? ImPlotAxisFlags_AutoFit : 0); + ImPlot::SetupAxes(nullptr, nullptr, axis_flags, axis_flags); + ImPlot::SetupAxisFormat(ImAxis_X1, x_formatter()); + ImPlot::SetupAxisFormat(ImAxis_Y1, y_formatter()); + if (ImPlot::IsPlotHovered()) g_plot_hovered = this; + render_internal(frame_size); + ImPlot::EndPlot(); + } + + render_post(frame_size); +} + +int Plot::hex_formatter(double value, char *buff, int size, void *data) { + (void)data; + snprintf(buff, size, "%s%#lx", value < 0. ? "-" : "", abs((int64_t)value)); + return 0; +} + +AxisFormatter Plot::x_formatter() const { + return Plot::hex_formatter; +} + +AxisFormatter Plot::y_formatter() const { + return Plot::hex_formatter; +} + +float Plot::right_margin() const { + return 0.f; +} + +int Plot::flags() const { + return 0; +} + +void Plot::render_post(const ImVec2 &frame_size) { + (void)frame_size; +} + +// ---------------------------------------------------------------------------- +// [section] PlotLines definition +// ---------------------------------------------------------------------------- +PlotLines::PlotLines(const char *name, const char *section, std::vector trace_keys) : Plot(name, section), m_trace_keys(trace_keys) {} + +void PlotLines::render_internal(const ImVec2 &frame_size) { + (void)frame_size; + + ImS64 *x = g_trace_map[g_x_axes[g_x_axis]]->start(); + ImPlotSpec spec = ImPlotSpec(ImPlotProp_Stride, sizeof(ImS64) * g_plot_stride); + + for (auto &trace : m_trace_keys) { + TraceNamed *trace_obj = g_trace_map[trace]; + ImS64 *y = trace_obj->start(); + ImPlot::PlotLine(trace_obj->get_label(), x, y, g_downsampled_trace_len, spec); + } +} + +// ---------------------------------------------------------------------------- +// [section] PlotStacked definition +// ---------------------------------------------------------------------------- +PlotStacked::PlotStacked(const char *name, const char *section, std::vector trace_keys) : Plot(name, section), m_trace_keys(trace_keys), m_trace_states(trace_keys.size(), true), m_trace_totals(), m_trace_normals(trace_keys.size()), m_needs_reset(true) {} + +void PlotStacked::flag_for_reset() { + m_needs_reset = true; +} + +int PlotStacked::percent_formatter(double value, char *buff, int size, void *data) { + (void)data; + snprintf(buff, size, "%3.0f%%", value * 100.); + return 0; +} + +AxisFormatter PlotStacked::y_formatter() const { + return PlotStacked::percent_formatter; +} + +void PlotStacked::render_internal(const ImVec2 &frame_size) { + (void)frame_size; + + for (size_t i = 0; i < m_trace_keys.size(); i++) { + ImPlot::PlotDummy(g_trace_map[m_trace_keys[i]]->get_label()); + } + + for (size_t i = 0; i < m_trace_keys.size(); i++) { + bool trace_visible = GImPlot->CurrentPlot->Items.GetLegendItem(i)->Show; + + if (m_trace_states[i] != trace_visible) { + m_trace_states[i] = trace_visible; + m_needs_reset = true; + } + } + + if (m_needs_reset) { + m_trace_totals.clear(); + for (auto &trace_normal : m_trace_normals) trace_normal.clear(); + m_needs_reset = false; + } + + size_t trace_size = m_trace_totals.size(); + + for (size_t i = trace_size; i < g_traces[0]->size(); i++) { + m_trace_totals.push_back(0l); + + for (size_t j = 0; j < m_trace_keys.size(); j++) { + if (GImPlot->CurrentPlot->Items.GetLegendItem(j)->Show) { + m_trace_totals.back() += g_trace_map[m_trace_keys[j]]->operator[](i); + } + } + } + + for (size_t i = trace_size; i < g_traces[0]->size(); i++) { + if (m_trace_totals[i]) { + double normal = 0.; + + for (size_t j = 0; j < m_trace_keys.size(); j++) { + if (GImPlot->CurrentPlot->Items.GetLegendItem(j)->Show) { + normal += (double)g_trace_map[m_trace_keys[j]]->operator[](i) / (double)m_trace_totals[i]; + m_trace_normals[j].push_back(normal); + } + } + } else { + for (size_t j = 0; j < m_trace_keys.size(); j++) { + if (GImPlot->CurrentPlot->Items.GetLegendItem(j)->Show) { + m_trace_normals[j].push_back(0.); + } + } + } + } + +#if !defined(NDEBUG) + m_trace_totals.validate(); + + for (size_t i = 0; i < m_trace_keys.size(); i++) { + if (GImPlot->CurrentPlot->Items.GetLegendItem(i)->Show) { + m_trace_normals[i].validate(); + } else { + assert(m_trace_normals[i].empty()); + } + } +#endif + + Trace *prev_trace = &g_zero_trace; + + for (size_t i = 0; i < m_trace_keys.size(); i++) { + assert(m_trace_states[i] == GImPlot->CurrentPlot->Items.GetLegendItem(i)->Show); + + if (m_trace_states[i]) { + bool hovered = GImPlot->CurrentPlot->Items.GetLegendItem(i)->LegendHovered; + ImPlotSpec spec = ImPlotSpec(ImPlotProp_FillAlpha, hovered ? 1.f : 0.9f, ImPlotProp_Stride, sizeof(double) * g_plot_stride); + const char *trace_name = g_trace_map[m_trace_keys[i]]->get_label(); + Trace *current_trace = &m_trace_normals[i]; + ImPlot::PlotShaded(trace_name, g_x_axis_double.start(), prev_trace->start(), current_trace->start(), g_downsampled_trace_len, spec); + ImPlot::PlotLine(trace_name, g_x_axis_double.start(), prev_trace->start(), g_downsampled_trace_len, spec); + if (hovered) ImPlot::PlotLine(trace_name, g_x_axis_double.start(), current_trace->start(), g_downsampled_trace_len, spec); + prev_trace = current_trace; + } + } +} + +// ---------------------------------------------------------------------------- +// [section] PlotHeatmap definition +// ---------------------------------------------------------------------------- +PlotHeatmap::PlotHeatmap(const char *name, const char *section, const char *trace_key) : Plot(name, section), m_trace_key(trace_key), m_tex_scale_pow(-1.f), m_tex_scale_high(0.f), m_tex_rendered_last(0), m_rows_rendered(0), m_tex_id(0), m_tex_render(), m_needs_reset(true) {} + +void PlotHeatmap::handle_input(int key, int mods) { + switch (mods) { + case GLFW_MOD_CONTROL: + switch (key) { + case GLFW_KEY_K: + m_tex_scale_pow = std::clamp(m_tex_scale_pow - 1, HM_SCALE_POW_MIN, HM_SCALE_POW_MAX); + m_needs_reset = true; + break; + case GLFW_KEY_L: + m_tex_scale_pow = std::clamp(m_tex_scale_pow + 1, HM_SCALE_POW_MIN, HM_SCALE_POW_MAX); + m_needs_reset = true; + break; + } + } +} + +void PlotHeatmap::flag_for_reset() { + m_needs_reset = true; +} + +float PlotHeatmap::right_margin() const { + return HM_COLORSCALE_WIDTH; +} + +int PlotHeatmap::flags() const { + return ImPlotFlags_NoLegend; +} + +void PlotHeatmap::render_internal(const ImVec2 &frame_size) { + Trace *trace = g_trace_map[m_trace_key]; + + if (m_needs_reset) { + m_tex_scale_high = 0.f; + m_tex_rendered_last = trace->start_offset(); + m_rows_rendered = 0; + glDeleteTextures(1, &m_tex_id); + m_tex_id = 0; + m_needs_reset = false; + } + + if (m_tex_rendered_last == trace->size()) { + render_end(frame_size); + return; + } + + assert(m_tex_render.empty()); + assert((trace->size() - m_tex_rendered_last) % g_hm_pixel_count == 0); + size_t rows_to_append = (trace->size() - m_tex_rendered_last) / (size_t)g_hm_pixel_count; + size_t rows_to_chop_off = (m_rows_rendered + rows_to_append) > (size_t)g_trace_len ? (m_rows_rendered + rows_to_append) - (size_t)g_trace_len : 0; + + if (rows_to_chop_off > m_rows_rendered) { + m_needs_reset = true; + render_internal(frame_size); + return; + } + + if (m_tex_scale_pow == HM_SCALE_POW_MIN) { + ImS64 max = 0; + + for (size_t i = trace->start_offset(); i < trace->size(); i++) { + max = std::max(max, trace->operator[](i)); + } + + m_tex_scale_high = (float)max; + } else { + m_tex_scale_high = pow(2.f, m_tex_scale_pow); + } + + m_tex_render.reserve((trace->size() - m_tex_rendered_last) * 3); + + if (m_tex_scale_high != 0.f) { + for (size_t i = m_tex_rendered_last; i < trace->size(); i++) { + ImS64 value = trace->operator[](i); + float normal = std::min((float)value / m_tex_scale_high, 1.f); + assert(normal >= 0.f && normal <= 1.f); + ImVec4 color = ImPlot::SampleColormap(normal, g_hm_colormap_id); + m_tex_render.push_back((uint8_t)(color.x * 255.f)); // red + m_tex_render.push_back((uint8_t)(color.y * 255.f)); // green + m_tex_render.push_back((uint8_t)(color.z * 255.f)); // blue + } + } else { + for (size_t i = m_tex_rendered_last; i < trace->size(); i++) { + m_tex_render.push_back(0); + m_tex_render.push_back(0); + m_tex_render.push_back(0); + } + } + + GLuint new_tex_id; + glGenTextures(1, &new_tex_id); + glBindTexture(GL_TEXTURE_2D, new_tex_id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, g_hm_pixel_count, g_trace_len, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); + assert(glGetError() == GL_NO_ERROR); + assert(new_tex_id); + + if (m_tex_id) { + ((PFNGLCOPYIMAGESUBDATAPROC)glfwGetProcAddress("glCopyImageSubData"))( + m_tex_id, GL_TEXTURE_2D, 0, 0, rows_to_chop_off, 0, + new_tex_id, GL_TEXTURE_2D, 0, 0, 0, 0, + g_hm_pixel_count, m_rows_rendered - rows_to_chop_off, 1 + ); + assert(glGetError() == GL_NO_ERROR); + glDeleteTextures(1, &m_tex_id); + } + + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_rows_rendered - rows_to_chop_off, g_hm_pixel_count, rows_to_append, GL_RGB, GL_UNSIGNED_BYTE, m_tex_render.data()); + assert(glGetError() == GL_NO_ERROR); + + render_end(frame_size); + + m_tex_rendered_last = trace->size(); + m_rows_rendered += rows_to_append - rows_to_chop_off; + assert(m_rows_rendered <= (size_t)g_trace_len); + m_tex_id = new_tex_id; + m_tex_render.clear(); +} + +void PlotHeatmap::render_end(const ImVec2 &frame_size) { + if (g_x_axis_double.empty()) return; + + ImVec2 bmin(g_hm_left, (float)*g_x_axis_double.start()); + ImVec2 bmax(g_hm_left + g_hm_pixel_count * pow(2.f, g_hm_pixel_pow), (float)g_x_axis_double.back()); + ImVec2 uv0(0.f, 1.f); + ImVec2 uv1(1.f, 0.f); + double scale_max = m_tex_scale_pow == HM_SCALE_POW_MIN ? (double)m_tex_scale_high : pow(2., (double)m_tex_scale_pow); + + ImPlot::PlotImage(get_name(), (ImTextureID)m_tex_id, bmin, bmax, uv0, uv1); + ImGui::SameLine(); + + ImPlot::PushColormap(g_hm_colormap_id); + ImPlot::ColormapScale("##hm-scale", 0., scale_max, ImVec2(HM_COLORSCALE_WIDTH, frame_size.y), "%.1e"); + ImPlot::PopColormap(); +} + +void PlotHeatmap::render_post(const ImVec2 &frame_size) { + (void)frame_size; +} + +// ---------------------------------------------------------------------------- +// [section] data functions +// ---------------------------------------------------------------------------- +static int64_t data_max_hm_pixel_pow(void) { + return (int64_t)floor(log2((double)(MVEC_SIZE - g_hm_left) / (double)g_hm_pixel_count)); +} + +static void data_update_stride(void) { + if (g_plot_maximized) { + g_plot_stride = 1; + g_downsampled_trace_len = g_trace_len; + return; + } + + int win_height = 0; + glfwGetWindowSize(g_window, nullptr, &win_height); + + int max_stride = g_plot_cols * ((win_height / (int)g_plot_height) + 1); + int max_points = CLIENT_DEFVAL_ENTRIES / max_stride; + g_plot_stride = 1; + + while (g_trace_len / g_plot_stride > max_points) { + g_plot_stride++; + } + + g_downsampled_trace_len = g_trace_len / g_plot_stride; + + log_info("Updated plot stride: %d", g_plot_stride); + log_info("Updated downsampled trace-len: %d", g_downsampled_trace_len); +} + +static void data_on_field_change(void) { + g_entries = std::clamp(g_entries, 1l, CLIENT_DEFVAL_ENTRIES); + g_nth = std::clamp(g_nth, CLIENT_DEFVAL_NTH, INT64_MAX); + g_x_low = std::clamp(g_x_low, CLIENT_DEFVAL_X_LOW, INT64_MAX); + g_x_high = std::clamp(g_x_high, g_x_low + 1l, CLIENT_DEFVAL_X_HIGH); + g_hm_left = std::clamp(g_hm_left, CLIENT_DEFVAL_HM_LEFT, (int64_t)MVEC_SIZE); + g_hm_pixel_count = std::clamp(g_hm_pixel_count, 1l, CLIENT_DEFVAL_HM_PIXEL_COUNT); + g_hm_pixel_pow = std::clamp(g_hm_pixel_pow, 0l, data_max_hm_pixel_pow()); + g_x_current = -1l; + + g_trace_len = 0l; + g_trace_offset = 0;; + g_plot_stride = 0; + g_downsampled_trace_len = 0; + + for (auto &trace : g_traces) trace->clear(); + for (auto &plot: g_plots) plot->flag_for_reset(); + g_x_axis_double.clear(); + g_zero_trace.clear(); +} + +static void data_reset_fields(void) { + g_entries = CLIENT_DEFVAL_ENTRIES; + g_nth = CLIENT_DEFVAL_NTH; + g_x_axis = CLIENT_DEFVAL_X_AXIS; + g_x_low = CLIENT_DEFVAL_X_LOW; + g_x_high = CLIENT_DEFVAL_X_HIGH; + g_hm_left = CLIENT_DEFVAL_HM_LEFT; + g_hm_pixel_count = CLIENT_DEFVAL_HM_PIXEL_COUNT; + g_hm_pixel_pow = data_max_hm_pixel_pow(); + data_on_field_change(); +} + +static void data_reset_plot_cells(void) { + std::fill(g_plot_cells.begin(), g_plot_cells.end(), nullptr); + std::fill(g_plot_cells_top.begin(), g_plot_cells_top.end(), 0.f); + std::fill(g_plot_cells_bottom.begin(), g_plot_cells_bottom.end(), 0.f); +} + +static void data_fetch(void) { + assert(g_status == STATUS_RUNNING); + g_status = STATUS_FETCHING; + + json_object *request = json_object_new_object(); + json_object_object_add(request, "request", json_object_new_string("data")); + json_object_object_add(request, "entries", json_object_new_int64(g_entries)); + json_object_object_add(request, "nth", json_object_new_int64(g_nth)); + json_object_object_add(request, "x-axis", json_object_new_string(g_x_axes[g_x_axis])); + json_object_object_add(request, "x-low", json_object_new_int64(g_x_low)); + json_object_object_add(request, "x-high", json_object_new_int64(g_x_high)); + json_object_object_add(request, "hm-left", json_object_new_int64(g_hm_left)); + json_object_object_add(request, "hm-pixel-count", json_object_new_int64(g_hm_pixel_count)); + json_object_object_add(request, "hm-pixel-pow", json_object_new_int64(g_hm_pixel_pow)); + json_object_object_add(request, "x-current", json_object_new_int64(g_x_current)); + const char *request_str = json_object_to_json_string(request); + + log_info("Sending request to server: %s", request_str); + int socket_fd = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in socket_addr; + memset(&socket_addr, 0, sizeof(sockaddr_in)); + socket_addr.sin_family = AF_INET; + socket_addr.sin_port = htons(PORT); + inet_pton(AF_INET, IP, &socket_addr.sin_addr); + if (connect(socket_fd, (sockaddr *)&socket_addr, sizeof(sockaddr_in))) assert(false); + json_object_to_fd(socket_fd, request, 0); + shutdown(socket_fd, SHUT_WR); + + json_object *response = json_object_from_fd(socket_fd); + mtx_lock(&g_fetching_mutex); + + json_object_object_foreach(response, key, value) { + size_t new_rows = json_object_array_length(value); + + if (!strcmp(key, g_x_axes[g_x_axis])) { + log_info("Received %lu rows of data from server", new_rows); + + for (size_t i = 0; i < new_rows; i++) { + ImS64 point = json_object_get_int64(json_object_array_get_idx(value, i)); + g_x_axis_double.push_back((double)point); + g_zero_trace.push_back(0.); + } + } + + if (g_trace_map.contains(key)) { + for (size_t i = 0; i < new_rows; i++) { + ImS64 point = json_object_get_int64(json_object_array_get_idx(value, i)); + g_trace_map[key]->push_back(point); + } + } + } + + g_x_current = g_trace_map[g_x_axes[g_x_axis]]->back(); + json_object_put(request); + json_object_put(response); + +#if !defined(NDEBUG) + for (auto &trace : g_traces) trace->validate(); + g_x_axis_double.validate(); + g_zero_trace.validate(); +#endif + + if ((int64_t)g_traces[0]->size() >= g_entries * 2) { + log_info("Trimming traces & flagging plots for reset"); + for (auto &trace : g_traces) trace->trim(); + for (auto &plot : g_plots) plot->flag_for_reset(); + g_x_axis_double.trim(); + g_zero_trace.trim(); + } + +#if !defined(NDEBUG) + for (auto &trace : g_traces) trace->validate(); + g_x_axis_double.validate(); + g_zero_trace.validate(); +#endif + + int64_t current_size = g_traces[0]->size(); + g_trace_len = std::min(current_size, g_entries); + g_trace_offset = current_size > g_entries ? current_size - g_entries : 0l; + data_update_stride(); + + mtx_unlock(&g_fetching_mutex); + g_status = STATUS_RUNNING; +} + +static int data_fetching_thread(void *data) { + (void)data; + assert(!data); + assert(g_status == STATUS_RUNNING); + + while (g_status == STATUS_RUNNING) { + data_fetch(); + + for (int i = 0; i < DATA_FETCH_INTERVAL_SUBDIV && g_status == STATUS_RUNNING; i++) { + usleep((DATA_FETCH_INTERVAL * 1000000) / DATA_FETCH_INTERVAL_SUBDIV); + } + } + + assert(g_status == STATUS_STOPPING); + g_status = STATUS_STOPPED; + return 0; +} + +static void data_start_fetching(void) { + log_info("Starting data fetching thread"); + g_status = STATUS_RUNNING; + thrd_create(&g_fetching_thread, (thrd_start_t)data_fetching_thread, nullptr); +} + +static void data_stop_fetching(void) { + assert(g_status == STATUS_RUNNING || g_status == STATUS_FETCHING); + log_info("Stopping data fetching thread"); + g_status = STATUS_STOPPING; +} + +// ---------------------------------------------------------------------------- +// [section] gui functions +// ---------------------------------------------------------------------------- +static void gui_render_data_input(const char *label, int64_t *target) { + assert(target); + + if (ImGui::InputScalar(label, ImGuiDataType_U64, target, nullptr, nullptr, "%#lx")) { + data_on_field_change(); + } +} + +static void gui_render_data_col(void) { + const ImGuiViewport *viewport = ImGui::GetMainViewport(); + const ImVec2 win_pos = viewport->Pos; + const ImVec2 win_size = ImVec2(-1.f, viewport->Size.y); + ImGui::SetNextWindowPos(win_pos); + ImGui::SetNextWindowSize(win_size); + ImGui::Begin("data-col", nullptr, IMGUI_WINDOW_FLAGS); + g_data_col_width = ImGui::GetWindowWidth(); + + ImGui::SeparatorText("SALIS data client"); + ImGui::LabelText("name", NAME); + ImGui::LabelText("seed", "%#lx", SEED); + ImGui::LabelText("server", IP ":" PORT_STR); + ImGui::LabelText("arch", ARCH); + ImGui::LabelText("cores", "%d", CORES); + ImGui::LabelText("mvec-size", "%#lx", MVEC_SIZE); + ImGui::LabelText("data-push", "%#lx", DATA_PUSH_INTERVAL); + ImGui::LabelText("fps", "%.1f", g_imgui_io->Framerate); + + ImGui::SeparatorText("Data fields"); + + switch (g_status) { + case STATUS_STOPPED: + gui_render_data_input("entries", &g_entries); + gui_render_data_input("nth", &g_nth); + + if (ImGui::BeginCombo("x-axis", g_x_axes[g_x_axis])) { + for (int i = 0; i < CORES + 2; i++) { + if (ImGui::Selectable(g_x_axes[i], g_x_axis == i)) { + data_reset_fields(); + g_x_axis = i; + } + } + + ImGui::EndCombo(); + } + + gui_render_data_input("x-low", &g_x_low); + gui_render_data_input("x-high", &g_x_high); + gui_render_data_input("hm-left", &g_hm_left); + gui_render_data_input("hm-pxl-count", &g_hm_pixel_count); + gui_render_data_input("hm-pxl-pow", &g_hm_pixel_pow); + break; + case STATUS_RUNNING: + case STATUS_FETCHING: + case STATUS_STOPPING: + ImGui::LabelText("entries", "%#lx", g_entries); + ImGui::LabelText("nth", "%#lx", g_nth); + ImGui::LabelText("x-axis", "%s", g_x_axes[g_x_axis]); + ImGui::LabelText("x-low", "%#lx", g_x_low); + ImGui::LabelText("x-high", "%#lx", g_x_high); + ImGui::LabelText("hm-left", "%#lx", g_hm_left); + ImGui::LabelText("hm-pxl-count", "%#lx", g_hm_pixel_count); + ImGui::LabelText("hm-pxl-pow", "%#lx", g_hm_pixel_pow); + } + + switch (g_status) { + case STATUS_STOPPED: + if (ImGui::Button("Run", ImVec2(-1.f, 0.f))) data_start_fetching(); + if (ImGui::Button("Reset", ImVec2(-1.f, 0.f))) data_reset_fields(); + break; + case STATUS_RUNNING: + if (ImGui::Button("Stop", ImVec2(-1.f, 0.f))) data_stop_fetching(); + ImGui::LabelText("##", "Running"); + break; + case STATUS_FETCHING: + if (ImGui::Button("Stop", ImVec2(-1.f, 0.f))) data_stop_fetching(); + ImGui::LabelText("##", "Fetching ..."); + break; + case STATUS_STOPPING: + ImGui::BeginDisabled(); + ImGui::Button("Stop", ImVec2(-1.f, 0.f)); + ImGui::EndDisabled(); + ImGui::LabelText("##", "Stopping"); + break; + } + + ImGui::SeparatorText("Layout"); + ImGui::DragInt("cols", &g_plot_cols, 1, PLOT_MIN_COLS, PLOT_MAX_COLS); + ImGui::DragFloat("plot-height", &g_plot_height, PLOT_HEIGHT_INTERVAL, PLOT_MIN_HEIGHT, PLOT_MAX_HEIGHT, "%.0f"); + + ImGui::SeparatorText("Plots"); + if (ImGui::Button("Show all", ImVec2(-1.f, 0.f))) for (auto &plot : g_plots) plot->m_visible = true; + if (ImGui::Button("Hide all", ImVec2(-1.f, 0.f))) for (auto &plot : g_plots) plot->m_visible = false; + ImGui::BeginTable("plot-visibility-table", 3); + + for (auto &plot : g_plots) { + ImGui::TableNextColumn(); + ImGui::Checkbox(plot->get_name(), &plot->m_visible); + } + + ImGui::EndTable(); + ImGui::End(); +} + +static size_t gui_plot_cell_index(size_t row, size_t col) { + size_t index = row * PLOT_MAX_COLS + col; + assert(index < g_plot_cells.size()); + assert(index < g_plot_cells_top.size()); + assert(index < g_plot_cells_bottom.size()); + return index; +} + +static size_t gui_plot_cell_row_up() { + for (size_t row = g_plot_row_selected - 1; row < g_plot_row_selected; row--) { + if (g_plot_cells[gui_plot_cell_index(row, g_plot_col_selected)]) { + return row; + } + } + + return g_plot_row_selected; +} + +static size_t gui_plot_cell_row_down() { + for (size_t row = g_plot_row_selected + 1; row < g_plots.size(); row++) { + if (g_plot_cells[gui_plot_cell_index(row, g_plot_col_selected)]) { + return row; + } + } + + return g_plot_row_selected; +} + +static void gui_render_plots(void) { + const char *section_current = g_plots[0]->get_section(); + const char *section_next = nullptr; + g_plots_covered.clear(); + g_plots_covered.resize(g_plots.size(), false); + const ImGuiViewport *viewport = ImGui::GetMainViewport(); + const ImVec2 win_pos = g_data_col_visible ? ImVec2(g_data_col_width, viewport->Pos.y) : viewport->Pos; + const ImVec2 win_size = g_data_col_visible ? ImVec2(viewport->Size.x - g_data_col_width, -1.f) : ImVec2(viewport->Size.x, -1.f); + + if (g_plot_scroll) { + ImGui::SetNextWindowScroll(ImVec2(-1.f, g_plot_scroll_to)); + g_plot_scroll = false; + g_plot_scroll_to = 0.f; + } + + ImGui::SetNextWindowPos(win_pos); + ImGui::SetNextWindowSize(win_size); + ImGui::Begin("plots", nullptr, IMGUI_WINDOW_FLAGS); + g_plot_scroll_current = ImGui::GetScrollY(); + g_plot_hovered = nullptr; + + if (!g_plot_selected->m_visible) { + g_plot_selected = g_plots[0]; + g_plot_col_selected = 0; + g_plot_row_selected = 0; + + for (auto &plot : g_plots) { + if (plot->m_visible) { + g_plot_selected = plot; + } + } + } + + size_t row = 0; + size_t col = 0; + + mtx_lock(&g_fetching_mutex); + + while (section_current) { + ImGui::SeparatorText(section_current); + ImGui::BeginTable("plots-table", g_plot_cols); + + for (size_t i = 0; i < g_plots.size(); i++) { + if (strcmp(g_plots[i]->get_section(), section_current)) { + section_next = (!section_next && !g_plots_covered[i]) ? g_plots[i]->get_section() : section_next; + continue; + } + + if (g_plots[i]->m_visible) { + ImGui::TableNextColumn(); + ImVec2 frame_size = ImVec2(ImGui::GetContentRegionAvail().x, g_plot_height); + g_plot_cells[gui_plot_cell_index(row, col)] = g_plots[i]; + g_plot_cells_top[gui_plot_cell_index(row, col)] = ImGui::GetCursorPosY(); + + if (g_plots[i] == g_plot_selected) { + g_plot_col_selected = col; + g_plot_row_selected = row; + g_implot_style->Colors[ImPlotCol_FrameBg] = g_imgui_style->Colors[ImGuiCol_FrameBg]; + } + + g_plots[i]->render(frame_size); + + if (g_plots[i] == g_plot_selected) { + g_implot_style->Colors[ImPlotCol_FrameBg] = COLOR_BLACK; + } + + g_plot_cells_bottom[gui_plot_cell_index(row, col)] = ImGui::GetCursorPosY(); + col = (col + 1) % g_plot_cols; + row += col ? 0 : 1; + } + + g_plots_covered[i] = true; + } + + section_current = section_next; + section_next = nullptr; + ImGui::EndTable(); + row += col ? 1 : 0; + col = 0; + } + + mtx_unlock(&g_fetching_mutex); + ImGui::End(); +} + +static void gui_render_plot_maximized(void) { + const ImGuiViewport *viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(viewport->Pos); + ImGui::SetNextWindowSize(viewport->Size); + ImGui::Begin("plot-fullscreen", nullptr, IMGUI_WINDOW_FLAGS); + + ImVec2 frame_size = { + viewport->Size.x - g_imgui_style->WindowPadding.x * 2, + viewport->Size.y - g_imgui_style->WindowPadding.y * 2, + }; + + mtx_lock(&g_fetching_mutex); + g_plot_selected->render(frame_size); + mtx_unlock(&g_fetching_mutex); + ImGui::End(); +} + +static void gui_plot_queue_scroll_to_position(bool increased_plot_height) { + size_t row = 0; + float selected_plot_top = 0.f; + + for (row = 0; row < g_plots.size(); row++) { + for (size_t col = 0; col < PLOT_MAX_COLS; col++) { + if (g_plot_selected == g_plot_cells[gui_plot_cell_index(row, col)]) { + selected_plot_top = g_plot_cells_top[gui_plot_cell_index(row, col)]; + goto loop_exit; + } + } + } + + loop_exit: + g_plot_scroll_to = selected_plot_top + (PLOT_HEIGHT_INTERVAL * (float)row * (increased_plot_height ? 1.f : -1.f)) - PLOT_SCROLL_MARGIN; + g_plot_scroll = true; +} + +static void gui_plot_queue_scroll_to_selected() { + const ImGuiViewport *viewport = ImGui::GetMainViewport(); + float plot_top = g_plot_cells_top[gui_plot_cell_index(g_plot_row_selected, g_plot_col_selected)]; + float plot_bottom = g_plot_cells_bottom[gui_plot_cell_index(g_plot_row_selected, g_plot_col_selected)]; + float win_bottom = g_plot_scroll_current + viewport->Size.y; + + if (plot_bottom > win_bottom) { + g_plot_scroll_to = g_plot_scroll_current + (plot_bottom - win_bottom); + g_plot_scroll = true; + } + + if (plot_top < g_plot_scroll_current) { + g_plot_scroll_to = plot_top - PLOT_SCROLL_MARGIN; + g_plot_scroll = true; + } +} + +static void gui_render(void) { + if (g_plot_maximized) { + gui_render_plot_maximized(); + return; + } + + if (g_data_col_visible) gui_render_data_col(); + gui_render_plots(); +} + +// ---------------------------------------------------------------------------- +// [section] main functions +// ---------------------------------------------------------------------------- +static void app_sig_handler(int signo) { + (void)signo; + log_attention("Signal received, will stop SALIS data client..."); + + if (g_status == STATUS_RUNNING || g_status == STATUS_FETCHING) { + data_stop_fetching(); + } + + glfwSetWindowShouldClose(g_window, GLFW_TRUE); +} + +static void app_error_callback(int error, const char* description) { + log_attention("GLFW error %d: %s", error, description); +} + +static void app_toggle_state() { + switch (g_status) { + case STATUS_STOPPED: + data_start_fetching(); + break; + case STATUS_RUNNING: + case STATUS_FETCHING: + data_stop_fetching(); + break; + } +} + +static void app_key_callback_plot_maximized(int key, int mods) { + switch (mods) { + case GLFW_MOD_CONTROL: + switch (key) { + case GLFW_KEY_C: + glfwSetWindowShouldClose(g_window, GLFW_TRUE); + break; + } + + break; + + case 0: + switch (key) { + case GLFW_KEY_F: + g_plot_maximized = false; + data_update_stride(); + break; + case GLFW_KEY_SPACE: + app_toggle_state(); + break; + } + + break; + } + + if (g_plot_selected->m_visible) { + g_plot_selected->handle_input(key, mods); + } +} + +static void app_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { + (void)window; + (void)scancode; + + if (ImGui::IsAnyItemActive()) { + return; + } + + if (action != GLFW_PRESS && action != GLFW_REPEAT) { + return; + } + + if (g_plot_maximized) { + app_key_callback_plot_maximized(key, mods); + return; + } + + switch (mods) { + case GLFW_MOD_CONTROL: + switch (key) { + case GLFW_KEY_C: + glfwSetWindowShouldClose(g_window, GLFW_TRUE); + break; + case GLFW_KEY_N: + g_data_col_visible = !g_data_col_visible; + break; + case GLFW_KEY_LEFT: + g_plot_cols = std::max(g_plot_cols - 1, PLOT_MIN_COLS); + data_reset_plot_cells(); + data_update_stride(); + break; + case GLFW_KEY_RIGHT: + g_plot_cols = std::min(g_plot_cols + 1, PLOT_MAX_COLS); + data_reset_plot_cells(); + data_update_stride(); + break; + case GLFW_KEY_UP: + g_plot_height = std::min(g_plot_height + PLOT_HEIGHT_INTERVAL, PLOT_MAX_HEIGHT); + gui_plot_queue_scroll_to_position(true); + data_update_stride(); + break; + case GLFW_KEY_DOWN: + g_plot_height = std::max(g_plot_height - PLOT_HEIGHT_INTERVAL, PLOT_MIN_HEIGHT); + gui_plot_queue_scroll_to_position(false); + data_update_stride(); + break; + } + + break; + + case 0: + switch (key) { + case GLFW_KEY_LEFT: + g_plot_col_selected -= g_plot_col_selected ? 1 : 0; + g_plot_selected = g_plot_cells[gui_plot_cell_index(g_plot_row_selected, g_plot_col_selected)]; + break; + case GLFW_KEY_RIGHT: + g_plot_col_selected += (g_plot_col_selected < PLOT_MAX_COLS - 1 && g_plot_cells[gui_plot_cell_index(g_plot_row_selected, g_plot_col_selected + 1)]) ? 1 : 0; + g_plot_selected = g_plot_cells[gui_plot_cell_index(g_plot_row_selected, g_plot_col_selected)]; + break; + case GLFW_KEY_UP: + g_plot_row_selected = gui_plot_cell_row_up(); + g_plot_selected = g_plot_cells[gui_plot_cell_index(g_plot_row_selected, g_plot_col_selected)]; + gui_plot_queue_scroll_to_selected(); + break; + case GLFW_KEY_DOWN: + g_plot_row_selected = gui_plot_cell_row_down(); + g_plot_selected = g_plot_cells[gui_plot_cell_index(g_plot_row_selected, g_plot_col_selected)]; + gui_plot_queue_scroll_to_selected(); + break; + case GLFW_KEY_F: + if (g_plot_selected->m_visible) { + g_plot_maximized = !g_plot_maximized; + data_update_stride(); + } + + break; + case GLFW_KEY_SPACE: + app_toggle_state(); + break; + } + + break; + } + + if (g_plot_selected->m_visible) { + g_plot_selected->handle_input(key, mods); + } +} + +static void app_window_size_callback(GLFWwindow* window, int width, int height) { + (void)window; + (void)width; + (void)height; + data_update_stride(); +} + +static void app_mouse_button_callback(GLFWwindow* window, int button, int action, int mods) { + (void)window; + (void)mods; + + if (button != GLFW_MOUSE_BUTTON_LEFT) return; + + switch (action) { + case GLFW_PRESS: + g_plot_selected = g_plot_hovered ? g_plot_hovered : g_plot_selected; + break; + } +} + +static void init() { + signal(SIGINT, app_sig_handler); + signal(SIGTERM, app_sig_handler); + + log_info("Starting SALIS data client"); + log_info("Initializing GLFW"); + glfwSetErrorCallback(app_error_callback); + glfwInitHint(GLFW_WAYLAND_LIBDECOR, GLFW_WAYLAND_DISABLE_LIBDECOR); + if (!glfwInit()) assert(false); + + float scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); + g_window = glfwCreateWindow((int)(800 * scale), (int)(600 * scale), "SALIS data client", nullptr, nullptr); + assert(g_window); + glfwSetKeyCallback(g_window, app_key_callback); + glfwSetMouseButtonCallback(g_window, app_mouse_button_callback); + glfwSetWindowSizeCallback(g_window, app_window_size_callback); + glfwMakeContextCurrent(g_window); + glfwSwapInterval(1); + + log_info("Initializing ImGui"); + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImPlot::CreateContext(); + + g_imgui_io = &ImGui::GetIO(); + g_imgui_io->Fonts->AddFontFromFileTTF(FONT_SOURCE, FONT_SIZE); + g_imgui_io->IniFilename = nullptr; + g_imgui_style = &ImGui::GetStyle(); + g_imgui_style->Colors[ImGuiCol_WindowBg] = COLOR_BLACK; + g_imgui_style->FontScaleDpi = scale; + g_imgui_style->FontSizeBase = FONT_SIZE; + g_imgui_style->ItemSpacing = ImVec2(g_imgui_style->ItemSpacing.x, 2.f); + g_imgui_style->ScaleAllSizes(scale); + g_implot_style = &ImPlot::GetStyle(); + g_implot_style->Colors[ImPlotCol_FrameBg] = COLOR_BLACK; + g_hm_colormap_id = ImPlot::AddColormap("heatmap", g_hm_colormap.data(), g_hm_colormap.size(), false); + + ImGui_ImplGlfw_InitForOpenGL(g_window, true); + ImGui_ImplOpenGL3_Init(GLSL_VERSION); + + for (auto &trace : g_core_traces) g_traces.push_back(&trace); + for (auto &trace : g_arch_traces) g_traces.push_back(&trace); + for (auto &trace : g_core_traces_heatmaps) g_traces.push_back(&trace); + for (auto &trace : g_arch_traces_heatmaps) g_traces.push_back(&trace); + for (auto &plot : g_core_plots) g_plots.push_back(&plot); + for (auto &plot : g_arch_plots) g_plots.push_back(&plot); + for (auto &plot : g_arch_plots_stacked) g_plots.push_back(&plot); + for (auto &plot : g_core_plots_heatmaps) g_plots.push_back(&plot); + for (auto &plot : g_arch_plots_heatmaps) g_plots.push_back(&plot); + for (auto &i : g_traces) g_trace_map[i->get_name()] = i; + + g_plot_cells = std::vector(g_plots.size() * PLOT_MAX_COLS); + g_plot_cells_top = std::vector(g_plots.size() * PLOT_MAX_COLS); + g_plot_cells_bottom = std::vector(g_plots.size() * PLOT_MAX_COLS); + g_plot_selected = g_plots[0]; + g_hm_pixel_pow = data_max_hm_pixel_pow(); + data_update_stride(); + + mtx_init(&g_fetching_mutex, mtx_plain); +} + +static void exec() { + while (!glfwWindowShouldClose(g_window)) { + glfwPollEvents(); + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + gui_render(); + ImGui::Render(); + int display_w; + int display_h; + glfwGetFramebufferSize(g_window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + glClearColor(0.f, 0.f, 0.f, 1.f); + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + glfwSwapBuffers(g_window); + } +} + +static void quit() { + mtx_destroy(&g_fetching_mutex); + + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImPlot::DestroyContext(); + ImGui::DestroyContext(); + + log_info("Stopping SALIS data client"); + glfwDestroyWindow(g_window); + glfwTerminate(); +} + +int main(int argc, char **argv) { + (void)argc; + (void)argv; + + init(); + exec(); + quit(); + + return 0; +} diff --git a/data/client.h b/data/client.h new file mode 100644 index 0000000..77ff948 --- /dev/null +++ b/data/client.h @@ -0,0 +1,137 @@ +// file : data/client.h +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] macros +// [section] trace declarations +// [section] plot declarations + +// ---------------------------------------------------------------------------- +// [section] macros +// ---------------------------------------------------------------------------- +#define CLIENT_DEFVAL_ENTRIES 0x800l +#define CLIENT_DEFVAL_HM_LEFT 0l +#define CLIENT_DEFVAL_HM_PIXEL_COUNT 0x400l +#define CLIENT_DEFVAL_NTH 1l +#define CLIENT_DEFVAL_X_AXIS 0 +#define CLIENT_DEFVAL_X_HIGH INT64_MAX +#define CLIENT_DEFVAL_X_LOW 0l + +// Client implementation must define this. +// Server implementation must not (as it's C not C++). +#if defined(CLIENT_DECLARE_TRACES_AND_PLOTS) + +// ---------------------------------------------------------------------------- +// [section] trace declarations +// ---------------------------------------------------------------------------- +struct StringComparator { + bool operator()(const char *a, const char *b) const; +}; + +template +struct Trace : public std::vector { + Trace(); + virtual ~Trace(); + virtual size_t start_offset() const; + virtual void trim(); +#if !defined(NDEBUG) + virtual void validate() const; +#endif + void clear(); + void push_back(T value); + T *start(); + T &operator[](size_t n); + T operator[](size_t n) const; +protected: + void trim_spec(int64_t multiplier = 1); +}; + +template +struct TraceNamed : public Trace { + TraceNamed(const char *name, const char *label); + const char *get_name() const; + const char *get_label() const; +private: + const char *m_name; + const char *m_label; +}; + +template +struct TraceHeatmap : public TraceNamed { + TraceHeatmap(const char *name, const char *label); + size_t start_offset() const; + void trim(); +#if !defined(NDEBUG) + void validate() const; +#endif +}; + +// ---------------------------------------------------------------------------- +// [section] plot declarations +// ---------------------------------------------------------------------------- +typedef int (*AxisFormatter)(double value, char *buff, int size, void *data); + +struct Plot { + Plot(const char *name, const char *section); + virtual ~Plot(); + virtual void handle_input(int key, int mods); + virtual void flag_for_reset(); + const char *get_name() const; + const char *get_section() const; + void render(const ImVec2 &frame_size); + bool m_visible; +private: + static int hex_formatter(double value, char *buff, int size, void *data); + virtual AxisFormatter x_formatter() const; + virtual AxisFormatter y_formatter() const; + virtual float right_margin() const; + virtual int flags() const; + virtual void render_internal(const ImVec2 &frame_size) = 0; + virtual void render_post(const ImVec2 &frame_size); + const char *m_name; + const char *m_section; +}; + +struct PlotLines : public Plot { + PlotLines(const char *name, const char *section, std::vector trace_keys); +private: + void render_internal(const ImVec2 &frame_size); + std::vector m_trace_keys; +}; + +struct PlotStacked : public Plot { + PlotStacked(const char *name, const char *section, std::vector trace_keys); + void flag_for_reset(); +private: + static int percent_formatter(double value, char *buff, int size, void *data); + AxisFormatter y_formatter() const; + void render_internal(const ImVec2 &frame_size); + std::vector m_trace_keys; + std::vector m_trace_states; + Trace m_trace_totals; + std::vector> m_trace_normals; + bool m_needs_reset; +}; + +struct PlotHeatmap : public Plot { + PlotHeatmap(const char *name, const char *section, const char *trace_key); + void handle_input(int key, int mods); + void flag_for_reset(); +private: + float right_margin() const; + int flags() const; + void render_internal(const ImVec2 &frame_size); + void render_end(const ImVec2 &frame_size); + void render_post(const ImVec2 &frame_size); + const char *m_trace_key; + float m_tex_scale_pow; + float m_tex_scale_high; + size_t m_tex_rendered_last; + size_t m_rows_rendered; + GLuint m_tex_id; + std::vector m_tex_render; + bool m_needs_reset; +}; + +#endif diff --git a/data/server.c b/data/server.c new file mode 100644 index 0000000..038c807 --- /dev/null +++ b/data/server.c @@ -0,0 +1,390 @@ +// file : data/server.c +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] includes and macros +// [section] structs +// [section] globals +// [section] event array render function +// [section] sql callbacks +// [section] main functions + +// ---------------------------------------------------------------------------- +// [section] includes and macros +// ---------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "client.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" +#include "sql.h" + +#define BACKLOG 10 + +// ---------------------------------------------------------------------------- +// [section] structs +// ---------------------------------------------------------------------------- +struct Socket { + struct sockaddr_in addr; + int fd; +}; + +struct CallbackContext { + struct json_object *response; + int64_t response_rows; + int64_t hm_left; + int64_t hm_pixel_count; + int64_t hm_pixel_pow; +}; + +struct RenderContext { + const struct CallbackContext *callback_context; + void *blob; + size_t blob_size; + uint64_t eva[MVEC_SIZE]; + int64_t out[CLIENT_DEFVAL_HM_PIXEL_COUNT]; +}; + +// ---------------------------------------------------------------------------- +// [section] globals +// ---------------------------------------------------------------------------- +static struct json_object *g_response_header; +static size_t g_eva_count; +static char g_eva_pbuf[EVA_SAVE_NAME_LEN]; + +// ---------------------------------------------------------------------------- +// [section] event array render function +// ---------------------------------------------------------------------------- +static int eva_render(void *data) { + assert(data); + struct RenderContext *render_context = (struct RenderContext *)data; + int64_t hm_left = render_context->callback_context->hm_left; + int64_t hm_pixel_count = render_context->callback_context->hm_pixel_count; + int64_t hm_pixel_pow = render_context->callback_context->hm_pixel_pow; + int64_t hm_pixel_res = 1 << hm_pixel_pow; + const void *blob = render_context->blob; + size_t blob_size = render_context->blob_size; + assert(blob); + +#if !defined(NDEBUG) + int64_t hm_right = hm_left + hm_pixel_res * hm_pixel_count; +#endif + + assert(hm_left < (int64_t)MVEC_SIZE); + assert(hm_right <= (int64_t)MVEC_SIZE); + + // Inflate blob + struct InflateParams params = { + .avail_in = blob_size, + .size = sizeof(uint64_t) * MVEC_SIZE, + .in = (Bytef *)blob, + .out = (Bytef *)render_context->eva, + }; + + comp_inflate(¶ms); + comp_inflate_end(¶ms); + + for (int64_t i = 0; i < hm_pixel_count; i++) { + render_context->out[i] = 0l; + + for (int64_t j = 0; j < hm_pixel_res; j++) { + int64_t coord = hm_left + (i * hm_pixel_res) + j; + render_context->out[i] += render_context->eva[coord]; + } + } + + return 0; +} + +// ---------------------------------------------------------------------------- +// [section] sql callbacks +// ---------------------------------------------------------------------------- +static void sql_callback_add_column_name(sqlite3_stmt *sql_stmt, void *data) { + assert(sql_stmt); + assert(data); + assert(sqlite3_column_type(sql_stmt, 0) == SQLITE_TEXT); + assert(sqlite3_column_type(sql_stmt, 1) == SQLITE_TEXT); + assert(!strcmp(sqlite3_column_name(sql_stmt, 0), "name")); + assert(!strcmp(sqlite3_column_name(sql_stmt, 1), "type")); + const char *col_name = (const char *)sqlite3_column_text(sql_stmt, 0); + struct json_object *response_header = (struct json_object *)data; + + if (!json_object_object_get_ex(response_header, col_name, NULL)) { + json_object_object_add(response_header, col_name, json_object_new_array()); + } + + if (strstr(col_name, SALIS_EVENT_ARRAY_SIZE_COL_MARKER)) { + g_eva_count++; + } +} + +static void sql_callback_add_data(sqlite3_stmt *sql_stmt, void *data) { + assert(sql_stmt); + assert(data); + FILE *eva_file = NULL; + struct CallbackContext *callback_context = (struct CallbackContext *)data; + struct RenderContext *render_contexts = calloc(g_eva_count, sizeof(struct RenderContext)); + thrd_t *threads = calloc(g_eva_count, sizeof(thrd_t)); + size_t tid = 0; + + for (int i = 0; i < sqlite3_column_count(sql_stmt); i++) { + assert(i < sqlite3_column_count(sql_stmt)); + assert(sqlite3_column_type(sql_stmt, i) == SQLITE_INTEGER || sqlite3_column_type(sql_stmt, i) == SQLITE_FLOAT); + const char *col_name = sqlite3_column_name(sql_stmt, i); + struct json_object *col_data = json_object_object_get(callback_context->response, col_name); + assert(col_name); + + if (!col_data) continue; + + int64_t col_value = sqlite3_column_int64(sql_stmt, i); + json_object_array_add(col_data, json_object_new_int64(col_value)); + + if (i == 1) { + assert(!strcmp(col_name, "step")); + + int rem = snprintf( + g_eva_pbuf, + EVA_SAVE_NAME_LEN, + "%s/evas-%016lx", + SIM_EVAS, + col_value + ); + + assert(rem >= 0); + assert(rem < EVA_SAVE_NAME_LEN); + (void)rem; + + eva_file = fopen(g_eva_pbuf, "rb"); + assert(eva_file); + } + + if (sqlite3_column_type(sql_stmt, i + 1) == SQLITE_NULL) { + assert(eva_file); + assert(strstr(col_name, SALIS_EVENT_ARRAY_SIZE_COL_MARKER)); + render_contexts[tid].callback_context = callback_context; + render_contexts[tid].blob = malloc(col_value); + render_contexts[tid].blob_size = col_value; + assert(render_contexts[tid].blob); + size_t red = fread(render_contexts[tid].blob, 1, col_value, eva_file); + assert(red == (size_t)col_value); + (void)red; + + thrd_create(&threads[tid], (thrd_start_t)eva_render, &render_contexts[tid]); + tid++; + i++; + } + } + + assert(tid == g_eva_count); + tid = 0; + + for (int i = 0; i < sqlite3_column_count(sql_stmt); i++) { + const char *col_name = sqlite3_column_name(sql_stmt, i); + struct json_object *col_data = json_object_object_get(callback_context->response, col_name); + assert(col_name); + + if (!col_data) continue; + + if (sqlite3_column_type(sql_stmt, i + 1) == SQLITE_NULL) { + assert(strstr(col_name, SALIS_EVENT_ARRAY_SIZE_COL_MARKER)); + assert(render_contexts[tid].blob); + const char *eva_col_name = sqlite3_column_name(sql_stmt, i + 1); + struct json_object *eva_col_data = json_object_object_get(callback_context->response, eva_col_name); + assert(eva_col_name); + assert(eva_col_data); + thrd_join(threads[tid], NULL); + + for (int64_t j = 0; j < callback_context->hm_pixel_count; j++) { + json_object_array_add(eva_col_data, json_object_new_int64(render_contexts[tid].out[j])); + } + + free(render_contexts[tid].blob); + tid++; + i++; + } + } + + assert(tid == g_eva_count); + callback_context->response_rows++; + log_info("Processed row #%ld", callback_context->response_rows); + + assert(eva_file); + fclose(eva_file); + free(render_contexts); + free(threads); +} + +// ---------------------------------------------------------------------------- +// [section] main functions +// ---------------------------------------------------------------------------- +static void sig_handler(int signo) { + (void)signo; + log_attention("Signal received, will stop SALIS data server"); + json_object_put(g_response_header); + sql_close(); + exit(0); +} + +static void respond_name(int socket_fd) { + log_info("Client requested simulation name"); + struct json_object *sim_name = json_object_new_object(); + json_object_object_add(sim_name, "name", json_object_new_string(NAME)); + json_object_to_fd(socket_fd, sim_name, 0); + json_object_put(sim_name); +} + +static void respond_opts(int socket_fd) { + log_info("Client requested simulation options"); + struct json_object *sim_opts = json_object_from_file(SIM_OPTS); + json_object_to_fd(socket_fd, sim_opts, 0); + json_object_put(sim_opts); +} + +static void respond_hash(int socket_fd) { + log_info("Client requested git hash"); + char buff[41] = { 0 }; + FILE *pipe = popen("git rev-parse HEAD", "r"); + fread(buff, sizeof(char), 40, pipe); + pclose(pipe); + struct json_object *git_hash = json_object_new_object(); + json_object_object_add(git_hash, "hash", json_object_new_string(buff)); + json_object_to_fd(socket_fd, git_hash, 0); + json_object_put(git_hash); +} + +static void respond_data(int socket_fd, struct json_object *request) { + assert(request); + const char *request_str = json_object_to_json_string(request); + log_info("Client requested simulation data with the following parameters: %s", request_str); + const char *x_axis = json_object_get_string(json_object_object_get(request, "x-axis")); + int64_t x_current = json_object_get_int64(json_object_object_get(request, "x-current")); + int64_t x_high = json_object_get_int64(json_object_object_get(request, "x-high")); + int64_t nth = json_object_get_int64(json_object_object_get(request, "nth")); + int64_t entries = json_object_get_int64(json_object_object_get(request, "entries")); + + struct CallbackContext callback_context = { + .response = NULL, + .response_rows = 0l, + .hm_left = json_object_get_int64(json_object_object_get(request, "hm-left")), + .hm_pixel_count = json_object_get_int64(json_object_object_get(request, "hm-pixel-count")), + .hm_pixel_pow = json_object_get_int64(json_object_object_get(request, "hm-pixel-pow")), + }; + + json_object_deep_copy(g_response_header, &callback_context.response, NULL); + const char *x_axis_pref = (!strcmp(x_axis, "rowid") || !strcmp(x_axis, "step")) ? "core." : ""; + + sql_exec( + sql_callback_add_data, + &callback_context, + "select * from (" + "select core.rowid, core.step, * from core inner join arch " + "where core.rowid = arch.rowid and %s%s > %ld and %s%s <= %ld and core.rowid %% %ld == 0 " + "order by %s%s desc limit %ld" + ") order by %s asc;", + x_axis_pref, + x_axis, + x_current, + x_axis_pref, + x_axis, + x_high, + nth, + x_axis_pref, + x_axis, + entries, + x_axis + ); + + log_info("Sending client %ld rows of data", callback_context.response_rows); + json_object_to_fd(socket_fd, callback_context.response, 0); + json_object_put(callback_context.response); + shutdown(socket_fd, SHUT_WR); +} + +static int handle_client(struct Socket *socket) { + assert(socket); + char socket_ip[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &socket->addr.sin_addr, socket_ip, INET_ADDRSTRLEN); + log_info("Client connected: %s:%d", socket_ip, ntohs(socket->addr.sin_port)); + struct json_object *request_json = json_object_from_fd(socket->fd); + struct json_object *request_str = NULL; + + if (!json_object_object_get_ex(request_json, "request", &request_str)) assert(false); + + const char *request = json_object_get_string(request_str); + assert(request); + + if (!strcmp(request, "name")) { + respond_name(socket->fd); + } else if (!strcmp(request, "opts")) { + respond_opts(socket->fd); + } else if (!strcmp(request, "hash")) { + respond_hash(socket->fd); + } else if (!strcmp(request, "data")) { + respond_data(socket->fd, request_json); + } else { + assert(false); + } + + json_object_put(request_json); + log_info("Client disconnected: %s:%d", socket_ip, ntohs(socket->addr.sin_port)); + close(socket->fd); + free(socket); + return 0; +} + +int main(void) { + log_info("Initializing salis data server"); + log_info("Connecting to database in: %s", DATA_PUSH_PATH); + sql_open(); + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + signal(SIGPIPE, SIG_IGN); // ignore broken pipes + + log_info("Creating response header"); + g_response_header = json_object_new_object(); + json_object_object_add(g_response_header, "rowid", json_object_new_array()); + sql_exec( + sql_callback_add_column_name, + g_response_header, + "select name, type from pragma_table_info('core') union select name, type from pragma_table_info('arch');" + ); + log_info("Found %lu eva-size columns in database", g_eva_count); + + log_info("Binding to port: %d", PORT); + int opt = 1; + int socket_fd = socket(AF_INET, SOCK_STREAM, 0); + setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + struct sockaddr_in socket_addr = { 0 }; + socket_addr.sin_family = AF_INET; + socket_addr.sin_addr.s_addr = INADDR_ANY; + socket_addr.sin_port = htons(PORT); + bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)); + + listen(socket_fd, BACKLOG); + log_info("Listening..."); + + while (true) { + struct Socket *socket = calloc(1, sizeof(struct Socket)); + socklen_t socket_len = sizeof(struct sockaddr_in); + socket->fd = accept(socket_fd, (struct sockaddr *)&socket->addr, &socket_len); + + thrd_t thread; + thrd_create(&thread, (thrd_start_t)handle_client, socket); + thrd_detach(thread); + } + + return 0; +} diff --git a/docs/flowchart.odg b/docs/flowchart.odg new file mode 100644 index 0000000..73c4b26 Binary files /dev/null and b/docs/flowchart.odg differ diff --git a/docs/flowchart.svg b/docs/flowchart.svg new file mode 100644 index 0000000..b3d9284 --- /dev/null +++ b/docs/flowchart.svg @@ -0,0 +1,844 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/salis.py b/salis.py new file mode 100755 index 0000000..48436de --- /dev/null +++ b/salis.py @@ -0,0 +1,444 @@ +#!/usr/bin/env -S PYTHONDONTWRITEBYTECODE=1 python + +# file : salis.py +# project : Salis-VM +# author : Paul Oliver + +# index: +# [section] imports +# [section] argument parser +# [section] unit tests +# [section] build class +# [section] logger +# [section] options dict formatter +# [section] entry-point; source options +# [section] load instruction set +# [section] assemble ancestor +# [section] general defines +# [section] build and launch main executable +# [section] build and launch data server +# [section] build and launch data client + +# ------------------------------------------------------------------------------ +# [section] imports +# ------------------------------------------------------------------------------ +import ctypes +import json +import os +import random +import shutil +import socket +import subprocess +import sys +import unittest + +from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError, RawTextHelpFormatter +from tempfile import TemporaryDirectory +from types import SimpleNamespace + +# ------------------------------------------------------------------------------ +# [section] argument parser +# ------------------------------------------------------------------------------ +prog = sys.argv[0] +epilog = f"Use '-h' to show command arguments; e.g. '{prog} new -h'" + +parser = ArgumentParser(description="Salis: Simple A-Life Simulator", epilog=epilog, formatter_class=RawTextHelpFormatter, prog=prog) +sub_parsers = parser.add_subparsers(dest="command", required=True) +formatter_class = lambda prog: ArgumentDefaultsHelpFormatter(max_help_position=48, prog=prog) + +new = sub_parsers.add_parser("new", formatter_class=formatter_class, help="create new simulation") +load = sub_parsers.add_parser("load", formatter_class=formatter_class, help="load saved simulation") +server = sub_parsers.add_parser("server", formatter_class=formatter_class, help="run data server") +client = sub_parsers.add_parser("client", formatter_class=formatter_class, help="run data client") +test = sub_parsers.add_parser("test", formatter_class=formatter_class, help="run unit tests") + +def seed(i): + ival = int(i, 0) + if ival < -1: raise ArgumentTypeError("invalid seed value") + return ival + +def pos(i): + ival = int(i, 0) + if ival < 0: raise ArgumentTypeError("value must be positive integer") + return ival + +def nat(i): + ival = int(i, 0) + if ival < 1: raise ArgumentTypeError("value must be greater than zero") + return ival + +def port(i): + ival = int(i, 0) + if not 1024 <= ival <= 49151: raise ArgumentTypeError("value must be valid port number") + return ival + +fmt_id = lambda val: val +fmt_hex = lambda val: hex(int(val, 0)) if type(val) == str else hex(val) + +supported_c_compilers = ("clang", "gcc", "tcc") +supported_cpp_compilers = ("clang++", "g++") + +options = ( + ("a", "anc", (new,), fmt_id, {"metavar": "ANC", "help": "ancestor file name without extension; to be compiled on all cores; ANC points to '{anc-path}/{arch}/{ANC}.asm'", "required": True, "type": str}), + ("A", "anc-path", (new,), fmt_id, {"metavar": "PATH", "help": "path to search for ancestor files", "default": "anc", "required": False, "type": str}), + ("b", "build-only", (new, load, server, client), fmt_id, {"action": "store_true", "help": "only build (do not run) executable", "required": False}), + ("C", "clones", (new,), fmt_id, {"metavar": "N", "help": "number of ancestor clones on each core", "default": 1, "required": False, "type": nat}), + ("c", "cores", (new,), fmt_id, {"metavar": "N", "help": "number of simulator cores", "default": 2, "required": False, "type": nat}), + ("d", "data-push-pow", (new,), fmt_id, {"metavar": "POW", "help": "data aggregation interval exponent; interval = 2^{POW} >= {sync-pow}", "default": 28, "required": False, "type": nat}), + ("f", "font", (client,), fmt_id, {"metavar": "FILE", "help": "font face to use", "default": "/usr/share/fonts/droid/DroidSansMono.ttf", "required": False}), + ("f", "force", (new,), fmt_id, {"action": "store_true", "help": "overwrite existing simulation of given name", "required": False}), + ("g", "c-compiler", (new, load, server, client), fmt_id, {"choices": supported_c_compilers, "help": "C compiler to use", "default": "gcc", "required": False, "type": str}), + ("G", "cpp-compiler", (client,), fmt_id, {"choices": supported_cpp_compilers, "help": "C++ compiler to use", "default": "g++", "required": False, "type": str}), + ("H", "home", (new, load, server), fmt_id, {"metavar": "PATH", "help": "salis home directory", "default": f"{os.environ["HOME"]}/.salis", "required": False, "type": str}), + ("i", "ip", (client,), fmt_id, {"metavar": "IP", "help": "ip address of server", "default": "127.0.0.1", "required": False, "type": str}), + ("l", "log-trace", (new, load, server, client), fmt_id, {"action": "store_true", "help": "log verbose trace messages", "required": False}), + ("M", "muta-pow", (new,), fmt_id, {"metavar": "POW", "help": "mutator range exponent; each step a cosmic ray hits addr = rand_uint64() %% 2^{POW}; lower values of POW mean higher mutation rates", "default": 32, "required": False, "type": pos}), + ("m", "mvec-pow", (new,), fmt_id, {"metavar": "POW", "help": "memory core size exponent; size = 2^{POW}", "default": 20, "required": False, "type": pos}), + ("n", "name", (new, load, server), fmt_id, {"metavar": "NAME", "help": "name of new or loaded simulation", "default": "def.sim", "required": False, "type": str}), + ("o", "optimized", (new, load, server, client), fmt_id, {"action": "store_true", "help": "build with optimizations", "required": False}), + ("p", "pattern", (test,), fmt_id, {"metavar": "PATTERN", "help": "pattern for unit test discovery", "default": "*", "required": False, "type": str}), + ("P", "port", (server, client), fmt_id, {"metavar": "PORT", "help": "port number for data server", "default": 8080, "required": False, "type": port}), + ("p", "pre-cmd", (new, load, server, client), fmt_id, {"metavar": "CMD", "help": "shell command with which to wrap call to executable; e.g. gdb, time, valgrind, etc.", "required": False, "type": str}), + ("s", "seed", (new,), fmt_hex, {"metavar": "SEED", "help": "seed value for new simulation; a value of 0 disables cosmic rays; a value of -1 creates a random seed", "default": 0, "required": False, "type": seed}), + ("T", "keep-temp-dir", (new, load, server, client), fmt_id, {"action": "store_true", "help": "keep temporary directory on exit", "required": False}), + ("u", "ui", (new, load), fmt_id, {"metavar": "UI", "help": "user interface", "default": "curses", "required": False, "type": str}), + ("U", "ui-path", (new, load), fmt_id, {"metavar": "PATH", "help": "path to search for UIs", "default": "ui", "required": False, "type": str}), + ("v", "vm-arch", (new,), fmt_id, {"metavar": "ARCH", "help": "VM architecture", "default": "null", "required": False, "type": str}), + ("y", "sync-pow", (new,), fmt_id, {"metavar": "POW", "help": "core sync interval exponent; sync events occur every N steps, where N = 2^{POW}", "default": 20, "required": False, "type": pos}), + ("z", "auto-save-pow", (new,), fmt_id, {"metavar": "POW", "help": "auto-save interval exponent; interval = 2^{POW} >= {sync_pow}", "default": 36, "required": False, "type": pos}), +) + +[sub_parser.add_argument(f"-{sopt}", f"--{lopt}", **kwargs) for sopt, lopt, sub_parsers, _, kwargs in options for sub_parser in sub_parsers] +args = parser.parse_args() + +# ------------------------------------------------------------------------------ +# [section] unit tests +# ------------------------------------------------------------------------------ +if args.command == "test": + loader = unittest.TestLoader() + loader.testNamePatterns = [args.pattern] + runner = unittest.TextTestRunner(verbosity=2) + result = runner.run(loader.discover("test")) + exit(0 if not result.errors and not result.failures else 1) + +# ------------------------------------------------------------------------------ +# [section] build class +# ------------------------------------------------------------------------------ +class Build: + def __init__(self, path, is_library=False, is_object=False, is_cpp=False, flags=None, defines=None, links=None): + assert [is_library, is_object].count(True) <= 1 + + self.path = path + self.is_library = is_library + self.is_object = is_object + self.is_cpp = is_cpp + + self.flags = flags or [] + self.defines = defines or [] + self.links = links or [] + + self.basename = os.path.splitext(os.path.basename(self.path))[0] + self.extension = ".so" if self.is_library else "" + self.extension = ".o" if self.is_object else "" + self.tempdir = TemporaryDirectory(prefix="salis_", delete=not args.keep_temp_dir) + self.binfile = f"{self.tempdir.name}/{self.basename}{self.extension}" + + self.flags += ["-Wall", "-Wextra", "-Werror", "-Wmissing-declarations", "-Wno-overlength-strings", "-pedantic"] + self.flags += ["-std=c++20"] if self.is_cpp else ["-Wmissing-prototypes"] + self.flags += ["-fPIC", "-shared"] if self.is_library else [] + self.flags += ["-c"] if self.is_object else [] + self.flags += ["-O3", "-flto", "-march=native", "-mtune=native"] if args.optimized else ["-ggdb"] + self.defines += ["-DNDEBUG"] if args.optimized else [] + + self.compiler = args.cpp_compiler if self.is_cpp else args.c_compiler + self.build_cmd = [self.compiler, *self.flags, *self.defines, *self.links, self.path, "-o", self.binfile] + + def build(self): + subprocess.run(self.build_cmd, check=True) + + # Clear exec-stack on dynamic libraries when using TCC. + # Otherwise python will complain when loading the library with CDLL. + # NOTE: Using TCC thus requires patchelf to be accessible. + if self.is_library and args.c_compiler == "tcc": + subprocess.run(["patchelf", "--clear-execstack", self.binfile], check=True) + + def log(self, log): + log.trace(f"Built source '{self.path}' to output: {self.binfile}") + log.trace(f"Using build command: {self.build_cmd}") + + def run(self): + run_cmd = args.pre_cmd.split() if args.pre_cmd else [] + run_cmd.append(self.binfile) + proc = subprocess.Popen(run_cmd, stdout=sys.stdout, stderr=sys.stderr) + + # When a signal is received (e.g. SIGINT), is propagates through the entire process group. + # Handle signal on the interpreter and allow the simulator to shut down gracefully. + try: + proc.wait() + except KeyboardInterrupt: + proc.terminate() + proc.wait() + + code = proc.returncode + + if code != 0: raise RuntimeError(f"Binary returned code: {code}") + +# ------------------------------------------------------------------------------ +# [section] logger +# ------------------------------------------------------------------------------ +logger_defines = ["-DLOG_TRACE"] if args.log_trace else [] +logger_shared_build = Build("core/logger.c", is_library=True, defines=logger_defines) +logger_shared_build.build() +logger_dll = ctypes.CDLL(logger_shared_build.binfile) + +# Pull in logging functions from C +# This way there's just a single logging system to care about +log = SimpleNamespace() +log.trace = lambda msg: logger_dll.log_trace_to_stdout(msg.encode()) +log.info = lambda msg: logger_dll.log_info_to_stdout(msg.encode()) +log.attention = lambda msg: logger_dll.log_attention_to_stdout(msg.encode()) +logger_shared_build.log(log) + +# ------------------------------------------------------------------------------ +# [section] options dict formatter +# ------------------------------------------------------------------------------ +fmt_h2u = lambda field: field.replace("-", "_") +fmt_u2h = lambda field: field.replace("_", "-") + +def fmt_field(field, val): + for _, lopt, _, fmt, _ in options: + if lopt == fmt_u2h(field): + return fmt(val) + + return fmt_id(val) + +def fmt_opts(opts): + opts_out = {field: fmt_field(field, val) for field, val in opts.items()} + return json.dumps(opts_out, indent=2) + +# ------------------------------------------------------------------------------ +# [section] entry-point; source options +# ------------------------------------------------------------------------------ +log.info(f"Called '{prog} {args.command}' with the following options: {fmt_opts(vars(args))}") +paths = SimpleNamespace() + +def gen_paths(): + paths.sim_dir = f"{args.home}/{args.name}" + paths.sim_data = f"{paths.sim_dir}/{args.name}.sqlite3" + paths.sim_evas = f"{paths.sim_dir}/evas" + paths.sim_opts = f"{paths.sim_dir}/opts.json" + paths.sim_path = f"{paths.sim_dir}/{args.name}" + +def source_options(): + if not os.path.isdir(paths.sim_dir): + raise RuntimeError(f"No simulation found named {args.name}") + + with open(paths.sim_opts, "r") as f: + opts = json.loads(f.read()) + + for key, val in opts.items(): + setattr(args, key, val) + + log.info(f"Sourced configuration from '{paths.sim_opts}': {fmt_opts(opts)}") + +def request_from_server(request): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client: + client.connect((args.ip, args.port)) + client.sendall(json.dumps(request).encode()) + client.shutdown(socket.SHUT_WR) + return json.load(client.makefile(mode="r")) + +# Create new simulation +if args.command == "new": + gen_paths() + + if 0 < args.data_push_pow < args.sync_pow: + raise RuntimeError("Data push power must be equal or greater than core sync power") + + if 0 < args.auto_save_pow < args.sync_pow: + raise RuntimeError("Autosave power must be equal or greater than core sync power") + + if os.path.isdir(paths.sim_dir) and args.force: + log.attention(f"Force flag used! Wiping old simulation at: {paths.sim_dir}") + shutil.rmtree(paths.sim_dir) + + if os.path.isdir(paths.sim_dir): + raise RuntimeError(f"Simulation directory found at: {paths.sim_dir}; use --force flag to remove it") + + if args.seed == -1: + args.seed = random.getrandbits(64) + log.info(f"Using random seed: {hex(args.seed)}") + + log.info(f"Creating new simulation directory at: {paths.sim_dir}") + log.info(f"Creating new event-array storage directory at: {paths.sim_evas}") + log.info(f"Creating configuration file at: {paths.sim_opts}") + os.mkdir(paths.sim_dir) + os.mkdir(paths.sim_evas) + + opts = {fmt_h2u(lopt): getattr(args, fmt_h2u(lopt)) for _, lopt, sub_parsers, _, kwargs in options if new in sub_parsers and load not in sub_parsers} + + with open(paths.sim_opts, "w") as f: + f.write(f"{fmt_opts(opts)}\n") + +# Load saved simulation +if args.command == "load": + gen_paths() + source_options() + +# Data server +if args.command == "server": + gen_paths() + source_options() + +# Data client +if args.command == "client": + # Check provided font file exists + if not os.path.isfile(args.font): + raise RuntimeError(f"Font file {args.font} does not exist; please provide a valid font") + + # Check chosen compilers are compatible + if (args.cpp_compiler == "clang++" and args.c_compiler != "clang") or (args.cpp_compiler == "g++" and args.c_compiler != "gcc"): + raise RuntimeError(f"Compilers {args.cpp_compiler} and {args.c_compiler} might have incompatible ABIs; use either clang++/clang or g++/gcc") + + # Pull basic information from the server + # Required to build the client with correct flags + args.name = request_from_server({"request": "name"})["name"] + log.info(f"Sourced simulation name from '{args.ip}:{args.port}': {args.name}") + + opts = request_from_server({"request": "opts"}) + + for key, val in opts.items(): + setattr(args, key, val) + + log.info(f"Sourced configuration from '{args.ip}:{args.port}': {fmt_opts(opts)}") + + # Server and client should be on the same hash + server_hash = request_from_server({"request": "hash"})["hash"] + client_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip() + + if server_hash != client_hash: + raise RuntimeError(f"Server hash '{server_hash}' and client hash '{client_hash}' do not match") + + log.info(f"Confirmed server and client are running against the same git hash: {server_hash}") + +# ------------------------------------------------------------------------------ +# [section] load instruction set +# ------------------------------------------------------------------------------ +arch_inst_path = f"arch/{args.vm_arch}/arch_inst.c" +arch_build = Build(arch_inst_path, is_library=True, flags=["-Icore"]) +arch_build.build() +arch_build.log(log) + +arch_dll = ctypes.CDLL(arch_build.binfile) +arch_dll.arch_inst_mnemonic.argtypes = [ctypes.c_uint8] +arch_dll.arch_inst_mnemonic.restype = ctypes.c_char_p +arch_dll.arch_inst_count.restype = ctypes.c_int + +# ------------------------------------------------------------------------------ +# [section] assemble ancestor +# ------------------------------------------------------------------------------ +anc_path = f"{args.anc_path}/{args.vm_arch}/{args.anc}.asm" + +if not os.path.isfile(anc_path): + raise RuntimeError(f"Could not find ancestor file: {anc_path}") + +with open(anc_path, "r") as f: + lines = f.read().splitlines() + +lines = filter(lambda line: line and not line.startswith(";") and not line.isspace(), lines) +lines = map(lambda line: tuple(line.split()), lines) + +inst_count = arch_dll.arch_inst_count() +mnemo_map = {tuple(arch_dll.arch_inst_mnemonic(byte).decode().split()): byte for byte in range(inst_count)} +anc_bytes = [mnemo_map[line] for line in lines] +anc_repr = f"{{{",".join(map(str, anc_bytes))}}}" + +log.info(f"Compiled ancestor file '{anc_path}' into byte array: {anc_repr}") + +# ------------------------------------------------------------------------------ +# [section] general defines +# ------------------------------------------------------------------------------ +general_defines = [ + f"-DANC=\"{args.anc}\"", + f"-DANC_BYTES={anc_repr}", + f"-DANC_SIZE={len(anc_bytes)}", + f"-DARCH=\"{args.vm_arch}\"", + f"-DAUTOSAVE_INTERVAL={2 ** args.auto_save_pow}ul", + f"-DCLONES={args.clones}", + f"-DCOMMAND_{args.command.upper()}", + f"-DCORES={args.cores}", + f"-DDATA_PUSH_INTERVAL={2 ** args.data_push_pow}ul", + f"-DFOR_CORES={" ".join(f"FOR_CORE({i})" for i in range(args.cores))}", + f"-DMUTA_RANGE={2 ** args.muta_pow}ul", + f"-DMVEC_SIZE={2 ** args.mvec_pow}ul", + f"-DNAME=\"{args.name}\"", + f"-DSEED={args.seed}ul", + f"-DSYNC_INTERVAL={2 ** args.sync_pow}ul", +] + +if args.command == "new" or args.command == "load" or args.command == "server": + local_defines = [ + f"-DAUTOSAVE_NAME_LEN={len(paths.sim_path) + 18}", + f"-DDATA_PUSH_PATH=\"{paths.sim_data}\"", + f"-DEVA_SAVE_NAME_LEN={len(paths.sim_evas) + 23}", + f"-DSIM_DATA=\"{paths.sim_data}\"", + f"-DSIM_DIR=\"{paths.sim_dir}\"", + f"-DSIM_EVAS=\"{paths.sim_evas}\"", + f"-DSIM_OPTS=\"{paths.sim_opts}\"", + f"-DSIM_PATH=\"{paths.sim_path}\"", + ] + +# ------------------------------------------------------------------------------ +# [section] build and launch main executable +# ------------------------------------------------------------------------------ +if args.command == "new" or args.command == "load": + ui_path = f"{args.ui_path}/{args.ui}" + + if not os.path.isdir(ui_path): + raise RuntimeError(f"UI not found at '{ui_path}'") + + with open(f"{ui_path}/links.json", "r") as f: + ui_links = json.load(f) + + ui_flags = [f"-Iarch/{args.vm_arch}", "-Icore", f"arch/{args.vm_arch}/arch.c", arch_inst_path, "core/compress.c", "core/logger.c", "core/salis.c", "core/sql.c", "core/timer.c"] + ui_defines = general_defines + local_defines + logger_defines + ui_links += ["-lsqlite3", "-lz"] + ui_build = Build(f"{ui_path}/ui.c", flags=ui_flags, defines=ui_defines, links=ui_links) + ui_build.build() + ui_build.log(log) + + if not args.build_only: + ui_build.run() + +# ------------------------------------------------------------------------------ +# [section] build and launch data server +# ------------------------------------------------------------------------------ +if args.command == "server": + server_flags = [f"-Iarch/{args.vm_arch}", "-Icore", "core/compress.c", "core/logger.c", "core/sql.c"] + server_defines = general_defines + local_defines + logger_defines + [f"-DPORT={args.port}"] + server_links = ["-ljson-c", "-lsqlite3", "-lz"] + server_build = Build("data/server.c", flags=server_flags, defines=server_defines, links=server_links) + server_build.build() + server_build.log(log) + + if not args.build_only: + server_build.run() + +# ------------------------------------------------------------------------------ +# [section] build and launch data client +# ------------------------------------------------------------------------------ +if args.command == "client": + # Build logger separately as it's written in C + logger_object_build = Build("core/logger.c", is_object=True, defines=logger_defines) + logger_object_build.build() + logger_object_build.log(log) + + client_flags = [f"-Iarch/{args.vm_arch}", "-Icore", logger_object_build.binfile] + client_defines = general_defines + [f"-DFONT_SOURCE=\"{args.font}\"", f"-DIP=\"{args.ip}\"", f"-DPORT={args.port}", f"-DPORT_STR=\"{args.port}\""] + client_links = ["-lGL", "-lglfw", "-limgui", "-limplot", "-ljson-c", "-lm"] + client_build = Build("data/client.cpp", is_cpp=True, flags=client_flags, defines=client_defines, links=client_links) + client_build.build() + client_build.log(log) + + if not args.build_only: + client_build.run() diff --git a/test/anc/v1/1rops.asm b/test/anc/v1/1rops.asm new file mode 100644 index 0000000..6ab33a9 --- /dev/null +++ b/test/anc/v1/1rops.asm @@ -0,0 +1,109 @@ +; file : test/anc/v1/1rops.asm +; project : Salis-VM +; author : Paul Oliver + +incn +incn +nop0 +incn +nop1 +nop2 +incn +nop2 +nop3 +nop0 +incn +nop3 + +noop + +decn +decn +nop0 +decn +nop1 +nop2 +decn +nop2 +nop3 +nop0 +decn +nop3 + +noop + +incn +incn +incn +notn +notn +nop0 +notn +nop1 +nop2 +notn +nop2 +nop3 +nop0 +notn +nop3 + +noop + +shfl +shfl +nop0 +shfl +nop1 +nop2 +shfl +nop2 +nop3 +nop0 +shfl +nop3 + +noop + +shfr +shfr +nop0 +shfr +nop1 +nop2 +shfr +nop2 +nop3 +nop0 +shfr +nop3 + +noop + +zero +zero +nop0 +zero +nop1 +nop2 +zero +nop2 +nop3 +nop0 +zero +nop3 + +noop + +unit +unit +nop0 +unit +nop1 +nop2 +unit +nop2 +nop3 +nop0 +unit +nop3 diff --git a/test/anc/v1/2rops.asm b/test/anc/v1/2rops.asm new file mode 100644 index 0000000..c1021d9 --- /dev/null +++ b/test/anc/v1/2rops.asm @@ -0,0 +1,26 @@ +; file : test/anc/v1/2rops.asm +; project : Salis-VM +; author : Paul Oliver + +incn +incn +incn + +noop + +rdup +nop0 +nop1 +rdup +nop2 +rdup +nop1 +nop3 + +noop + +rswp +nop1 +rswp +nop3 +nop2 diff --git a/test/anc/v1/3rops.asm b/test/anc/v1/3rops.asm new file mode 100644 index 0000000..84c6596 --- /dev/null +++ b/test/anc/v1/3rops.asm @@ -0,0 +1,70 @@ +; file : test/anc/v1/3rops.asm +; project : Salis-VM +; author : Paul Oliver + +incn +addn +addn +nop0 +addn +nop1 +nop0 +addn +nop2 +nop1 +nop0 +addn +nop3 +nop2 +nop1 + +noop + +subn +nop3 +nop2 +nop1 +subn +nop3 +nop2 +subn +nop3 +subn +nop1 +nop2 +subn + +noop + +incn +incn +muln +muln +nop0 +muln +nop1 +nop0 +muln +nop2 +nop1 +nop0 +muln +nop3 +nop2 +nop1 + +noop + +divn +nop3 +nop2 +nop1 +divn +nop3 +nop2 +divn +nop3 +divn +nop1 +nop2 +divn diff --git a/test/anc/v1/addrs.asm b/test/anc/v1/addrs.asm new file mode 100644 index 0000000..b67532e --- /dev/null +++ b/test/anc/v1/addrs.asm @@ -0,0 +1,75 @@ +; file : test/anc/v1/addrs.asm +; project : Salis-VM +; author : Paul Oliver + +lokb +lokd +lokf +lokh +lokj +lokl +lokn +lokp + +noop +noop + +adrf +keya +adrb +keyb +nop1 +adrf +keyc +nop2 +adrb +keyd +nop3 +adrf +keye +nop0 +adrb +keyf +nop1 +adrf +keyg +nop2 +adrb +keyh +nop3 +adrf +keyi +nop0 +adrb +keyj +nop1 +adrf +keyk +nop2 +adrb +keyl +nop3 +adrf +keym +nop0 +adrb +keyn +nop1 +adrf +keyo +nop2 +adrb +keyp +nop3 + +noop +noop + +loka +lokc +loke +lokg +loki +lokk +lokm +loko diff --git a/test/anc/v1/allos.asm b/test/anc/v1/allos.asm new file mode 100644 index 0000000..37f8540 --- /dev/null +++ b/test/anc/v1/allos.asm @@ -0,0 +1,33 @@ +; file : test/anc/v1/allos.asm +; project : Salis-VM +; author : Paul Oliver + +incn +shfl +shfl + +malf +nop0 +nop1 +malf +malb +bspl +bspl +bspl + +malf +nop0 +nop1 +malb +malf +bswp +bclr + +noop +noop +noop + +malb +nop0 +nop1 +bspl diff --git a/test/anc/v1/ifnzs.asm b/test/anc/v1/ifnzs.asm new file mode 100644 index 0000000..86899b0 --- /dev/null +++ b/test/anc/v1/ifnzs.asm @@ -0,0 +1,50 @@ +; file : test/anc/v1/ifnzs.asm +; project : Salis-VM +; author : Paul Oliver + +ifnz +incn +incn +ifnz +nop0 +incn +nop0 + +noop + +ifnz +nop1 +incn +nop1 +incn +nop1 +ifnz +nop1 +incn +nop1 + +noop + +ifnz +nop2 +incn +nop2 +incn +nop2 +ifnz +nop2 +incn +nop2 + +noop + +ifnz +nop3 +incn +nop3 +incn +nop3 +ifnz +nop3 +incn +nop3 diff --git a/test/anc/v1/ioops.asm b/test/anc/v1/ioops.asm new file mode 100644 index 0000000..ca02d57 --- /dev/null +++ b/test/anc/v1/ioops.asm @@ -0,0 +1,104 @@ +; file : test/anc/v1/ioops.asm +; project : Salis-VM +; author : Paul Oliver + +keya +lokb +keyc +lokd +keye +lokf +keyg +lokh + +noop + +incn +nop1 +incn +nop2 +incn +nop2 +incn +nop3 +incn +nop3 +incn +nop3 +rpsh +rpsh +nop1 +rpsh +nop2 +rpsh +nop3 + +noop + +rloa +rloa +nop1 +nop1 +rloa +nop2 +nop2 +rloa +nop3 +nop3 + +rpop +nop3 +rpop +nop2 +rpop +nop1 +rpop +rpsh +rpsh +nop1 +rpsh +nop2 +rpsh +nop3 + +noop + +rloa +nop2 +nop3 +rloa +nop1 +nop2 +rloa +nop0 +nop1 +rloa + +noop + +muln +nop0 +nop1 +nop2 +muln +nop1 +nop2 +nop3 +muln +nop2 +nop3 +zero +nop3 + +rwrt +nop3 +incn +nop3 +rwrt +nop3 +nop1 +incn +nop3 +rwrt +nop3 +nop2 diff --git a/test/anc/v1/jumps.asm b/test/anc/v1/jumps.asm new file mode 100644 index 0000000..6cde582 --- /dev/null +++ b/test/anc/v1/jumps.asm @@ -0,0 +1,56 @@ +; file : test/anc/v1/jumps.asm +; project : Salis-VM +; author : Paul Oliver + +jmpf +keya +lokb +jmpf +keyc +lokd +jmpf +keye +lokf +jmpf +keyg +lokh +jmpf +keyi +lokj +jmpf +keyk +lokl +jmpf +keym +lokn +jmpf +keyo +lokp + +noop +noop + +loka +jmpb +keyb +lokc +jmpb +keyd +loke +jmpb +keyf +lokg +jmpb +keyh +loki +jmpb +keyj +lokk +jmpb +keyl +lokm +jmpb +keyn +loko +jmpb +keyp diff --git a/test/anc/v1/mbops.asm b/test/anc/v1/mbops.asm new file mode 100644 index 0000000..b57e88b --- /dev/null +++ b/test/anc/v1/mbops.asm @@ -0,0 +1,17 @@ +; file : test/anc/v1/mbops.asm +; project : Salis-VM +; author : Paul Oliver + +incn +shfl +shfl +malf + +bswp +bswp +bswp + +bclr + +bswp +bclr diff --git a/test/anc/v1/noops.asm b/test/anc/v1/noops.asm new file mode 100644 index 0000000..e4547c2 --- /dev/null +++ b/test/anc/v1/noops.asm @@ -0,0 +1,42 @@ +; file : test/anc/v1/noops.asm +; project : Salis-VM +; author : Paul Oliver + +noop +nop0 +nop1 +nop2 +nop3 + +keya +keyb +keyc +keyd +keye +keyf +keyg +keyh +keyi +keyj +keyk +keyl +keym +keyn +keyo +keyp +loka +lokb +lokc +lokd +loke +lokf +lokg +lokh +loki +lokj +lokk +lokl +lokm +lokn +loko +lokp diff --git a/test/anc/v1/stack.asm b/test/anc/v1/stack.asm new file mode 100644 index 0000000..bce1a25 --- /dev/null +++ b/test/anc/v1/stack.asm @@ -0,0 +1,51 @@ +; file : test/anc/v1/stack.asm +; project : Salis-VM +; author : Paul Oliver + +incn +incn +nop1 +shfl +nop1 +incn +nop2 +shfl +nop2 +shfl +nop2 + +noop + +rpsh +rpsh +nop1 +rpsh +nop2 +rpsh +nop3 +rpsh +nop0 +rpsh +nop1 +rpsh +nop2 +rpsh +nop3 + +noop + +rpop +rpop +nop1 +rpop +nop2 +rpop +nop3 +rpop +nop0 +rpop +nop1 +rpop +nop2 +rpop +nop3 diff --git a/test/test_build.py b/test/test_build.py new file mode 100644 index 0000000..e13669a --- /dev/null +++ b/test/test_build.py @@ -0,0 +1,114 @@ +# file : test/test_build.py +# project : Salis-VM +# author : Paul Oliver + +# index: +# [section] command lists +# [section] tests null +# [section] tests v1 +# [section] tests server & client + +import signal + +from test_general import SalisTest + +class TestBuild(SalisTest): + SIM_NAME = "test-build.sim" + + # -------------------------------------------------------------------------- + # [section] command lists + # -------------------------------------------------------------------------- + def commands_new(self, anc, arch): + return [ + f"./salis.py new -a{anc} -b -f -g{compiler} -H{self.tempdir.name} -n{self.SIM_NAME} {log_trace} {optimized} -s{seed} -u{ui} -v{arch}" + for compiler in ["clang", "gcc", "tcc"] + for log_trace in ["", "-l"] + for optimized in ["", "-o"] + for seed in [-1, 0, 1234] + for ui in ["curses", "daemon"] + ] + + def commands_load(self): + return [ + f"./salis.py load -b -g{compiler} -H{self.tempdir.name} -n{self.SIM_NAME} {log_trace} {optimized} -u{ui}" + for compiler in ["clang", "gcc", "tcc"] + for log_trace in ["", "-l"] + for optimized in ["", "-o"] + for ui in ["curses", "daemon"] + ] + + def cmds_server(self): + return [ + f"./salis.py server -b -g{compiler} -H{self.tempdir.name} -n{self.SIM_NAME} {optimized}" + for compiler in ["clang", "gcc"] + for optimized in ["", "-o"] + ] + + def cmds_client(self): + return [ + f"./salis.py client -b -g{c_compiler} -G{cpp_compiler} {optimized}" + for c_compiler, cpp_compiler in [("clang", "clang++"), ("gcc", "g++")] + for optimized in ["", "-o"] + ] + + # -------------------------------------------------------------------------- + # [section] tests null + # -------------------------------------------------------------------------- + def test_null_new(self): + for command in self.commands_new("0123", "null"): + self.run_subprocess(command) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + def test_null_load(self): + self.run_subprocess(self.commands_new("0123", "null")[0]) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + for command in self.commands_load(): + self.run_subprocess(command) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + # -------------------------------------------------------------------------- + # [section] tests v1 + # -------------------------------------------------------------------------- + def test_v1_new(self): + for command in self.commands_new("55a", "v1"): + self.run_subprocess(command) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + def test_v1_load(self): + self.run_subprocess(self.commands_new("55a", "v1")[0]) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + for command in self.commands_load(): + self.run_subprocess(command) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + # -------------------------------------------------------------------------- + # [section] tests server & client + # -------------------------------------------------------------------------- + def client_test(self): + with self.run_pipe(f"./salis.py server -H{self.tempdir.name}") as server_proc: + for line in server_proc.stdout: + if "Listening..." in line: + break + + for cmd in self.cmds_client(): + self.run_subprocess(cmd) + + server_proc.send_signal(signal.SIGINT) + + def test_server(self): + self.run_subprocess(self.commands_new("0123", "null")[0]) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + for cmd in self.cmds_server(): + self.run_subprocess(cmd) + self.assert_files_exist([f"{self.tempdir.name}/{self.SIM_NAME}/opts.json"]) + + def test_client_null(self): + self.run_ui_test("0123", "anc", "null", "test/ui", "null") + self.client_test() + + def test_client_v1(self): + self.run_ui_test("55a", "anc", "null", "test/ui", "v1") + self.client_test() diff --git a/test/test_determinism.py b/test/test_determinism.py new file mode 100644 index 0000000..0d40fcb --- /dev/null +++ b/test/test_determinism.py @@ -0,0 +1,50 @@ +# file : test/test_determinism.py +# project : Salis-VM +# author : Paul Oliver +# +# This test ensures simulator produces deterministic results. +# Resulting save, eva and DB files must be exactly the same for all simulations, +# whenever the same seed value is used. + +import hashlib +import os + +from test_general import SalisTest + +class TestDeterminism(SalisTest): + SIM_OPTS = { + "anc": "55a", + "anc_path": "anc", + "name": "determinism.sim", + "ui_path": "test/ui", + "vm_arch": "v1", + } + + SIM_OPTS_ASA4 = SIM_OPTS | {"ui": "asa4"} + SIM_OPTS_PUSH = SIM_OPTS | {"ui": "push"} + + def get_asa4_sums(self): + self.run_ui_test(**self.SIM_OPTS_ASA4, clones=2, cores=2, mvec_pow=16, seed=8765432, data_push_pow=20, sync_pow=16, auto_save_pow=20) + sim_path = f"{self.tempdir.name}/{self.SIM_OPTS["name"]}" + auto_saves = sorted(f"{sim_path}/{f}" for f in os.listdir(sim_path)) + auto_saves = filter(lambda f: f.startswith(f"{sim_path}/{self.SIM_OPTS["name"]}-"), auto_saves) + auto_saves = list(auto_saves)[:4] + evas = sorted(f"{sim_path}/evas/{f}" for f in os.listdir(f"{sim_path}/evas")) + evas = list(evas)[:4] + return {f: hashlib.md5(open(f, "rb").read()).hexdigest() for f in auto_saves + evas} + + def get_db_sums(self): + self.run_ui_test(**self.SIM_OPTS_PUSH, clones=2, cores=2, mvec_pow=16, seed=8765432, data_push_pow=20, sync_pow=16, auto_save_pow=20) + sim_path = f"{self.tempdir.name}/{self.SIM_OPTS["name"]}" + db_path = f"{sim_path}/{self.SIM_OPTS["name"]}.sqlite3" + return hashlib.md5(open(db_path, "rb").read()).hexdigest() + + def test_deterministic_saves_and_evas(self): + sums_1 = self.get_asa4_sums() + sums_2 = self.get_asa4_sums() + self.assertEqual(sums_1, sums_2) + + def test_deterministic_db(self): + sum_1 = self.get_db_sums() + sum_2 = self.get_db_sums() + self.assertEqual(sum_1, sum_2) diff --git a/test/test_general.py b/test/test_general.py new file mode 100644 index 0000000..2286b4f --- /dev/null +++ b/test/test_general.py @@ -0,0 +1,58 @@ +# file : test/test_general.py +# project : Salis-VM +# author : Paul Oliver + +# index: +# [section] setup/teardown +# [section] file lists +# [section] assertions +# [section] subprocess + +import os +import subprocess +import unittest + +from tempfile import TemporaryDirectory + +class SalisTest(unittest.TestCase): + # -------------------------------------------------------------------------- + # [section] setup/teardown + # -------------------------------------------------------------------------- + def setUp(self): + self.tempdir = TemporaryDirectory(prefix="salis_test_") + + def tearDown(self): + self.tempdir.cleanup() + + # -------------------------------------------------------------------------- + # [section] file lists + # -------------------------------------------------------------------------- + def files_on_new(self, name): + return [ + f"{self.tempdir.name}/{name}/evas/evas-{0:016x}", + f"{self.tempdir.name}/{name}/opts.json", + f"{self.tempdir.name}/{name}/{name}", + f"{self.tempdir.name}/{name}/{name}-{0:016x}", + f"{self.tempdir.name}/{name}/{name}.sqlite3", + ] + + # -------------------------------------------------------------------------- + # [section] assertions + # -------------------------------------------------------------------------- + def assert_files_exist(self, files): + for file in files: + assert os.path.isfile(file), f"{file} does not exist" + + # -------------------------------------------------------------------------- + # [section] subprocess + # -------------------------------------------------------------------------- + def run_subprocess(self, command): + subprocess.run(command.split(), check=True, stdout=subprocess.DEVNULL) + + def run_pipe(self, command): + return subprocess.Popen(command.split(), stdout=subprocess.PIPE, text=True) + + def run_ui_test(self, anc, anc_path, ui, ui_path, vm_arch, clones=1, cores=1, mvec_pow=20, name="def.sim", seed=0, data_push_pow=28, sync_pow=20, auto_save_pow=36, **kwargs): + command = f"./salis.py new -a{anc} -A{anc_path} -C{clones} -c{cores} -d{data_push_pow} -f -H{self.tempdir.name} -m{mvec_pow} -n{name} -s{seed} -u{ui} -U{ui_path} -v{vm_arch} -y{sync_pow} -z{auto_save_pow}" + self.run_subprocess(command) + self.assert_files_exist(self.files_on_new(name)) diff --git a/test/test_run.py b/test/test_run.py new file mode 100644 index 0000000..c86b42b --- /dev/null +++ b/test/test_run.py @@ -0,0 +1,59 @@ +# file : test/test_run.py +# project : Salis-VM +# author : Paul Oliver + +# index: +# [section] command lists +# [section] file lists +# [section] tests null +# [section] tests v1 + +from test_general import SalisTest + +class TestRun(SalisTest): + SIM_NAME = "test-run.sim" + PUSH_POW = 8 + + # -------------------------------------------------------------------------- + # [section] command lists + # -------------------------------------------------------------------------- + def commands(self, anc, arch): + return [ + ( + f"./salis.py new -a{anc} -d{self.PUSH_POW} -f -g{compiler} -H{self.tempdir.name} {log_trace} -n{self.SIM_NAME} {optimized} -s{seed} -upush -Utest/ui/ -v{arch} -y{self.PUSH_POW}", + f"./salis.py load -g{compiler} -H{self.tempdir.name} {log_trace} -n{self.SIM_NAME} {optimized} -upush -Utest/ui/", + ) + for compiler in ["clang", "gcc", "tcc"] + for log_trace in ["", "-l"] + for optimized in ["", "-o"] + for seed in [-1, 0, 1234] + ] + + # -------------------------------------------------------------------------- + # [section] file lists + # -------------------------------------------------------------------------- + def files_on_first_push(self): + return [*self.files_on_new(self.SIM_NAME), f"{self.tempdir.name}/{self.SIM_NAME}/evas/evas-{2 ** self.PUSH_POW:016x}"] + + def files_on_second_push(self): + return [*self.files_on_first_push(), f"{self.tempdir.name}/{self.SIM_NAME}/evas/evas-{2 * (2 ** self.PUSH_POW):016x}"] + + # -------------------------------------------------------------------------- + # [section] tests null + # -------------------------------------------------------------------------- + def test_null(self): + for command in self.commands("0123", "null"): + self.run_subprocess(command[0]) + self.assert_files_exist(self.files_on_first_push()) + self.run_subprocess(command[1]) + self.assert_files_exist(self.files_on_second_push()) + + # -------------------------------------------------------------------------- + # [section] tests v1 + # -------------------------------------------------------------------------- + def test_v1(self): + for command in self.commands("55a", "v1"): + self.run_subprocess(command[0]) + self.assert_files_exist(self.files_on_first_push()) + self.run_subprocess(command[1]) + self.assert_files_exist(self.files_on_second_push()) diff --git a/test/test_v1.py b/test/test_v1.py new file mode 100644 index 0000000..b84feb2 --- /dev/null +++ b/test/test_v1.py @@ -0,0 +1,51 @@ +# file : test/test_v1.py +# project : Salis-VM +# author : Paul Oliver + +# index: +# [section] tests + +from test_general import SalisTest + +class TestV1(SalisTest): + SIM_OPTS = { + "anc_path": "test/anc", + "ui_path": "test/ui/v1", + "vm_arch": "v1", + } + + # -------------------------------------------------------------------------- + # [section] tests + # -------------------------------------------------------------------------- + def test_noops(self): + self.run_ui_test(**self.SIM_OPTS, anc="noops", ui="noops") + + def test_jumps(self): + self.run_ui_test(**self.SIM_OPTS, anc="jumps", ui="jumps") + + def test_addrs(self): + self.run_ui_test(**self.SIM_OPTS, anc="addrs", ui="addrs") + + def test_ifnzs(self): + self.run_ui_test(**self.SIM_OPTS, anc="ifnzs", ui="ifnzs") + + def test_allos(self): + self.run_ui_test(**self.SIM_OPTS, anc="allos", ui="allos") + + def test_mbops(self): + self.run_ui_test(**self.SIM_OPTS, anc="mbops", ui="mbops") + + def test_3rops(self): + self.run_ui_test(**self.SIM_OPTS, anc="3rops", ui="3rops") + + def test_1rops(self): + self.run_ui_test(**self.SIM_OPTS, anc="1rops", ui="1rops") + + def test_stack(self): + self.run_ui_test(**self.SIM_OPTS, anc="stack", ui="stack") + + def test_ioops(self): + self.run_ui_test(**self.SIM_OPTS, anc="ioops", ui="ioops") + + def test_2rops(self): + self.run_ui_test(**self.SIM_OPTS, anc="2rops", ui="2rops") diff --git a/test/ui/asa4/links.json b/test/ui/asa4/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/asa4/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/asa4/ui.c b/test/ui/asa4/ui.c new file mode 100644 index 0000000..5a96e3f --- /dev/null +++ b/test/ui/asa4/ui.c @@ -0,0 +1,29 @@ +// file : test/ui/asa4/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" + +int main(void) { + salis_init(); + + // Run threads until 4 autosaves are made + while (g_step < AUTOSAVE_INTERVAL * 4) { + salis_start(); + sleep(1); + salis_signal_stop(); + salis_wait(); + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/null/links.json b/test/ui/null/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/null/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/null/ui.c b/test/ui/null/ui.c new file mode 100644 index 0000000..7366e53 --- /dev/null +++ b/test/ui/null/ui.c @@ -0,0 +1,24 @@ +// file : test/ui/null/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" + +int main(void) { +#if defined(COMMAND_NEW) + salis_init(); +#elif defined(COMMAND_LOAD) + salis_load(); +#endif + salis_save_and_free(); + return 0; +} diff --git a/test/ui/push/links.json b/test/ui/push/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/push/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/push/ui.c b/test/ui/push/ui.c new file mode 100644 index 0000000..cc5922f --- /dev/null +++ b/test/ui/push/ui.c @@ -0,0 +1,26 @@ +// file : test/ui/push/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" + +int main(void) { +#if defined(COMMAND_NEW) + salis_init(); +#elif defined(COMMAND_LOAD) + salis_load(); +#endif + + salis_step(DATA_PUSH_INTERVAL); + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/1rops/links.json b/test/ui/v1/1rops/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/1rops/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/1rops/ui.c b/test/ui/v1/1rops/ui.c new file mode 100644 index 0000000..637e3f5 --- /dev/null +++ b/test/ui/v1/1rops/ui.c @@ -0,0 +1,74 @@ +// file : test/ui/v1/1rops/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + salis_init(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + uint8_t inst = mvec_get_inst(g_cores, g_cores->pvec->ip); + + if (inst != incn && inst != decn && inst != notn && inst != shfl && inst != shfr && inst != zero && inst != unit) { + salis_step(1); + continue; + } + + uint64_t *reg = &g_cores->pvec->r0x; + + switch (mvec_get_inst(g_cores, g_cores->pvec->ip + 1)) { + case nop0: + reg = &g_cores->pvec->r0x; + break; + case nop1: + reg = &g_cores->pvec->r1x; + break; + case nop2: + reg = &g_cores->pvec->r2x; + break; + case nop3: + reg = &g_cores->pvec->r3x; + break; + } + + uint64_t reg_val = *reg; + + salis_step(1); + + switch (inst) { + case incn: + assert(*reg == reg_val + 1); + break; + case decn: + assert(*reg == reg_val - 1); + break; + case notn: + assert(*reg == !reg_val); + break; + case shfl: + assert(*reg == reg_val << 1); + break; + case shfr: + assert(*reg == reg_val >> 1); + break; + case zero: + assert(*reg == 0); + break; + case unit: + assert(*reg == 1); + break; + } + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/2rops/links.json b/test/ui/v1/2rops/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/2rops/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/2rops/ui.c b/test/ui/v1/2rops/ui.c new file mode 100644 index 0000000..e74172b --- /dev/null +++ b/test/ui/v1/2rops/ui.c @@ -0,0 +1,56 @@ +// file : test/ui/v1/2rops/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + salis_init(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + uint8_t inst = mvec_get_inst(g_cores, g_cores->pvec->ip); + + if (inst != rdup && inst != rswp) { + salis_step(1); + continue; + } + + uint64_t *regs[2] = { + &g_cores->pvec->r0x, + &g_cores->pvec->r0x, + }; + + for (uint64_t i = 0; i < 2; i++) { + uint8_t rmod = mvec_get_inst(g_cores, g_cores->pvec->ip + 1 + i); + if (rmod == nop0) regs[i] = &g_cores->pvec->r0x; + else if (rmod == nop1) regs[i] = &g_cores->pvec->r1x; + else if (rmod == nop2) regs[i] = &g_cores->pvec->r2x; + else if (rmod == nop3) regs[i] = &g_cores->pvec->r3x; + else break; + } + + uint64_t reg_vals[2] = {*regs[0], *regs[1]}; + salis_step(1); + + switch (inst) { + case rdup: + assert(*regs[1] == reg_vals[0]); + break; + case rswp: + assert(*regs[0] == reg_vals[1]); + assert(*regs[1] == reg_vals[0]); + break; + } + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/3rops/links.json b/test/ui/v1/3rops/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/3rops/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/3rops/ui.c b/test/ui/v1/3rops/ui.c new file mode 100644 index 0000000..2a53197 --- /dev/null +++ b/test/ui/v1/3rops/ui.c @@ -0,0 +1,65 @@ +// file : test/ui/v1/3rops/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + salis_init(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + uint8_t inst = mvec_get_inst(g_cores, g_cores->pvec->ip); + + if (inst != addn && inst != subn && inst != muln && inst != divn) { + salis_step(1); + continue; + } + + uint64_t *regs[3] = { + &g_cores->pvec->r0x, + &g_cores->pvec->r0x, + &g_cores->pvec->r0x, + }; + + for (uint64_t i = 0; i < 3; i++) { + uint8_t rmod = mvec_get_inst(g_cores, g_cores->pvec->ip + 1 + i); + + if (rmod == nop0) regs[i] = &g_cores->pvec->r0x; + else if (rmod == nop1) regs[i] = &g_cores->pvec->r1x; + else if (rmod == nop2) regs[i] = &g_cores->pvec->r2x; + else if (rmod == nop3) regs[i] = &g_cores->pvec->r3x; + else break; + } + + uint64_t reg_vals[3] = {*regs[0], *regs[1], *regs[2]}; + salis_step(1); + uint64_t result = *regs[0]; + + switch (inst) { + case addn: + assert(result == reg_vals[1] + reg_vals[2]); + break; + case subn: + assert(result == reg_vals[1] - reg_vals[2]); + break; + case muln: + assert(result == reg_vals[1] * reg_vals[2]); + break; + case divn: + assert(reg_vals[2] != 0); + assert(result == reg_vals[1] / reg_vals[2]); + break; + } + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/addrs/links.json b/test/ui/v1/addrs/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/addrs/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/addrs/ui.c b/test/ui/v1/addrs/ui.c new file mode 100644 index 0000000..eb4361c --- /dev/null +++ b/test/ui/v1/addrs/ui.c @@ -0,0 +1,58 @@ +// file : test/ui/v1/addrs/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +static void check_found(uint64_t addr, uint64_t lok, uint64_t regi) { + assert(mvec_get_inst(g_cores, addr - 1) == (regi % 2 == 0 ? adrf : adrb)); + assert(mvec_get_inst(g_cores, (&g_cores->pvec->r0x)[regi]) == lok); +} + +int main(void) { + uint64_t regi = 0; + + salis_init(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + uint8_t inst = mvec_get_inst(g_cores, g_cores->pvec->ip); + + switch (inst) { +#define CASE(id) \ + case key##id: \ + check_found(g_cores->pvec->ip, lok##id, regi); \ + regi = (regi + 1) % 4; \ + break; + CASE(a) + CASE(b) + CASE(c) + CASE(d) + CASE(e) + CASE(f) + CASE(g) + CASE(h) + CASE(i) + CASE(j) + CASE(k) + CASE(l) + CASE(m) + CASE(n) + CASE(o) + CASE(p) +#undef CASE + } + + salis_step(1); + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/allos/links.json b/test/ui/v1/allos/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/allos/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/allos/ui.c b/test/ui/v1/allos/ui.c new file mode 100644 index 0000000..5b0da79 --- /dev/null +++ b/test/ui/v1/allos/ui.c @@ -0,0 +1,80 @@ +// file : test/ui/v1/allos/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +#define CHILD_SIZE 4 + +static const struct Proc *first_proc(void) { + return proc_fetch(g_cores, g_cores->pfst); +} + +static void check_new_proc(void) { + const struct Proc *proc = proc_get(g_cores, g_cores->plst); + + assert(proc->mb0s == CHILD_SIZE); + assert(proc->mb1a == 0); + assert(proc->mb1s == 0); + assert(proc->r0x == 0); + assert(proc->r1x == 0); + assert(proc->r2x == 0); + assert(proc->r3x == 0); + assert(proc->s0 == 0); + assert(proc->s1 == 0); + assert(proc->s2 == 0); + assert(proc->s3 == 0); + assert(proc->s4 == 0); + assert(proc->s5 == 0); + assert(proc->s6 == 0); + assert(proc->s7 == 0); + + uint64_t birth_addr = (uint64_t)-1; + + switch (g_cores->plst) { + case 1: + birth_addr = ANC_SIZE; + break; + case 2: + birth_addr = ANC_SIZE - 7; + break; + default: + assert(false); + } + + assert(proc->ip == birth_addr); + assert(proc->sp == birth_addr); + assert(proc->mb0a == birth_addr); +} + +int main(void) { + salis_init(); + + while (first_proc()->ip < (ANC_SIZE + (CHILD_SIZE * 2))) { + bool will_split = g_cores->pcur == g_cores->pfst && mvec_get_inst(g_cores, first_proc()->ip) == bspl && first_proc()->mb1s; + uint64_t pnum = g_cores->pnum; + + salis_step(1); + + if (will_split) { + assert(g_cores->pnum == pnum + 1); + assert(g_cores->plst == pnum); + check_new_proc(); + } else { + assert(g_cores->pnum == pnum); + } + } + + assert(g_cores->pnum == 3); + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/ifnzs/links.json b/test/ui/v1/ifnzs/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/ifnzs/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/ifnzs/ui.c b/test/ui/v1/ifnzs/ui.c new file mode 100644 index 0000000..0c3921e --- /dev/null +++ b/test/ui/v1/ifnzs/ui.c @@ -0,0 +1,56 @@ +// file : test/ui/v1/ifnzs/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + uint64_t next_addr = 0; + + salis_init(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + assert(g_cores->pvec->ip == next_addr); + + if (mvec_get_inst(g_cores, g_cores->pvec->ip) == ifnz) { + switch (mvec_get_inst(g_cores, g_cores->pvec->ip + 1)) { + case nop0: + next_addr += g_cores->pvec->r0x ? 1 : 2; + break; + case nop1: + next_addr += g_cores->pvec->r1x ? 1 : 2; + break; + case nop2: + next_addr += g_cores->pvec->r2x ? 1 : 2; + break; + case nop3: + next_addr += g_cores->pvec->r3x ? 1 : 2; + break; + default: + next_addr += g_cores->pvec->r0x ? 0 : 1; + } + } + + salis_step(1); + next_addr++; + + assert(g_cores->pvec->ip == next_addr); + } + + assert(g_cores->pvec->ip == ANC_SIZE); + assert(g_cores->pvec->r0x == 2); + assert(g_cores->pvec->r1x == 2); + assert(g_cores->pvec->r2x == 2); + assert(g_cores->pvec->r3x == 2); + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/ioops/links.json b/test/ui/v1/ioops/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/ioops/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/ioops/ui.c b/test/ui/v1/ioops/ui.c new file mode 100644 index 0000000..8ec21b2 --- /dev/null +++ b/test/ui/v1/ioops/ui.c @@ -0,0 +1,66 @@ +// file : test/ui/v1/ioops/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + salis_init(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + uint8_t inst = mvec_get_inst(g_cores, g_cores->pvec->ip); + + if (inst != rloa && inst != rwrt) { + salis_step(1); + continue; + } + + uint64_t *regs[2] = { + &g_cores->pvec->r0x, + &g_cores->pvec->r0x, + }; + + for (uint64_t i = 0; i < 2; i++) { + uint8_t rmod = mvec_get_inst(g_cores, g_cores->pvec->ip + 1 + i); + + if (rmod == nop0) regs[i] = &g_cores->pvec->r0x; + else if (rmod == nop1) regs[i] = &g_cores->pvec->r1x; + else if (rmod == nop2) regs[i] = &g_cores->pvec->r2x; + else if (rmod == nop3) regs[i] = &g_cores->pvec->r3x; + else break; + } + + if (g_cores->pvec->sp != *regs[0]) { + salis_step(1); + continue; + } + + uint64_t reg_vals[2] = {*regs[0], *regs[1]}; + + assert(g_cores->pvec->sp == *regs[0]); + salis_step(1); + assert(g_cores->pvec->sp == g_cores->pvec->ip); + + switch (inst) { + case rloa: + assert(*regs[1] == (uint64_t)mvec_get_inst(g_cores, reg_vals[0])); + break; + case rwrt: + assert(*regs[0] == reg_vals[0]); + assert(*regs[1] == reg_vals[1]); + assert(mvec_get_inst(g_cores, *regs[0]) == (*regs[1] & ARCH_INST_MASK)); + break; + } + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/jumps/links.json b/test/ui/v1/jumps/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/jumps/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/jumps/ui.c b/test/ui/v1/jumps/ui.c new file mode 100644 index 0000000..2ef5caa --- /dev/null +++ b/test/ui/v1/jumps/ui.c @@ -0,0 +1,31 @@ +// file : test/ui/v1/jumps/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + uint8_t lok = loka; + + salis_init(); + + while (lok != lokp) { + if (mvec_get_inst(g_cores, g_cores->pvec->ip) == lok) { + lok = lok + 1; + } + + salis_step(1); + assert(g_cores->pvec->ip < ANC_SIZE); + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/mbops/links.json b/test/ui/v1/mbops/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/mbops/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/mbops/ui.c b/test/ui/v1/mbops/ui.c new file mode 100644 index 0000000..ea417d6 --- /dev/null +++ b/test/ui/v1/mbops/ui.c @@ -0,0 +1,46 @@ +// file : test/ui/v1/mbops/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + salis_init(); + + while (g_cores->pvec->ip < ANC_SIZE) { + bool on_swap = mvec_get_inst(g_cores, g_cores->pvec->ip) == bswp; + bool on_clear = mvec_get_inst(g_cores, g_cores->pvec->ip) == bclr; + + uint64_t mb0a = g_cores->pvec->mb0a; + uint64_t mb0s = g_cores->pvec->mb0s; + uint64_t mb1a = g_cores->pvec->mb1a; + uint64_t mb1s = g_cores->pvec->mb1s; + + salis_step(1); + + if (on_swap) { + if (mb1s) { + assert(g_cores->pvec->mb0a == mb1a); + assert(g_cores->pvec->mb0s == mb1s); + assert(g_cores->pvec->mb1a == mb0a); + assert(g_cores->pvec->mb1s == mb0s); + } + } + + if (on_clear) { + assert(g_cores->pvec->mb1a == 0); + assert(g_cores->pvec->mb1s == 0); + } + } + + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/noops/links.json b/test/ui/v1/noops/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/noops/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/noops/ui.c b/test/ui/v1/noops/ui.c new file mode 100644 index 0000000..b0ad004 --- /dev/null +++ b/test/ui/v1/noops/ui.c @@ -0,0 +1,136 @@ +// file : test/ui/v1/noops/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +static uint8_t g_anc_bytes[] = ANC_BYTES; + +static void check_invariants(void) { + assert(g_cores->mall == ANC_SIZE); + assert(g_cores->muta[0] == 0); + assert(g_cores->muta[1] == 0); + assert(g_cores->muta[2] == 0); + assert(g_cores->muta[3] == 0); + assert(g_cores->pnum == 1); + assert(g_cores->pcap == 1); + assert(g_cores->pfst == 0); + assert(g_cores->plst == 0); + assert(g_cores->pcur == 0); + assert(g_cores->psli == 1); + + for (uint64_t i = 0; i < SYNC_INTERVAL; i++) { + assert(g_cores->ivav[i] == 0); + assert(g_cores->iviv[i] == 0); + } + + for (uint64_t i = 0; i < MVEC_SIZE; i++) { + if (i < ANC_SIZE) { + assert(mvec_get_inst(g_cores, i) == g_anc_bytes[i]); + } else { + assert(mvec_get_inst(g_cores, i) == 0); + } + } + + assert(g_cores->pvec->mb0a == 0); + assert(g_cores->pvec->mb0s == ANC_SIZE); + assert(g_cores->pvec->mb1a == 0); + assert(g_cores->pvec->mb1s == 0); + assert(g_cores->pvec->r0x == 0); + assert(g_cores->pvec->r1x == 0); + assert(g_cores->pvec->r2x == 0); + assert(g_cores->pvec->r3x == 0); + assert(g_cores->pvec->s0 == 0); + assert(g_cores->pvec->s1 == 0); + assert(g_cores->pvec->s2 == 0); + assert(g_cores->pvec->s3 == 0); + assert(g_cores->pvec->s4 == 0); + assert(g_cores->pvec->s5 == 0); + assert(g_cores->pvec->s6 == 0); + assert(g_cores->pvec->s7 == 0); + + assert(g_cores->amb0 == 0.); + assert(g_cores->amb1 == 0.); + + assert(g_cores->emb1 == 0); + assert(g_cores->eliv == 0); + assert(g_cores->edea == 0); + + for (uint64_t i = 0; i < MVEC_SIZE; i++) { + assert(g_cores->aeva.data[i] == 0); + assert(g_cores->beva.data[i] == 0); + } + + // V1 arch specific + for (uint64_t i = 0; i < ARCH_INST_COUNT; i++) { + assert(g_cores->ipop[i] == 0); + assert(g_cores->iwrt[i] == 0); + } + + assert(g_cores->wmb0 == 0); + assert(g_cores->wmb1 == 0); + assert(g_cores->wdea == 0); + assert(g_cores->hlts == 0); + + for (uint64_t i = 0; i < MVEC_SIZE; i++) { + assert(g_cores->weva.data[i] == 0); + assert(g_cores->xeva.data[i] == 0); + } +} + +static void check_variants(void) { + assert(g_cores->step == g_step); + assert(g_cores->cycl == g_step); + assert(g_cores->ivpt == g_step); + + assert(g_cores->pvec->ip == g_step); + assert(g_cores->pvec->sp == g_step); + + assert(g_cores->emb0 == g_step); + + for (uint64_t i = 0; i < MVEC_SIZE; i++) { + assert(g_cores->eeva.data[i] == (i < g_step ? 1 : 0)); + } + + // V1 arch specific + for (uint64_t i = 0; i < ARCH_INST_COUNT; i++) { + uint64_t exed = 0; + + for (uint64_t j = 0; j < g_step; j++) { + if (mvec_get_inst(g_cores, j) == i) { + exed++; + } + } + + assert(g_cores->iexe[i] == exed); + } +} + +static void check_final(void) { + assert(g_cores->pvec->ip == ANC_SIZE); + assert(g_cores->pvec->sp == ANC_SIZE); +} + +int main(void) { + salis_init(); + check_invariants(); + check_variants(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + salis_step(1); + check_invariants(); + check_variants(); + } + + check_final(); + salis_save_and_free(); + return 0; +} diff --git a/test/ui/v1/stack/links.json b/test/ui/v1/stack/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/ui/v1/stack/links.json @@ -0,0 +1 @@ +[] diff --git a/test/ui/v1/stack/ui.c b/test/ui/v1/stack/ui.c new file mode 100644 index 0000000..f74b629 --- /dev/null +++ b/test/ui/v1/stack/ui.c @@ -0,0 +1,85 @@ +// file : test/ui/v1/stack/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "salis.h" + +int main(void) { + salis_init(); + + while (mvec_is_proc_owner(g_cores, g_cores->pvec->ip, g_cores->pcur)) { + uint8_t inst = mvec_get_inst(g_cores, g_cores->pvec->ip); + + if (inst != rpsh && inst != rpop) { + salis_step(1); + continue; + } + + uint64_t *reg = &g_cores->pvec->r0x; + + switch (mvec_get_inst(g_cores, g_cores->pvec->ip + 1)) { + case nop0: + reg = &g_cores->pvec->r0x; + break; + case nop1: + reg = &g_cores->pvec->r1x; + break; + case nop2: + reg = &g_cores->pvec->r2x; + break; + case nop3: + reg = &g_cores->pvec->r3x; + break; + } + + uint64_t reg_val = *reg; + uint64_t stack_val[8] = { + g_cores->pvec->s0, + g_cores->pvec->s1, + g_cores->pvec->s2, + g_cores->pvec->s3, + g_cores->pvec->s4, + g_cores->pvec->s5, + g_cores->pvec->s6, + g_cores->pvec->s7, + }; + + salis_step(1); + + switch (inst) { + case rpsh: + assert(*reg == reg_val); + assert(g_cores->pvec->s0 == *reg); + assert(g_cores->pvec->s1 == stack_val[0]); + assert(g_cores->pvec->s2 == stack_val[1]); + assert(g_cores->pvec->s3 == stack_val[2]); + assert(g_cores->pvec->s4 == stack_val[3]); + assert(g_cores->pvec->s5 == stack_val[4]); + assert(g_cores->pvec->s6 == stack_val[5]); + assert(g_cores->pvec->s7 == stack_val[6]); + break; + case rpop: + assert(*reg == stack_val[0]); + assert(g_cores->pvec->s0 == stack_val[1]); + assert(g_cores->pvec->s1 == stack_val[2]); + assert(g_cores->pvec->s2 == stack_val[3]); + assert(g_cores->pvec->s3 == stack_val[4]); + assert(g_cores->pvec->s4 == stack_val[5]); + assert(g_cores->pvec->s5 == stack_val[6]); + assert(g_cores->pvec->s6 == stack_val[7]); + assert(g_cores->pvec->s7 == 0); + break; + } + } + + salis_save_and_free(); + return 0; +} diff --git a/ui/curses/links.json b/ui/curses/links.json new file mode 100644 index 0000000..0cfb567 --- /dev/null +++ b/ui/curses/links.json @@ -0,0 +1 @@ +["-lncurses"] diff --git a/ui/curses/ui.c b/ui/curses/ui.c new file mode 100644 index 0000000..dbf3e93 --- /dev/null +++ b/ui/curses/ui.c @@ -0,0 +1,1378 @@ +// file : ui/curses/ui.c +// project : Salis-VM +// author : Paul Oliver + +// index: +// [section] includes +// [section] macros/enums +// [section] gfx globals +// [section] tui globals +// [section] gfx functions +// [section] tui functions +// [section] process page functions +// [section] world page functions +// [section] ipc page functions +// [section] log page functions +// [section] main print functions +// [section] event functions +// [section] main functions + +// ---------------------------------------------------------------------------- +// [section] includes +// ---------------------------------------------------------------------------- +#include +#define NCURSES_WIDECHAR 1 +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arch.h" +#include "arch_inst.h" +#include "arch_spec.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" +#include "timer.h" + +// ---------------------------------------------------------------------------- +// [section] macros/enums +// ---------------------------------------------------------------------------- +#define LOG_LINE_COUNT 0x400 + +#define NCURSES_CTRL(x) (x & 0x1f) + +#define MAIN_LINES 15 +#define PANE_WIDTH 27 +#define PANE_WIDTH_AND_MARGIN (PANE_WIDTH + 2) +#define PROC_FIELD_WIDTH 21 +#define PROC_PAGE_LINES (MAIN_LINES + 3) + +#define FPS_MIN 10 +#define FPS_MAX 20 + +enum UIPage { + PAGE_CORE, + PAGE_PROCESS, + PAGE_WORLD, + PAGE_IPC, + PAGE_LOG, + PAGE_COUNT, +}; + +enum UIColorPair { + PAIR_NORMAL, + PAIR_HEADER, + PAIR_LIVE_PROC, + PAIR_SELECTED_PROC, + PAIR_FREE_CELL, + PAIR_ALLOC_CELL, + PAIR_MEM_BLOCK_START, + PAIR_MEM_BLOCK_START_WITH_POINTERS, + PAIR_POINTERS, + PAIR_SELECTED_MB1, + PAIR_SELECTED_MB2, + PAIR_SELECTED_IP, + PAIR_SELECTED_SP, + PAIR_LOG_TRACE, + PAIR_LOG_INFO, + PAIR_LOG_ATTENTION, +}; + +enum UILogLevel { + TRACE, + INFO, + ATTENTION, +}; + +// ---------------------------------------------------------------------------- +// [section] gfx globals +// ---------------------------------------------------------------------------- +static uint64_t g_gfx_vsiz; +static uint64_t *g_gfx_inst; +static uint64_t *g_gfx_mall; +static uint64_t *g_gfx_mbst; +static uint64_t *g_gfx_ptrs; +static uint64_t *g_gfx_mb0s; +static uint64_t *g_gfx_mb1s; +static uint64_t *g_gfx_ipas; +static uint64_t *g_gfx_spas; + +static const wchar_t *g_zoomed_symbols = ( + L"⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟" + L"⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿" +); + +// ---------------------------------------------------------------------------- +// [section] tui globals +// ---------------------------------------------------------------------------- +static bool g_exit; +static bool g_running; +static unsigned g_core; +static unsigned g_page; +static char *g_line_buff; +static bool g_proc_genes; +static uint64_t g_proc_scroll; +static uint64_t g_proc_field_scroll; +static uint64_t g_proc_gene_scroll; +static uint64_t g_proc_selected; +static uint64_t g_wrld_pos; +static uint64_t g_wrld_zoom; +static bool g_wcursor_mode; +static int g_wcursor_x; +static int g_wcursor_y; +static uint64_t g_wcursor_pointed; +static uint64_t g_log_cnt; +static unsigned g_log_ptr; +static unsigned g_log_scroll; +static mtx_t g_log_mutex; +static enum UILogLevel g_log_levels[LOG_LINE_COUNT]; +static char g_logs[LOG_LINE_COUNT][LOG_BUFFER_SIZE]; +static uint64_t g_vlin; +static uint64_t g_vsiz; +static uint64_t g_vlin_rng; +static uint64_t g_vsiz_rng; +static uint64_t g_ivpt_scroll; +static uint64_t g_step_block; +static float g_steps_per_sec; + +// ---------------------------------------------------------------------------- +// [section] gfx functions +// ---------------------------------------------------------------------------- +static void gfx_init(uint64_t vsiz) { + assert(vsiz); + + g_gfx_vsiz = vsiz; + g_gfx_inst = calloc(g_gfx_vsiz, sizeof(uint64_t)); + g_gfx_mall = calloc(g_gfx_vsiz, sizeof(uint64_t)); + g_gfx_mbst = calloc(g_gfx_vsiz, sizeof(uint64_t)); + g_gfx_ptrs = calloc(g_gfx_vsiz, sizeof(uint64_t)); + g_gfx_mb0s = calloc(g_gfx_vsiz, sizeof(uint64_t)); + g_gfx_mb1s = calloc(g_gfx_vsiz, sizeof(uint64_t)); + g_gfx_ipas = calloc(g_gfx_vsiz, sizeof(uint64_t)); + g_gfx_spas = calloc(g_gfx_vsiz, sizeof(uint64_t)); + + assert(g_gfx_inst); + assert(g_gfx_mall); + assert(g_gfx_mbst); + assert(g_gfx_ptrs); + assert(g_gfx_mb0s); + assert(g_gfx_mb1s); + assert(g_gfx_ipas); + assert(g_gfx_spas); +} + +static void gfx_free(void) { + if (g_gfx_vsiz == 0) { + return; + } + + assert(g_gfx_inst); + assert(g_gfx_mall); + assert(g_gfx_mbst); + assert(g_gfx_ptrs); + assert(g_gfx_mb0s); + assert(g_gfx_mb1s); + assert(g_gfx_ipas); + assert(g_gfx_spas); + + g_gfx_vsiz = 0; + + free(g_gfx_inst); + free(g_gfx_mall); + free(g_gfx_mbst); + free(g_gfx_ptrs); + free(g_gfx_mb0s); + free(g_gfx_mb1s); + free(g_gfx_ipas); + free(g_gfx_spas); + + g_gfx_inst = NULL; + g_gfx_mall = NULL; + g_gfx_mbst = NULL; + g_gfx_ptrs = NULL; + g_gfx_mb0s = NULL; + g_gfx_mb1s = NULL; + g_gfx_ipas = NULL; + g_gfx_spas = NULL; +} + +static void gfx_resize(uint64_t vsiz) { + assert(vsiz); + gfx_free(); + gfx_init(vsiz); +} + +static void gfx_render_inst(const struct Core *core, uint64_t pos, uint64_t zoom) { + assert(core); + + for (uint64_t i = 0; i < g_gfx_vsiz; i++) { + g_gfx_inst[i] = 0; + g_gfx_mall[i] = 0; + + for (uint64_t j = 0; j < zoom; j++) { + uint64_t addr = pos + (i * zoom) + j; + + if (addr < MVEC_SIZE) { + g_gfx_inst[i] += mvec_get_inst(core, addr); + g_gfx_mall[i] += mvec_is_alloc(core, addr) ? 1 : 0; + } + } + } +} + +static void gfx_clear_array(uint64_t *arry) { + assert(arry); + memset(arry, 0, g_gfx_vsiz * sizeof(uint64_t)); +} + +static void gfx_accumulate_pixel(uint64_t pos, uint64_t zoom, uint64_t pixa, uint64_t *arry) { + assert(arry); + uint64_t end = pos + (g_gfx_vsiz * zoom); + + if (pixa < pos || pixa >= end) { + return; + } + + uint64_t pixi = (pixa - pos) / zoom; + assert(pixi < g_gfx_vsiz); + arry[pixi]++; +} + +static void gfx_render_mbst(const struct Core *core, uint64_t pos, uint64_t zoom) { + assert(core); + gfx_clear_array(g_gfx_mbst); + + for (uint64_t pix = core->pfst; pix <= core->plst; pix++) { + uint64_t mb0a = arch_proc_get_mb0_addr(core, pix); + uint64_t mb1a = arch_proc_get_mb1_addr(core, pix); + gfx_accumulate_pixel(pos, zoom, mb0a, g_gfx_mbst); + + if (arch_proc_get_mb1_size(core, pix)) { + gfx_accumulate_pixel(pos, zoom, mb1a, g_gfx_mbst); + } + } +} + +static void gfx_render_ptrs(const struct Core *core, uint64_t pos, uint64_t zoom) { + assert(core); + gfx_clear_array(g_gfx_ptrs); + + for (uint64_t pix = core->pfst; pix <= core->plst; pix++) { + uint64_t ipa = arch_proc_get_ip_addr(core, pix); + uint64_t spa = arch_proc_get_sp_addr(core, pix); + gfx_accumulate_pixel(pos, zoom, ipa, g_gfx_ptrs); + gfx_accumulate_pixel(pos, zoom, spa, g_gfx_ptrs); + } +} + +static void gfx_render_mb0s(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) { + assert(core); + gfx_clear_array(g_gfx_mb0s); + + if (psel < core->pfst || psel > core->plst) { + return; + } + + uint64_t mb0a = arch_proc_get_mb0_addr(core, psel); + uint64_t mb0s = arch_proc_get_mb0_size(core, psel); + + for (uint64_t i = 0; i < mb0s; i++) { + gfx_accumulate_pixel(pos, zoom, mb0a + i, g_gfx_mb0s); + } +} + +static void gfx_render_mb1s(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) { + assert(core); + gfx_clear_array(g_gfx_mb1s); + + if (psel < core->pfst || psel > core->plst) { + return; + } + + uint64_t mb1a = arch_proc_get_mb1_addr(core, psel); + uint64_t mb1s = arch_proc_get_mb1_size(core, psel); + + for (uint64_t i = 0; i < mb1s; i++) { + gfx_accumulate_pixel(pos, zoom, mb1a + i, g_gfx_mb1s); + } +} + +static void gfx_render_ipas(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) { + assert(core); + gfx_clear_array(g_gfx_ipas); + + if (psel < core->pfst || psel > core->plst) { + return; + } + + gfx_accumulate_pixel(pos, zoom, arch_proc_get_ip_addr(core, psel), g_gfx_ipas); +} + +static void gfx_render_spas(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) { + assert(core); + gfx_clear_array(g_gfx_spas); + + if (psel < core->pfst || psel > core->plst) { + return; + } + + gfx_accumulate_pixel(pos, zoom, arch_proc_get_sp_addr(core, psel), g_gfx_spas); +} + +static void gfx_render(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) { + assert(core); + gfx_render_inst(core, pos, zoom); + gfx_render_mbst(core, pos, zoom); + gfx_render_ptrs(core, pos, zoom); + gfx_render_mb0s(core, pos, zoom, psel); + gfx_render_mb1s(core, pos, zoom, psel); + gfx_render_ipas(core, pos, zoom, psel); + gfx_render_spas(core, pos, zoom, psel); +} + +// ---------------------------------------------------------------------------- +// [section] tui functions +// ---------------------------------------------------------------------------- +static void tui_line_buff_free(void) { + if (g_line_buff) { + free(g_line_buff); + } + + g_line_buff = NULL; +} + +static void tui_line_buff_resize(void) { + tui_line_buff_free(); + g_line_buff = calloc(COLS + 1, sizeof(char)); +} + +static void tui_line(bool clear, int line, int color, int attr, const char *format, ...) { + assert(line >= 0); + assert(format); + + if (line >= LINES) { + return; + } + + if (clear) { + move(line, 0); + clrtoeol(); + } + + va_list args; + va_start(args, format); + attron(COLOR_PAIR(color) | attr); + vsnprintf(g_line_buff, COLS, format, args); + mvprintw(line, 1, "%s", g_line_buff); + attroff(COLOR_PAIR(color) | attr); + va_end(args); +} + +static void tui_clear_line(int l) { + tui_line(true, l, PAIR_NORMAL, A_NORMAL, ""); +} + +static void tui_field(int line, int col, int color, int attr, const char *format, ...) { + assert(line >= 0); + assert(col >= 0); + assert(format); + + if (line >= LINES || col >= COLS) { + return; + } + + va_list args; + va_start(args, format); + attron(COLOR_PAIR(color) | attr); + vsnprintf(g_line_buff, COLS - col, format, args); + mvprintw(line, col, "%s", g_line_buff); + attroff(COLOR_PAIR(color) | attr); + va_end(args); +} + +static void tui_str_field(int l, const char *label, const char *value) { + assert(label); + assert(strlen(label) <= 4); + assert(value); + tui_line(false, l, PAIR_NORMAL, A_NORMAL, "%-4s : %18s", label, value); +} + +static void tui_ulx_field(int l, const char *label, uint64_t value) { + assert(label); + assert(strlen(label) <= 4); + tui_line(false, l, PAIR_NORMAL, A_NORMAL, "%-4s : %#18lx", label, value); +} + +static void tui_print_core(int l) { + tui_line(false, ++l, PAIR_HEADER, A_BOLD, "CORE [%d]", g_core); + tui_ulx_field(++l, "cycl", g_cores[g_core].cycl); + tui_ulx_field(++l, "mall", g_cores[g_core].mall); + tui_ulx_field(++l, "mut0", g_cores[g_core].muta[0]); + tui_ulx_field(++l, "mut1", g_cores[g_core].muta[1]); + tui_ulx_field(++l, "mut2", g_cores[g_core].muta[2]); + tui_ulx_field(++l, "mut3", g_cores[g_core].muta[3]); + tui_ulx_field(++l, "pnum", g_cores[g_core].pnum); + tui_ulx_field(++l, "pcap", g_cores[g_core].pcap); + tui_ulx_field(++l, "pfst", g_cores[g_core].pfst); + tui_ulx_field(++l, "plst", g_cores[g_core].plst); + tui_ulx_field(++l, "pcur", g_cores[g_core].pcur); + tui_ulx_field(++l, "psli", g_cores[g_core].psli); + tui_ulx_field(++l, "ivpt", g_cores[g_core].ivpt); +} + +// ---------------------------------------------------------------------------- +// [section] process page functions +// ---------------------------------------------------------------------------- +static int tui_proc_pair(uint64_t pix) { + if (pix == g_proc_selected) { + return PAIR_SELECTED_PROC; + } + + if (mvec_proc_is_live(&g_cores[g_core], pix)) { + return PAIR_LIVE_PROC; + } + + return PAIR_NORMAL; +} + +static const char *tui_proc_state(uint64_t pix) { + return mvec_proc_is_live(&g_cores[g_core], pix) ? "live" : "dead"; +} + +static void tui_print_process_genome_header(int l) { + tui_line(false, l++, PAIR_NORMAL, A_NORMAL, "%s : %18s : %s", "stat", "pix", "genome"); +} + +static void tui_print_process_gene(int l, int gcol, uint64_t gidx, uint64_t mba, uint64_t pix, int pair) { + assert(gcol >= PANE_WIDTH_AND_MARGIN); + assert(gcol < COLS); + assert(mvec_proc_is_live(&g_cores[g_core], pix)); + assert(pair == PAIR_SELECTED_MB1 || pair == PAIR_SELECTED_MB2); + + const struct Core *core = &g_cores[g_core]; + uint64_t addr = mba + gidx; + uint8_t inst = mvec_get_inst(core, addr); + wchar_t gsym[2] = { arch_inst_symbol(inst), L'\0' }; + cchar_t cchar = { 0 }; + int pair_cell; + + if (arch_proc_get_ip_addr(core, pix) == addr) { + pair_cell = PAIR_SELECTED_IP; + } else if (arch_proc_get_sp_addr(core, pix) == addr) { + pair_cell = PAIR_SELECTED_SP; + } else { + pair_cell = pair; + } + + setcchar(&cchar, gsym, 0, pair_cell, NULL); + mvadd_wch(l, gcol, &cchar); +} + +static void tui_print_process_genes(int l, uint64_t pix) { + tui_line(true, l, tui_proc_pair(pix), A_NORMAL, "%s : %#18lx :", tui_proc_state(pix), pix); + + if (!mvec_proc_is_live(&g_cores[g_core], pix)) { + return; + } + + const struct Core *core = &g_cores[g_core]; + int scol = PANE_WIDTH_AND_MARGIN; + int gcol = scol - g_proc_gene_scroll; + uint64_t mb0a = arch_proc_get_mb0_addr(core, pix); + uint64_t mb0s = arch_proc_get_mb0_size(core, pix); + uint64_t mb1a = arch_proc_get_mb1_addr(core, pix); + uint64_t mb1s = arch_proc_get_mb1_size(core, pix); + + for (uint64_t gidx = 0; gidx < mb0s && gcol < COLS; gidx++, gcol++) { + if (gcol >= scol) { + tui_print_process_gene(l, gcol, gidx, mb0a, pix, PAIR_SELECTED_MB1); + } + } + + for (uint64_t gidx = 0; gidx < mb1s && gcol < COLS; gidx++, gcol++) { + if (gcol >= scol) { + tui_print_process_gene(l, gcol, gidx, mb1a, pix, PAIR_SELECTED_MB2); + } + } + + clrtoeol(); +} + +static void tui_print_process_field_header_element(int l, int fidx, const char *name) { + assert(fidx >= 0); + assert(name); + + if (fidx < (int)g_proc_field_scroll) { + return; + } + + int foff = fidx - g_proc_field_scroll; + int fcol = foff * PROC_FIELD_WIDTH + PANE_WIDTH - 1; + tui_field(l, fcol, PAIR_NORMAL, A_NORMAL, " : %18s", name); +} + +static void tui_print_process_field_header(int l) { + tui_line(true, l, PAIR_NORMAL, A_NORMAL, "%s : %18s", "stat", "pix"); + int fidx = 0; + +#define ARCH_SPEC_PROC_FIELD(type, name) tui_print_process_field_header_element(l, fidx++, #name); + ARCH_SPEC_PROC_FIELDS +#undef ARCH_SPEC_PROC_FIELD +} + +static void tui_print_process_field_element(int l, int fidx, int fclr, uint64_t field) { + assert(fidx >= 0); + + if (fidx < (int)g_proc_field_scroll) { + return; + } + + int foff = fidx - g_proc_field_scroll; + int fcol = foff * PROC_FIELD_WIDTH + PANE_WIDTH - 1; + tui_field(l, fcol, fclr, A_NORMAL, " : %#18lx", field); +} + +static void tui_print_process_fields(int l, uint64_t pix) { + tui_line(true, l, tui_proc_pair(pix), A_NORMAL, "%s : %#18lx", tui_proc_state(pix), pix); + const struct Proc *proc = proc_get(&g_cores[g_core], pix); + int fidx = 0; + int fclr = tui_proc_pair(pix); + +#define ARCH_SPEC_PROC_FIELD(type, name) tui_print_process_field_element(l, fidx++, fclr, proc->name); + ARCH_SPEC_PROC_FIELDS +#undef ARCH_SPEC_PROC_FIELD +} + +static void tui_print_process(int l) { + l++; + + tui_line(true, l++, PAIR_HEADER, A_BOLD, + "PROCESS [vs:%#lx | psel:%#lx | pfst:%#lx | plst:%#lx | fs:%#lx | gs:%#lx]", + g_proc_scroll, + g_proc_selected, + g_cores[g_core].pfst, + g_cores[g_core].plst, + g_proc_field_scroll, + g_proc_gene_scroll + ); + + uint64_t pix = g_proc_scroll; + + if (g_proc_genes) { + tui_print_process_genome_header(l++); + + while (l < LINES) { + tui_print_process_genes(l++, pix++); + } + + return; + } else { + tui_print_process_field_header(l++); + + while (l < LINES) { + tui_print_process_fields(l++, pix++); + } + } +} + +// ---------------------------------------------------------------------------- +// [section] world page functions +// ---------------------------------------------------------------------------- +static void tui_world_resize(void) { + assert(g_wrld_zoom); + g_vlin = 0; + g_vsiz = 0; + g_vlin_rng = 0; + g_vsiz_rng = 0; + + if (COLS > PANE_WIDTH) { + g_vlin = COLS - PANE_WIDTH; + g_vsiz = LINES * g_vlin; + g_vlin_rng = g_vlin * g_wrld_zoom; + g_vsiz_rng = g_vsiz * g_wrld_zoom; + gfx_resize(g_vsiz); + } +} + +static void tui_print_cell(uint64_t i, uint64_t r, uint64_t x, uint64_t y, uint64_t a) { + wchar_t inst_nstr[2] = { L'\0', L'\0' }; + uint64_t inst_avrg = g_gfx_inst[i] / g_wrld_zoom; + cchar_t cchar = { 0 }; + assert(inst_avrg < ARCH_INST_COUNT); + + if (a >= MVEC_SIZE) { + inst_nstr[0] = L' '; + } else if (g_wrld_zoom == 1) { + inst_nstr[0] = arch_inst_symbol((uint8_t)inst_avrg); + } else { + inst_nstr[0] = g_zoomed_symbols[(uint8_t)inst_avrg]; + } + + int pair_cell; + + if ((g_wcursor_mode && r == (uint64_t)g_wcursor_x && y == (uint64_t)g_wcursor_y) || a >= MVEC_SIZE) { + pair_cell = PAIR_NORMAL; + } else if (g_wcursor_mode && r == (uint64_t)g_wcursor_x && y == (uint64_t)g_wcursor_y) { + pair_cell = PAIR_NORMAL; + } else if (g_gfx_ipas[i] != 0) { + pair_cell = PAIR_SELECTED_IP; + } else if (g_gfx_spas[i] != 0) { + pair_cell = PAIR_SELECTED_SP; + } else if (g_gfx_mb0s[i] != 0) { + pair_cell = PAIR_SELECTED_MB1; + } else if (g_gfx_mb1s[i] != 0) { + pair_cell = PAIR_SELECTED_MB2; + } else if (g_gfx_ptrs[i] != 0 && g_gfx_mbst[i] != 0) { + pair_cell = PAIR_MEM_BLOCK_START_WITH_POINTERS; + } else if (g_gfx_ptrs[i] != 0) { + pair_cell = PAIR_POINTERS; + } else if (g_gfx_mbst[i] != 0) { + pair_cell = PAIR_MEM_BLOCK_START; + } else if (g_gfx_mall[i] != 0) { + pair_cell = PAIR_ALLOC_CELL; + } else { + pair_cell = PAIR_FREE_CELL; + } + + setcchar(&cchar, inst_nstr, 0, pair_cell, NULL); + mvadd_wch(y, x, &cchar); +} + +static void tui_print_wcursor_bar(void) { + tui_clear_line(LINES - 1); + const struct Core *core = &g_cores[g_core]; + char cownr[PROC_FIELD_WIDTH]; + uint64_t cpos = g_vlin * g_wcursor_y + g_wcursor_x; + uint64_t caddr = cpos * g_wrld_zoom + g_wrld_pos; + uint8_t cinst = mvec_get_inst(core, caddr); + + if (caddr < MVEC_SIZE && mvec_is_alloc(core, caddr)) { + g_wcursor_pointed = mvec_get_owner(core, caddr); + snprintf(cownr, PROC_FIELD_WIDTH, "%#lx", g_wcursor_pointed); + } else { + g_wcursor_pointed = (uint64_t)(-1); + snprintf(cownr, PROC_FIELD_WIDTH, "-"); + } + + mvprintw( + LINES - 1, 1, + "cursor | x:%#x | y:%#x | addr:%#lx | isum:%#lx | iavr:%#lx | mall:%#lx | mbst:%#lx | mnem:<%s> | ownr:%s", + g_wcursor_x, + g_wcursor_y, + caddr, + g_gfx_inst[cpos], + g_gfx_inst[cpos] / g_wrld_zoom, + g_gfx_mall[cpos], + g_gfx_mbst[cpos], + arch_inst_mnemonic(cinst), + cownr + ); +} + +static void tui_print_world(int l) { + l++; + tui_line(false, l++, PAIR_HEADER, A_BOLD, "WORLD"); + tui_ulx_field(l++, "wrlp", g_wrld_pos); + tui_ulx_field(l++, "wrlz", g_wrld_zoom); + tui_ulx_field(l++, "psel", g_proc_selected); + tui_ulx_field(l++, "pabs", g_proc_selected % g_cores[g_core].pcap); + tui_ulx_field(l++, "vrng", g_vsiz_rng); + tui_str_field(l++, "curs", g_wcursor_mode ? "on" : "off"); + + l++; + tui_line(false, l++, PAIR_HEADER, A_BOLD, "SELECTED"); + const struct Proc *psel = proc_get(&g_cores[g_core], g_proc_selected); + +#define ARCH_SPEC_PROC_FIELD(type, name) tui_ulx_field(l++, #name, psel->name); + ARCH_SPEC_PROC_FIELDS +#undef ARCH_SPEC_PROC_FIELD + + if (!g_vlin) { + return; + } + + gfx_render(&g_cores[g_core], g_wrld_pos, g_wrld_zoom, g_proc_selected); + + if (g_wcursor_mode) { + int xmax = g_vlin - 1; + int ymax = LINES - 2; + g_wcursor_x = (g_wcursor_x < xmax) ? g_wcursor_x : xmax; + g_wcursor_y = (g_wcursor_y < ymax) ? g_wcursor_y : ymax; + } + + for (uint64_t i = 0; i < g_vsiz; i++) { + uint64_t r = i % g_vlin; + uint64_t x = r + PANE_WIDTH; + uint64_t y = i / g_vlin; + uint64_t a = g_wrld_pos + (i * g_wrld_zoom); + tui_print_cell(i, r, x, y, a); + } + + if (g_wcursor_mode) { + tui_print_wcursor_bar(); + } +} + +// ---------------------------------------------------------------------------- +// [section] ipc page functions +// ---------------------------------------------------------------------------- +static void tui_print_ipc_field(int l, uint64_t i, int color) { + uint8_t iinst = g_cores[g_core].iviv[i]; + uint64_t iaddr = g_cores[g_core].ivav[i]; + tui_field(l, PANE_WIDTH, color, A_NORMAL, "%#18x : %#18x : %#18x", i, iinst, iaddr); +} + +static void tui_print_ipc_data(void) { + tui_field(0, PANE_WIDTH, PAIR_NORMAL, A_NORMAL, "%18s : %18s : %18s", "ipci", "inst", "addr"); + int l = 1 - g_ivpt_scroll; + + for (uint64_t i = 0; i < SYNC_INTERVAL; i++) { + if (i == g_cores[g_core].ivpt) { + if (l >= 1) { + tui_print_ipc_field(l++, i, PAIR_SELECTED_PROC); + } + + continue; + } + + uint8_t iinst = g_cores[g_core].iviv[i]; + + if ((iinst & ARCH_INST_IPCM_FLAG) != 0) { + if (l >= 1) { + tui_print_ipc_field(l++, i, PAIR_LIVE_PROC); + } + + continue; + } + } + + for (; l < LINES; l++) { + if (l >= 1) { + move(l, PANE_WIDTH); + clrtoeol(); + } + } +} + +static void tui_print_ipc(int l) { + l++; + const struct Core *core = &g_cores[g_core]; + tui_line(true, l++, PAIR_HEADER, A_BOLD, "IPC [%#lx]", g_ivpt_scroll); + tui_ulx_field(l++, "ivpt", core->ivpt); + tui_ulx_field(l++, "ivpi", core->iviv[core->ivpt]); + tui_ulx_field(l++, "ivpa", core->ivav[core->ivpt]); + tui_print_ipc_data(); +} + +// ---------------------------------------------------------------------------- +// [section] log page functions +// ---------------------------------------------------------------------------- +static void tui_trace_impl(const char *format, ...) { + assert(format); + (void)format; + +#if defined(LOG_TRACE) + va_list args; + va_start(args, format); + mtx_lock(&g_log_mutex); + vlog_trace_to_buffer(g_logs[g_log_ptr], format, args); + mtx_unlock(&g_log_mutex); + va_end(args); + g_log_levels[g_log_ptr] = TRACE; + g_log_cnt++; + g_log_ptr = (g_log_ptr + 1) % LOG_LINE_COUNT; +#endif +} + +static void tui_info_impl(const char *format, ...) { + assert(format); + va_list args; + va_start(args, format); + mtx_lock(&g_log_mutex); + vlog_info_to_buffer(g_logs[g_log_ptr], format, args); + mtx_unlock(&g_log_mutex); + va_end(args); + g_log_levels[g_log_ptr] = INFO; + g_log_cnt++; + g_log_ptr = (g_log_ptr + 1) % LOG_LINE_COUNT; +} + +static void tui_attention_impl(const char *format, ...) { + assert(format); + va_list args; + va_start(args, format); + mtx_lock(&g_log_mutex); + vlog_attention_to_buffer(g_logs[g_log_ptr], format, args); + mtx_unlock(&g_log_mutex); + va_end(args); + g_log_levels[g_log_ptr] = ATTENTION; + g_log_cnt++; + g_log_ptr = (g_log_ptr + 1) % LOG_LINE_COUNT; +} + +static void tui_clear_log_line(int line) { + assert(line >= 0 && line < LINES); + move(line, PANE_WIDTH); + clrtoeol(); +} + +static void tui_print_log_line(unsigned lptr, int line) { + assert(lptr < LOG_LINE_COUNT); + assert(line >= 0 && line < LINES); + tui_clear_log_line(line); + + if (strlen(g_logs[lptr])) { + int pair = 0; + + switch (g_log_levels[lptr]) { + case TRACE: + pair = PAIR_LOG_TRACE; + break; + case INFO: + pair = PAIR_LOG_INFO; + break; + case ATTENTION: + pair = PAIR_LOG_ATTENTION; + break; + default: + assert(false); + } + + tui_field(line, PANE_WIDTH_AND_MARGIN, pair, A_NORMAL, g_logs[lptr]); + } +} + +static void tui_print_log(int l) { + l++; + tui_line(true, l++, PAIR_HEADER, A_BOLD, "LOG"); + tui_ulx_field(l++, "lscr", g_log_scroll); + tui_ulx_field(l++, "lcnt", g_log_cnt); + tui_ulx_field(l++, "lptr", g_log_ptr); + + unsigned lptr = g_log_ptr; + int line = LINES + g_log_scroll; + + while (line) { + lptr = (lptr - 1 + LOG_LINE_COUNT) % LOG_LINE_COUNT; + line--; + + if (line < LINES) { + tui_print_log_line(lptr, line); + } + + if (lptr == g_log_ptr) { + break; + } + } + + while (line) { + line--; + tui_clear_log_line(line); + } +} + +// ---------------------------------------------------------------------------- +// [section] main print functions +// ---------------------------------------------------------------------------- +static void tui_print(void) { + int l = 1; + tui_line(false, l++, PAIR_HEADER, A_BOLD, "SALIS [%d:%d]", g_core, CORES); + tui_str_field(l++, "name", NAME); + tui_ulx_field(l++, "seed", SEED); + tui_str_field(l++, "anc", ANC); + tui_str_field(l++, "arch", ARCH); + tui_ulx_field(l++, "asav", AUTOSAVE_INTERVAL); + tui_ulx_field(l++, "mrng", MUTA_RANGE); + tui_ulx_field(l++, "size", MVEC_SIZE); + tui_ulx_field(l++, "syni", SYNC_INTERVAL); +#if defined(DATA_PUSH_INTERVAL) + tui_ulx_field(l++, "dpsi", DATA_PUSH_INTERVAL); +#endif + tui_ulx_field(l++, "step", g_step); + tui_ulx_field(l++, "sync", g_sync); + tui_ulx_field(l++, "step", g_step_block); + tui_ulx_field(l++, "stps", (uint64_t)g_steps_per_sec); + assert(l == MAIN_LINES); + + switch (g_page) { + case PAGE_CORE: + tui_print_core(l); + break; + case PAGE_PROCESS: + tui_print_process(l); + break; + case PAGE_WORLD: + tui_print_world(l); + break; + case PAGE_IPC: + tui_print_ipc(l); + break; + case PAGE_LOG: + tui_print_log(l); + break; + default: + break; + } +} + +// ---------------------------------------------------------------------------- +// [section] event functions +// ---------------------------------------------------------------------------- +static void ev_vscroll(int ev) { + switch (g_page) { + case PAGE_PROCESS: + switch (ev) { + case 'W': + if (LINES > PROC_PAGE_LINES) { + g_proc_scroll += LINES - PROC_PAGE_LINES; + } + + break; + case 'S': + if (LINES > PROC_PAGE_LINES) { + uint64_t dv = LINES - PROC_PAGE_LINES; + + if (g_proc_scroll < dv) { + g_proc_scroll = 0; + } else { + g_proc_scroll -= dv; + } + } + + break; + case 'w': + g_proc_scroll += 1; + break; + case 's': + if (g_proc_scroll) { + g_proc_scroll -= 1; + } + + break; + case 'q': + g_proc_scroll = 0; + break; + default: + break; + } + + break; + case PAGE_WORLD: { + switch (ev) { + case 'W': + g_wrld_pos += g_vsiz_rng; + break; + case 'S': + if (g_wrld_pos < g_vsiz_rng) { + g_wrld_pos = 0; + } else { + g_wrld_pos -= g_vsiz_rng; + } + + break; + case 'w': + g_wrld_pos += g_vlin_rng; + break; + case 's': + if (g_wrld_pos < g_vlin_rng) { + g_wrld_pos = 0; + } else { + g_wrld_pos -= g_vlin_rng; + } + + break; + case 'q': + g_wrld_pos = 0; + break; + default: + break; + } + + break; + } + case PAGE_IPC: + switch (ev) { + case 'W': + g_ivpt_scroll += LINES; + break; + case 'S': + g_ivpt_scroll -= g_ivpt_scroll < (uint64_t)LINES ? g_ivpt_scroll : (uint64_t)LINES; + break; + case 'w': + g_ivpt_scroll += 1; + break; + case 's': + g_ivpt_scroll -= g_ivpt_scroll ? 1 : 0; + break; + case 'q': + g_ivpt_scroll = 0; + break; + } + + break; + case PAGE_LOG: + switch (ev) { + case 'W': + g_log_scroll += LINES; + g_log_scroll = g_log_scroll >= LOG_LINE_COUNT ? LOG_LINE_COUNT - 1 : g_log_scroll; + break; + case 'S': + g_log_scroll -= g_log_scroll < (uint64_t)LINES ? g_log_scroll : (uint64_t)LINES; + break; + case 'w': + g_log_scroll += 1; + g_log_scroll = g_log_scroll >= LOG_LINE_COUNT ? LOG_LINE_COUNT - 1 : g_log_scroll; + break; + case 's': + g_log_scroll -= g_log_scroll ? 1 : 0; + break; + case 'q': + g_log_scroll = 0; + break; + } + + break; + default: + break; + } +} + +static void ev_hscroll(int ev) { + switch (g_page) { + case PAGE_PROCESS: { + uint64_t *hs_var = g_proc_genes ? &g_proc_gene_scroll : &g_proc_field_scroll; + + switch (ev) { + case 'A': + *hs_var = 0; + break; + case 'a': + *hs_var -= *hs_var ? 1 : 0; + break; + case 'd': + (*hs_var)++; + break; + default: + break; + } + + break; + } + + case PAGE_WORLD: + switch (ev) { + case 'a': + if (g_wrld_pos < g_wrld_zoom) { + g_wrld_pos = 0; + } else { + g_wrld_pos -= g_wrld_zoom; + } + + break; + case 'd': + g_wrld_pos += g_wrld_zoom; + break; + default: + break; + } + + break; + default: + break; + } +} + +static void ev_zoom(int ev) { + switch (g_page) { + case PAGE_WORLD: + switch (ev) { + case 'x': + g_wrld_zoom *= (g_vlin != 0 && g_vsiz_rng < MVEC_SIZE) ? 2 : 1; + tui_world_resize(); + break; + case 'z': + g_wrld_zoom /= (g_wrld_zoom != 1) ? 2 : 1; + tui_world_resize(); + break; + default: + break; + } + + break; + default: + break; + } +} + +static void ev_move_wcursor(int ev) { + switch (ev) { + case KEY_UP: + g_wcursor_y -= (g_wcursor_y != 0) ? 1 : 0; + break; + case KEY_DOWN: + g_wcursor_y += (g_wcursor_y < LINES - 2) ? 1 : 0; + break; + case KEY_LEFT: + g_wcursor_x -= (g_wcursor_x != 0) ? 1 : 0; + break; + case KEY_RIGHT: + g_wcursor_x += ((uint64_t)g_wcursor_x < g_vlin - 1) ? 1 : 0; + break; + default: + break; + } +} + +static void ev_sel_proc(int ev) { + if (g_page != PAGE_PROCESS && g_page != PAGE_WORLD) { + return; + } + + switch (ev) { + case 'o': + g_proc_selected -= 1; + break; + case 'p': + g_proc_selected += 1; + break; + case 'f': + g_proc_selected = g_cores[g_core].pfst; + break; + case 'l': + g_proc_selected = g_cores[g_core].plst; + break; + default: + break; + } +} + +static void ev_goto_sel_proc(void) { + switch (g_page) { + case PAGE_PROCESS: + g_proc_scroll = g_proc_selected; + break; + case PAGE_WORLD: + g_wrld_pos = g_cores[g_core].pvec[g_proc_selected % g_cores[g_core].pcap].mb0a; + break; + default: + break; + } +} + +static void ev_handle(void) { + int ev = getch(); + + if (g_page == PAGE_WORLD && g_wcursor_mode) { + switch (ev) { + case KEY_UP: + case KEY_DOWN: + case KEY_LEFT: + case KEY_RIGHT: + ev_move_wcursor(ev); + return; + case '\n': + if (g_wcursor_pointed != (uint64_t)(-1)) { + g_proc_selected = g_wcursor_pointed; + } + + break; + default: + break; + } + } + + switch (ev) { + case NCURSES_CTRL('c'): + g_exit = true; + break; + case KEY_SLEFT: + clear(); + g_core = (g_core + CORES - 1) % CORES; + break; + case KEY_SRIGHT: + clear(); + g_core = (g_core + 1) % CORES; + break; + case KEY_LEFT: + clear(); + g_page = (g_page + PAGE_COUNT - 1) % PAGE_COUNT; + break; + case KEY_RIGHT: + clear(); + g_page = (g_page + 1) % PAGE_COUNT; + break; + case KEY_RESIZE: + clear(); + tui_line_buff_resize(); + tui_world_resize(); + + if (g_vlin) { + while (g_vsiz_rng >= MVEC_SIZE * 2 && g_wrld_zoom != 1) { + g_wrld_zoom /= 2; + tui_world_resize(); + } + } + + g_wcursor_mode = false; + break; + case 'W': + case 'S': + case 'w': + case 's': + case 'q': + ev_vscroll(ev); + break; + case 'A': + case 'a': + case 'd': + ev_hscroll(ev); + break; + case 'z': + case 'x': + ev_zoom(ev); + break; + case 'o': + case 'p': + case 'f': + case 'l': + ev_sel_proc(ev); + break; + case 'k': + ev_goto_sel_proc(); + break; + case 'g': + if (g_page == PAGE_PROCESS) { + clear(); + g_proc_genes = !g_proc_genes; + } + + break; + case 'c': + if (g_page == PAGE_WORLD) { + clear(); + + if (g_vlin == 0) { + g_wcursor_mode = false; + } else { + g_wcursor_mode = !g_wcursor_mode; + } + } + + break; + case ' ': + g_running = !g_running; + g_steps_per_sec = 0.f; + nodelay(stdscr, g_running); + break; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '0': + if (!g_running) { + uint64_t cycles = 1 << (((ev - '0') ? (ev - '0') : 10) - 1); + salis_step(cycles); + } + + break; + default: + break; + } +} + +// ---------------------------------------------------------------------------- +// [section] main functions +// ---------------------------------------------------------------------------- +static void init(void) { + setlocale(LC_ALL, ""); + initscr(); + raw(); + noecho(); + curs_set(0); + keypad(stdscr, TRUE); + start_color(); + init_color(COLOR_BLACK, 0, 0, 0); + + init_pair(PAIR_NORMAL, COLOR_WHITE, COLOR_BLACK); + init_pair(PAIR_HEADER, COLOR_BLUE, COLOR_BLACK); + init_pair(PAIR_LIVE_PROC, COLOR_BLUE, COLOR_BLACK); + init_pair(PAIR_SELECTED_PROC, COLOR_YELLOW, COLOR_BLACK); + init_pair(PAIR_FREE_CELL, COLOR_BLUE, COLOR_BLACK); + init_pair(PAIR_ALLOC_CELL, COLOR_BLACK, COLOR_BLUE); + init_pair(PAIR_MEM_BLOCK_START, COLOR_BLACK, COLOR_WHITE); + init_pair(PAIR_MEM_BLOCK_START_WITH_POINTERS, COLOR_CYAN, COLOR_WHITE); + init_pair(PAIR_POINTERS, COLOR_BLACK, COLOR_CYAN); + init_pair(PAIR_SELECTED_MB1, COLOR_BLACK, COLOR_YELLOW); + init_pair(PAIR_SELECTED_MB2, COLOR_BLACK, COLOR_GREEN); + init_pair(PAIR_SELECTED_IP, COLOR_BLACK, COLOR_RED); + init_pair(PAIR_SELECTED_SP, COLOR_BLACK, COLOR_MAGENTA); + init_pair(PAIR_LOG_TRACE, COLOR_BLUE, COLOR_BLACK); + init_pair(PAIR_LOG_INFO, COLOR_GREEN, COLOR_BLACK); + init_pair(PAIR_LOG_ATTENTION, COLOR_YELLOW, COLOR_BLACK); + + log_trace = tui_trace_impl; + log_info = tui_info_impl; + log_attention = tui_attention_impl; + +#if defined(COMMAND_NEW) + salis_init(); +#elif defined(COMMAND_LOAD) + salis_load(); +#endif + + g_wrld_zoom = 1; + g_step_block = 1; + tui_line_buff_resize(); + tui_world_resize(); + mtx_init(&g_log_mutex, mtx_plain); +} + +static void exec(void) { + struct TimerSPS timer_sps; + struct timespec time_sleep = {.tv_sec = 0, .tv_nsec = 1000000000 / 60}; + + while (!g_exit) { + if (g_running) { + timer_sps_reset(&timer_sps); + salis_start(); + nanosleep(&time_sleep, NULL); + salis_signal_stop(); + salis_wait(); + timer_sps_measure(&timer_sps); + g_steps_per_sec = timer_sps.steps_per_sec; + } + + tui_print(); + ev_handle(); + } +} + +static void quit(void) { + gfx_free(); + mtx_destroy(&g_log_mutex); + tui_line_buff_free(); + endwin(); + + // Log cleanup to stdout + log_trace = log_trace_to_stdout; + log_info = log_info_to_stdout; + log_attention = log_attention_to_stdout; + salis_save_and_free(); +} + +int main(void) { + init(); + exec(); + quit(); + return 0; +} diff --git a/ui/daemon/links.json b/ui/daemon/links.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/ui/daemon/links.json @@ -0,0 +1 @@ +[] diff --git a/ui/daemon/ui.c b/ui/daemon/ui.c new file mode 100644 index 0000000..1cdc55b --- /dev/null +++ b/ui/daemon/ui.c @@ -0,0 +1,54 @@ +// file : ui/daemon/ui.c +// project : Salis-VM +// author : Paul Oliver + +#include +#include +#include +#include +#include + +#include "arch_spec.h" +#include "arch_inst.h" +#include "compress.h" +#include "logger.h" +#include "salis.h" +#include "timer.h" + +static bool g_running = true; + +static void quit(int signo) { + log_attention("Signal %d received, will stop simulator soon", signo); + salis_signal_stop_no_check(); + g_running = false; +} + +int main(void) { + log_info("Initializing daemon UI"); + log_info("Installing signal handlers"); + signal(SIGINT, quit); + signal(SIGTERM, quit); + +#if defined(COMMAND_NEW) + salis_init(); +#endif + +#if defined(COMMAND_LOAD) + salis_load(); +#endif + + struct TimerSPS timer_sps; + + while (g_running) { + timer_sps_reset(&timer_sps); + salis_start(); + sleep(1); + salis_signal_stop_no_check(); + salis_wait(); + timer_sps_measure(&timer_sps); + log_info("Running simulation @%#018lx steps/s", (uint64_t)timer_sps.steps_per_sec); + } + + salis_save_and_free(); + return 0; +} diff --git a/util/doc-check.py b/util/doc-check.py new file mode 100755 index 0000000..1f4351b --- /dev/null +++ b/util/doc-check.py @@ -0,0 +1,43 @@ +#!/usr/bin/env -S PYTHONDONTWRITEBYTECODE=1 python + +# file : util/doc-check.py +# project : Salis-VM +# author : Paul Oliver +# +# Simple utility to check file sections match indices. +# Enforced indexing style is inspired by ImGui. + +import pathlib +import re + +def check_pattern(pattern, content, path): + if not re.findall(pattern, content, re.MULTILINE): + raise RuntimeError(f"Could not find pattern: '{pattern}' in '{path}'") + +def check_path(path, doc_prefix): + with open(path, "r") as f: + content = f.read() + + check_pattern(fr"^{doc_prefix} file : {path}$", content, path) + check_pattern(fr"^{doc_prefix} project : Salis-VM$", content, path) + check_pattern(fr"^{doc_prefix} author : Paul Oliver $", content, path) + + entries = [s.strip() for s in re.findall(fr"^\s*{doc_prefix} \[section\] .*$", content, re.MULTILINE)] + entries_index = entries[len(entries) // 2:] + entries_content = entries[:len(entries) // 2] + + if entries_index != entries_content: + raise RuntimeError(f"Index/section mismatch in: '{path}': {entries_index} != {entries_content}") + +prefix_map = { + "#": ("py", ), + "//": ("c", "cpp", "h"), + ";": ("asm", ), +} + +[ + check_path(path, f"\\s*{prefix}") + for prefix, extensions in prefix_map.items() + for extension in extensions + for path in pathlib.Path(".").rglob(f"*.{extension}") +] -- cgit v1.2.1