2 # This module implements Gacks handles
8 # GacksHandle is an object representing a handle. A handle is the following
11 # (id, unitstart, unitstop, timestart, timestop)
13 # id is a text identifier for the resource, for example "CPU"
15 # unitstart and unitstop form an interval [unitstart, unitstop) in unit
16 # space. For example, [0, 10) could represent 10% of a CPU.
18 # timestart and timestop form an interval [timestart, timestop) in time
21 # If unitstop==INFINITY, or timestop==INFINITY, then it is treated as infinity
23 # The GacksHandle also doubles as an RSPEC
25 INFINITY = "inf" #sys.maxint
28 def __init__(self, id=None, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY, string=None):
30 self.unitStart = unitStart
31 self.unitStop = unitStop
32 self.timeStart = timeStart
33 self.timeStop = timeStop
35 self.load_from_string(string)
38 return str(self.id) + "#" + \
39 str(self.unitStart) + "-" + str(self.unitStop) + "#" + \
40 str(self.timeStart) + "-" + str(self.timeStop)
42 def parse_range(self, str):
43 parts = str.split("-")
47 if parts[0] != INFINITY:
48 parts[0] = int(parts[0])
50 if parts[1] != INFINITY:
51 parts[1] = int(parts[1])
55 def load_from_string(self, str):
56 parts = str.split("#")
61 (self.unitStart, self.unitStop) = self.parse_range(parts[1])
64 (self.timeStart, self.timeStop) = self.parse_range(parts[2])
66 def get_quantity(self):
67 if self.unitStop == INFINITY:
70 return self.unitStop-self.unitStart
72 def get_duration(self):
73 if self.timeStop == INFINITY:
76 return self.timeStop-self.timeStart
79 print str(self.id) + ": " + \
80 "units " + str(self.unitStart) + "-" + str(self.unitStop) + \
81 "time " + str(self.timeStart) + "-" + str(self.timeStop)
84 return GacksHandle(self.id, self.unitStart, self.unitStop,
85 self.timeStart, self.timeStop)
87 def split(self, unit=None, time=None):
102 class GacksRecord(GacksHandle):
103 def __init__(self, id=None, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY, allocatorHRNs=[], consumerHRN=None):
104 GacksHandle.__init__(self, id, unitStart, unitStop, timeStart, timeStop)
105 self.allocatorHRNs = allocatorHRNs
106 self.consumerHRN = consumerHRN
109 GacksHandle.dump(self)
110 print " allocators:", ", ".join(self.allocatorHRNs)
111 print " consumer:", self.consumerHRN
114 return GacksRecord(self.id, self.unitStart, self.unitStop,
115 self.timeStart, self.timeStop,
116 self.allocatorHRNs, self.consumerHRN)
118 def set_allocator(self, callerHRN, allocatorHRN, which, where):
119 # build up a list of the positions of callerHRN inside of the
123 for i, hrn in enumerate(self.allocatorHRNs):
127 pos = positions[which]
129 # truncate the allocator list at the appropriate place.
131 # keep callerHRN in the list and append allocatorHRN after
133 # remove callerHRN and replace with allocatorHRN
136 self.allocatorHRNs = self.allocatorHRNs[:(pos+1)]
138 self.allocatorHRNs = self.allocatorHRNs[:pos]
140 self.allocatorHRNs.append(allocatorHRN)
142 def get_allocators(self):
143 return self.allocatorHRNs[:]
145 def set_consumer(self, consumerHRN):
146 self.consumerHRN = consumerHRN
148 def get_consumer(self):
149 return self.consumerHRN