Compare commits

...

3 Commits

6 changed files with 151 additions and 22 deletions

View File

@ -68,6 +68,7 @@ add_subdirectory(third-party/libxm ${CMAKE_CURRENT_BINARY_DIR}/third-party/libxm
if(LINUX) if(LINUX)
set(SYSTEM_SOURCE_FILES set(SYSTEM_SOURCE_FILES
src/system/linux/twn_elf.c src/system/linux/twn_elf.c
src/system/linux/twn_timer.c
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_linux_game_object.c>) $<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_linux_game_object.c>)
elseif(WIN32) elseif(WIN32)
set(SYSTEM_SOURCE_FILES set(SYSTEM_SOURCE_FILES

View File

@ -76,6 +76,26 @@ static void benchmark(struct state *state) {
profile_end("int32_t acc"); profile_end("int32_t acc");
profile_start("int32_t acc precalc");
for (int i = 0; i < 1000; ++i) {
int32_t const rsi = (int32_t)state->r * (int32_t)state->r;
int32_t acc = (int32_t)(sqrtf(state->r * state->r - (state->r - 1) * (state->r - 1)));
for (int32_t iy = (int32_t)state->r - 1; iy >= 0; --iy) {
while (acc * acc < rsi - iy * iy) acc++;
for (int32_t ix = -acc; ix < acc; ++ix) {
/* lower portion */
x = (float)ix;
y = (float)iy;
/* upper portion */
x = (float)ix;
y = (float)(int32_t)(~(uint32_t)iy);
}
}
}
profile_end("int32_t acc precalc");
(void)x; (void)y; (void)x; (void)y;
} }
@ -85,7 +105,7 @@ void game_tick(void) {
if (!ctx.udata) { if (!ctx.udata) {
ctx.udata = ccalloc(1, sizeof (struct state)); ctx.udata = ccalloc(1, sizeof (struct state));
struct state *state = ctx.udata; struct state *state = ctx.udata;
state->r = 10; state->r = 24;
} }
} }

View File

@ -0,0 +1,71 @@
#include "../twn_timer.h"
#include "twn_engine_context_c.h"
#include <SDL_messagebox.h>
#include <signal.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
static timer_t timerid;
static sigset_t mask;
static struct sigaction sa;
static struct sigevent sev;
static bool created;
/* stop application */
static void sanity_timer_handler(int sig, siginfo_t *si, void *uc) {
(void)uc; (void)sig; (void)si;
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"sanity timer", "Your game tick exceeded its allocated time, application is closing",
ctx.window);
raise(SIGKILL);
}
bool start_sanity_timer(uint64_t milliseconds_to_expire) {
if (!created) {
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = sanity_timer_handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGRTMIN, &sa, NULL) == -1)
goto ERR_SIGACTION;
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1)
goto ERR_TIMERCREATE;
created = true;
ERR_TIMERCREATE:
// ERR_SIGPROCMASK:
ERR_SIGACTION:
return false;
}
struct itimerspec its = {0};
its.it_value.tv_sec = milliseconds_to_expire / 1000;
its.it_value.tv_nsec = (milliseconds_to_expire * 1000000 % 1000000000);
if (timer_settime(timerid, 0, &its, NULL) == -1)
return false;
return true;
}
bool end_sanity_timer(void) {
struct itimerspec its = {0};
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 0;
if (timer_settime(timerid, 0, &its, NULL) == -1)
return false;
return false;
}

10
src/system/twn_timer.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef TWN_TIMER_H
#define TWN_TIMER_H
#include <stdbool.h>
#include <stdbit.h>
bool start_sanity_timer(uint64_t milliseconds_to_expire);
bool end_sanity_timer(void);
#endif // TWN_TIMER_H

View File

