Keep the state file's line ahead of RivaTuner Statistics Server's profile, and keep what a run owes the profile after a put back that did not land.

The review found three ways rtss-profile.txt could fall behind the profile. Every write after a run's first one saved
the profile before the line, so a kill between the two left a line whose value the profile no longer held, and the
next start took the game's limit for the player's (rtss_restore_plan LEAVE). A put back whose write did not land kept
the line, but rtss_put_back_done cleared S.changed, so a later limit took the run's first-write path again and read the
game's own limit from the profile as the value to put back, overwriting the true one in the line. And put_back removed
the line after DeleteProfile without checking that the profile went away.

The line now carries the value the run wrote before the newest one (rtss_decide.h RtssSaved prev_limit and prev_den,
rtss1 with two more fields; the file came with this branch and was never released). rtss_limit.c apply writes the new
value into the line before the profile (record_before) and drops the previous one once the profile holds the new one,
or returns to it when RTSS refused it (record_after), and rtss_restore_plan takes a profile holding either value for
the game's. A new owed flag keeps had_profile, old_limit and old_denominator from the run's first write until a put
back lands: a later first write keeps them and the line's values, a failed first write leaves the line alone, and
rtss_limit_restore tries the put back again at the exit. put_back and restore_left check that a deleted profile is
gone before they remove the line, and restore_left's lines name the value the profile held. tests/rtss_limit_check.c
covers the new fields and the plan for either value; tests/rtss_worker_check.c checks the line before and after a
write (state_record), a run killed inside the save of its second limit and the next start removing the profile
(kill_restart_mid_write), a put back that did not land followed by a limit and the exit (put_back_owed), and a removal
that did not land (put_back_undeleted). Lines over 120 columns from the earlier fixes are wrapped.

Not reproduced before the change. Checks that passed: tests/rtss_limit_check.py (44 of 44), tests/exit_order_check.py,
tests/fatal_exit_check.py, pass_cc_check.py --wall on rtss_limit.c, tools/check_content.py --staged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 19:52:29 -05:00
parent 1744681f1a
commit da822abec3
5 changed files with 194 additions and 74 deletions

View File

