def __init__(self, xid, name, rspec): self.xid = xid self.name = name self.time = 0 self.bytes = 0 self.i2bytes = 0 self.MaxRate = default_MaxRate self.MinRate = bwlimit.bwmin / 1000 self.Maxi2Rate = default_Maxi2Rate self.Mini2Rate = bwlimit.bwmin / 1000 self.MaxKByte = default_MaxKByte self.ThreshKByte = int(.8 * self.MaxKByte) self.Maxi2KByte = default_Maxi2KByte self.Threshi2KByte = int(.8 * self.Maxi2KByte) self.Share = default_Share self.Sharei2 = default_Share self.emailed = False self.capped = False self.updateSliceTags(rspec) bwlimit.set(xid = self.xid, dev = dev_default, minrate = self.MinRate * 1000, maxrate = self.MaxRate * 1000, maxexemptrate = self.Maxi2Rate * 1000, minexemptrate = self.Mini2Rate * 1000, share = self.Share)
def reset(self, runningrates, rspec): """ Begin a new recording period. Remove caps by restoring limits to their default values. """ # Cache share for later comparison self.Share = runningrates.get('share', 1) # Query Node Manager for max rate overrides self.updateSliceTags(rspec) # Reset baseline time self.time = time.time() # Reset baseline byte coutns self.bytes = runningrates.get('usedbytes', 0) self.i2bytes = runningrates.get('usedi2bytes', 0) # Reset email self.emailed = False # Reset flag self.capped = False # Reset rates. maxrate = self.MaxRate * 1000 minrate = self.MinRate * 1000 maxi2rate = self.Maxi2Rate * 1000 mini2rate = self.Mini2Rate * 1000 if (maxrate != runningrates.get('maxrate', 0)) or \ (minrate != runningrates.get('maxrate', 0)) or \ (maxi2rate != runningrates.get('maxexemptrate', 0)) or \ (mini2rate != runningrates.get('minexemptrate', 0)) or \ (self.Share != runningrates.get('share', 0)): logger.log("bwmon: %s reset to %s/%s" % \ (self.name, bwlimit.format_tc_rate(maxrate), bwlimit.format_tc_rate(maxi2rate))) bwlimit.set(xid = self.xid, dev = dev_default, minrate = self.MinRate * 1000, maxrate = self.MaxRate * 1000, maxexemptrate = self.Maxi2Rate * 1000, minexemptrate = self.Mini2Rate * 1000, share = self.Share)
def update(self, runningrates, rspec): """ Update byte counts and check if byte thresholds have been exceeded. If exceeded, cap to remaining bytes in limit over remaining time in period. Recalculate every time module runs. """ # cache share for later comparison runningrates['share'] = self.Share # Query Node Manager for max rate overrides self.updateSliceTags(rspec) usedbytes = runningrates['usedbytes'] usedi2bytes = runningrates['usedi2bytes'] # Check limits. if usedbytes >= (self.bytes + (self.ThreshKByte * 1024)): sum = self.bytes + (self.ThreshKByte * 1024) maxbyte = self.MaxKByte * 1024 bytesused = usedbytes - self.bytes timeused = int(time.time() - self.time) # Calcuate new rate. in bit/s new_maxrate = int(((maxbyte - bytesused) * 8)/(period - timeused)) # Never go under MinRate if new_maxrate < (self.MinRate * 1000): new_maxrate = self.MinRate * 1000 # State information. I'm capped. self.capped += True else: # Sanity Check new_maxrate = self.MaxRate * 1000 self.capped += False if usedi2bytes >= (self.i2bytes + (self.Threshi2KByte * 1024)): maxi2byte = self.Maxi2KByte * 1024 i2bytesused = usedi2bytes - self.i2bytes timeused = int(time.time() - self.time) # Calcuate New Rate. new_maxi2rate = int(((maxi2byte - i2bytesused) * 8)/(period - timeused)) # Never go under MinRate if new_maxi2rate < (self.Mini2Rate * 1000): new_maxi2rate = self.Mini2Rate * 1000 # State information. I'm capped. self.capped += True else: # Sanity new_maxi2rate = self.Maxi2Rate * 1000 self.capped += False # Check running values against newly calculated values so as not to run tc # unnecessarily if (runningrates['maxrate'] != new_maxrate) or \ (runningrates['minrate'] != self.MinRate * 1000) or \ (runningrates['maxexemptrate'] != new_maxi2rate) or \ (runningrates['minexemptrate'] != self.Mini2Rate * 1000) or \ (runningrates['share'] != self.Share): # Apply parameters bwlimit.set(xid = self.xid, dev = dev_default, minrate = self.MinRate * 1000, maxrate = new_maxrate, minexemptrate = self.Mini2Rate * 1000, maxexemptrate = new_maxi2rate, share = self.Share) # Notify slice if self.capped == True: self.notify(new_maxrate, new_maxi2rate, usedbytes, usedi2bytes)