today's tutorial -- hysteresis

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

today's tutorial -- hysteresis

Post 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
User avatar
Solar
Member
Member
Posts: 7632
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:today's tutorial -- hysteresis

Post 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. ;) )
Every good solution is obvious once you've found it.
BI lazy

Re:today's tutorial -- hysteresis

Post 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.
User avatar
Solar
Member
Member
Posts: 7632
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:today's tutorial -- hysteresis

Post 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". ;-)
Every good solution is obvious once you've found it.
BI lazy

Re:today's tutorial -- hysteresis

Post 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).
Schol-R-LEA

Re:today's tutorial -- hysteresis

Post by Schol-R-LEA »

I think the English term you want is "predicate logic".
Post Reply