@ -5,6 +5,7 @@
#include "twn_util_c.h" #include "twn_util_c.h"
#include "twn_game_object_c.h" #include "twn_game_object_c.h"
#include "twn_textures_c.h" #include "twn_textures_c.h"
#include "system/twn_timer.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <physfs.h> #include <physfs.h>
@ -214,7 +215,13 @@ static void main_loop(void) {
if (ctx.window_size_has_changed) if (ctx.window_size_has_changed)
update_viewport(); update_viewport();
input_state_update(&ctx.input); input_state_update(&ctx.input);
profile_start("game tick");
start_sanity_timer(2000);
game_object_tick(); game_object_tick();
end_sanity_timer();
profile_end("game tick");
input_state_update_postframe(&ctx.input); input_state_update_postframe(&ctx.input);
/* TODO: make it works when ctx.ticks_per_second != 60 */ /* TODO: make it works when ctx.ticks_per_second != 60 */

View File

@ -16,6 +16,7 @@ static struct ProfileItem {
uint64_t tick_accum; uint64_t tick_accum;
uint64_t sample_count; uint64_t sample_count;
uint64_t worst_tick; uint64_t worst_tick;
bool active;
} value; } value;
} *profiles; } *profiles;
@ -301,41 +302,62 @@ char *expand_asterisk(const char *mask, const char *to) {
void profile_start(char profile[const static 1]) { void profile_start(char profile[const static 1]) {
uint64_t tick_accum = 0, sample_count = 0, worst_tick = 0; /* stamp time immediately, so to not have influence of our profile lookup */
uint64_t const counter = SDL_GetPerformanceCounter();
struct ProfileItem const *p = shgetp_null(profiles, profile); struct ProfileItem *p = shgetp_null(profiles, profile);
if (p) { if (p) {
tick_accum = p->value.tick_accum; p->value.tick_start = counter;
sample_count = p->value.sample_count; p->value.active = true;
worst_tick = p->value.worst_tick; } else {
shput(profiles, profile, ((struct Profile) {
.tick_start = counter,
.active = true,
}));
} }
shput(profiles, profile, ((struct Profile) {
.tick_start = SDL_GetPerformanceCounter(),
.tick_accum = tick_accum,
.sample_count = sample_count,
.worst_tick = worst_tick,
}));
} }
void profile_end(char profile[const static 1]) { void profile_end(char profile[const static 1]) {
/* stamp time immediately, so to not have influence of our profile lookup */
uint64_t const counter = SDL_GetPerformanceCounter();
struct ProfileItem *p = shgetp_null(profiles, profile); struct ProfileItem *p = shgetp_null(profiles, profile);
if (!p) { if (!p || !p->value.active) {
log_warn("Profile %s wasn't started!", profile); log_warn("Profile %s wasn't started!", profile);
return; return;
} }
uint64_t took = SDL_GetPerformanceCounter() - p->value.tick_start; uint64_t const took = counter - p->value.tick_start;
p->value.tick_accum += took; p->value.tick_accum += took;
p->value.sample_count++; p->value.sample_count++;
p->value.active = false;
if (p->value.worst_tick < took) if (p->value.worst_tick < took)
p->value.worst_tick = took; p->value.worst_tick = took;
} }
static char const *format_profile_time(double ticks) {
static char strings[2][128];
static char *current = strings[0];
double const seconds = (double)ticks / (double)(SDL_GetPerformanceFrequency());
/* display in seconds */
if (seconds >= 0.1)
SDL_snprintf(current, 128, "%fs", seconds);
/* display in milliseconds */
else
SDL_snprintf(current, 128, "%fms", seconds * 1000);
const char *const result = current;
current += 128;
if (current >= strings[1]) current = strings[0];
return result;
}
void profile_list_stats(void) { void profile_list_stats(void) {
for (size_t i = 0; i < shlenu(profiles); ++i) { for (size_t i = 0; i < shlenu(profiles); ++i) {
if (profiles[i].value.sample_count == 0) { if (profiles[i].value.sample_count == 0) {
@ -343,18 +365,16 @@ void profile_list_stats(void) {
} }
else if (profiles[i].value.sample_count == 1) { else if (profiles[i].value.sample_count == 1) {
log_info("Profile '%s' took: %fs", log_info("Profile '%s' took: %s",
profiles[i].key, profiles[i].key,
(double)profiles[i].value.tick_accum / (double)(SDL_GetPerformanceFrequency())); format_profile_time((double)profiles[i].value.tick_accum));
} }
else if (profiles[i].value.sample_count > 1) { else if (profiles[i].value.sample_count > 1) {
log_info("Profile '%s' on average took: %fs, worst case: %fs, sample count: %llu", log_info("Profile '%s' on average took: %s, worst case: %s, sample count: %llu",
profiles[i].key, profiles[i].key,
(double)profiles[i].value.tick_accum / format_profile_time((double)profiles[i].value.tick_accum / (double)profiles[i].value.sample_count),
(double)profiles[i].value.sample_count / format_profile_time((double)profiles[i].value.worst_tick),
(double)(SDL_GetPerformanceFrequency()),
(double)profiles[i].value.worst_tick / (double)(SDL_GetPerformanceFrequency()),
profiles[i].value.sample_count); profiles[i].value.sample_count);
} }
} }