diff options
| author | Paul Oliver <contact@pauloliver.dev> | 2026-05-25 21:15:22 +0200 |
|---|---|---|
| committer | Paul Oliver <contact@pauloliver.dev> | 2026-05-25 21:15:22 +0200 |
| commit | 54342ed0cc61585d953183ec29309eb0db846b72 (patch) | |
| tree | d852870f968869fd74ccdac1943e02fe5655059e | |
| parent | 5ce8953bcb98e037f50a37abadf664d95ee69cc2 (diff) | |
Reorganizes includes
| -rw-r--r-- | arch/v1/arch.c | 8 | ||||
| -rw-r--r-- | core/compress.c | 10 | ||||
| -rw-r--r-- | core/logger.c | 4 | ||||
| -rw-r--r-- | core/salis.c | 58 | ||||
| -rw-r--r-- | core/sql.c | 24 | ||||
| -rw-r--r-- | data/client.cpp | 24 | ||||
| -rw-r--r-- | data/render.c | 4 | ||||
| -rw-r--r-- | data/server.c | 46 | ||||
| -rwxr-xr-x | salis.py | 1 | ||||
| -rw-r--r-- | ui/curses/ui.c | 1 | ||||
| -rw-r--r-- | ui/daemon/ui.c | 6 |
11 files changed, 85 insertions, 101 deletions
diff --git a/arch/v1/arch.c b/arch/v1/arch.c index dc3b316..3a43b43 100644 --- a/arch/v1/arch.c +++ b/arch/v1/arch.c @@ -879,7 +879,7 @@ void arch_push_data_header(void) { assert(g_sim_db); g_info("Creating arch table in SQLite database"); - salis_exec_sql( + sql_exec( 0, NULL, NULL, "create table arch (" #define INST(core, pref, index, label, mnemonic, symbol) \ @@ -952,7 +952,7 @@ void arch_push_data_line(void) { params->size = EVA_SIZE; params->in = (Bytef *)in; params->out = (Bytef *)malloc(EVA_SIZE); - thrd_create(&g_arch_eva_thrds[i][j], (thrd_start_t)salis_deflate, params); + thrd_create(&g_arch_eva_thrds[i][j], (thrd_start_t)comp_deflate, params); } } @@ -967,7 +967,7 @@ void arch_push_data_line(void) { } g_info("Pushing row to arch table in SQLite database"); - salis_exec_sql( + sql_exec( CORES * ARCH_EVENT_ARRAYS_COUNT, (const void **)blobs, (int *)blob_sizes, "insert into arch (" #define INST(core, pref, index, label, mnemonic, symbol) \ @@ -1032,7 +1032,7 @@ void arch_push_data_line(void) { for (int i = 0; i < CORES; ++i) { for (int j = 0; j < ARCH_EVENT_ARRAYS_COUNT; ++j) { struct DeflateParams *params = &g_arch_eva_deflate_params[i][j]; - salis_deflate_end(params); + comp_deflate_end(params); free(params->out); } } diff --git a/core/compress.c b/core/compress.c index 1938bb3..8ef3c41 100644 --- a/core/compress.c +++ b/core/compress.c @@ -1,3 +1,5 @@ +#pragma once + #define EVA_SIZE (sizeof(uint64_t) * MVEC_SIZE) struct DeflateParams { @@ -17,7 +19,7 @@ struct InflateParams { uint8_t tgap[THREAD_GAP]; }; -int salis_deflate(struct DeflateParams *params) { +int comp_deflate(struct DeflateParams *params) { assert(params); assert(params->size); assert(params->in); @@ -39,12 +41,12 @@ int salis_deflate(struct DeflateParams *params) { return 0; } -void salis_deflate_end(struct DeflateParams *params) { +void comp_deflate_end(struct DeflateParams *params) { assert(params); deflateEnd(¶ms->strm); } -int salis_inflate(struct InflateParams *params) { +int comp_inflate(struct InflateParams *params) { assert(params); assert(params->avail_in); assert(params->size); @@ -71,7 +73,7 @@ int salis_inflate(struct InflateParams *params) { return 0; } -void salis_inflate_end(struct InflateParams *params) { +void comp_inflate_end(struct InflateParams *params) { assert(params); inflateEnd(¶ms->strm); } diff --git a/core/logger.c b/core/logger.c index 4ff7562..7d5b69e 100644 --- a/core/logger.c +++ b/core/logger.c @@ -98,3 +98,7 @@ void log_warn(const char *format, ...) { log_msg(WARN, true, format, args); va_end(args); } + +// Client may install their own loggers +void (*g_info)(const char *fmt, ...) = log_info; +void (*g_warn)(const char *fmt, ...) = log_warn; diff --git a/core/salis.c b/core/salis.c index f6d259f..abed1ec 100644 --- a/core/salis.c +++ b/core/salis.c @@ -5,18 +5,18 @@ #include <stdlib.h> #include <string.h> #include <threads.h> - -#if defined(DATA_PUSH) -#include <sqlite3.h> -#include <zlib.h> -#endif +#include "logger.c" #if defined(COMPRESS) #include <zlib.h> +#include "compress.c" #endif -#if defined(COMPRESS) || defined(DATA_PUSH) +#if defined(DATA_PUSH) +#include <sqlite3.h> +#include <zlib.h> #include "compress.c" +#include "sql.c" #endif #define INST_CAP 0x80 @@ -92,16 +92,7 @@ const struct Proc g_dead_proc; char g_asav_pbuf[AUTOSAVE_NAME_LEN]; #endif -// Each UI must install these logger functions before salis_init() gets invoked -void (*g_info)(const char *fmt, ...); -void (*g_warn)(const char *fmt, ...); - #if defined(DATA_PUSH) -// Include custom SQL wrapper -// This is also used by the data-server -sqlite3 *g_sim_db; -#include "sql.c" - thrd_t g_eva_thrds[CORES][EVENT_ARRAYS_COUNT]; struct DeflateParams g_eva_deflate_params[CORES][EVENT_ARRAYS_COUNT]; #endif @@ -695,7 +686,7 @@ void salis_save(const char *path) { .out = (Bytef *)out, }; - salis_deflate(¶ms); + comp_deflate(¶ms); FILE *fx = fopen(path, "wb"); assert(fx); @@ -704,7 +695,7 @@ void salis_save(const char *path) { fwrite(out, sizeof(char), params.strm.total_out, fx); fclose(fx); - salis_deflate_end(¶ms); + comp_deflate_end(¶ms); free(in); free(out); @@ -738,7 +729,7 @@ void salis_push_data_header(void) { assert(g_sim_db); g_info("Creating core table in SQLite database"); - salis_exec_sql( + sql_exec( 0, NULL, NULL, "create table core (" #define EVENT_ARRAY(core, index, ev) \ @@ -810,7 +801,7 @@ void salis_push_data_line(void) { params->size = EVA_SIZE, params->in = (Bytef *)in, params->out = (Bytef *)malloc(EVA_SIZE), - thrd_create(&g_eva_thrds[i][j], (thrd_start_t)salis_deflate, params); + thrd_create(&g_eva_thrds[i][j], (thrd_start_t)comp_deflate, params); } } @@ -825,7 +816,7 @@ void salis_push_data_line(void) { } g_info("Pushing row to core table in SQLite database"); - salis_exec_sql( + sql_exec( CORES * EVENT_ARRAYS_COUNT, (const void **)blobs, (int *)blob_sizes, "insert into core (" #define EVENT_ARRAY(core, index, ev) \ @@ -883,7 +874,7 @@ void salis_push_data_line(void) { for (int i = 0; i < CORES; ++i) { for (int j = 0; j < EVENT_ARRAYS_COUNT; ++j) { struct DeflateParams *params = &g_eva_deflate_params[i][j]; - salis_deflate_end(params); + comp_deflate_end(params); free(params->out); } } @@ -924,18 +915,8 @@ void salis_init(void) { #endif #if defined(DATA_PUSH) - 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 - salis_exec_sql(0, NULL, NULL, "pragma journal_mode=wal;"); - // Initialize database + sql_open(); salis_push_data_header(); salis_push_data_line(); #endif @@ -971,8 +952,8 @@ void salis_load(void) { .out = (Bytef *)out, }; - salis_inflate(¶ms); - salis_inflate_end(¶ms); + comp_inflate(¶ms); + comp_inflate_end(¶ms); FILE *f = fmemopen(out, size, "rb"); #else @@ -995,11 +976,7 @@ void salis_load(void) { #endif #if defined(DATA_PUSH) - 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); + sql_open(); #endif } #endif @@ -1131,8 +1108,7 @@ void salis_step(uint64_t ns) { void salis_free(void) { #if defined(DATA_PUSH) - assert(g_sim_db); - sqlite3_close(g_sim_db); + sql_close(); #endif for (int i = 0; i < CORES; ++i) { @@ -1,4 +1,8 @@ -void salis_exec_sql(int blob_cnt, const void **blobs, const int *blob_sizes, const char *sql_format, ...) { +#define DATA_PUSH_BUSY_TIMEOUT 600000 + +sqlite3 *g_sim_db; + +void sql_exec(int blob_cnt, const void **blobs, const int *blob_sizes, const char *sql_format, ...) { assert(sql_format); va_list args; @@ -47,3 +51,21 @@ void salis_exec_sql(int blob_cnt, const void **blobs, const int *blob_sizes, con sqlite3_finalize(sql_stmt); } + +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(0, NULL, NULL, "pragma journal_mode=wal;"); +} + +void sql_close(void) { + assert(g_sim_db); + sqlite3_close(g_sim_db); +} diff --git a/data/client.cpp b/data/client.cpp index 65494b0..2a162f2 100644 --- a/data/client.cpp +++ b/data/client.cpp @@ -4,14 +4,12 @@ #include <imgui_impl_glfw.h> #include <imgui_impl_opengl3.h> #include <implot.h> +#include <initializer_list> #include <json-c/json.h> #include <limits.h> #include <math.h> #include <signal.h> #include <threads.h> - -#include <initializer_list> - #include "logger.c" #define FETCH_INTERVAL 10 @@ -187,7 +185,7 @@ void data_fetch(void) { json_object_object_add(request, "x-current", json_object_new_int(g_x_current)); const char *request_str = json_object_to_json_string(request); - log_info("Sending request to server: %s", request_str); + g_info("Sending request to server: %s", request_str); int socket_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in socket_addr; memset(&socket_addr, 0, sizeof(struct sockaddr_in)); @@ -200,7 +198,7 @@ void data_fetch(void) { struct json_object *response = json_object_from_fd(socket_fd); const char *response_str = json_object_to_json_string(response); - log_info("Server responded with: %s", response_str); + g_info("Server responded with: %s", response_str); json_object_put(request); json_object_put(response); @@ -225,14 +223,14 @@ int data_fetching_thread(void *data) { } void data_start_fetching(void) { - log_info("Starting data fetching thread"); + g_info("Starting data fetching thread"); g_status = STATUS_RUNNING; thrd_create(&g_data_fetching_thread, (thrd_start_t)data_fetching_thread, nullptr); } void data_stop_fetching(void) { assert(g_status == STATUS_RUNNING); - log_info("Stopping data fetching thread"); + g_info("Stopping data fetching thread"); g_status = STATUS_STOPPING; thrd_join(g_data_fetching_thread, nullptr); } @@ -412,13 +410,13 @@ void gui_print(void) { void sig_handler(int signo) { (void)signo; - log_warn("Signal received, will stop SALIS data client..."); + g_warn("Signal received, will stop SALIS data client..."); if (g_status == STATUS_RUNNING) data_stop_fetching(); glfwSetWindowShouldClose(g_window, GLFW_TRUE); } void glfw_error_callback(int error, const char* description) { - log_warn("GLFW error %d: %s", error, description); + g_warn("GLFW error %d: %s", error, description); } void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { @@ -506,9 +504,9 @@ int main(int argc, char **argv) { signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); - log_info("Starting SALIS data client"); + g_info("Starting SALIS data client"); - log_info("Initializing GLFW"); + g_info("Initializing GLFW"); glfwSetErrorCallback(glfw_error_callback); glfwInitHint(GLFW_WAYLAND_LIBDECOR, GLFW_WAYLAND_DISABLE_LIBDECOR); if (!glfwInit()) assert(false); @@ -520,7 +518,7 @@ int main(int argc, char **argv) { glfwMakeContextCurrent(g_window); glfwSwapInterval(1); // enable vsync - log_info("Initializing ImGui"); + g_info("Initializing ImGui"); IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImPlot::CreateContext(); @@ -569,7 +567,7 @@ int main(int argc, char **argv) { ImPlot::DestroyContext(); ImGui::DestroyContext(); - log_info("Stopping SALIS data client"); + g_info("Stopping SALIS data client"); glfwDestroyWindow(g_window); glfwTerminate(); return 0; diff --git a/data/render.c b/data/render.c index f42e821..08ff6dd 100644 --- a/data/render.c +++ b/data/render.c @@ -46,8 +46,8 @@ void eva_render(sqlite3_context *context, int argc, sqlite3_value **argv) { .out = (Bytef *)eva, }; - salis_inflate(¶ms); - salis_inflate_end(¶ms); + comp_inflate(¶ms); + comp_inflate_end(¶ms); // Render image for (size_t i = 0; i < px_count; i++) { diff --git a/data/server.c b/data/server.c index ea94364..4574415 100644 --- a/data/server.c +++ b/data/server.c @@ -6,6 +6,7 @@ #include <threads.h> #include "logger.c" +#include "sql.c" #define BACKLOG 10 @@ -14,11 +15,6 @@ struct Socket { struct sockaddr_in addr; }; -sqlite3 *g_sim_db; -void (*g_info)(const char *fmt, ...) = log_info; -void (*g_warn)(const char *fmt, ...) = log_warn; -#include "sql.c" - // ---------------------------------------------------------------------------- // SQL functions // ---------------------------------------------------------------------------- @@ -27,12 +23,14 @@ void (*g_warn)(const char *fmt, ...) = log_warn; // Main functions // ---------------------------------------------------------------------------- void sig_handler(int signo) { - log_warn("Signal %d received, shutting down data server", signo); + (void)signo; + g_warn("Signal received, will stop SALIS data server"); + sql_close(); exit(0); } void respond_name(int socket_fd) { - log_info("Client requested simulation name"); + g_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)); @@ -41,7 +39,7 @@ void respond_name(int socket_fd) { } void respond_opts(int socket_fd) { - log_info("Client requested simulation options"); + g_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); @@ -49,7 +47,7 @@ void respond_opts(int socket_fd) { } void respond_hash(int socket_fd) { - log_info("Client requested git hash"); + g_info("Client requested git hash"); char buff[41] = { 0 }; FILE *pipe = popen("git rev-parse HEAD", "r"); @@ -66,12 +64,12 @@ 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); + g_info("Client requested simulation data with the following parameters: %s", request_str); struct json_object *response = json_object_new_object(); json_object_object_add(response, "response", json_object_new_string("hello!")); const char *response_str = json_object_to_json_string(response); - log_info("Sending response to client: %s", response_str); + g_info("Sending response to client: %s", response_str); json_object_to_fd(socket_fd, response, 0); shutdown(socket_fd, SHUT_WR); @@ -83,7 +81,7 @@ int handle_client(struct Socket *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)); + g_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; @@ -107,7 +105,7 @@ int handle_client(struct Socket *socket) { json_object_put(request_json); - log_info("Client disconnected: %s:%d", socket_ip, ntohs(socket->addr.sin_port)); + g_info("Client disconnected: %s:%d", socket_ip, ntohs(socket->addr.sin_port)); close(socket->fd); free(socket); @@ -115,23 +113,14 @@ int handle_client(struct Socket *socket) { } int main(void) { + g_info("Initializing salis data server"); + g_info("Connecting to database in: %s", DATA_PUSH_PATH); + sql_open(); + signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); - log_info("Initializing salis data server"); - log_info("Connecting to database in: %s", DATA_PUSH_PATH); - 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 - salis_exec_sql(0, NULL, NULL, "pragma journal_mode=wal;"); - - log_info("Binding to port: %d", PORT); + g_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)); @@ -141,7 +130,7 @@ int main(void) { socket_addr.sin_port = htons(PORT); bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)); - log_info("Listening..."); + g_info("Listening..."); listen(socket_fd, BACKLOG); while (true) { @@ -154,6 +143,5 @@ int main(void) { thrd_detach(thrd); } - close(socket_fd); return 0; } @@ -332,7 +332,6 @@ def pop_ui_vars(): def pop_db_vars(): ns.sim_db = os.path.join(ns.sim_dir, f"{args.name}.sqlite3") - ns.b.defines.add(f"-DDATA_PUSH_BUSY_TIMEOUT=600000") ns.b.defines.add(f"-DDATA_PUSH_PATH=\"{ns.sim_db}\"") ns.b.links.add("-lsqlite3") ns.b.links.add("-lz") diff --git a/ui/curses/ui.c b/ui/curses/ui.c index 287745b..6755619 100644 --- a/ui/curses/ui.c +++ b/ui/curses/ui.c @@ -1,7 +1,6 @@ #include <curses.h> #include <locale.h> -#include "logger.c" #include "tui.c" #define LOG_LINE_COUNT 1024 diff --git a/ui/daemon/ui.c b/ui/daemon/ui.c index e62c80a..cec3296 100644 --- a/ui/daemon/ui.c +++ b/ui/daemon/ui.c @@ -1,8 +1,6 @@ #include <signal.h> -#include "logger.c" - -volatile bool g_running; +bool g_running; uint64_t g_step_block; void sig_handler(int signo) { @@ -36,8 +34,6 @@ void step_block(void) { int main(void) { g_running = true; g_step_block = 1; - g_info = log_info; - g_warn = log_warn; signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); |
