Changeset 83

Show
Ignore:
Timestamp:
06/29/05 03:23:09 (4 years ago)
Author:
jtv
Message:

Documented state machine

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/state.c

    r82 r83  
    2727#include "swaps.h" 
    2828 
     29/* The allocation/deallocation algorithm is driven by a state machine. 
     30 */ 
    2931 
    3032// TODO: Adaptive cooldown_time? 
     
    8082  timer_tick(); 
    8183 
    82   if (likely(the_state != st_diet) && unlikely(reqbytes > 0)) 
     84  if (unlikely(reqbytes > 0) && likely(the_state != st_diet)) 
    8385  { 
     86    /* In any state except "diet," where allocation is inhibited, a shortage of 
     87     * memory means we forget what state we're in and jump straight to "hungry" 
     88     * mode, allocating a new swapfile along the way.  If the allocation fails, 
     89     * we bail out into "diet" mode. 
     90     */ 
    8491    if (likely(alloc_swapfile(reqbytes))) state_to(st_hungry); 
    8592    else state_to(st_diet); 
     
    8794  else if (unlikely(timer_timeout())) 
    8895  { 
     96    /* All states except "steady" are designed to time out eventually; in every 
     97     * case that leads back to "steady" so we can make it a general rule that 
     98     * timeout leads to "steady." 
     99     * The only other action that accompanies timeout is when timing out of the 
     100     * "overfed" state, which is where we normally deallocate. 
     101     */ 
    89102    if (the_state == st_overfed) free_swapfile(-reqbytes); 
    90103    state_to(st_steady); 
     
    93106  { 
    94107  case st_diet: 
     108    /* If we overallocated and now find ourselves with more swap space than we 
     109     * think we need, deallocate it right away.  Don't leave "diet" state just 
     110     * yet in that case, however, or we may invite thrashing. 
     111     */ 
    95112    if (unlikely(reqbytes < 0)) free_swapfile(-reqbytes); 
    96113    break; 
    97114  case st_hungry: 
     115    /* The "hungry" state can either time out, or allocate more swap space and 
     116     * loop back to itself, resetting the timer (or if allocation fails, bail 
     117     * out to "diet").  In other words, all actions for this state have been 
     118     * handled by the general cases above. 
     119     */ 
    98120    break; 
    99121  case st_steady: 
     122    /* If we have more swap space than we need, go to "overfed" state which may 
     123     * eventually lead to deallocation. 
     124     */ 
    100125    if (unlikely(reqbytes < 0)) state_to(st_overfed); 
    101126    break; 
    102127  case st_overfed: 
     128    /* There are two ways out of "overfed" state: either we find that we no 
     129     * longer have more memory than we need, in which case we default back to 
     130     * steady state; or we time out as per the general case described above, 
     131     * having had excess swap space for an entire timer period and therefore 
     132     * deallocating swap space. 
     133     */ 
    103134    if (unlikely(reqbytes >= 0)) state_to(st_steady); 
    104135    break;