Files
RetroArch/samples/tasks/save_state/Makefile
LibretroAdmin f9c77d8e80 Savestates: keep the replaced file for undo as it is, not inflated
A save over a slot first reads the file there into the undo buffer,
so Undo Save State can put it back. That read went through rzip: on
every save over a compressed slot the old state was inflated in full,
and kept inflated in memory for the rest of the session. Undo then
wrote it back through rzip, deflated afresh, compressed or not as the
setting had since become rather than as the file was.

The backup is now the file's own bytes, read plainly, and undo writes
them back as they are. Nothing is inflated on a save, nothing deflated
on undo, the buffer held for undo is the file's size rather than the
state's, and the slot comes back byte for byte.

An 8 MiB state that compresses to 4 MiB: 2.1 -> 0.6 ms for the backup
on every save over the slot, 12.4 -> 3.3 ms for the undo, and 4 MiB
less held for it. At 32 MiB: 9.9 -> 3.8 ms and 41.6 -> 25.3 ms.

save_state_io_test gains an RZIP=1 build with HAVE_COMPRESSION, as the
frontend ships it, where every lane also passes, and a lane that saves
over a slot with the compression setting turned in between, both ways,
then undoes: the slot must come back byte for byte. It fails on the
previous tree. Linux-samples-tasks runs that build under ASan.
2026-11-03 00:25:14 +01:00

126 lines
5.1 KiB
Makefile

include ../../sample_objdir.mk
TARGET := save_state_io_test
# Path back to the repo root from this sample dir. The unit under
# test is compiled from the tree rather than copied - the shipping
# tasks/task_save.c - so the oracle exercises the translation unit
# that ships. RARCH_INTERNAL stays undefined so it builds without
# the frontend behind it.
#
# features/features_cpu.c is deliberately NOT linked: the test
# provides cpu_features_get_time_usec() itself, as a deterministic
# clock that advances a fixed step per observation. That is what
# turns the tick budget into something a test can assert exactly -
# step 0 is a budget that never expires, step >= the budget is one
# that expires on first observation.
#
# HAVE_THREADS is defined so the threaded queue impl is built and the
# "conc" lane can run the handler off the main thread, which is where
# it runs in production. The default lane still initialises the queue
# unthreaded, so its tick-count assertions are unaffected: the pacing
# protocol is identical either way and the locks only serialise it.
#
# HAVE_COMPRESSION stays undefined, so the plain intfstream path is
# the one under test; the rzip path wraps the same handler loop. RZIP=1
# below is the build with it.
REPO_ROOT := ../../..
LIBRETRO_COMM_DIR := $(REPO_ROOT)/libretro-common
SOURCES := save_state_io_test.c \
$(REPO_ROOT)/tasks/task_save.c \
$(LIBRETRO_COMM_DIR)/compat/compat_strl.c \
$(LIBRETRO_COMM_DIR)/compat/compat_strldup.c \
$(LIBRETRO_COMM_DIR)/compat/fopen_utf8.c \
$(LIBRETRO_COMM_DIR)/encodings/encoding_crc32.c \
$(LIBRETRO_COMM_DIR)/encodings/encoding_utf.c \
$(LIBRETRO_COMM_DIR)/file/file_path.c \
$(LIBRETRO_COMM_DIR)/file/file_path_io.c \
$(LIBRETRO_COMM_DIR)/lists/string_list.c \
$(LIBRETRO_COMM_DIR)/queues/task_queue.c \
$(LIBRETRO_COMM_DIR)/rthreads/rthreads.c \
$(LIBRETRO_COMM_DIR)/rthreads/retro_eventcount.c \
$(LIBRETRO_COMM_DIR)/streams/file_stream.c \
$(LIBRETRO_COMM_DIR)/string/rstrtod.c \
$(LIBRETRO_COMM_DIR)/streams/interface_stream.c \
$(LIBRETRO_COMM_DIR)/streams/memory_stream.c \
$(LIBRETRO_COMM_DIR)/string/stdstring.c \
$(LIBRETRO_COMM_DIR)/time/rtime.c \
$(LIBRETRO_COMM_DIR)/vfs/vfs_implementation.c
# RZIP=1 builds the same oracle with HAVE_COMPRESSION, as the frontend
# ships: states go through rzip, and the lanes that need it (a backup
# of a compressed slot) run. Its objects go under their own directory.
ifeq ($(RZIP),1)
TARGET := save_state_io_test_rzip
SAMPLE_OBJDIR := obj_rzip
SOURCES += $(LIBRETRO_COMM_DIR)/streams/rzip_stream.c \
$(LIBRETRO_COMM_DIR)/streams/trans_stream.c \
$(LIBRETRO_COMM_DIR)/streams/trans_stream_pipe.c \
$(LIBRETRO_COMM_DIR)/streams/trans_stream_deflate.c \
$(LIBRETRO_COMM_DIR)/encodings/encoding_deflate.c
CFLAGS += -DHAVE_COMPRESSION
endif
CFLAGS += -Wall -std=gnu99 -g1 -O1 \
-DHAVE_THREADS \
-I$(REPO_ROOT) \
-I$(LIBRETRO_COMM_DIR)/include
# --wrap=malloc so the sample's counting allocator sees the allocations
# by tasks/task_save.c, not only its own. That count is the only way
# to observe the undo snapshot reusing its buffer: the addresses are
# identical either way, because a state-sized block is mmap'd and the
# kernel hands back the same region on the next request.
# --wrap=calloc so a lane can fail the allocations a finishing task
# would make, to show the notified caller is told regardless.
# --wrap=task_queue_park so a lane can count the load handler's parks.
LDFLAGS += -Wl,--wrap=malloc -Wl,--wrap=calloc -Wl,--wrap=task_queue_park \
-lpthread -lm
# Extra flags for the caller; CFLAGS= on the command line would replace
# everything set above instead of adding to it.
CFLAGS += $(EXTRA_CFLAGS)
OBJS := $(call sample_objs,$(SOURCES:.c=.o))
ifneq ($(SANITIZER),)
CFLAGS := -fsanitize=$(SANITIZER) -fno-omit-frame-pointer $(CFLAGS)
LDFLAGS := -fsanitize=$(SANITIZER) $(LDFLAGS)
endif
all: $(TARGET)
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
$(SAMPLE_OBJDIR)/%.o: $$(call sample_src,$$*).c
@mkdir -p $(@D)
$(CC) -c -o $@ $< $(CFLAGS)
$(TARGET): $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
# ASan and TSan are mutually exclusive, hence separate passes. Leak
# detection is strict: the handler owns the serialized buffer and the
# intfstream, and the terminals this oracle drives (completed, failed
# serialize, failed open, short file) are exactly the ones where an
# early return could drop either.
sweep:
$(MAKE) clean && $(MAKE) && ./$(TARGET) && ./$(TARGET) conc
$(MAKE) clean && $(MAKE) SANITIZER=address,undefined && \
ASAN_OPTIONS=detect_leaks=1:detect_stack_use_after_return=1 \
UBSAN_OPTIONS=print_stacktrace=1 ./$(TARGET) && \
ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1 \
./$(TARGET) conc
$(MAKE) clean && $(MAKE) SANITIZER=thread && \
TSAN_OPTIONS=halt_on_error=0 ./$(TARGET) && \
TSAN_OPTIONS=halt_on_error=0 ./$(TARGET) conc
@echo "sweep clean"
clean:
rm -rf $(SAMPLE_OBJDIR)
rm -f $(TARGET) $(OBJS)
.PHONY: all sweep clean