# python imports

import os, string
# our imports

import defs


"""This module figures out how much CPU we are

   using in terms of how many iterations on a

   generator we can use in a second.  On my machine

   (Athlon 2000+) it is about 14k, it starts

   out at 10k for safety.  The amount actually

   used is dynamically changed from there if

   we are using too much, or aren't using any

   jiffies (imitation of linux 1/100th second)

"""



__version__ = "$Revision: 1.3 $"



class Jiffies:

  """Keep track of our jiffies"""

  def __init__(self):

    self._turns = 1

    self._jiffies = self.get_jiffies()

    self._start = self._jiffies

    self.avail = 100 * defs.ONE_TENTH_OF_A_SECOND # give ourselves a starting bonus

    self._inc = 10 * defs.ONE_TENTH_OF_A_SECOND

    return



  def get_jiffies(self):

    """Get our current jiffie count"""

    parts = os.times()[0:4]

    return parts[0] + parts[1] + parts[2] + parts[3]



  def avg_jiffies(self):

    return (self._jiffies / self._turns)



  def bump_turn(self):

    self._turns += 1

    self._jiffies = self.get_jiffies()

    # don't screw with jiffies if we have an abundance

    if (self.avail < 10000000):

      self.avail += self._inc

      if (self.avg_jiffies() > 1.0):

        self._inc -= defs.ONE_TENTH_OF_A_SECOND

      elif (self.avg_jiffies() < 0.8):

        self._inc += defs.ONE_TENTH_OF_A_SECOND

    # endif

    print "Jiffies avail ", self.avail, "Current inc is ", self._inc

    

  def test(self):

    print self.get_jiffies()