11 |
* MemoryConsumers. |
* MemoryConsumers. |
12 |
*/ |
*/ |
13 |
public class MemoryPartitioner { |
public class MemoryPartitioner { |
14 |
public static boolean dbg = true; |
public static boolean dbg = false; |
15 |
static final void pa(String s) { System.out.println(s); } |
static final void pa(String s) { System.out.println(s); } |
16 |
|
|
17 |
/** The amount of memory reserved to MemoryConsumers. |
/** The amount of memory reserved to MemoryConsumers. |
144 |
while(true) { |
while(true) { |
145 |
if(!keepRepartitioner) return; |
if(!keepRepartitioner) return; |
146 |
try { |
try { |
147 |
if(dbg) pa("Going to rerate\n"); |
if(dbg) pa("Going to rerate"); |
148 |
rerate(); |
rerate(); |
149 |
if(dbg) pa("rerated, sleeping\n"); |
if(dbg) pa("rerated, sleeping"); |
150 |
Thread.sleep(10); |
Thread.sleep(10); |
151 |
} catch(Exception e) { |
} catch(Exception e) { |
152 |
ZZLogger.exc(e, "gzz.mem.MemoryPartitioner thread!"); |
ZZLogger.exc(e, "gzz.mem.MemoryPartitioner thread!"); |
158 |
/** Reallocate all memory. |
/** Reallocate all memory. |
159 |
*/ |
*/ |
160 |
private void rerate() { |
private void rerate() { |
161 |
|
// Maximum importance found |
162 |
|
float maximportance = 0; |
163 |
|
// Sum of maxbytes with priority 1 |
164 |
|
int sum1 = 0; |
165 |
|
for(Iterator i = consumer2record.keySet().iterator(); i.hasNext(); ) { |
166 |
|
MemoryConsumer cons = (MemoryConsumer)i.next(); |
167 |
|
ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons); |
168 |
|
rec.bgUpdate(); |
169 |
|
if(rec.curImportance > maximportance) maximportance = rec.curImportance; |
170 |
|
if(rec.curImportance == 1) |
171 |
|
sum1 += cons.getMaxBytes(rec.curQuality); |
172 |
|
} |
173 |
|
if(sum1 > memory) { |
174 |
|
// Problem! Not enough memory for all 1-importances |
175 |
|
// - fill from start as many as we can |
176 |
|
int left = memory; |
177 |
|
for(Iterator i = consumer2record.keySet().iterator(); i.hasNext(); ) { |
178 |
|
MemoryConsumer cons = (MemoryConsumer)i.next(); |
179 |
|
ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons); |
180 |
|
if(rec.curImportance == 1) |
181 |
|
left = rec.maximum(cons, left); |
182 |
|
else |
183 |
|
rec.fit(cons, 0); |
184 |
|
} |
185 |
|
return; |
186 |
|
} |
187 |
|
if(maximportance <= 0) return; |
188 |
|
// Else, find a good exponent |
189 |
for(int round = 0; ; round++) { |
for(int round = 0; ; round++) { |
|
// Sum of maxbytes with priority 1 |
|
|
int sum1 = 0; |
|
190 |
// Sum of maxbytes multiplied with priority^round |
// Sum of maxbytes multiplied with priority^round |
191 |
int sumReduced = 0; |
int sumReduced = 0; |
192 |
if(dbg) pa("Rerate round "+round); |
if(dbg) pa("Rerate round "+round); |
193 |
for(Iterator i = consumer2record.keySet().iterator(); i.hasNext(); ) { |
for(Iterator i = consumer2record.keySet().iterator(); i.hasNext(); ) { |
194 |
MemoryConsumer cons = (MemoryConsumer)i.next(); |
MemoryConsumer cons = (MemoryConsumer)i.next(); |
195 |
ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons); |
ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons); |
|
if(round == 0) rec.bgUpdate(); |
|
196 |
float imp = 0; |
float imp = 0; |
197 |
if(rec.curImportance > 0) |
if(rec.curImportance > 0) |
198 |
imp = (float)Math.pow(rec.curImportance, round); |
imp = (float)Math.pow(rec.curImportance/maximportance, round); |
199 |
else if(round == 0) |
else if(round == 0) |
200 |
imp = 1; |
imp = 1; |
201 |
int byt = cons.getMaxBytes(rec.curQuality); |
int byt = cons.getMaxBytes(rec.curQuality); |
|
if(rec.curImportance == 1) |
|
|
sum1 += byt; |
|
202 |
sumReduced += (int)(imp * byt); |
sumReduced += (int)(imp * byt); |
203 |
} |
} |
204 |
if(dbg) pa("Counted: "+sum1+" "+sumReduced); |
if(dbg) pa("Counted: "+sum1+" "+sumReduced); |
|
if(sum1 > memory) { |
|
|
// Problem! Not enough memory for all 1-importances |
|
|
// - fill from start as many as we can |
|
|
int left = memory; |
|
|
for(Iterator i = consumer2record.keySet().iterator(); i.hasNext(); ) { |
|
|
MemoryConsumer cons = (MemoryConsumer)i.next(); |
|
|
ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons); |
|
|
if(rec.curImportance == 1) |
|
|
left = rec.maximum(cons, left); |
|
|
else |
|
|
rec.fit(cons, 0); |
|
|
} |
|
|
return; |
|
|
} |
|
205 |
if(sumReduced <= memory) { |
if(sumReduced <= memory) { |
206 |
// Hey, now it fits! |
// Hey, now it fits! |
207 |
// |
// |
210 |
ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons); |
ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons); |
211 |
float imp = 0; |
float imp = 0; |
212 |
if(rec.curImportance > 0) |
if(rec.curImportance > 0) |
213 |
imp = (float)Math.pow(rec.curImportance, round); |
imp = (float)Math.pow(rec.curImportance/maximportance, round); |
214 |
else if(round == 0) |
else if(round == 0) |
215 |
imp = 1; |
imp = 1; |
216 |
int byt = cons.getMaxBytes(rec.curQuality); |
int byt = cons.getMaxBytes(rec.curQuality); |