@@ -353,7 +353,8 @@ static void unproven(void) {
{0, 1, "no API seen", "RivaTuner Statistics Server lists the game but has not seen its Vulkan presents yet"},
{0x7, 1, "another API's presents",
"RivaTuner Statistics Server lists the game but has not seen its Vulkan presents yet"},
{RTSS_API_VULKAN, 0, "no period measured", "RivaTuner Statistics Server has measured no frames of the game lately"},
{RTSS_API_VULKAN, 0, "no period measured",
"RivaTuner Statistics Server has measured no frames of the game lately"},
};
Run r;
RtssOut o;
@@ -598,26 +599,30 @@ static void pace_period(void) {
// The state file (rtss-profile.txt): its lines, the file's text as the worker rewrites it, and what a start does with a
// line that a run which did not close left behind.
static void saved(void) {
RtssSaved a = {0, 0, 1, 60, 1, "ocarina.exe"}, r;
RtssSaved a = {0, 0, 1, 60, 1, 60, 1, "ocarina.exe"}, r;
char line[400], out[2048];
size_t n = rtss_saved_format(line, sizeof line, &a);
CHECK(n == strlen(line) && !strcmp(line, "rtss1 0 0 1 60 1 ocarina.exe\n"), "the line: %s", line);
CHECK(n == strlen(line) && !strcmp(line, "rtss1 0 0 1 60 1 60 1 ocarina.exe\n"), "the line: %s", line);
CHECK(rtss_saved_parse(line, n - 1, &r) && !r.existed && !r.old_limit && r.old_den == 1 && r.limit == 60 &&
r.den == 1 && !strcmp(r.name, "ocarina.exe"),
r.den == 1 && r.prev_limit == 60 && r.prev_den == 1 && !strcmp(r.name, "ocarina.exe"),
"read back");
const char *spaced = "rtss1 1 59940 1000 144 1 My Game.exe\r";
const char *spaced = "rtss1 1 59940 1000 144 1 60 1 My Game.exe\r";
CHECK(rtss_saved_parse(spaced, strlen(spaced), &r) && r.existed && r.old_limit == 59940 && r.old_den == 1000 &&
!strcmp(r.name, "My Game.exe"),
r.limit == 144 && r.prev_limit == 60 && !strcmp(r.name, "My Game.exe"),
"a name with a space, a line ending in CR");
static const char *bad[] = {"rtss1 2 0 1 60 1 x.exe", "rtss1 0 0 0 60 1 x.exe", "rtss1 0 0 1 60 0 x.exe",
"rtss1 0 0 1 60 1", "rtss1 0 0 1 60 1 ", "rtss2 0 0 1 60 1 x.exe", "rtss1 0 0 1 x 1 x.exe",
"", "# rtss1 0 0 1 60 1 x.exe"};
static const char *bad[] = {"rtss1 2 0 1 60 1 60 1 x.exe", "rtss1 0 0 0 60 1 60 1 x.exe",
"rtss1 0 0 1 60 0 60 1 x.exe", "rtss1 0 0 1 60 1 60 0 x.exe",
"rtss1 0 0 1 60 1 60 1", "rtss1 0 0 1 60 1 60 1 ",
"rtss2 0 0 1 60 1 60 1 x.exe", "rtss1 0 0 1 x 1 60 1 x.exe",
"rtss1 0 0 1 60 1 x.exe", "",
"# rtss1 0 0 1 60 1 60 1 x.exe"};
for (unsigned i = 0; i < sizeof bad / sizeof *bad; i++)
CHECK(!rtss_saved_parse(bad[i], strlen(bad[i]), &r), "not a line: '%s'", bad[i]);
CHECK(!rtss_saved_format(line, 20, &a), "a line that does not fit");
// found past a comment, a malformed line and another profile's line, whatever the case of its letters
const char *text = RTSS_SAVED_HEADER "rtss1 9 garbage\nrtss1 1 30 1 144 1 My Game.exe\nrtss1 0 0 1 60 1 OCARINA.EXE";
const char *text = RTSS_SAVED_HEADER
"rtss1 9 garbage\nrtss1 1 30 1 144 1 144 1 My Game.exe\nrtss1 0 0 1 60 1 60 1 OCARINA.EXE";
CHECK(rtss_saved_find(text, "ocarina.exe", &r) && r.limit == 60 && !r.existed, "ours, the last line, no newline");
CHECK(rtss_saved_find(text, "my game.exe", &r) && r.limit == 144 && r.old_limit == 30, "the other one");
CHECK(!rtss_saved_find(text, "ocarina_dev.exe", &r) && !rtss_saved_find("", "ocarina.exe", &r), "none");
@@ -628,20 +633,23 @@ static void saved(void) {
a2.limit = 144;
n = rtss_saved_update(text, "ocarina.exe", &a2, out, sizeof out, &records);
CHECK(n == strlen(out) && records == 2 &&
!strcmp(out, RTSS_SAVED_HEADER "rtss1 1 30 1 144 1 My Game.exe\nrtss1 0 0 1 144 1 ocarina.exe\n"),
!strcmp(out, RTSS_SAVED_HEADER
"rtss1 1 30 1 144 1 144 1 My Game.exe\nrtss1 0 0 1 144 1 60 1 ocarina.exe\n"),
"replaced:\n%s", out);
char again[2048];
n = rtss_saved_update(out, "OCARINA.exe", NULL, again, sizeof again, &records);
CHECK(records == 1 && !strcmp(again, RTSS_SAVED_HEADER "rtss1 1 30 1 144 1 My Game.exe\n"), "removed:\n%s", again);
CHECK(records == 1 && !strcmp(again, RTSS_SAVED_HEADER "rtss1 1 30 1 144 1 144 1 My Game.exe\n"), "removed:\n%s",
again);
n = rtss_saved_update(again, "My Game.exe", NULL, out, sizeof out, &records);
CHECK(n && !records, "the last line removed: no record left, the file goes");
n = rtss_saved_update("", "ocarina.exe", &a, out, sizeof out, &records);
CHECK(records == 1 && !strcmp(out, RTSS_SAVED_HEADER "rtss1 0 0 1 60 1 ocarina.exe\n"), "a new file:\n%s", out);
CHECK(records == 1 && !strcmp(out, RTSS_SAVED_HEADER "rtss1 0 0 1 60 1 60 1 ocarina.exe\n"), "a new file:\n%s",
out);
CHECK(!rtss_saved_update(text, "ocarina.exe", &a, out, sizeof RTSS_SAVED_HEADER + 10, &records),
"a text that does not fit");
// What a start does. Run A made the profile and wrote 60/1, and was killed: B finds 60/1 and removes the profile.
RtssSaved made = {0, 0, 1, 60, 1, "ocarina.exe"}, had = {1, 30, 1, 60, 1, "ocarina.exe"};
RtssSaved made = {0, 0, 1, 60, 1, 60, 1, "ocarina.exe"}, had = {1, 30, 1, 60, 1, 60, 1, "ocarina.exe"};
CHECK(rtss_restore_plan(&made, 1, 60, 1) == RTSS_PLAN_DELETE, "the run's own profile: removed");
CHECK(rtss_restore_plan(&made, 1, 75, 1) == RTSS_PLAN_LEAVE, "the player set 75 since: theirs");
CHECK(rtss_restore_plan(&made, 0, 0, 1) == RTSS_PLAN_NONE, "the player removed it, or the run was killed before "
@@ -650,10 +658,22 @@ static void saved(void) {
CHECK(rtss_restore_plan(&had, 1, 30, 1) == RTSS_PLAN_LEAVE, "killed before the write landed: the player's 30/1");
CHECK(rtss_restore_plan(&had, 0, 0, 1) == RTSS_PLAN_LEAVE, "the player removed their profile since: left so");
CHECK(rtss_restore_plan(&had, 1, 60, 0) == RTSS_PLAN_WRITE, "an RTSS without the denominator reads none: 1");
RtssSaved frac = {1, 0, 1, 59940, 1000, "ocarina.exe"};
RtssSaved frac = {1, 0, 1, 59940, 1000, 59940, 1000, "ocarina.exe"};
CHECK(rtss_restore_plan(&frac, 1, 59940, 1) == RTSS_PLAN_LEAVE && rtss_restore_plan(&frac, 1, 59940, 1000) ==
RTSS_PLAN_WRITE,
"the denominator is compared too");
// A run that wrote 60/1 and then 144/1 ended between the line and the profile's change, or after it: the profile
// holds 60/1 or 144/1, both the game's; anything else is the player's.
RtssSaved moving = {1, 30, 1, 144, 1, 60, 1, "ocarina.exe"}, moving_made = {0, 0, 1, 144, 1, 60, 1, "ocarina.exe"};
CHECK(rtss_restore_plan(&moving, 1, 60, 1) == RTSS_PLAN_WRITE && rtss_restore_plan(&moving, 1, 144, 1) ==
RTSS_PLAN_WRITE,
"either value of the game's: 30/1 back");
CHECK(rtss_restore_plan(&moving_made, 1, 60, 1) == RTSS_PLAN_DELETE &&
rtss_restore_plan(&moving_made, 1, 144, 1) == RTSS_PLAN_DELETE,
"either value of the game's, in a profile the run made: removed");
CHECK(rtss_restore_plan(&moving, 1, 75, 1) == RTSS_PLAN_LEAVE && rtss_restore_plan(&moving, 1, 60, 1000) ==
RTSS_PLAN_LEAVE,
"another value: the player's");
}
// The profile's values for a limit (T15): whole rates as they are, a fraction over 1000 where RTSS knows the

View File

@@ -905,15 +905,21 @@ static void churn_run(void) {
// The state file: the run's first write records the profile as it was, each later one the value written, and the exit's
// restore removes the line and the file with it.
static char line_at_save[4096]; // the state file as SaveProfile found it (keep_line)
static void keep_line(void) { snprintf(line_at_save, sizeof line_at_save, "%s", state()); }
static void state_record(void) {
app_set(1, 0xA, 1000, 2000, 60);
ask(60000);
poll();
show("playing at 60");
CHECK(own.exists && own.limit == 60 && state_has("\nrtss1 0 0 1 60 1 ocarina.exe\n"), "recorded:\n%s", state());
CHECK(own.exists && own.limit == 60 && state_has("\nrtss1 0 0 1 60 1 60 1 ocarina.exe\n"), "recorded:\n%s",
state());
CHECK(!strncmp(state(), "# RivaTuner Statistics Server's profiles", 40), "the header first");
during_save = keep_line;
settle(144000);
CHECK(own.limit == 144 && state_has("\nrtss1 0 0 1 144 1 ocarina.exe\n"), "the line follows:\n%s", state());
CHECK(strstr(line_at_save, "\nrtss1 0 0 1 144 1 60 1 ocarina.exe\n"),
"before the profile changes, the line holds 144 and the 60 before it:\n%s", line_at_save);
CHECK(own.limit == 144 && state_has("\nrtss1 0 0 1 144 1 144 1 ocarina.exe\n"), "then 144 alone:\n%s", state());
rtss_limit_restore();
show("after the restore");
CHECK(!own.exists && !*state(), "the profile and the state file gone:\n%s", state());
@@ -923,11 +929,11 @@ static void state_player(void) {
app_set(1, 0xA, 1000, 2000, 60);
ask(60000);
poll();
CHECK(own.limit == 60 && state_has("\nrtss1 1 30 1 60 1 ocarina.exe\n"), "recorded:\n%s", state());
CHECK(own.limit == 60 && state_has("\nrtss1 1 30 1 60 1 60 1 ocarina.exe\n"), "recorded:\n%s", state());
settle(RTSS_OUT_LOWLAT); // kept out: put back during the run, and the line goes with it
CHECK(own.limit == 30 && !*state(), "put back, no line:\n%s", state());
settle(60000);
CHECK(own.limit == 60 && state_has("\nrtss1 1 30 1 60 1 ocarina.exe\n"), "recorded again:\n%s", state());
CHECK(own.limit == 60 && state_has("\nrtss1 1 30 1 60 1 60 1 ocarina.exe\n"), "recorded again:\n%s", state());
rtss_limit_restore();
CHECK(own.exists && own.limit == 30 && !*state(), "the player's 30 back, no line");
}
@@ -950,6 +956,48 @@ static void state_unwritable(void) {
CHECK(!own.exists && !rtss_limit_active(), "the crash put the profile back");
}
// A put back during the run that does not land (the profile went read-only): the line and the values found at the
// run's first write stay. A later limit is the run's first write again and keeps the player's 30 to put back, not the
// game's 60 the profile still holds, and the exit puts 30 back.
static void put_back_owed(void) {
own = (Prof){1, 30, 1, 0, 0, 0, 0, 0, 0, 0, 1};
app_set(1, 0xA, 1000, 2000, 60);
ask(60000);
poll();
readonly = 1;
settle(RTSS_OUT_LOWLAT);
show("kept out, the put back did not land");
CHECK(own.limit == 60 && logged("did not take its old limit back") &&
state_has("\nrtss1 1 30 1 60 1 60 1 ocarina.exe\n"),
"the line stays:\n%s", state());
readonly = 0;
settle(144000);
show("a limit again");
CHECK(own.limit == 144 && state_has("\nrtss1 1 30 1 144 1 144 1 ocarina.exe\n"), "the player's 30 still owed:\n%s",
state());
rtss_limit_restore();
show("closed");
CHECK(own.exists && own.limit == 30 && !*state(), "the exit puts the player's 30 back:\n%s", state());
}
// The same where the run made the profile: a removal that does not land is no removal; the line stays, and the exit
// removes the profile.
static void put_back_undeleted(void) {
app_set(1, 0xA, 1000, 2000, 60);
ask(60000);
poll();
readonly = 1;
settle(RTSS_OUT_LOWLAT);
show("kept out, the removal did not land");
CHECK(own.exists && logged("profile ocarina.exe did not go away") && !removed &&
state_has("\nrtss1 0 0 1 60 1 60 1 ocarina.exe\n"),
"the line stays:\n%s", state());
readonly = 0;
rtss_limit_restore();
show("closed");
CHECK(!own.exists && removed == 1 && !*state(), "the exit removes it:\n%s", state());
}
// A crash with the line in the state file: no call into RTSS's library, the profile and the line stay for the next
// start, and nothing after it reaches the profile.
static void crash_recorded(void) {
@@ -960,7 +1008,8 @@ static void crash_recorded(void) {
rtss_limit_crash();
show("a crash");
CHECK(saves == s0 && deletes == d0 && updates == u0, "no profile call");
CHECK(own.exists && own.limit == 60 && state_has("\nrtss1 0 0 1 60 1 ocarina.exe\n"), "left for the next start");
CHECK(own.exists && own.limit == 60 && state_has("\nrtss1 0 0 1 60 1 60 1 ocarina.exe\n"),
"left for the next start");
CHECK(!rtss_limit_active() && !rtss_limit_state(), "the game's limiter");
ask(144000);
poll();
@@ -972,23 +1021,36 @@ static void crash_recorded(void) {
// variant 2: the player set 75 in RTSS between the two runs; variant 3: the next start has frame generation on, which
// keeps RTSS out, and still puts the profile back (the hooks library loaded for that alone) with no write of its own;
// variant 4: the next start has NX2NAVI_RTSS=0, which keeps the thread read-only: the log says what the file holds,
// and the profile and the line stay for a start with RTSS let in.
// and the profile and the line stay for a start with RTSS let in; variant 5: the run is killed inside the save of its
// second limit, after the line took 144 and before the profile did: the next start finds the 60 before it and removes
// the profile.
static int variant;
static void kill_in_save(void) {
CHECK(state_has("\nrtss1 0 0 1 144 1 60 1 ocarina.exe\n") && own.limit == 60, "the line before the profile:\n%s",
state());
prof_save();
ExitProcess(fails != 0);
}
static void kill_first(void) {
prof_load();
app_set(1, 0xA, 1000, 2000, 60);
ask(60000);
poll();
show("the run before the kill");
CHECK(own.exists && own.limit == 60 && state_has(variant == 1 ? "\nrtss1 1 30 1 60 1 ocarina.exe\n"
: "\nrtss1 0 0 1 60 1 ocarina.exe\n"),
CHECK(own.exists && own.limit == 60 && state_has(variant == 1 ? "\nrtss1 1 30 1 60 1 60 1 ocarina.exe\n"
: "\nrtss1 0 0 1 60 1 60 1 ocarina.exe\n"),
"recorded:\n%s", state());
if (variant == 5) { // killed inside the save of 144: the line has 144, the profile still 60
during_save = kill_in_save;
settle(144000);
CHECK(0, "the run went on past the kill");
}
prof_save(); // killed: no restore
}
static void kill_next(void) {
prof_load();
app_set(1, 0xA, 1000, 2000, 144);
if (variant >= 3) {
if (variant == 3 || variant == 4) {
ask(variant == 3 ? RTSS_OUT_FRAMEGEN : RTSS_OUT_SETTING);
poll();
poll();
@@ -1005,7 +1067,8 @@ static void kill_next(void) {
"profile ocarina.exe from a run that did not close; NX2NAVI_RTSS=0 leaves the "
"profile as it is"),
"read only: logged");
CHECK(own.exists && own.limit == 60 && state_has("\nrtss1 0 0 1 60 1 ocarina.exe\n"), "left:\n%s", state());
CHECK(own.exists && own.limit == 60 && state_has("\nrtss1 0 0 1 60 1 60 1 ocarina.exe\n"), "left:\n%s",
state());
}
rtss_limit_restore();
show("closed");
@@ -1015,22 +1078,22 @@ static void kill_next(void) {
ask(144000);
poll();
show("the next start");
if (variant == 0) {
if (variant == 0 || variant == 5) {
CHECK(logged("profile ocarina.exe had the game's limit of 60/1 from a run that did not close") &&
logged("; removed, as before that run"),
"the killed run's profile removed");
CHECK(logged("created; it goes away when the game closes (creation 1 this run)"), "made again by this run");
CHECK(state_has("\nrtss1 0 0 1 144 1 ocarina.exe\n"), "this run's line:\n%s", state());
CHECK(state_has("\nrtss1 0 0 1 144 1 144 1 ocarina.exe\n"), "this run's line:\n%s", state());
} else if (variant == 1) {
CHECK(logged("profile ocarina.exe had the game's limit of 60/1 from a run that did not close") &&
logged("; its limit of 30/1 is back"),
"the player's 30 back");
CHECK(logged("held a limit of 30/1; it gets that back"), "this run takes 30 for the player's");
CHECK(state_has("\nrtss1 1 30 1 144 1 ocarina.exe\n"), "this run's line:\n%s", state());
CHECK(state_has("\nrtss1 1 30 1 144 1 144 1 ocarina.exe\n"), "this run's line:\n%s", state());
} else {
CHECK(logged("profile ocarina.exe changed since a run that did not close") && logged("; left as it is"),
"the player's 75 left");
CHECK(state_has("\nrtss1 1 75 1 144 1 ocarina.exe\n"), "this run's line:\n%s", state());
CHECK(state_has("\nrtss1 1 75 1 144 1 144 1 ocarina.exe\n"), "this run's line:\n%s", state());
}
CHECK(own.exists && own.limit == 144, "this run's limit");
rtss_limit_restore();
@@ -1060,18 +1123,19 @@ static void kill_restart_as(int v) {
CHECK(run_phase("kill_next") == 0, "the next start");
prof_load();
show("after the next start closed");
if (v == 0) CHECK(!own.exists, "the profile is gone, as before the killed run");
if (v == 0 || v == 5) CHECK(!own.exists, "the profile is gone, as before the killed run");
if (v == 1) CHECK(own.exists && own.limit == 30, "the player's 30");
if (v == 2) CHECK(own.exists && own.limit == 75, "the player's 75");
if (v == 3) CHECK(!own.exists, "the profile is gone, as before the killed run");
if (v == 4) CHECK(own.exists && own.limit == 60, "the killed run's 60 stays for a start with RTSS let in");
CHECK(v == 4 ? state_has("\nrtss1 0 0 1 60 1 ocarina.exe\n") : !*state(), "the state file:\n%s", state());
CHECK(v == 4 ? state_has("\nrtss1 0 0 1 60 1 60 1 ocarina.exe\n") : !*state(), "the state file:\n%s", state());
}
static void kill_restart(void) { kill_restart_as(0); }
static void kill_restart_player(void) { kill_restart_as(1); }
static void kill_restart_edited(void) { kill_restart_as(2); }
static void kill_restart_framegen(void) { kill_restart_as(3); }
static void kill_restart_setting(void) { kill_restart_as(4); }
static void kill_restart_mid_write(void) { kill_restart_as(5); }
// A movie at 60 under a limit of 144: the want stays 144 (frame_rate.c frame_step_cap_ns) and RTSS's profile keeps it
// with no write, while the game's pacer holds the movie at its rate; RTSS holds the game again at its end.
@@ -1151,6 +1215,9 @@ static const struct { const char *name; void (*run)(void); } scenarios[] = {
{"kill_restart_edited", kill_restart_edited},
{"kill_restart_framegen", kill_restart_framegen},
{"kill_restart_setting", kill_restart_setting},
{"kill_restart_mid_write", kill_restart_mid_write},
{"put_back_owed", put_back_owed},
{"put_back_undeleted", put_back_undeleted},
};
int main(int argc, char **argv) {