mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 00:29:58 -04:00
Let the game's limiter stand aside only once RivaTuner Statistics Server is seen holding the game, and give up on RTSS at 1.05 times the limit over three fresh periods.
The worker stood aside as soon as RTSS listed the process and the profile read back, before RTSS had seen a Vulkan present (with InjectionDelay=15000 it lists the game some seconds after start) or read the new profile. Its frame count check took the newest period whatever its age: a period frozen at 144 fps after the limit went to 60 and the window was minimised failed RTSS for the run, and 1.15 times the limit was never caught (lane A's probe: listed_no_api, stale_fps, over115). rtss_decide now parses the entry's flags and stands aside only when the profile held the target at the poll before, RTSS lists the game with the Vulkan API value (flags & 0xFFFF == 0xA), RTSS_UPDATE_ASKED is clear, a new period was seen within max(2.5 s, 2.5 times its length), no movie plays and a host present went out within 200 ms. Only a new period after a poll that stood aside is checked: three in a row over 1.05 times the limit put the profile back for the run, and a stale period counts neither way. The worker reads the poll time and the newest host present (gfx_window_hosted_ns), and logs each change of why the game's limiter paces, up to 100 lines. Not reproduced before the change. rtss_limit_check (the flags at +264; T2 injection delay through not listed, no API, the update flag, no period and holding; T3 quit; T6 the frame count at 144, 69, 63 fps, a frozen period and a newer want; the idle case) and rtss_worker_check (over_run, over_slack_run, new api_run and idle_run, and every scenario that now holds from the poll after the write) pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -32,13 +32,20 @@ static inline const char *rtss_out_limiter(int64_t want) {
|
||||
|
||||
// ---- RTSS's shared memory, "RTSSSharedMemoryV2". The header: signature, version, application entry size, offset of
|
||||
// the application array, its length. An entry: the process id, the executable path (260 bytes), flags, then RTSS's
|
||||
// last frame rate period for the process: its start and end in ms and the frames in it.
|
||||
// last frame rate period for the process: its start and end in ms and the frames in it. The flags' low 16 bits hold
|
||||
// the API RTSS saw the process present with, a value rather than bits (Vulkan is 0xA), and RTSS_UPDATE_ASKED stays set
|
||||
// from UpdateProfiles until the process has read its profile again.
|
||||
#define RTSS_SIGNATURE 0x52545353u // 'RTSS' as RTSS writes it: the memory holds valid data
|
||||
enum { RTSS_PERIOD_AT = 4 + 260 + 4, RTSS_ENTRY_MIN = RTSS_PERIOD_AT + 12, RTSS_APPS_MAX = 4096 };
|
||||
#define RTSS_API_MASK 0xFFFFu
|
||||
#define RTSS_API_VULKAN 0xAu
|
||||
#define RTSS_UPDATE_ASKED 0x10000000u
|
||||
enum { RTSS_FLAGS_AT = 4 + 260, RTSS_PERIOD_AT = RTSS_FLAGS_AT + 4, RTSS_ENTRY_MIN = RTSS_PERIOD_AT + 12,
|
||||
RTSS_APPS_MAX = 4096 };
|
||||
|
||||
typedef struct {
|
||||
int running; // the signature and a version of 2.0 or later
|
||||
int listed; // the application list holds this process: RTSS has hooked it
|
||||
uint32_t flags; // the entry's flags
|
||||
uint32_t t0, t1, frames; // the period of this process's entry
|
||||
} RtssApp;
|
||||
|
||||
@@ -56,6 +63,7 @@ static inline void rtss_parse(const uint8_t *p, size_t size, uint32_t pid, RtssA
|
||||
memcpy(&id, p + at, 4);
|
||||
if (id != pid) continue;
|
||||
a->listed = 1;
|
||||
memcpy(&a->flags, p + at + RTSS_FLAGS_AT, 4);
|
||||
memcpy(t, p + at + RTSS_PERIOD_AT, sizeof t);
|
||||
a->t0 = t[0], a->t1 = t[1], a->frames = t[2];
|
||||
break;
|
||||
@@ -68,8 +76,10 @@ static inline double rtss_fps(const RtssApp *a) {
|
||||
}
|
||||
|
||||
// ---- the worker's decisions
|
||||
#define RTSS_CHECK_POLLS 6 // polls in a row RTSS's own frame rate of the game may stay above the limit
|
||||
#define RTSS_CHECK_SLACK 1.2 // above limit * this, RTSS is taken not to hold the game to it
|
||||
#define RTSS_OVER_PERIODS 3 // fresh periods in a row RTSS's frame rate of the game may stay above the limit
|
||||
#define RTSS_OVER_SLACK 1.05 // above limit * this, RTSS is taken not to hold the game to it
|
||||
#define RTSS_FRESH_NS 2500000000ull // a period is stale after this, or 2.5 times its own length if longer
|
||||
#define RTSS_HOLD_IDLE_NS 200000000ull // no host present for this long: RTSS holds nothing (vk_lowlat.c HOLD_IDLE_NS)
|
||||
|
||||
enum { RTSS_FAIL_NONE, RTSS_FAIL_LIBRARY, RTSS_FAIL_READONLY, RTSS_FAIL_NOT_LIMITING }; // out of it for the run
|
||||
enum { RTSS_DO_NOTHING, RTSS_DO_APPLY, RTSS_DO_PUT_BACK };
|
||||
@@ -83,17 +93,24 @@ enum {
|
||||
RTSS_WHY_NEWER, // the want changed while the poll ran: nothing decided, the next poll takes the new one
|
||||
RTSS_WHY_UNSET, // no want yet
|
||||
RTSS_WHY_MOVIE, // a movie plays: the game's pacer holds it at its rate and keeps its slots
|
||||
RTSS_WHY_NOT_VULKAN, // RTSS lists the game but has not seen its Vulkan presents
|
||||
RTSS_WHY_UPDATE, // RTSS has not read the profile the game wrote yet
|
||||
RTSS_WHY_NOT_COUNTING, // RTSS has measured no period of the game's frames lately
|
||||
RTSS_WHY_IDLE, // no picture went out lately (a minimised window, a frame hold): RTSS has none to hold
|
||||
};
|
||||
|
||||
// What the worker keeps between polls.
|
||||
typedef struct {
|
||||
int64_t applied; // the millihertz the profile holds from this run; -1 untouched
|
||||
int64_t refused; // a limit RTSS refused (out of its range): the game's own limiter keeps that one
|
||||
int changed; // the profile holds a value from this run: put the old one back on exit
|
||||
int over; // polls in a row RTSS's frame rate of the game stayed above the limit
|
||||
int fail; // RTSS_FAIL_*
|
||||
int64_t applied; // the millihertz the profile holds from this run; -1 untouched
|
||||
int64_t refused; // a limit RTSS refused (out of its range): the game's own limiter keeps that one
|
||||
int changed; // the profile holds a value from this run: put the old one back on exit
|
||||
int over; // fresh periods in a row RTSS's frame rate of the game stayed above the limit
|
||||
int fail; // RTSS_FAIL_*
|
||||
int held; // the poll before stood aside: a fresh period now shows how RTSS holds the game
|
||||
uint32_t t1, span; // the end and length (ms) of the newest period seen
|
||||
uint64_t fresh_ns; // when a new period was last seen; 0 never
|
||||
} RtssState;
|
||||
#define RTSS_STATE_INIT {-1, -1, 0, 0, RTSS_FAIL_NONE}
|
||||
#define RTSS_STATE_INIT {-1, -1, 0, 0, RTSS_FAIL_NONE, 0, 0, 0, 0}
|
||||
|
||||
// One poll's view.
|
||||
typedef struct {
|
||||
@@ -101,6 +118,8 @@ typedef struct {
|
||||
int current; // the want is still that one when the poll decides
|
||||
int hooks; // RTSS's hooks library: 1 loaded, 0 failed to load, -1 not tried (rtss_wants_hooks)
|
||||
int64_t movie_mhz; // the rate of a movie that plays (rtss_limit_movie), 0 none
|
||||
uint64_t now_ns; // kern_now_ns at the poll
|
||||
uint64_t hosted_ns; // kern_now_ns of the newest host present (gfx_present.c gfx_window_hosted_ns), 0 none yet
|
||||
RtssApp app;
|
||||
} RtssIn;
|
||||
|
||||
@@ -128,39 +147,58 @@ static inline int rtss_wants_hooks(const RtssState *s, const RtssIn *in) {
|
||||
// The decision of one poll, in this order: no want does nothing; a kept-out want puts back what this run wrote (once
|
||||
// the hooks are loaded); a failure lasts the run; RTSS not running leaves the profile for its return; a library that
|
||||
// did not load fails the run; a refused limit stays with the game; a new limit (rtss_target) goes into the profile at
|
||||
// once, and RTSS holds it as soon as it lists the game. On an unchanged limit RTSS's own frame count is checked: more
|
||||
// than RTSS_CHECK_SLACK times the limit for RTSS_CHECK_POLLS polls in a row puts the profile back for the run. While a
|
||||
// movie plays the game's pacer holds it (rtss_target). After the exit put the profile back (rtss_limit_restore) no poll
|
||||
// decides: rtss_limit.c worker skips it under the lock.
|
||||
// once. The game's limiter stands aside only once RTSS is seen to hold the game: the profile held the limit at the poll
|
||||
// before (RTSS reads it after the write), RTSS lists the game with its Vulkan presents, has read the profile
|
||||
// (RTSS_UPDATE_ASKED clear) and measured a period of the game's frames lately (a fresh one, rtss_fresh), no movie plays
|
||||
// (rtss_target) and a picture went out within RTSS_HOLD_IDLE_NS. Each fresh period after a poll that stood aside is
|
||||
// checked against the limit: more than RTSS_OVER_SLACK times it for RTSS_OVER_PERIODS periods in a row puts the profile
|
||||
// back for the run; a stale period counts neither way. After the exit put the profile back (rtss_limit_restore) no
|
||||
// poll decides: rtss_limit.c worker skips it under the lock.
|
||||
static inline RtssOut rtss_decide(RtssState *s, const RtssIn *in) {
|
||||
int64_t w = in->want;
|
||||
RtssOut o = {RTSS_DO_NOTHING, w, 0, RTSS_WHY_NOT_LISTED, 0};
|
||||
if (w == RTSS_UNSET) return o.why = RTSS_WHY_UNSET, o;
|
||||
const RtssApp *a = &in->app;
|
||||
int fresh = a->listed && a->t1 > a->t0 && a->frames && a->t1 != s->t1; // a period RTSS measured since
|
||||
if (fresh) s->t1 = a->t1, s->span = a->t1 - a->t0, s->fresh_ns = in->now_ns;
|
||||
int held = s->held;
|
||||
s->held = 0; // until this poll stands aside again
|
||||
if (w < 0) {
|
||||
if (in->hooks == 1 && s->changed) o.act = RTSS_DO_PUT_BACK;
|
||||
s->over = 0;
|
||||
return o.why = RTSS_WHY_OUT, o;
|
||||
}
|
||||
if (s->fail) return o.why = RTSS_WHY_FAILED, o;
|
||||
if (!in->app.running) return o.why = RTSS_WHY_NOT_RUNNING, o;
|
||||
if (!a->running) return o.why = RTSS_WHY_NOT_RUNNING, o;
|
||||
if (in->hooks == 0) return s->fail = RTSS_FAIL_LIBRARY, o.why = RTSS_WHY_FAILED, o;
|
||||
int64_t t = o.mhz = rtss_target(w, in->movie_mhz);
|
||||
if (t == s->refused) return o.why = RTSS_WHY_REFUSED, o;
|
||||
o.holds = in->app.listed;
|
||||
o.why = o.holds ? RTSS_WHY_HOLDS : RTSS_WHY_NOT_LISTED;
|
||||
if (in->movie_mhz) o.holds = 0, o.why = RTSS_WHY_MOVIE;
|
||||
if (t != s->applied) { // the result of the write may still undo the hold (rtss_written)
|
||||
if (t != s->applied) { // RTSS reads it after the write: a later poll sees whether it holds
|
||||
o.act = RTSS_DO_APPLY;
|
||||
o.why = in->movie_mhz ? RTSS_WHY_MOVIE : a->listed ? RTSS_WHY_UPDATE : RTSS_WHY_NOT_LISTED;
|
||||
s->over = 0;
|
||||
return o;
|
||||
}
|
||||
if (!in->current) return o.holds = 0, o.why = RTSS_WHY_NEWER, o; // these frames are not the new want's
|
||||
double fps = rtss_fps(&in->app);
|
||||
if (!in->app.listed || t == 0 || fps <= t / 1000.0 * RTSS_CHECK_SLACK) s->over = 0;
|
||||
else if (++s->over == RTSS_CHECK_POLLS) {
|
||||
s->fail = RTSS_FAIL_NOT_LIMITING;
|
||||
o.act = RTSS_DO_PUT_BACK, o.holds = 0, o.why = RTSS_WHY_FAILED, o.fps = fps;
|
||||
if (!in->current) return o.why = RTSS_WHY_NEWER, o; // these frames are not the new want's
|
||||
if (!a->listed) return o.why = RTSS_WHY_NOT_LISTED, o;
|
||||
if ((a->flags & RTSS_API_MASK) != RTSS_API_VULKAN) return o.why = RTSS_WHY_NOT_VULKAN, o;
|
||||
if (a->flags & RTSS_UPDATE_ASKED) return o.why = RTSS_WHY_UPDATE, o;
|
||||
uint64_t stale = (uint64_t)s->span * 2500000ull > RTSS_FRESH_NS ? (uint64_t)s->span * 2500000ull : RTSS_FRESH_NS;
|
||||
if (!s->fresh_ns || in->now_ns - s->fresh_ns > stale) return o.why = RTSS_WHY_NOT_COUNTING, o;
|
||||
if (fresh && held && t > 0) {
|
||||
double fps = rtss_fps(a);
|
||||
s->over = fps > t / 1000.0 * RTSS_OVER_SLACK ? s->over + 1 : 0;
|
||||
if (s->over == RTSS_OVER_PERIODS) {
|
||||
s->fail = RTSS_FAIL_NOT_LIMITING;
|
||||
o.act = RTSS_DO_PUT_BACK, o.why = RTSS_WHY_FAILED, o.fps = fps;
|
||||
return o;
|
||||
}
|
||||
}
|
||||
return o;
|
||||
if (in->movie_mhz) return o.why = RTSS_WHY_MOVIE, o;
|
||||
uint64_t idle = in->now_ns > in->hosted_ns ? in->now_ns - in->hosted_ns : 0;
|
||||
if (!in->hosted_ns || idle >= RTSS_HOLD_IDLE_NS) return o.why = RTSS_WHY_IDLE, o;
|
||||
s->held = o.holds = 1;
|
||||
return o.why = RTSS_WHY_HOLDS, o;
|
||||
}
|
||||
|
||||
// What a write of the limit did (RtssOps.apply).
|
||||
@@ -221,12 +259,17 @@ static inline void rtss_why_text(char *out, size_t n, const RtssOut *o, int fail
|
||||
else if (o->why == RTSS_WHY_NOT_RUNNING) snprintf(out, n, "RivaTuner Statistics Server is not running");
|
||||
else if (o->why == RTSS_WHY_REFUSED) snprintf(out, n, "RivaTuner Statistics Server refused this limit");
|
||||
else if (o->why == RTSS_WHY_MOVIE) snprintf(out, n, "a movie plays: the game keeps its pictures on time");
|
||||
else if (o->why == RTSS_WHY_NOT_VULKAN)
|
||||
snprintf(out, n, "RivaTuner Statistics Server lists the game but has not seen its Vulkan presents yet");
|
||||
else if (o->why == RTSS_WHY_UPDATE)
|
||||
snprintf(out, n, "RivaTuner Statistics Server has not read the game's limit from its profile yet");
|
||||
else if (o->why == RTSS_WHY_NOT_COUNTING)
|
||||
snprintf(out, n, "RivaTuner Statistics Server has measured no frames of the game lately");
|
||||
else if (o->why == RTSS_WHY_IDLE) snprintf(out, n, "no picture went out lately: RivaTuner has none to hold");
|
||||
else snprintf(out, n, "RivaTuner Statistics Server has not hooked the game yet");
|
||||
}
|
||||
|
||||
// ---- the game's pacer beside RTSS (frame_rate.c frame_step_cap_ns)
|
||||
#define RTSS_HOLD_IDLE_NS 200000000ull // no host present for this long: RTSS holds nothing (vk_lowlat.c HOLD_IDLE_NS)
|
||||
|
||||
// The period the pacer keeps (now_ns and hosted_ns are kern_now_ns times; hosted_ns of the newest host present,
|
||||
// gfx_present.c gfx_window_hosted_ns, 0 before the first). The limit's own period while RTSS does not hold the game,
|
||||
// while a movie plays (from its first frame: the pacer keeps the movie's slots, which RTSS does not know, and the
|
||||
|
||||
@@ -13,11 +13,13 @@
|
||||
// then fails silently. So every change is read back
|
||||
// from the saved profile, and a limit that did not stick leaves the game's own limiter in charge for the session.
|
||||
//
|
||||
// RTSS runs when its shared memory "RTSSSharedMemoryV2" carries the signature 'RTSS'; it limits this process once the
|
||||
// memory's application list holds this process id (RTSS hooks a process some seconds after it starts, its injection
|
||||
// delay). Until then, and again if RTSS quits, the game's own limiter paces the frames. While RTSS limits, the frame
|
||||
// rate RTSS itself measures for the game is checked against the limit: well above it for three seconds, and the game
|
||||
// takes the limiting back for the rest of the run.
|
||||
// RTSS runs when its shared memory "RTSSSharedMemoryV2" carries the signature 'RTSS'. The game's own limiter stands
|
||||
// aside only once RTSS is seen to hold the game (rtss_decide.h rtss_decide): the memory's application list holds this
|
||||
// process id with its Vulkan presents (RTSS hooks a process some seconds after it starts, its injection delay), RTSS
|
||||
// has read the profile the game wrote, it measured a period of the game's frames lately, no movie plays and pictures
|
||||
// go out. Until then, and again whenever one of these ends (RTSS quits, the window is minimised), the game's own limiter
|
||||
// paces the frames. While RTSS limits, each new period RTSS measures for the game is checked against the limit: above
|
||||
// 1.05 times it for three periods in a row, and the game takes the limiting back for the rest of the run.
|
||||
//
|
||||
// The calls run on a thread of their own, every half second, which also looks RTSS up (find_rtss), so a present never
|
||||
// waits on the registry or profile files. On every orderly exit (rtss_limit_restore) the profile gets back what it held
|
||||
@@ -40,6 +42,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "gfx.h"
|
||||
#include "host.h"
|
||||
#include "platform/platform.h"
|
||||
#include "rtss_decide.h"
|
||||
@@ -328,17 +331,23 @@ static void put_back_op(void *ctx, const RtssOut *o) { // rtss_poll's RtssOps.
|
||||
}
|
||||
|
||||
// Whether RTSS holds the game to the want w this poll read, and the log of which limiter is in use: the first decision,
|
||||
// then each change. The want may have changed since the poll read it: rtss_limit_active compares the two, so a stale
|
||||
// store never lets the game's limiter stand aside for a newer want. The store is made either way (a want that went
|
||||
// w, w2, w again must not find an older hold), but a hold is logged only while its want is still the current one: the
|
||||
// game's limiter paces a newer want, and the next poll logs what holds it.
|
||||
// then each change of the limiter or of why the game's own one paces (RTSS not hooked yet, not read the profile, a
|
||||
// minimised window), up to LIMITER_LINES lines. The want may have changed since the poll read it: rtss_limit_active
|
||||
// compares the two, so a stale store never lets the game's limiter stand aside for a newer want. The store is made
|
||||
// either way (a want that went w, w2, w again must not find an older hold), but a hold is logged only while its want
|
||||
// is still the current one: the game's limiter paces a newer want, and the next poll logs what holds it.
|
||||
#define LIMITER_LINES 100
|
||||
static void set_active(int64_t w, int on, const char *why) {
|
||||
static int logged = -1;
|
||||
static int logged = -1, lines;
|
||||
static char said[128];
|
||||
atomic_store(&active_for, on ? w : RTSS_UNSET);
|
||||
if (logged == on || (on && atomic_load(&want) != w)) return;
|
||||
if ((on && atomic_load(&want) != w) || (logged == on && (on || !strcmp(why, said)))) return;
|
||||
logged = on;
|
||||
snprintf(said, sizeof said, "%s", on ? "" : why);
|
||||
if (++lines > LIMITER_LINES) return;
|
||||
if (on) rt_log("frame rate: RivaTuner Statistics Server does the limiting; the game's own limiter stands aside");
|
||||
else rt_log("frame rate: the game's own limiter (%s)", why);
|
||||
if (lines == LIMITER_LINES) rt_log("frame rate: later changes of the limiter in charge are not logged");
|
||||
}
|
||||
|
||||
// RTSS's own settings for the game, read from its profile file as RTSS keeps it: Profiles\<exe>.cfg, else
|
||||
@@ -473,7 +482,8 @@ static void worker(void *arg) {
|
||||
plat_mutex_unlock(&lock);
|
||||
continue;
|
||||
}
|
||||
RtssIn in = {.want = w, .movie_mhz = atomic_load(&movie_mhz)};
|
||||
RtssIn in = {.want = w, .movie_mhz = atomic_load(&movie_mhz), .now_ns = kern_now_ns(),
|
||||
.hosted_ns = gfx_window_hosted_ns()};
|
||||
rtss_map(&in.app);
|
||||
if (in.app.running != was_running || in.app.listed != was_listed) {
|
||||
if (!in.app.running) rt_log("frame rate: RivaTuner Statistics Server is installed but not running");
|
||||
|
||||
Reference in New Issue
Block a user