Compare commits
No commits in common. "9ddc5c4a6690094c4886197e624101f6a7fe13c1" and "ab3c03231376fe459744d852893996a78f7f37c6" have entirely different histories.
9ddc5c4a66
...
ab3c032313
@ -68,7 +68,6 @@ 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
|
||||||
|
@ -76,26 +76,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +85,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 = 24;
|
state->r = 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,71 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
#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
|
|
@ -5,7 +5,6 @@
|
|||||||
#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>
|
||||||
@ -215,13 +214,7 @@ 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 */
|
||||||
|
@ -16,7 +16,6 @@ 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;
|
||||||
|
|
||||||
@ -302,62 +301,41 @@ 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]) {
|
||||||
/* stamp time immediately, so to not have influence of our profile lookup */
|
uint64_t tick_accum = 0, sample_count = 0, worst_tick = 0;
|
||||||
uint64_t const counter = SDL_GetPerformanceCounter();
|
|
||||||
|
|
||||||
struct ProfileItem *p = shgetp_null(profiles, profile);
|
struct ProfileItem const *p = shgetp_null(profiles, profile);
|
||||||
if (p) {
|
if (p) {
|
||||||
p->value.tick_start = counter;
|
tick_accum = p->value.tick_accum;
|
||||||
p->value.active = true;
|
sample_count = p->value.sample_count;
|
||||||
} else {
|
worst_tick = p->value.worst_tick;
|
||||||
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 || !p->value.active) {
|
if (!p) {
|
||||||
log_warn("Profile %s wasn't started!", profile);
|
log_warn("Profile %s wasn't started!", profile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t const took = counter - p->value.tick_start;
|
uint64_t took = SDL_GetPerformanceCounter() - 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) {
|
||||||
@ -365,16 +343,18 @@ 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: %s",
|
log_info("Profile '%s' took: %fs",
|
||||||
profiles[i].key,
|
profiles[i].key,
|
||||||
format_profile_time((double)profiles[i].value.tick_accum));
|
(double)profiles[i].value.tick_accum / (double)(SDL_GetPerformanceFrequency()));
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (profiles[i].value.sample_count > 1) {
|
else if (profiles[i].value.sample_count > 1) {
|
||||||
log_info("Profile '%s' on average took: %s, worst case: %s, sample count: %llu",
|
log_info("Profile '%s' on average took: %fs, worst case: %fs, sample count: %llu",
|
||||||
profiles[i].key,
|
profiles[i].key,
|
||||||
format_profile_time((double)profiles[i].value.tick_accum / (double)profiles[i].value.sample_count),
|
(double)profiles[i].value.tick_accum /
|
||||||
format_profile_time((double)profiles[i].value.worst_tick),
|
(double)profiles[i].value.sample_count /
|
||||||
|
(double)(SDL_GetPerformanceFrequency()),
|
||||||
|
(double)profiles[i].value.worst_tick / (double)(SDL_GetPerformanceFrequency()),
|
||||||
profiles[i].value.sample_count);
|
profiles[i].value.sample_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user