Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

RCS_TIMER Class Reference

#include <timer.hh>

Collaboration diagram for RCS_TIMER:

Collaboration graph
[legend]

Public Methods

 RCS_TIMER (double timeout, RCS_TIMERFUNC function=(RCS_TIMERFUNC) NULL, void *arg=NULL)
 RCS_TIMER (char *process_name, char *timer_config_file)
 RCS_TIMER (double _timeout, char *process_name, char *timer_config_file)
 ~RCS_TIMER ()
int wait ()
double load ()
void sync ()

Data Fields

double timeout

Private Methods

void init (double _timeout, int _id)
void zero_timer ()
void set_timeout (double _timeout)

Private Attributes

RCS_TIMERFUNC function
void * arg
double last_time
double start_time
double idle
int counts
int counts_since_real_sleep
int counts_per_real_sleep
double time_since_real_sleep
int num_sems
int sem_key
int id
int create_sems
int poller_pid
double clk_tck_val

Constructor & Destructor Documentation

RCS_TIMER::RCS_TIMER double    _timeout,
RCS_TIMERFUNC    _function = (RCS_TIMERFUNC) NULL,
void *    _arg = NULL
 

Definition at line 337 of file timer.cc.

00338 {
00339   zero_timer ();
00340   counts_per_real_sleep = 0;
00341   counts_since_real_sleep = 0;
00342 
00343   if (_timeout < clk_tck_val)
00344     {
00345       counts_per_real_sleep = (int) (clk_tck_val / _timeout);
00346       /* bump interval up to minimum system clock tick */
00347       timeout = clk_tck_val;
00348     }
00349   else
00350     {
00351       timeout = _timeout;
00352     }
00353   function = _function;
00354   arg = _arg;
00355   last_time = etime ();         /* initialize start time and last time called
00356                                    to current time since epoch */
00357   idle = 0.0;                   /* set accumulated idle time to 0.0 */
00358   counts = 0;                   /* set accumulated waits to 0 */
00359   start_time = etime ();        /* set creation time to now */
00360   time_since_real_sleep = start_time;
00361 }

RCS_TIMER::RCS_TIMER char *    process_name,
char *    config_file
 

Definition at line 177 of file timer.cc.

00178 {
00179 #if 0
00180   read_config_file (process_name, config_file);
00181 #endif
00182 }

RCS_TIMER::RCS_TIMER double    _timeout,
char *    process_name,
char *    config_file
 

Definition at line 184 of file timer.cc.

00185 {
00186 #if 0
00187   read_config_file (process_name, config_file);
00188 #endif
00189   set_timeout (_timeout);
00190 }

RCS_TIMER::~RCS_TIMER  
 

Definition at line 513 of file timer.cc.

00514 {
00515 #if 0
00516   if (poller_pid > 0)
00517     {
00518       kill (poller_pid, SIGINT);
00519 #ifdef SUN
00520       waitpid (poller_pid, NULL, 0);
00521 #endif
00522       poller_pid = 0;
00523     }
00524   if (NULL != sems)
00525     {
00526       for (int i = 0; i < num_sems; i++)
00527         {
00528           if (sems[i] != NULL)
00529             {
00530               delete sems[i];
00531             }
00532           sems[i] = NULL;
00533         }
00534       DEBUG_FREE (sems);
00535     }
00536 #endif
00537 }


Member Function Documentation

int RCS_TIMER::wait  
 

Definition at line 372 of file timer.cc.

Referenced by main(), nml_perf_combined_test(), nml_perf_read_test(), nml_perf_write_test(), nml_watch(), and NODE::wait_for_timer().

