aboutsummaryrefslogtreecommitdiff
path: root/core/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/server.c')
-rw-r--r--core/server.c418
1 files changed, 418 insertions, 0 deletions
diff --git a/core/server.c b/core/server.c
new file mode 100644
index 0000000..6848cf6
--- /dev/null
+++ b/core/server.c
@@ -0,0 +1,418 @@
+// index
+// [section] includes
+// [section] macros
+// [section] structs
+// [section] globals
+// [section] event array render function
+// [section] sql callbacks
+// [section] main functions
+
+// ----------------------------------------------------------------------------
+// [section] includes
+// ----------------------------------------------------------------------------
+#include <arpa/inet.h>
+#include <assert.h>
+#include <json-c/json.h>
+#include <signal.h>
+#include <sqlite3.h>
+#include <string.h>
+#include <threads.h>
+#include <zlib.h>
+
+#include "common.c"
+#include "compress.c"
+#include "logger.c"
+#include "sql.c"
+
+// ----------------------------------------------------------------------------
+// [section] macros
+// ----------------------------------------------------------------------------
+#define BACKLOG 10
+
+// ----------------------------------------------------------------------------
+// [section] structs
+// ----------------------------------------------------------------------------
+struct Socket {
+ int fd;
+ struct sockaddr_in addr;
+};
+
+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[EVENT_ARRAYS_SIZE];
+ int64_t out[DEFVAL_HM_PIXEL_COUNT];
+};
+
+// ----------------------------------------------------------------------------
+// [section] globals
+// ----------------------------------------------------------------------------
+struct json_object *g_response_header;
+size_t g_eva_count;
+char g_eva_pbuf[EVA_SAVE_NAME_LEN];
+
+// ----------------------------------------------------------------------------
+// [section] event array render function
+// ----------------------------------------------------------------------------
+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(MVEC_LOOP)
+ hm_left %= MVEC_SIZE;
+#endif
+
+#if !defined(MVEC_LOOP)
+#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);
+#endif
+
+ // Inflate blob
+ struct InflateParams params = {
+ .avail_in = blob_size,
+ .size = EVENT_ARRAYS_SIZE,
+ .in = (Bytef *)blob,
+ .out = (Bytef *)render_context->eva,
+ };
+
+ comp_inflate(&params);
+ comp_inflate_end(&params);
+
+ 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;
+#if defined(MVEC_LOOP)
+ coord %= MVEC_SIZE;
+#endif
+ render_context->out[i] += render_context->eva[coord];
+ }
+ }
+
+ return 0;
+}
+
+// ----------------------------------------------------------------------------
+// [section] sql callbacks
+// ----------------------------------------------------------------------------
+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, EVENT_ARRAYS_SIZE_COL_MARKER)) {
+ g_eva_count++;
+ }
+}
+
+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-%#018lx",
+ SIM_EDIR,
+ 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, EVENT_ARRAYS_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, EVENT_ARRAYS_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
+// ----------------------------------------------------------------------------
+void sig_handler(int signo) {
+ (void)signo;
+
+ log_warn("Signal received, will stop SALIS data server");
+ json_object_put(g_response_header);
+ sql_close();
+ exit(0);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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);
+}
+
+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;
+}