Move RivaTuner Statistics Server's decisions into a function of their own, and pin them with a check that needs no RTSS.

The worker read RTSS's shared memory, decided and wrote the profile in one loop over file statics, so its decisions
could be checked only against a running RTSS. rtss_decide.h now holds the parts that make no system call: rtss_parse
reads the shared memory's header and this process's entry (bounded by the memory's size and 4096 entries, as before),
rtss_decide takes one poll's view (the want, whether it is still current, a closed run, the hooks library, the entry)
and the state between polls (RtssState: the limit applied and the one refused, whether the run changed the profile,
the frame count, the failure) and returns what to do and whether RTSS holds the game, and rtss_poll carries that out
through the profile calls rtss_limit.c hands it (apply and put_back, which keep their I/O and log lines). The worker,
rtss_limit_restore and the menu state read the same state.

No decision changes: the order of the rules, the immediate write before RTSS lists the game, six polls over 1.2 times
the limit, a write per movie and per switch of low latency all stay as they are. The rules that wait for approval are
not in it (the pacer keeping movies and windows with no host present, Vulkan flags, settling and a fresh period
before standing aside, rounding the limit down, the state file).

tests/rtss_worker_check.c gains nine scenarios that pin those decisions against the real worker (a read-only profile,
a refused limit, the frame count rule at 144, 69, 73 fps and unlimited, RTSS quitting, a hooks library that does not
load, low latency switching, a movie); the logs of all 23 scenarios are the same, line for line, before and after this
change. tests/rtss_limit_check.c checks the same decisions on rtss_decide.h alone, with a fake RTSSSharedMemoryV2 for
the reader (a bad header, short entries, an entry past the memory or past the 4096th) and a fake profile behind
RtssOps; mutating the frame count's polls or slack, the refusal, the library failure or the clear of a refused limit
makes it fail. docs/building.md describes the check. Every runtime file compiles; -Wall adds no warning;
frame_step_check passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:28:46 -05:00
parent 7613b7ade6
commit 8e8d6a2f51
5 changed files with 739 additions and 103 deletions

View File

@@ -1,9 +1,11 @@
// RivaTuner Statistics Server as the frame limiter (rtss_limit.c): the parts that make no system call, so that
// tests/rtss_limit_check.c builds them on any system.
// tests/rtss_limit_check.c builds them on any system. rtss_parse reads RTSS's shared memory, rtss_decide decides each
// poll of the worker, and rtss_poll carries that decision out through the calls rtss_limit.c gives it (RtssOps).
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
// rtss_limit_want's argument: a limit in millihertz (>0), 0 for none, or why RTSS is kept out of it (frame_rate.c
// frame_step_cap_ns). RTSS_UNSET is the want before the first one.
@@ -28,6 +30,190 @@ static inline const char *rtss_out_limiter(int64_t want) {
: "the game's limiter";
}
// ---- 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.
#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 };
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 t0, t1, frames; // the period of this process's entry
} RtssApp;
// Reads the memory of size bytes at p for the process pid; never reads past size.
static inline void rtss_parse(const uint8_t *p, size_t size, uint32_t pid, RtssApp *a) {
memset(a, 0, sizeof *a);
if (!p || size < 20) return;
uint32_t h[5];
memcpy(h, p, sizeof h);
a->running = h[0] == RTSS_SIGNATURE && h[1] >= 0x20000;
for (uint32_t i = 0; a->running && h[2] >= RTSS_ENTRY_MIN && i < h[4] && i < RTSS_APPS_MAX; i++) {
uint64_t at = (uint64_t)h[3] + (uint64_t)i * h[2];
if (at + RTSS_ENTRY_MIN > size) break;
uint32_t id, t[3];
memcpy(&id, p + at, 4);
if (id != pid) continue;
a->listed = 1;
memcpy(t, p + at + RTSS_PERIOD_AT, sizeof t);
a->t0 = t[0], a->t1 = t[1], a->frames = t[2];
break;
}
}
// The frame rate RTSS measured for the process over its period; 0 unknown.
static inline double rtss_fps(const RtssApp *a) {
return a->listed && a->t1 > a->t0 ? 1000.0 * a->frames / (a->t1 - a->t0) : 0;
}
// ---- 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
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 };
enum {
RTSS_WHY_HOLDS, // RTSS holds the game to the want
RTSS_WHY_NOT_LISTED, // RTSS has the limit but has not hooked the game yet
RTSS_WHY_OUT, // kept out (the want says why)
RTSS_WHY_FAILED, // RtssState.fail
RTSS_WHY_NOT_RUNNING,
RTSS_WHY_REFUSED, // RTSS refused this limit
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_CLOSED, // the exit put the profile back (rtss_limit_restore)
};
// 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_*
} RtssState;
#define RTSS_STATE_INIT {-1, -1, 0, 0, RTSS_FAIL_NONE}
// One poll's view.
typedef struct {
int64_t want; // the want the poll read
int current; // the want is still that one when the poll decides
int closed; // rtss_limit_restore ran
int hooks; // RTSS's hooks library: 1 loaded, 0 failed to load, -1 not tried (rtss_wants_hooks)
RtssApp app;
} RtssIn;
typedef struct {
int act; // RTSS_DO_*
int64_t mhz; // the want decided for
int holds; // RTSS holds the game to it: the game's own limiter stands aside
int why; // RTSS_WHY_*
double fps; // RTSS's frame rate of the game, when the frame count check gave up on it
} RtssOut;
// The hooks library is loaded only on a poll that may write the profile: a limit is wanted, RTSS is not out of it for
// the run, and it runs. A kept-out run never loads it.
static inline int rtss_wants_hooks(const RtssState *s, const RtssIn *in) {
return in->want >= 0 && !s->fail && in->app.running;
}
// The decision of one poll, in this order: a closed run or 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 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.
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 (in->closed) return o.why = RTSS_WHY_CLOSED, o;
if (w == RTSS_UNSET) return o.why = RTSS_WHY_UNSET, o;
if (w < 0) {
if (in->hooks == 1 && s->changed) o.act = RTSS_DO_PUT_BACK;
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 (in->hooks == 0) return s->fail = RTSS_FAIL_LIBRARY, o.why = RTSS_WHY_FAILED, o;
if (w == 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 (w != s->applied) { // the result of the write may still undo the hold (rtss_written)
o.act = RTSS_DO_APPLY;
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 || w == 0 || fps <= w / 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;
}
return o;
}
// What a write of the limit did (RtssOps.apply).
enum {
RTSS_WROTE_READONLY = -1, // the save did not reach the file
RTSS_WROTE_REFUSED = 0, // RTSS refused the value; the profile keeps what it held
RTSS_WROTE_HELD = 1, // the profile holds the limit
RTSS_WROTE_REFUSED_CLEARED = 2, // refused, and the earlier limit of this run was cleared to none
};
static inline void rtss_written(RtssState *s, RtssOut *o, int r) {
if (r == RTSS_WROTE_READONLY) {
s->fail = RTSS_FAIL_READONLY, o->holds = 0, o->why = RTSS_WHY_FAILED;
return;
}
s->changed = 1;
if (r == RTSS_WROTE_HELD) {
s->applied = o->mhz;
return;
}
if (r == RTSS_WROTE_REFUSED_CLEARED) s->applied = 0;
s->refused = o->mhz, o->holds = 0, o->why = RTSS_WHY_REFUSED;
}
// The profile was put back: nothing of this run is in it.
static inline void rtss_put_back_done(RtssState *s) { s->changed = 0, s->applied = -1; }
// The profile calls rtss_poll makes. apply writes mhz (first: the first write of the run, which records what to put
// back; clear: on a refusal, clear this run's earlier limit to none) and returns RTSS_WROTE_*; put_back puts the profile
// back as it was before the run (o says why: kept out, or the frame count check gave up).
typedef struct {
void *ctx;
int (*apply)(void *ctx, int64_t mhz, int first, int clear);
void (*put_back)(void *ctx, const RtssOut *o);
} RtssOps;
static inline RtssOut rtss_poll(RtssState *s, const RtssIn *in, const RtssOps *ops) {
RtssOut o = rtss_decide(s, in);
if (o.act == RTSS_DO_APPLY) {
rtss_written(s, &o, ops->apply(ops->ctx, o.mhz, !s->changed, s->applied != 0));
} else if (o.act == RTSS_DO_PUT_BACK) {
ops->put_back(ops->ctx, &o);
rtss_put_back_done(s);
}
return o;
}
static inline const char *rtss_fail_text(int fail) {
return fail == RTSS_FAIL_READONLY ? "RivaTuner Statistics Server's profile cannot be changed"
: fail == RTSS_FAIL_NOT_LIMITING ? "RivaTuner Statistics Server does not hold the game to the limit"
: "RivaTuner Statistics Server's hooks library did not load";
}
// Why the game's own limiter paces, for the log line "the game's own limiter (<text>)".
static inline void rtss_why_text(char *out, size_t n, const RtssOut *o, int fail) {
if (o->why == RTSS_WHY_OUT) snprintf(out, n, "RivaTuner Statistics Server kept out: %s", rtss_out_text(o->mhz));
else if (o->why == RTSS_WHY_FAILED) snprintf(out, n, "%s", rtss_fail_text(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 snprintf(out, n, "RivaTuner Statistics Server has not hooked the game yet");
}
// ---- RTSS's own settings
// RTSS's own [Framerate] settings for the game, as its profile file holds them (rtss_limit.c read_settings): a frame
// rate limit (Limit over LimitDenominator), a frame time limit (LimitTime over LimitTimeDenominator) and scanline sync
// (SyncScanline0 and SyncScanline1), 0 off for each; and the modes beside them, for the log: SyncDisplay, ReflexSleep
@@ -40,6 +226,12 @@ typedef struct {
// RTSS holds the game's frames by a setting of its own.
static inline int rtss_settings_limit(const RtssSettings *s) { return s->limit || s->time || s->scan0 || s->scan1; }
// Two limiters: RTSS is kept out of the game's limit, and while it lists the game a setting of its own still holds the
// frames (rtss_settings_limit), beside the game's limiter, the driver's or frame generation's.
static inline int rtss_two(int64_t want, int listed, int own_limit) {
return want < 0 && want != RTSS_UNSET && listed && own_limit;
}
// "limit L/D, frame time limit T/TD, scanline sync S0/S1", and with modes ", display sync X, Reflex sleep R, passive
// wait P": the values as the file holds them.
static inline void rtss_settings_text(char *out, size_t n, const RtssSettings *s, int modes) {

View File

@@ -47,17 +47,12 @@ typedef void (*ProfileFn)(LPCSTR name);
typedef BOOL (*PropertyFn)(LPCSTR property, LPBYTE data, DWORD size);
typedef void (*UpdateFn)(void);
#define RTSS_SIGNATURE 0x52545353u // 'RTSS' as RTSS writes it: the memory holds valid data
#define POLL_MS 500
#define CHECK_POLLS 6 // polls in a row RTSS's own frame rate of the game may stay above the limit
#define CHECK_SLACK 1.2 // above limit * this, RTSS is taken not to hold the game to it
enum { FAIL_NONE, FAIL_LIBRARY, FAIL_READONLY, FAIL_NOT_LIMITING };
static PlatMutex lock = PLAT_MUTEX_INIT; // the hooks calls and the state below
static _Atomic int64_t want = RTSS_UNSET; // millihertz from the pacer: >0 a limit, 0 none; RTSS_OUT_* kept out
static _Atomic int64_t active_for = RTSS_UNSET; // the want RTSS holds the game to now; RTSS_UNSET none
static _Atomic int failed; // FAIL_*: RTSS is out of it for the rest of the run
static _Atomic int failed; // RTSS_FAIL_*: RTSS is out of it for the rest of the run (S.fail)
static _Atomic int closed; // rtss_limit_restore ran: nothing reaches the profile any more
static _Atomic int two; // kept out, and RTSS limits the game by a setting of its own (watch)
static _Atomic int started; // rtss_limit_want spawned the worker
@@ -68,45 +63,21 @@ static EnumProfilesFn enum_profiles;
static ProfileFn load_profile, save_profile, delete_profile;
static PropertyFn get_property, set_property;
static UpdateFn update_profiles;
static RtssState S = RTSS_STATE_INIT; // the worker's decisions (rtss_decide.h), under lock
static int has_denominator; // this RTSS knows FramerateLimitDenominator
static int64_t applied = -1; // the millihertz the profile holds from this run; -1 untouched
static int64_t refused = -1; // a limit RTSS refused (out of its range): the game's own limiter keeps that one
static int changed; // the profile holds a value from this run: put the old one back on exit
static int had_profile; // the profile existed before this run changed it
static DWORD old_limit, old_denominator = 1;
// RTSS's shared memory: running (1), whether its application list holds this process, and the frame rate RTSS measured
// for it over its last period (0 unknown). The header: signature, version, application entry size, offset of the
// application array, its length. An entry: the process id, the executable path (MAX_PATH), flags, then the period's
// start and end in ms and the frames in it.
static int rtss_running(int *hooked, double *fps) {
*hooked = 0, *fps = 0;
// RTSS's shared memory as rtss_parse reads it: whether RTSS runs, whether it lists this process, and its frame count.
static void rtss_map(RtssApp *a) {
memset(a, 0, sizeof *a);
HANDLE m = OpenFileMappingA(FILE_MAP_READ, FALSE, "RTSSSharedMemoryV2");
if (!m) return 0;
if (!m) return;
const uint8_t *p = MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0);
int run = 0;
MEMORY_BASIC_INFORMATION mi;
if (p && VirtualQuery(p, &mi, sizeof mi) && mi.RegionSize >= 20) {
DWORD h[5];
memcpy(h, p, sizeof h);
run = h[0] == RTSS_SIGNATURE && h[1] >= 0x20000;
DWORD pid = GetCurrentProcessId();
enum { PERIOD_AT = 4 + MAX_PATH + 4, ENTRY_MIN = PERIOD_AT + 12 };
for (DWORD i = 0; run && h[2] >= ENTRY_MIN && i < h[4] && i < 4096; i++) {
uint64_t at = (uint64_t)h[3] + (uint64_t)i * h[2];
if (at + ENTRY_MIN > mi.RegionSize) break;
DWORD id, t[3];
memcpy(&id, p + at, 4);
if (id != pid) continue;
*hooked = 1;
memcpy(t, p + at + PERIOD_AT, sizeof t); // start, end, frames
if (t[1] > t[0]) *fps = 1000.0 * t[2] / (t[1] - t[0]);
break;
}
}
if (p && VirtualQuery(p, &mi, sizeof mi)) rtss_parse(p, mi.RegionSize, GetCurrentProcessId(), a);
if (p) UnmapViewOfFile(p);
CloseHandle(m);
return run;
}
static int load_hooks(void) {
@@ -176,10 +147,11 @@ static int write_limit(DWORD limit, DWORD denominator) {
return l == limit && (!has_denominator || d == denominator) ? 1 : -1;
}
// 1: the profile holds mhz; 0: RTSS refused it (the profile then holds no limit of this run); -1: the profile cannot be
// written.
static int apply(int64_t mhz) {
if (!changed) { // what to put back on exit
// Writes mhz into the profile (rtss_poll's RtssOps.apply): RTSS_WROTE_*. first: the run's first write, which records
// what to put back on exit; clear: on a refusal, clear the limit of this run the profile holds.
static int apply(void *ctx, int64_t mhz, int first, int clear) {
(void)ctx;
if (first) { // what to put back on exit
had_profile = profile_exists();
load_profile(profile);
old_limit = 0, old_denominator = 1;
@@ -195,23 +167,23 @@ static int apply(int64_t mhz) {
if (r < 0) {
rt_log("frame rate: RivaTuner Statistics Server's profile %s did not keep the limit: its Profiles folder needs the "
"game run as administrator", profile);
return -1;
return RTSS_WROTE_READONLY;
}
if (!changed) {
if (first) {
if (!had_profile)
rt_log("frame rate: RivaTuner Statistics Server profile %s created; it goes away when the game closes", profile);
else
rt_log("frame rate: RivaTuner Statistics Server profile %s held a limit of %lu/%lu; it gets that back when the "
"game closes", profile, old_limit, old_denominator);
changed = 1;
}
if (r == 0) { // RTSS must not keep an earlier limit of this run under the game's own one
rt_log("frame rate: RivaTuner Statistics Server refused a limit of %.3f fps", mhz / 1000.0);
if (applied != 0 && write_limit(0, 1) > 0) applied = 0, update_profiles();
refused = mhz;
return 0;
if (clear && write_limit(0, 1) > 0) {
update_profiles();
return RTSS_WROTE_REFUSED_CLEARED;
}
return RTSS_WROTE_REFUSED;
}
applied = mhz;
update_profiles();
if (!mhz) rt_log("frame rate: RivaTuner Statistics Server's profile %s has no limit (unlimited)", profile);
else if ((int64_t)limit * 1000 == mhz * denominator)
@@ -219,13 +191,11 @@ static int apply(int64_t mhz) {
else // rounded to whole frames by an RTSS without FramerateLimitDenominator: the log says what RTSS holds
rt_log("frame rate: RivaTuner Statistics Server's profile %s limits to %lu/%lu (%.3f fps; the game asked %.3f)",
profile, limit, denominator, (double)limit / denominator, mhz / 1000.0);
return 1;
return RTSS_WROTE_HELD;
}
// The profile as it was before this run changed it (the caller checked S.changed, and marks it done).
static void put_back(void) {
if (!changed) return;
changed = 0;
applied = -1;
if (!had_profile) {
delete_profile(profile);
rt_log("frame rate: RivaTuner Statistics Server profile %s removed, as before the game started", profile);
@@ -238,6 +208,14 @@ static void put_back(void) {
update_profiles();
}
static void put_back_op(void *ctx, const RtssOut *o) { // rtss_poll's RtssOps.put_back
(void)ctx;
if (o->why == RTSS_WHY_FAILED) // the frame count check gave up on RTSS
rt_log("frame rate: RivaTuner Statistics Server measures %.1f fps with its limit at %.3f fps", o->fps,
o->mhz / 1000.0);
put_back();
}
// 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.
@@ -250,12 +228,6 @@ static void set_active(int64_t w, int on, const char *why) {
else rt_log("frame rate: the game's own limiter (%s)", why);
}
static const char *fail_reason(int f) {
return f == FAIL_READONLY ? "RivaTuner Statistics Server's profile cannot be changed"
: f == FAIL_NOT_LIMITING ? "RivaTuner Statistics Server does not hold the game to the limit"
: "RivaTuner Statistics Server's hooks library did not load";
}
// RTSS's own settings for the game, read from its profile file as RTSS keeps it: Profiles\<exe>.cfg, else
// Profiles\Global, and a key the game's profile lacks from Global. The system's ini reader does it, so no call goes into
// RTSS's hooks library, which a run that keeps RTSS out never loads. The files are read again only when the time
@@ -314,7 +286,7 @@ static void watch(int64_t w, int listed) {
snprintf(said_settings, sizeof said_settings, "%s", all);
}
if (w < 0 && listed) {
on = rtss_settings_limit(&settings);
on = rtss_two(w, listed, rtss_settings_limit(&settings));
rtss_settings_text(text, sizeof text, &settings, 0);
}
if (w >= 0) said = 0; // a later kept-out want logs afresh
@@ -368,7 +340,8 @@ static void worker(void *arg) {
(void)arg;
if (!find_rtss()) return;
atomic_store_explicit(&ready, 1, memory_order_release); // rtss_limit_restore may read the names now
int was_running = -1, was_hooked = 0, over = 0;
int was_running = -1, was_listed = 0;
const RtssOps ops = {NULL, apply, put_back_op};
for (;; plat_sleep_ms(POLL_MS)) {
int64_t w = atomic_load(&want);
if (w == RTSS_UNSET) continue;
@@ -377,49 +350,24 @@ static void worker(void *arg) {
plat_mutex_unlock(&lock);
continue;
}
int hooked;
double fps;
int run = rtss_running(&hooked, &fps), f = atomic_load(&failed);
if (run != was_running || hooked != was_hooked) {
if (!run) rt_log("frame rate: RivaTuner Statistics Server is installed but not running");
else if (!hooked) rt_log("frame rate: RivaTuner Statistics Server runs; it has not hooked the game yet");
RtssIn in = {.want = w};
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");
else if (!in.app.listed) rt_log("frame rate: RivaTuner Statistics Server runs; it has not hooked the game yet");
else rt_log("frame rate: RivaTuner Statistics Server has hooked the game");
was_running = run, was_hooked = hooked;
was_running = in.app.running, was_listed = in.app.listed;
}
if (w < 0) {
if (dll) put_back();
char why[96];
snprintf(why, sizeof why, "RivaTuner Statistics Server kept out: %s", rtss_out_text(w));
set_active(w, 0, why);
} else if (f) {
set_active(w, 0, fail_reason(f));
} else if (!run) {
set_active(w, 0, "RivaTuner Statistics Server is not running");
} else if (!load_hooks()) {
atomic_store(&failed, FAIL_LIBRARY);
set_active(w, 0, fail_reason(FAIL_LIBRARY));
} else if (w == refused) {
set_active(w, 0, "RivaTuner Statistics Server refused this limit");
} else if (w != applied) {
int r = apply(w);
over = 0;
if (r < 0) atomic_store(&failed, FAIL_READONLY), set_active(w, 0, fail_reason(FAIL_READONLY));
else if (r == 0) set_active(w, 0, "RivaTuner Statistics Server refused this limit");
else set_active(w, hooked, "RivaTuner Statistics Server has not hooked the game yet");
} else if (atomic_load(&want) == w) { // a newer want waits for the next poll: these frames are not its
set_active(w, hooked, "RivaTuner Statistics Server has not hooked the game yet");
// RTSS's own count of the game's frames while the game's limiter stands aside: well above the limit for
// three seconds means RTSS does not hold the game to it, and the game takes the limiting back for the run
if (!hooked || w == 0 || fps <= w / 1000.0 * CHECK_SLACK) over = 0;
else if (++over == CHECK_POLLS) {
rt_log("frame rate: RivaTuner Statistics Server measures %.1f fps with its limit at %.3f fps", fps,
w / 1000.0);
atomic_store(&failed, FAIL_NOT_LIMITING);
put_back();
set_active(w, 0, fail_reason(FAIL_NOT_LIMITING));
}
in.hooks = dll ? 1 : rtss_wants_hooks(&S, &in) ? load_hooks() : -1;
in.current = atomic_load(&want) == w;
RtssOut o = rtss_poll(&S, &in, &ops);
atomic_store(&failed, S.fail);
if (o.why != RTSS_WHY_NEWER) { // a newer want waits for the next poll
char why[128];
rtss_why_text(why, sizeof why, &o, S.fail);
set_active(w, o.holds, why);
}
watch(w, run && hooked);
watch(w, in.app.listed);
plat_mutex_unlock(&lock);
}
}
@@ -446,7 +394,7 @@ int rtss_limit_state(void) {
if (atomic_load_explicit(&closed, memory_order_relaxed)) return 0;
int f = atomic_load_explicit(&failed, memory_order_relaxed);
int64_t w = atomic_load(&want);
if (w >= 0 && (f == FAIL_READONLY || f == FAIL_NOT_LIMITING)) return f == FAIL_READONLY ? 2 : 3;
if (w >= 0 && (f == RTSS_FAIL_READONLY || f == RTSS_FAIL_NOT_LIMITING)) return f == RTSS_FAIL_READONLY ? 2 : 3;
if (w < 0 && w != RTSS_UNSET && atomic_load_explicit(&two, memory_order_relaxed)) return 4;
return rtss_limit_active() && w > 0;
}
@@ -461,7 +409,7 @@ void rtss_limit_restore(void) {
// the exit may come from a crash on any thread: give up after a second rather than wait on a lock held there
for (int i = 0; i < 100; i++) {
if (plat_mutex_trylock(&lock)) {
if (dll) put_back();
if (dll && S.changed) put_back(), rtss_put_back_done(&S);
plat_mutex_unlock(&lock);
return;
}