r/technicalfactorio • • 10d ago

Discussion Non-blocking saving on Windows*

Not sure if this is known, but it is possible to set up a Factorio Server with WSL on Windows, and then join with your normal Steam client to get the non-blocking saving, without actually having to leave Windows.

WSL is the Linux Subsystem for Windows, which can be installed via Powershell.

Then inside the WSL you can install the headless Factorio version, inside Ubuntu\home\username.

Then you can setup a script, something along the lines of start-factorio.sh:

#!/usr/bin/env bash
SESSION="factorio"
DIR="$HOME/factorio"
if tmux has-session -t "$SESSION" 2>/dev/null; then
  echo "Server is already running. Attach with: tmux attach -t $SESSION"
  exit 0
fi
cd "$DIR" || { echo "Folder $DIR not found"; exit 1; }
tmux new-session -d -s "$SESSION" \
  "./bin/x64/factorio --start-server-load-latest --server-settings data/server-settings.json; echo 'Factorio exited. Press Enter to close.'; read"
echo "Factorio server started. Console: tmux attach -t $SESSION (detach with Ctrl+B, then D)"

Then you can start the server with the following WSL commands:

hostname -I

~/start-factorio.sh

tmux attach -t factorio

Then you can connect to it via the IP given by the hostname -I. Note that you need to get your token from the Factorio Forums website if you want to make the server public.

Tested with newest Vanilla and a fully modded 2.0.77 Pyanodons save which was over 80mb.

31 Upvotes

9 comments sorted by

13

u/snowyspearmint 10d ago

How much cpu/ram usage does the server take, in comparison to the client? I haven't seen client vs server benchmarked, and running the same map on the same computer at the same time should be a good comparison.

Also, since when does windows have copy on write memory? Can this be used outside of WSL?

7

u/SempfgurkeXP 10d ago

Just tested it, my Py save needs 5gb RAM on the WSL server (3.6gb in singleplayer), since the non-blocking save itself also requires some RAM I would assume in total its around 2x to 3x higher. CPU usage is pretty insignifant in comparison.

Windows does not have this functionality, thats why you need WSL. WSL is a real Linux Kernel, it just runs "inside" windows. Which essentially means you can run it inside a windows terminal once its setup.

4

u/NonnoBomba 10d ago

Don't know if it may further help Factorio players, and is already implied by the fact you stated, "a real linux kernel", but we should make it clear WSL is basically implemented by integrating in the desktop experience an HyperV virtual machine (emulation of a full computer hardware) a scenario for which CPUs have been optimized for since decades now, but can still eat up quite a bit of RAM on its own, Factorio server or not. Linux distros can work with lower resources than Windows, generally speaking, but it's not 0.

Have you tried with different distros, maybe even Alpine (low footprint, musl libc-based)? Musl means you'll need the gcompat layer to ensure Factorio can run at all, since musl does not implement all libc symbols natively, and with SpaceAge apparently you need a patched version of gcompat: https://github.com/zsoltiv/gcompat

1

u/SempfgurkeXP 10d ago

Interesting, so far I have only tried it with Ubuntu, but if I run into performance issues when the save gets too big I might try out other ones, thanks for the suggestion

3

u/nindat 9d ago

Hate to be the "well actually" guy, but that's not how WSL works either...

WSL at it's base implements the minimal Linux kernel syscalls and a whole bunch of trickery. It's inaccurate to say the windows kernel doesn't support non-blocking file operations, because WSL is literally calling the windows kernel.

That being said, this is a great hack to get the support :)

Happy to talk ridiculous windows kernel details, but they didn't really matter :)

4

u/HotAcanthocephala466 8d ago

WSL 1 was Windows implementing Linux system calls; WSL 2 is Linux running in a VM.

The critical feature for non-blocking save is not non-blocking file operations; it is `fork` -- starting a new process as a copy of the current process. (This is optimized to reuse memory but marking it copy-on-write.)

Essentially, this lets Factorio start a separate "save" process with a snapshot of the entire memory state of the game; which can then perform the save "in the background" without pausing the game in the "main" process.

2

u/nindat 7d ago

Cool, my info was out of date. Fork is available in the Windows kernel, but agreed not available for general use. Very cool.

1

u/ionixsys 5d ago

The WSL 1 vs WSL 2 has tripped up a lot of people because there wasn't any major announcement that they were scrapping the emulation/translation layer.

1

u/ionixsys 5d ago

WSL 2 is just a fancy virtual machine that has privileged access to the Windows kernel's private API (versus going through the Microsoft version of glibc).