Skip to content

Commit 29051f2

Browse files
authored
(Feature): Check if swap is active; Drop VM caches and sync disk (#1042)
* (Feature): Check if swap is active; Drop VM caches and sync disk * (fix): sysctl must be with sudo * (Tests): Swap off for tests
1 parent 4b0f028 commit 29051f2

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

.github/actions/gmt-pytest/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ runs:
9898
# - name: Setup upterm session
9999
# uses: lhotari/action-upterm@v1
100100

101+
- name: Disable swap
102+
shell: bash
103+
run: |
104+
sudo swapoff -a
105+
101106
- name: Run Tests
102107
shell: bash
103108
working-directory: ${{ inputs.gmt-directory }}/tests

install_linux.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ if [[ $build_sgx == true ]] ; then
3939
rm lib/sgx-software-enable/sgx_enable.o
4040
fi
4141

42+
print_message "Enabling cache cleanup without sudo via sudoers entry"
43+
echo "ALL ALL=(ALL) NOPASSWD:/usr/sbin/sysctl -w vm.drop_caches=3" | sudo tee /etc/sudoers.d/green-coding-drop-caches
44+
sudo chmod 500 /etc/sudoers.d/green-coding-drop-caches
45+
4246
print_message "Setting the cluster cleanup.sh file to be owned by root"
4347
sudo cp -f $PWD/tools/cluster/cleanup_original.sh $PWD/tools/cluster/cleanup.sh
4448
sudo chown root:root $PWD/tools/cluster/cleanup.sh

lib/system_checks.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import subprocess
1313
import psutil
1414
import locale
15+
import platform
1516

1617
from psycopg import OperationalError as psycopg_OperationalError
1718

@@ -73,6 +74,22 @@ def check_docker_daemon():
7374
def check_utf_encoding():
7475
return locale.getpreferredencoding().lower() == sys.getdefaultencoding().lower() == 'utf-8'
7576

77+
# This text we compare with indicates that no swap is used
78+
#pylint: disable=no-else-return
79+
def check_swap_disabled():
80+
if platform.system() == 'Darwin':
81+
result = subprocess.check_output(['sysctl', 'vm.swapusage'], encoding='utf-8')
82+
return result == 'vm.swapusage: total = 0.00M used = 0.00M free = 0.00M (encrypted)'
83+
else:
84+
result = subprocess.check_output(['free'], encoding='utf-8')
85+
for line in result.splitlines():
86+
# we want this output: Swap: 0 0 0
87+
# and condense it to Swap:000
88+
if line.startswith('Swap') and line.replace(' ', '') != 'Swap:000':
89+
return False
90+
return True
91+
92+
7693
######## END CHECK FUNCTIONS ########
7794

7895
start_checks = [
@@ -85,6 +102,8 @@ def check_utf_encoding():
85102
(check_docker_daemon, Status.ERROR, 'docker daemon', 'The docker daemon could not be reached. Are you running in rootless mode or have added yourself to the docker group? See installation: [See https://docs.green-coding.io/docs/installation/]'),
86103
(check_containers_running, Status.WARN, 'running containers', 'You have other containers running on the system. This is usually what you want in local development, but for undisturbed measurements consider going for a measurement cluster [See https://docs.green-coding.io/docs/installation/installation-cluster/].'),
87104
(check_utf_encoding, Status.ERROR, 'utf file encoding', 'Your system encoding is not set to utf-8. This is needed as we need to parse console output.'),
105+
(check_swap_disabled, Status.ERROR, 'swap disabled', 'Your system uses a swap filesystem. This can lead to very instable measurements. Please disable swap.'),
106+
88107
]
89108

90109
def check_start():

runner.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ def save_notes_runner(self):
180180
print(TerminalColors.HEADER, '\nSaving notes: ', TerminalColors.ENDC, self.__notes_helper.get_notes())
181181
self.__notes_helper.save_to_db(self._run_id)
182182

183+
def clear_caches(self):
184+
subprocess.check_output(['sync'])
185+
186+
if platform.system() == 'Darwin':
187+
return
188+
# 3 instructs kernel to drops page caches AND inode caches
189+
subprocess.check_output(['sudo', '/usr/sbin/sysctl', '-w', 'vm.drop_caches=3'])
190+
183191
def check_system(self, mode='start'):
184192
if self._skip_system_checks:
185193
print("System check skipped")
@@ -1579,6 +1587,7 @@ def run(self):
15791587
try:
15801588
config = GlobalConfig().config
15811589
self.start_measurement() # we start as early as possible to include initialization overhead
1590+
self.clear_caches()
15821591
self.check_system('start')
15831592
self.initialize_folder(self._tmp_folder)
15841593
self.checkout_repository()

tests/test_functions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def run_until(self, step):
161161
try:
162162
config = GlobalConfig().config
163163
self.__runner.start_measurement()
164+
self.__runner.clear_caches()
164165
self.__runner.check_system('start')
165166
self.__runner.initialize_folder(self.__runner._tmp_folder)
166167
self.__runner.checkout_repository()

0 commit comments

Comments
 (0)