aboutsummaryrefslogtreecommitdiff
path: root/ui/daemon/ui.c
blob: 93f088fff3a06bbc031d5019a3dd1c1538486845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <signal.h>

bool g_running;
uint64_t g_step_block;

void sig_handler(int signo) {
    (void)signo;

    if (g_running) {
        log_warn("Signal received, will stop simulator soon...");
        g_running = false;
    }
}

void step_block(void) {
    clock_t beg = clock();
    salis_step(g_step_block - (g_steps % g_step_block));
    clock_t end = clock();

    if ((end - beg) < (CLOCKS_PER_SEC * 4)) {
        g_step_block <<= 1;
    }

    if ((end - beg) >= (CLOCKS_PER_SEC * 2) && g_step_block != 1) {
        g_step_block >>= 1;
    }

    float secs = (float)(end - beg) / (float)CLOCKS_PER_SEC;
    float steps_per_sec = (float)g_step_block / secs;

    log_info("Simulator running on step %#lx @%.1f steps/s", g_steps, steps_per_sec);
}

int main(void) {
    g_running = true;
    g_step_block = 1;

    signal(SIGINT, sig_handler);
    signal(SIGTERM, sig_handler);

#if defined(COMMAND_NEW)
    salis_init();
#elif defined(COMMAND_LOAD)
    salis_load();
#endif

    while (g_running) {
        step_block();
    }

    log_info("Saving simulation...");
    salis_save(SIM_PATH);
    salis_free();

    log_info("Exiting salis...");
    return 0;
}