gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] gzz gzz/mem/MemoryPartitioner.java test/gzz/mem...


From: Tuomas J. Lukka
Subject: [Gzz-commits] gzz gzz/mem/MemoryPartitioner.java test/gzz/mem...
Date: Wed, 08 Jan 2003 08:51:20 -0500

CVSROOT:        /cvsroot/gzz
Module name:    gzz
Changes by:     Tuomas J. Lukka <address@hidden>        03/01/08 08:51:20

Modified files:
        gzz/mem        : MemoryPartitioner.java 
Added files:
        test/gzz/mem   : partition.test 

Log message:
        Algorithms, failing test for them

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/gzz/mem/MemoryPartitioner.java.diff?tr1=1.2&tr2=1.3&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/test/gzz/mem/partition.test?rev=1.1

Patches:
Index: gzz/gzz/mem/MemoryPartitioner.java
diff -u gzz/gzz/mem/MemoryPartitioner.java:1.2 
gzz/gzz/mem/MemoryPartitioner.java:1.3
--- gzz/gzz/mem/MemoryPartitioner.java:1.2      Wed Jan  8 05:35:11 2003
+++ gzz/gzz/mem/MemoryPartitioner.java  Wed Jan  8 08:51:20 2003
@@ -2,6 +2,7 @@
 
 package gzz.mem;
 import gzz.Obs;
+import gzz.util.ZZLogger;
 
 import java.util.*;
 
@@ -13,18 +14,18 @@
     /** The amount of memory reserved to MemoryConsumers.
      * Let's start with 64MB
      */
-    public static int memory = 64 * 1024 * 1024;
+    public int memory = 64 * 1024 * 1024;
 
 
-    static Stack currentScene = new Stack();
-    static {
+    Stack currentScene = new Stack();
+    {
        currentScene.push(null);
     }
 
-    static public void start(Object id) {
+    public void start(Object id) {
        currentScene.push(id);
     }
-    static public void end(Object id) {
+    public void end(Object id) {
        currentScene.pop();
     }
 
@@ -43,12 +44,12 @@
        /** The number of bytes requested from the consumer.
         */
        int setBytes = 0;
-       int setQuality = -1;
+       float setQuality = -1;
 
        /** The number of bytes the consumer reports using currently.
         */
        int gotBytes = 0;
-       int gotQuality = -1;
+       float gotQuality = -1;
 
        Obs o;
 
@@ -87,9 +88,23 @@
            this.o = o;
        }
 
+       void fit(MemoryConsumer cons, int bytes) {
+           cons.setReservation(bytes, curQuality, o);
+           setBytes = bytes;
+           setQuality = curQuality;
+       }
+
+       int maximum(MemoryConsumer cons, int left) {
+           int m = cons.getMaxBytes(curQuality);
+           cons.setReservation(m, curQuality, o);
+           setBytes = m;
+           setQuality = curQuality;
+           return left-m;
+       }
+
     }
 
-    static Map consumer2record = Collections.synchronizedMap(
+    Map consumer2record = Collections.synchronizedMap(
                                        new WeakHashMap());
 
     /** Indicate that a request for the data of the given consumer was made.
@@ -106,9 +121,10 @@
      *                 Note that the semantics are different from 
address@hidden MemoryConsumer.setReservation}'s 
      *                 Obs, since here it <b>is</b> guaranteed that all Obses 
will be called.
      *                 This difference is up to MemoryPartitioner to handle 
internally.
+     *   XXX OBS NOT YET CALLED RIGHT
      * @see MemoryConsumer
      */
-    static public void request(MemoryConsumer consumer, float importance, 
float quality, Obs o) {
+    public void request(MemoryConsumer consumer, float importance, float 
quality, Obs o) {
        ConsumerRecord rec = (ConsumerRecord)consumer2record.get(consumer);
        if(rec == null) {
            rec = new ConsumerRecord();
@@ -118,33 +134,81 @@
 
     }
 
-    static Thread repartitioner = new Thread() {
+    Thread repartitioner = new Thread() {
        public void run() {
-           int sum1 = 0;
-           int sumReduced = 0;
-           for(int round = 0; ; round++) {
+           while(true) {
+               try {
+                   rerate();
+                   Thread.sleep(10);
+               } catch(Exception e) { 
+                   ZZLogger.exc(e, "gzz.mem.MemoryPartitioner thread!");
+               }
+           }
+       }
+    };
+
+    /** Reallocate all memory.
+     */
+    private void rerate() {
+       // Sum of maxbytes with priority 1
+       int sum1 = 0;
+       // Sum of maxbytes multiplied with priority^round
+       int sumReduced = 0;
+       for(int round = 0; ; round++) {
+           for(Iterator i = consumer2record.keySet().iterator(); i.hasNext(); 
) {
+               MemoryConsumer cons = (MemoryConsumer)i.next();
+               ConsumerRecord rec = (ConsumerRecord)consumer2record.get(cons);
+               if(round == 0) rec.bgUpdate();
+               float imp = 0;
+               if(rec.curImportance > 0)
+                   imp = (float)Math.pow(rec.curImportance, round);
+               else if(round == 0)
+                   imp = 1;
+               int byt = cons.getMaxBytes(rec.curQuality);
+               if(rec.curImportance == 1)
+                   sum1 += byt;
+               sumReduced += (int)(imp * byt);
+           }
+           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;
+           }
+           if(sumReduced <= memory) {
+               // Hey, now it fits!
+               //
                for(Iterator i = consumer2record.keySet().iterator(); 
i.hasNext(); ) {
                    MemoryConsumer cons = (MemoryConsumer)i.next();
                    ConsumerRecord rec = 
(ConsumerRecord)consumer2record.get(cons);
-                   if(round == 0) rec.bgUpdate();
                    float imp = 0;
                    if(rec.curImportance > 0)
                        imp = (float)Math.pow(rec.curImportance, round);
                    else if(round == 0)
                        imp = 1;
                    int byt = cons.getMaxBytes(rec.curQuality);
-                   if(rec.curImportance == 1)
-                       sum1 += byt;
-                   sumReduced += (int)(imp * byt);
-               }
-               if(sum1 > memory) {
-                   // Problem! Not enough memory for all 1-importances
-               }
-               if(sumReduced <= memory) {
-                   // Hey, now it fits!
+                   rec.fit(cons, (int)(imp * byt));
                }
            }
+           // didn't fit - try with a larger exponent
        }
-    };
+    }
+
+    public MemoryPartitioner(int size) {
+       memory = size;
+       repartitioner.start();
+    }
+
+    public void stop() {
+       repartitioner.stop();
+    }
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]