Monitors RAM usage and enables swap devices
Project description
SwapDog
SwapDog is a swap watchdog that monitors RAM usage and enables or disables swap devices automatically based on user-defined thresholds. It is designed to prevent excessive swapping, which can lead to performance degradation and SSD wear, while still allowing the system to function without crashing when RAM is exhausted.
Rationale
SwapDog helps you avoid unnecessary swap usage when RAM is available, but will enable swap if memory pressure gets high—and disable it again when pressure drops. This approach is more radical than simply tuning swappiness:
- Swap memory is always slower than RAM; HDDs are especially slow.
- SSDs wear out with read and write cycles, while RAM does not.
- Disabling swap when not needed reduces SSD wear and improves performance.
These reasons are enough for me to want to limit the usage of swap in a more dynamic way than swappiness does.
Installation
Any installation method assumes that you have access to the swapdog.py, swapdog.json, requirements.txt and swapdog.service files in the current directory.
git clone https://github.com/FLAK-ZOSO/SwapDog
cd SwapDog
You may want to proceed with the configuration before installing, but you can also do it later.
Automated
You can use the provided install.sh script to automate the installation process. Just run the following command:
chmod +x install.sh
./install.sh
Manual
Take this as a documentation of the steps that the install.sh script performs, so you can do it manually if you prefer:
- Install dependencies with
sudo pip3 install -r requirements.txt - Copy the swapdog.py file to
/usr/local/sbin/ - Make it executable with
sudo chmod 744 /usr/local/sbin/swapdog.py - Copy the swapdog.json file to
/etc/ - Copy the swapdog.service file to
/etc/systemd/system/ - Enable the service with
sudo systemctl enable swapdog - Start the service with
sudo systemctl start swapdog - Check the status of the service with
sudo systemctl status swapdog
sudo pip3 install -r requirements.txt
sudo cp swapdog.py /usr/local/sbin/
sudo chmod 744 /usr/local/sbin/swapdog.py
sudo cp swapdog.json /etc/
sudo cp swapdog.service /etc/systemd/system/
sudo systemctl enable swapdog
sudo systemctl start swapdog
sudo systemctl status swapdog
Configuration
In order to configure the behavior of SwapDog, you need to edit the swapdog.json file located in /etc/. The file follows a simple JSON structure that allows you to set thresholds and the swap devices to be used.
Fields
thresholds: An array of objects, each representing a threshold for enabling a swap device.percentage: The percentage of RAM usage that triggers the swap device to be enabled.swap: The path to the swap device (e.g.,/dev/sda1or/swapfile).
period: The time in seconds between checks of the RAM usage. Default is1.0seconds if not specified.disable_swaps: A boolean flag that indicates whether to disable swaps when RAM usage is below the threshold. Default isfalse.hysteresis: A float value that defines the hysteresis for disabling swaps. This is the percentage below the threshold at which swaps will be disabled. Default is10.0if not specified.
How to configure
- Enable all swaps in order to make them easily detectable.
sudo swapon --all
- List the currently enabled swap devices.
sudo swapon --show
sudo cat /proc/swaps
What follows is an example output. You are looking for the NAME column to identify the swap devices.
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 7.4G 0B -2
/swapfile file 2.0G 0B -3
- Edit the
swapdog.jsonfile to set the desired thresholds and swap devices. For example, if you want to enable a swap device when RAM usage exceeds 95%, you can set it like this:
{
"thresholds": [
{
"percentage": 95.0,
"swap": "/dev/dm-1"
},
{
"percentage": 90.0,
"swap": "/swapfile"
}
],
"period": 1.0,
"disable_swaps": true,
"hysteresis": 15.0
}
Uninstallation
To uninstall SwapDog, you can use the provided uninstall.sh script or perform the steps manually.
Automated
You can run the following command to uninstall SwapDog using the script:
chmod +x uninstall.sh
./uninstall.sh
Manual
Take this as a documentation of the steps that the uninstall.sh script performs, so you can do it manually if you prefer:
- Stop the service with
sudo systemctl stop swapdog - Disable the service with
sudo systemctl disable swapdog - Remove the service file with
sudo rm /etc/systemd/system/swapdog.service - Remove the script with
sudo rm /usr/local/sbin/swapdog.py - Remove the configuration file with
sudo rm /etc/swapdog.json
sudo systemctl stop swapdog
sudo systemctl disable swapdog
sudo rm /etc/systemd/system/swapdog.service /usr/local/sbin/swapdog.py /etc/swapdog.json
sudo systemctl daemon-reload
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swapdog-0.2.1.tar.gz.
File metadata
- Download URL: swapdog-0.2.1.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5251d98382b64d57684d95f351b20a9977b67b703a2be02489152bc4a4024c2
|
|
| MD5 |
417b60888b91cfdd403caab234cf3834
|
|
| BLAKE2b-256 |
274b182e60d1d96b6f8c55242266970593b3a01fbc333729697bfc500ba73ca6
|
Provenance
The following attestation bundles were made for swapdog-0.2.1.tar.gz:
Publisher:
python-publish.yml on FLAK-ZOSO/SwapDog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swapdog-0.2.1.tar.gz -
Subject digest:
f5251d98382b64d57684d95f351b20a9977b67b703a2be02489152bc4a4024c2 - Sigstore transparency entry: 413040492
- Sigstore integration time:
-
Permalink:
FLAK-ZOSO/SwapDog@c037ac4cab8280d6b761ada213be12d3d393c215 -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/FLAK-ZOSO
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c037ac4cab8280d6b761ada213be12d3d393c215 -
Trigger Event:
release
-
Statement type:
File details
Details for the file swapdog-0.2.1-py3-none-any.whl.
File metadata
- Download URL: swapdog-0.2.1-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f98eaec1df636a68dfa36b863034c6ee70f3a53b573fd0a07da9d9f3cb553500
|
|
| MD5 |
23f1f476bbbff64b59950b65a0d3e8c8
|
|
| BLAKE2b-256 |
0245467ee8f2566b5940909b513f701be120eb34c288d398d1ef952f157df5cb
|
Provenance
The following attestation bundles were made for swapdog-0.2.1-py3-none-any.whl:
Publisher:
python-publish.yml on FLAK-ZOSO/SwapDog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swapdog-0.2.1-py3-none-any.whl -
Subject digest:
f98eaec1df636a68dfa36b863034c6ee70f3a53b573fd0a07da9d9f3cb553500 - Sigstore transparency entry: 413040506
- Sigstore integration time:
-
Permalink:
FLAK-ZOSO/SwapDog@c037ac4cab8280d6b761ada213be12d3d393c215 -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/FLAK-ZOSO
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c037ac4cab8280d6b761ada213be12d3d393c215 -
Trigger Event:
release
-
Statement type: