fix: Корректировка вычисления Use%, как это делает df
This commit is contained in:
parent
0bcb0af57e
commit
17d3e3bf7b
|
|
@ -160,3 +160,5 @@ cython_debug/
|
|||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
/mnt/
|
||||
/img
|
||||
32
src/main.py
32
src/main.py
|
|
@ -5,30 +5,38 @@ import time
|
|||
|
||||
BS = 1024**2
|
||||
|
||||
def set_utilization(du, utilization_percent=0.6, baloon_filename='baloon'):
|
||||
fs = os.path.realpath(du)
|
||||
def set_utilization(fs, utilization_percent=0.6, baloon_filename='baloon', hysteresis_percent=0.01):
|
||||
fs = os.path.realpath(fs)
|
||||
du = disk_usage(fs)
|
||||
print(f'total: {int(du.total/ 2**30)}GB, used: {int(du.used/ 2**30)}GB, free: {int(du.free/ 2**30)}GB')
|
||||
to_be_wasted = int((du.total * utilization_percent) - du.used)
|
||||
print(f'to_be_wasted: {int(to_be_wasted/ 2**30)}GB')
|
||||
if to_be_wasted < 0:
|
||||
if utilization_percent < 0.1 or utilization_percent > 0.9:
|
||||
raise ValueError('utilization_percent must be between 0.1 and 0.9')
|
||||
if hysteresis_percent < 0.0001 or hysteresis_percent > 0.1:
|
||||
raise ValueError('hysteresis_percent must be between 0.0001 and 0.1')
|
||||
hysteresis = int(du.total * hysteresis_percent)
|
||||
if hysteresis < BS: hysteresis = BS
|
||||
print(f'hysteresis: {hysteresis}bytes')
|
||||
print(f'total: {round(du.total/ 2**30, 3)}GB, used: {round(du.used/ 2**30, 3)}GB, free: {round(du.free/ 2**30, 3)}GB usage: {round(du.used * 100 / (du.used+du.free), 3)}%')
|
||||
to_be_wasted = int(((du.used+du.free) * utilization_percent) - ((du.used+du.free) - du.free))
|
||||
print(f'to_be_wasted: {round(to_be_wasted/ 2**30, 3)}GB ')
|
||||
if to_be_wasted < -hysteresis:
|
||||
to_be_trimmed = abs(to_be_wasted)
|
||||
baloon_size = os.path.getsize(os.path.join(fs, baloon_filename))
|
||||
if baloon_size> to_be_trimmed:
|
||||
os.truncate(os.path.join(fs, baloon_filename), baloon_size - to_be_trimmed)
|
||||
else: print(f'can\'t trim {baloon_size} > {to_be_trimmed}')
|
||||
elif to_be_wasted > 0:
|
||||
print(f'trimmed {to_be_trimmed} Bytes')
|
||||
else: print(f'can\'t trim {baloon_size} < {to_be_trimmed}Bytes')
|
||||
elif to_be_wasted > hysteresis:
|
||||
with open(os.path.join(fs, baloon_filename) , 'ab') as f:
|
||||
last = 0
|
||||
writed = to_be_wasted
|
||||
while to_be_wasted > 0:
|
||||
if time.time() - last > 1:
|
||||
print('\r', end='')
|
||||
print(f'left {int(to_be_wasted/ 2**30)}GB ', end='')
|
||||
print(f'left {int(to_be_wasted/ 2**30)}GB \r', end='')
|
||||
last = time.time()
|
||||
write_size = min(to_be_wasted, BS)
|
||||
f.write(b'x' * write_size)
|
||||
to_be_wasted -= write_size
|
||||
print('Done ')
|
||||
print(f'Done, added {writed} bytes')
|
||||
|
||||
|
||||
|
||||
set_utilization('.')
|
||||
Loading…
Reference in New Issue