X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=python%2Fbwlimit.py;h=60b290eaceacdab38a91db749d242409aaaeea86;hb=5468868e9415bbc83b5db6516f245df52b589ece;hp=6591b8398bd854fa63ca31b877cd46de564d000f;hpb=8bdc71ddc20ec5695cc96ab00c62439c10a4aeb0;p=util-vserver-pl.git diff --git a/python/bwlimit.py b/python/bwlimit.py index 6591b83..60b290e 100644 --- a/python/bwlimit.py +++ b/python/bwlimit.py @@ -66,7 +66,7 @@ verbose = 0 # bwmin should be small enough that it can be considered negligibly # slow compared to the hardware. 8 bits/second appears to be the # smallest value supported by tc. -bwmin = 8 +bwmin = 1000 # bwmax should be large enough that it can be considered at least as # fast as the hardware. @@ -191,6 +191,24 @@ def get_tc_rate(s): else: return -1 +def format_bytes(bytes, si = True): + """ + Formats bytes into a string + """ + if si: + kilo = 1000. + else: + # Officially, a kibibyte + kilo = 1024. + + if bytes >= (kilo * kilo * kilo): + return "%.1f GB" % (bytes / (kilo * kilo * kilo)) + elif bytes >= 1000000: + return "%.1f MB" % (bytes / (kilo * kilo)) + elif bytes >= 1000: + return "%.1f KB" % (bytes / kilo) + else: + return "%.0f bytes" % bytes def format_tc_rate(rate): """ @@ -546,9 +564,15 @@ def on(xid, dev = dev, share = None, minrate = None, maxrate = None, minexemptra def set(xid, share = None, minrate = None, maxrate = None, minexemptrate = None, maxexemptrate = None): - on(xid = xid, share = share, - minrate = minrate, maxrate = maxrate, - minexemptrate = minexemptrate, maxexemptrate = maxexemptrate) + # Get defaults from current state if available + # get(xid,dev) = ( xid, share, minrate, maxrate, minexemptrate, maxexemptrate, bytes, i2bytes ) + current = get(xid, dev) + if current == None: current = () # must be a new slice if returned nothing. + new = ( share, minrate, maxrate, minexemptrate, maxexemptrate ) + if current[1:6] != new: + on(xid = xid, share = share, + minrate = minrate, maxrate = maxrate, + minexemptrate = minexemptrate, maxexemptrate = maxexemptrate) # Remove class associated with specified slice xid. If further packets @@ -680,11 +704,11 @@ def main(): minexemptrate, maxexemptrate, bytes, exemptbytes) else: - print "%s %d %s %s %s %s %d %d" % \ + print "%s %d %s %s %s %s %s %s" % \ (slice, share, format_tc_rate(minrate), format_tc_rate(maxrate), format_tc_rate(minexemptrate), format_tc_rate(maxexemptrate), - bytes, exemptbytes) + format_bytes(bytes), format_bytes(exemptbytes)) elif len(argv) >= 2: # slice, ...