From 59beeb2f68e34449973f98a0d6754d30af1915d3 Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Mon, 12 Feb 2024 16:56:38 +0000 Subject: [PATCH] emulator UI: Timing measurement helpers Used that to check obvious timing issues for framerates etc, didn't find any problem... Signed-off-by: Michel Pollet --- ui_gl/mii_emu_gl.c | 18 +++++++++++++++-- ui_gl/mii_thread.c | 24 +++++++++++++++++----- ui_gl/mii_thread.h | 11 +++++++++++ ui_gl/miigl_counter.h | 46 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 ui_gl/miigl_counter.h diff --git a/ui_gl/mii_emu_gl.c b/ui_gl/mii_emu_gl.c index 56a336d..f9cd00f 100644 --- a/ui_gl/mii_emu_gl.c +++ b/ui_gl/mii_emu_gl.c @@ -27,6 +27,7 @@ #include "mii_mui.h" #include "mii-icon-64.h" #include "minipt.h" +#include "miigl_counter.h" /* * Note: This *assumes* that the GL implementation has support for non-power-of-2 @@ -83,6 +84,8 @@ typedef struct mii_x11_t { Atom wm_delete_window; int width, height; GLXContext glContext; + + miigl_counter_t videoc, redrawc, sleepc; } mii_x11_t; @@ -997,9 +1000,11 @@ main( glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); pixman_region32_clear(&mui->redraw); } - if (mii->video.frame_count != mii->video.frame_drawn) { + uint32_t current_frame = mii->video.frame_count; + if (current_frame != mii->video.frame_drawn) { + miigl_counter_tick(&ui->videoc, miigl_get_time()); draw = true; - mii->video.frame_drawn = mii->video.frame_count; + mii->video.frame_drawn = current_frame; // update the whole texture glBindTexture(GL_TEXTURE_2D, ui->mii_tex.id); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, @@ -1010,6 +1015,7 @@ main( } /* Draw */ if (draw) { + miigl_counter_tick(&ui->redrawc, miigl_get_time()); XGetWindowAttributes(ui->dpy, ui->win, &ui->attr); glViewport(0, 0, ui->width, ui->height); _mii_transition(ui); @@ -1023,6 +1029,14 @@ main( perror(__func__); goto cleanup; } + #if 0 + miigl_counter_tick(&ui->sleepc, miigl_get_time()); + if (!(current_frame % 60)) + printf("VID: %3d Draw:%3d sleep:%3d\n", + miigl_counter_get_read_size(&ui->videoc), + miigl_counter_get_read_size(&ui->redrawc), + miigl_counter_get_read_size(&ui->sleepc)); + #endif } cleanup: mii_emu_save(&ui->video.cf, &ui->video.config); diff --git a/ui_gl/mii_thread.c b/ui_gl/mii_thread.c index 520b920..7ea168b 100644 --- a/ui_gl/mii_thread.c +++ b/ui_gl/mii_thread.c @@ -22,10 +22,12 @@ #include "mii.h" #include "mii_thread.h" +#include "miigl_counter.h" static float default_fps = 60; mii_th_fifo_t signal_fifo; + int mii_thread_set_fps( int timerfd, @@ -60,6 +62,9 @@ mii_thread_cpu_regulator( mii_thread_set_fps(timerfd, default_fps); mii->state = MII_RUNNING; uint32_t last_frame = mii->video.frame_count; + +// miigl_counter_t frame_counter = {}; + while (running) { mii_th_signal_t sig; while (!mii_th_fifo_isempty(&signal_fifo)) { @@ -102,11 +107,13 @@ mii_thread_cpu_regulator( mii->state = MII_STOPPED; } break; - case MII_RUNNING: - sleep = mii->video.frame_count != last_frame; - if (sleep) - last_frame = mii->video.frame_count; - break; + case MII_RUNNING: { + uint32_t fi = mii->video.frame_count; + sleep = fi != last_frame; + if (sleep) { + last_frame = fi; + } + } break; case MII_TERMINATE: running = 0; break; @@ -114,6 +121,13 @@ mii_thread_cpu_regulator( if (sleep) { uint64_t timer_v; read(timerfd, &timer_v, sizeof(timer_v)); +/* + long current_fps = miigl_counter_tick(&frame_counter, + miigl_get_time()); + if (!(last_frame % 60)) { + printf("FPS: %4ld\n", current_fps); + } +*/ } } mii_dispose(mii); // this sets mii->state to MII_INIT diff --git a/ui_gl/mii_thread.h b/ui_gl/mii_thread.h index 157e22a..a7f5517 100644 --- a/ui_gl/mii_thread.h +++ b/ui_gl/mii_thread.h @@ -26,6 +26,17 @@ typedef struct mii_th_signal_t { DECLARE_FIFO(mii_th_signal_t, mii_th_fifo, 16); DEFINE_FIFO(mii_th_signal_t, mii_th_fifo); +DECLARE_FIFO(char*, mii_th_msg_fifo, 16); +DEFINE_FIFO(char*, mii_th_msg_fifo); + +typedef struct mii_thread_t { + pthread_t thread; + uint8_t state; + struct mii_t * mii; + mii_th_fifo_t signal; + mii_th_msg_fifo_t msg; +} mii_thread_t; + struct mii_t; pthread_t diff --git a/ui_gl/miigl_counter.h b/ui_gl/miigl_counter.h new file mode 100644 index 0000000..055234e --- /dev/null +++ b/ui_gl/miigl_counter.h @@ -0,0 +1,46 @@ +/* + * miigl_counter.h + * + * Copyright (C) 2023 Michel Pollet + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include "fifo_declare.h" + +/* + * Cheapish way of counting how many time 'stuff' happends in a second, + * Can be used to count FPS or other things as long as the frequency is less + * than 1024hz. + */ +DECLARE_FIFO(uint64_t, miigl_counter, 1024); +DEFINE_FIFO(uint64_t, miigl_counter); +static uint64_t +miigl_get_time() +{ + struct timespec tim; + clock_gettime(CLOCK_MONOTONIC_RAW, &tim); + uint64_t time = ((uint64_t)tim.tv_sec) * (1000000 / 1) + + tim.tv_nsec / (1000 * 1); + return time; +} +static int +miigl_counter_tick( + miigl_counter_t *c, + uint64_t time) +{ + // = miigl_get_time(); + // delete stamps that are older than a second + while (!miigl_counter_isempty(c) && + (time - miigl_counter_read_at(c, 0)) > 1000000) { + miigl_counter_read(c); + } + long freq = miigl_counter_get_read_size(c); + if (!miigl_counter_isfull(c)) + miigl_counter_write(c, time); + return freq; +}