Guest User

Untitled

a guest
Oct 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. def fair_share(scheduables, totalCapacity):
  4. print "demand:%s totalCapacity:%s" % (scheduables, totalCapacity)
  5.  
  6. totalDemand = reduce(lambda a, b: int(a) + int(b), [x['demand'] for x in scheduables])
  7. cap = min(totalDemand, totalCapacity)
  8.  
  9. weightSlotRatio = 1.0
  10.  
  11. while slots_used_with_ratio(scheduables, weightSlotRatio) < cap:
  12. weightSlotRatio *= 2.0
  13.  
  14. left = 0
  15. right = weightSlotRatio
  16.  
  17. for i in range(0, 25):
  18. mid = (left + right) / 2.0
  19. if slots_used_with_ratio(scheduables, mid) < cap:
  20. left = mid
  21. else:
  22. right = mid
  23.  
  24. slots = slots_used_with_ratio(scheduables, right)
  25.  
  26. for scheduable in scheduables:
  27. scheduable['share'] = compute_share(scheduable, right)
  28.  
  29. print "totalDemand:%s cap:%s scheduables:%s slots:%s weightSlotRatio:%s" % (totalDemand, cap, scheduables, slots, right)
  30.  
  31. def slots_used_with_ratio(scheduables, weightSlotRatio):
  32. shares = [compute_share(x, weightSlotRatio) for x in scheduables]
  33. slots = reduce(lambda a, b: a + b, shares)
  34.  
  35. #print "weightSlotRatio:%s shares:%s slots:%s" % (weightSlotRatio, shares, slots)
  36.  
  37. return slots
  38.  
  39. def compute_share(scheduable, weightSlotRatio):
  40. share = scheduable['weight'] * weightSlotRatio
  41. share = max(share, scheduable['minshare'])
  42. share = min(share, scheduable['demand'])
  43.  
  44. #print "scheduable:%s weightSlotRatio:%s share:%s" % (scheduable, weightSlotRatio, share)
  45.  
  46. return share
  47.  
  48. if __name__ == '__main__':
  49. total = 180
  50. scheduables = [
  51. { 'demand': 30, 'minshare': 40, 'weight': 1.0 },
  52. { 'demand': 40, 'minshare': 0, 'weight': 1.0 },
  53. { 'demand': 120, 'minshare': 50, 'weight': 1.0 }
  54. ]
  55.  
  56. fair_share(scheduables, total)
Add Comment
Please, Sign In to add comment