00373 {
00374   double interval;              /* interval between this and last wakeup */
00375   double numcycles;             /* interval, in units of timeout */
00376   int missed = 0;               /* cycles missed */
00377   double remaining = 0.0;       /* time remaining until timeout */
00378   double time_in = 0.0;         /* time wait() was entered */
00379   double time_done = 0.0;       /* time user function finished */
00380   /* first call the user timing function, if any */
00381   if (function != NULL)
00382     {
00383       /* set time in */
00384       time_in = etime ();
00385 
00386       if ((*function) (arg) == -1)
00387         {
00388           return -1;            /* fatal error in timing function */
00389         }
00390       time_done = etime ();
00391     }
00392   else
00393     {
00394       /* set time in, time done not used */
00395       time_in = etime ();
00396     }
00397 
00398   /* calculate the interval-- for user timing functions, this is how
00399      long between this wakeup and the last wakeup.  For internal timers,
00400      this is how long we need to sleep to make it to the next interval
00401      on time. */
00402   interval = time_in - last_time;
00403   numcycles = interval / timeout;
00404 
00405   /* synchronize and set last_time correctly; update idle time */
00406   counts++;
00407   if (function != NULL)
00408     {
00409       missed = INT (numcycles - (clk_tck_val / timeout));
00410       idle += interval;
00411       last_time = time_done;
00412     }
00413   else
00414     {
00415       missed = INT (numcycles);
00416       remaining = timeout * (1.0 - FRAC (numcycles));
00417       idle += interval;
00418 #if 0
00419       if (counts_per_real_sleep > 0)
00420         {
00421           if (counts_since_real_sleep < counts_per_real_sleep)
00422             {
00423               counts_since_real_sleep++;
00424               if (sems != NULL && num_sems > 0)
00425                 {
00426                   for (i = id + 1; i < num_sems && !sem_posted; i++)
00427                     {
00428                       if (i != id && NULL != sems[i])
00429                         {
00430                           if (sems[i]->post () < 0)
00431                             {
00432                               delete sems[i];
00433                               sems[i] = NULL;
00434                               if (i == num_sems - 1)
00435                                 {
00436                                   num_sems--;
00437                                 }
00438                             }
00439                           else
00440                             {
00441                               sem_posted = 1;
00442                             }
00443                         }
00444                     }
00445                   for (i = 0; i < id && !sem_posted; i++)
00446                     {
00447                       if (i != id && NULL != sems[i])
00448                         {
00449                           if (sems[i]->post () < 0)
00450                             {
00451                               delete sems[i];
00452                               sems[i] = NULL;
00453                               if (i == num_sems - 1)
00454                                 {
00455                                   num_sems--;
00456                                 }
00457                             }
00458                           else
00459                             {
00460                               sem_posted = 1;
00461                             }
00462                         }
00463                     }
00464                   if (NULL != sems[id] && sem_posted)
00465                     {
00466                       if (sems[id]->wait () < 0)
00467                         {
00468                           delete sems[id];
00469                           sems[id] = NULL;
00470                           return 0;
00471                         }
00472                       while (etime () - time_in < remaining)
00473                         {
00474                           if (sems[id]->wait () < 0)
00475                             {
00476                               delete sems[id];
00477                               sems[id] = NULL;
00478                               break;
00479                             }
00480                         }
00481                       for (i = 0; i < num_sems; i++)
00482                         {
00483                           if (i != id && NULL != sems[i])
00484                             {
00485                               sems[i]->clear ();
00486                             }
00487                         }
00488                     }
00489                   return 0;
00490                 }
00491             }
00492           counts_since_real_sleep = 0;
00493           time_since_real_sleep = etime ();
00494           esleep (clk_tck_val);
00495           last_time = etime ();
00496           return missed;
00497         }
00498 #endif
00499     }
00500   esleep (remaining);
00501   last_time = etime ();
00502   return missed;
00503 }

double RCS_TIMER::load  
 

Definition at line 506 of file timer.cc.

00507 {
00508   if (counts * timeout != 0.0)
00509     return idle / (counts * timeout);
00510   return -1.0;
00511 }

void RCS_TIMER::sync  
 

Definition at line 365 of file timer.cc.

Referenced by NODE::check_timer().

00366 {
00367   last_time = etime ();         /* initialize start time and last time called
00368                                    to current time since epoch */
00369 }

