Created
March 5, 2018 00:07
-
-
Save Aaron-Macneill/7b423badc775505c66a71f1d14978e99 to your computer and use it in GitHub Desktop.
Flushing bcache via an automated python script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import time | |
import os | |
dirty_data_path = "/sys/block/bcache0/bcache/dirty_data" | |
writeback_path = "/sys/block/bcache0/bcache/writeback_percent" | |
def get_writeback(): | |
with open(writeback_path, 'r') as f: | |
return int(f.read()) | |
def get_dirty_data(): | |
with open(dirty_data_path, 'r') as f: | |
data = f.read() | |
if data[-2:-1] == 'M': | |
data = float(data[:-2]) * 1024 | |
elif data[-2:-1] == 'G': | |
data = float(data[:-2]) * 1048576 | |
else: | |
data = data[:-2] | |
try: | |
return int(round(int(data))) | |
except: | |
return float(data) | |
def set_writeback_percent(percent): | |
with open(writeback_path, 'w') as f: | |
f.write(str(percent)) | |
def main(): | |
writeback_percent = get_writeback() | |
dirty = get_dirty_data() | |
print("Flushing ", dirty, "bytes") | |
print("Setting writeback from ", writeback_percent, " to 0") | |
set_writeback_percent(0) | |
while dirty > 1024: | |
dirty = get_dirty_data() | |
print(dirty) | |
time.sleep(10) | |
print("Completed. Setting writeback back") | |
set_writeback_percent(writeback_percent) | |
print("Done!") | |
if __name__ == '__main__': | |
if os.geteuid() == 0: | |
main() | |
else: | |
print("Run as root") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment