Skip to content

Commit

Permalink
emulator UI: Timing measurement helpers
Browse files Browse the repository at this point in the history
Used that to check obvious timing issues for framerates etc, didn't find
any problem...

Signed-off-by: Michel Pollet <[email protected]>
  • Loading branch information
buserror committed Feb 12, 2024
1 parent f9b604c commit 59beeb2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
18 changes: 16 additions & 2 deletions ui_gl/mii_emu_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;


Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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);
Expand Down
24 changes: 19 additions & 5 deletions ui_gl/mii_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -102,18 +107,27 @@ 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;
}
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
Expand Down
11 changes: 11 additions & 0 deletions ui_gl/mii_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions ui_gl/miigl_counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* miigl_counter.h
*
* Copyright (C) 2023 Michel Pollet <[email protected]>
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <time.h>
#include <stdint.h>
#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;
}

0 comments on commit 59beeb2

Please sign in to comment.