void RCS_TIMER::init double    _timeout,
int    _id
[private]
 

Definition at line 303 of file timer.cc.

00304 {
00305   zero_timer ();
00306   id = _id;
00307 #if 0
00308   num_sems = _num_sems;
00309   int sem_key = _sem_key;
00310   int create_sems = _create_sems;
00311   if (num_sems > 0 && id >= 0 && id < num_sems)
00312     {
00313       sems =
00314         (RCS_SEMAPHORE **) DEBUG_MALLOC (sizeof (RCS_SEMAPHORE *) * num_sems);
00315       for (int i = 0; i < num_sems; i++)
00316         {
00317           sems[i] = new RCS_SEMAPHORE (sem_key + i, create_sems, -1);
00318         }
00319       sems[id]->post ();
00320       poller_pid = fork ();
00321       if (poller_pid == 0)
00322         {
00323           timer_poll (_timeout, sems[id]);
00324         }
00325     }
00326   else
00327     {
00328       num_sems = 0;
00329     }
00330 #endif
00331   set_timeout (_timeout);
00332 
00333 }

void RCS_TIMER::zero_timer   [private]
 

Definition at line 282 of file timer.cc.

Referenced by RCS_TIMER(), and init().

00283 {
00284   num_sems = 0;
00285 #if USE_SEMS_FOR_TIMER
00286   sems = NULL;
00287 #endif
00288   id = 0;
00289   function = NULL;
00290   idle = 0.0;                   /* set accumulated idle time to 0.0 */
00291   counts = 0;                   /* set accumulated waits to 0 */
00292   start_time = etime ();        /* set creation time to now */
00293   time_since_real_sleep = start_time;
00294   counts_per_real_sleep = 0;
00295   counts_since_real_sleep = 0;
00296   clk_tck_val = clk_tck ();
00297   timeout = clk_tck_val;
00298 }

void RCS_TIMER::set_timeout double    _timeout [private]
 

Definition at line 193 of file timer.cc.

Referenced by RCS_TIMER(), and init().

00194 {
00195   timeout = _timeout;
00196   if (timeout < clk_tck ())
00197     {
00198       counts_per_real_sleep = (int) (clk_tck () / _timeout) + 1;
00199     }
00200   else
00201     {
00202       counts_per_real_sleep = 0;
00203     }
00204 }


Field Documentation

double RCS_TIMER::timeout
 

Definition at line 69 of file timer.hh.

Referenced by NODE::check_timer(), and NODE::task().

RCS_TIMERFUNC RCS_TIMER::function [private]
 

Definition at line 79 of file timer.hh.

void* RCS_TIMER::arg [private]
 

Definition at line 80 of file timer.hh.

double RCS_TIMER::last_time [private]
 

Definition at line 81 of file timer.hh.

double RCS_TIMER::start_time [private]
 

Definition at line 82 of file timer.hh.

double RCS_TIMER::idle [private]
 

Definition at line 83 of file timer.hh.

int RCS_TIMER::counts [private]
 

Definition at line 84 of file timer.hh.

int RCS_TIMER::counts_since_real_sleep [private]
 

Definition at line 85 of file timer.hh.

int RCS_TIMER::counts_per_real_sleep [private]
 

Definition at line 86 of file timer.hh.

double RCS_TIMER::time_since_real_sleep [private]
 

Definition at line 87 of file timer.hh.

int RCS_TIMER::num_sems [private]
 

Definition at line 91 of file timer.hh.

int RCS_TIMER::sem_key [private]
 

Definition at line 92 of file timer.hh.

int RCS_TIMER::id [private]
 

Definition at line 93 of file timer.hh.

int RCS_TIMER::create_sems [private]
 

Definition at line 94 of file timer.hh.

int RCS_TIMER::poller_pid [private]
 

Definition at line 95 of file timer.hh.

double RCS_TIMER::clk_tck_val [private]
 

Definition at line 96 of file timer.hh.


The documentation for this class was generated from the following files:
Generated on Sun Dec 2 15:59:10 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001