Page 1 of 1

today's tutorial -- hysteresis

Posted: Tue Oct 07, 2003 7:45 am
by Pype.Clicker
Hi there ;)

today comes a small tutorial (if i can call it that way) about hysteresis cycles. This technique is widely used in monitoring systems and networking, and i think it can have a lot of interresting applications in the field of OS programming.

What is hysteresis
Let's say you have an integer variable X (which may cover a large kind of values like CPU time, available memory space, disk fragmentation level, etc) and an action that you wish to execute to keep X under a given threshold Xmax.
One simple example (on which i'll focus) is that
if the amount of available physical page frame is lower than 10% of the physical memory, wake up the swapper subsystem to free some memory
where X will be the used page count, and Xmax is computed at system boot time to be (available_memory * 9)/10

Now, if you just let the swapper start when X>Xmax and stop it when X<Xmax, you'll find yourself in a situation where the swapper will be (almost) continuously running as soon as Xmax is reached and that the system needs more memory:
  • on X==Xmax, the swapper will find one page to be swapped out
  • X will be decremented (as there's one more free page) and the swapper stops.
  • X then the system requests one more physical page and the swapper is waken up again, freeing one single page, etc.
The hysteresis technique will consist of defining two thresholds instead of a single one: Xstart, which is the value at which the 'cooling process' will be activated and Xstop, which is the value at which the 'cooling process' is stopped. Usually, Xstop is defined as a ratio of Xstart, like 0.9 or 0.8, or whatever you like ...

If we apply this to our swapping technique, it will get the following behaviour:
  • As soon as the amount of free physical page gets too low (say, only 10Mb left, or something), the system enters "stressed mode" and starts swapping (the cooling process).
  • the swap will go on as long as the amount of freed pages is unsufficient to say we returned to the "normal mode". (say, as long as there is less than 30Mb left). Only then do we consider that the system has recovered a 'stable' state and that the swapper can be turned off.

Code: Select all

virtual class HysteresisController extends PeriodicMonitor {
     int start_level;
     int stop_level;
     virtual int probe(); // override this so that it returns X
     thread cooler;
     boolean cooling=false;

     /* this function is called periodically by the Monitoring System
      * (we assume this is a sideeffect of creating a new   
      *  PeriodicMonitor object)
      */
     void monitor() {
          if (cooling && probe()<stop_level) {
             cooler.stop();
             cooling=false;
          }
          if (!cooling && probe()>start_level) {
             cooling.start();
             cooling=true;
          }
     }
}
So, voil?. That's all for now about hysteresis

Re:today's tutorial -- hysteresis

Posted: Tue Oct 07, 2003 8:19 am
by Solar
What, they even have a name for this technique? :D

(Everytime you think you know something in your sleep, you learn something new about it. Even if it's just the name of the thing. ;) )

Re:today's tutorial -- hysteresis

Posted: Tue Oct 07, 2003 8:31 am
by BI lazy
I've heared about Hysteresis in the context of fuzzy logic and had define a set of predicate logic rules for a hysteresis in a fuzzy logic heating control system.

Oh sweet gosh, that's been ... weird stuff. The almost weirdest I've ever got hands on whilst at study.

Re:today's tutorial -- hysteresis

Posted: Tue Oct 07, 2003 9:00 am
by Solar
Well, you need this concept virtually everywhere when a digital on/off switch is used to keep an analogous value within certain bounds.

Picture a radiator that can be either "on" or "off", and a room temperature to be kept between 18 and 21 degrees. If the temperature drops to 17 degrees and you switch the radiator on only until 18 degrees are reached again, you'll end up in rapid on-off cycles at or around 18. If you heat up to 20 degrees...

That's not what I'd call "black magic". ;-)

Re:today's tutorial -- hysteresis

Posted: Tue Oct 07, 2003 11:08 am
by BI lazy
*hehe*

With plain logic and reasonable thinking, it's like that, solar.

But what I talk about is taking a hysteresis curve and abstract it to some kind of weird maths - something they called Pr?dikatenlogik (dont know how to call it in english).

Re:today's tutorial -- hysteresis

Posted: Tue Oct 07, 2003 5:07 pm
by Schol-R-LEA
I think the English term you want is "predicate logic".