timer.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %                    TTTTT  IIIII  M   M  EEEEE  RRRR                         %
00007 %                      T      I    MM MM  E      R   R                        %
00008 %                      T      I    M M M  EEE    RRRR                         %
00009 %                      T      I    M   M  E      R R                          %
00010 %                      T    IIIII  M   M  EEEEE  R  R                         %
00011 %                                                                             %
00012 %                                                                             %
00013 %                     Wizard's Toolkit Timing Methods                         %
00014 %                                                                             %
00015 %                             Software Design                                 %
00016 %                               John Cristy                                   %
00017 %                              January 1993                                   %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.wizards-toolkit.org/WizardsToolkit/script/license.php         %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %  Contributed by Bill Radcliffe and Bob Friesenhahn.
00037 %
00038 */
00039 
00040 /*
00041   Include declarations.
00042 */
00043 #include "wizard/studio.h"
00044 #include "wizard/exception.h"
00045 #include "wizard/exception-private.h"
00046 #include "wizard/log.h"
00047 #include "wizard/memory_.h"
00048 #include "wizard/timer.h"
00049 
00050 /*
00051   Typedef declarations.
00052 */
00053 struct _TimerInfo
00054 {
00055   Timer
00056     user,
00057     elapsed;
00058 
00059   TimerState
00060     state;
00061 
00062   size_t
00063     signature;
00064 };
00065 
00066 /*
00067   Define declarations.
00068 */
00069 #if defined(macintosh)
00070 #define CLK_TCK  CLOCKS_PER_SEC
00071 #endif
00072 #if !defined(CLK_TCK)
00073 #define CLK_TCK  sysconf(_SC_CLK_TCK)
00074 #endif
00075 
00076 /*
00077   Forward declarations.
00078 */
00079 static double
00080   UserTime(void);
00081 
00082 static void
00083   StopTimer(TimerInfo *);
00084 
00085 /*
00086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00087 %                                                                             %
00088 %                                                                             %
00089 %                                                                             %
00090 %   A c q u i r e T i m e r I n f o                                           %
00091 %                                                                             %
00092 %                                                                             %
00093 %                                                                             %
00094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00095 %
00096 %  AcquireTimerInfo() initializes the TimerInfo structure.  It effectively
00097 %  creates a stopwatch and starts it.
00098 %
00099 %  The format of the AcquireTimerInfo method is:
00100 %
00101 %      TimerInfo *AcquireTimerInfo(void)
00102 %
00103 */
00104 WizardExport TimerInfo *AcquireTimerInfo(void)
00105 {
00106   TimerInfo
00107     *timer_info;
00108 
00109   timer_info=(TimerInfo *) AcquireAlignedMemory(1,sizeof(*timer_info));
00110   if (timer_info == (TimerInfo *) NULL)
00111     ThrowWizardFatalError(CipherDomain,MemoryError);
00112   (void) ResetWizardMemory(timer_info,0,sizeof(*timer_info));
00113   timer_info->signature=WizardSignature;
00114   GetTimerInfo(timer_info);
00115   return(timer_info);
00116 }
00117 
00118 /*
00119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00120 %                                                                             %
00121 %                                                                             %
00122 %                                                                             %
00123 %   C o n t i n u e T i m e r                                                 %
00124 %                                                                             %
00125 %                                                                             %
00126 %                                                                             %
00127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00128 %
00129 %  ContinueTimer() resumes a stopped stopwatch. The stopwatch continues
00130 %  counting from the last StartTimer() onwards.
00131 %
00132 %  The format of the ContinueTimer method is:
00133 %
00134 %      WizardBooleanType ContinueTimer(TimerInfo *timer_info)
00135 %
00136 %  A description of each parameter follows.
00137 %
00138 %    o  timer_info: Time statistics structure.
00139 %
00140 */
00141 WizardExport WizardBooleanType ContinueTimer(TimerInfo *timer_info)
00142 {
00143   assert(timer_info != (TimerInfo *) NULL);
00144   assert(timer_info->signature == WizardSignature);
00145   if (timer_info->state == UndefinedTimerState)
00146     return(WizardFalse);
00147   if (timer_info->state == StoppedTimerState)
00148     {
00149       timer_info->user.total-=timer_info->user.stop-timer_info->user.start;
00150       timer_info->elapsed.total-=
00151         timer_info->elapsed.stop-timer_info->elapsed.start;
00152     }
00153   timer_info->state=RunningTimerState;
00154   return(WizardTrue);
00155 }
00156 
00157 /*
00158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00159 %                                                                             %
00160 %                                                                             %
00161 %                                                                             %
00162 %   D e s t r o y T i m e r I n f o                                           %
00163 %                                                                             %
00164 %                                                                             %
00165 %                                                                             %
00166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00167 %
00168 %  DestroyTimerInfo() zeros memory associated with the TimerInfo structure.
00169 %
00170 %  The format of the DestroyTimerInfo method is:
00171 %
00172 %      TimerInfo *DestroyTimerInfo(TimerInfo *timer_info)
00173 %
00174 %  A description of each parameter follows:
00175 %
00176 %    o timer_info: The cipher context.
00177 %
00178 */
00179 WizardExport TimerInfo *DestroyTimerInfo(TimerInfo *timer_info)
00180 {
00181   WizardAssert(CipherDomain,timer_info != (TimerInfo *) NULL);
00182         WizardAssert(CipherDomain,timer_info->signature == WizardSignature);
00183   timer_info->signature=(~WizardSignature);
00184   timer_info=(TimerInfo *) RelinquishWizardMemory(timer_info);
00185   return(timer_info);
00186 }
00187 
00188 /*
00189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00190 %                                                                             %
00191 %                                                                             %
00192 %                                                                             %
00193 +   E l a p s e d T i m e                                                     %
00194 %                                                                             %
00195 %                                                                             %
00196 %                                                                             %
00197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00198 %
00199 %  ElapsedTime() returns the elapsed time (in seconds) since the last call to
00200 %  StartTimer().
00201 %
00202 %  The format of the ElapsedTime method is:
00203 %
00204 %      double ElapsedTime()
00205 %
00206 */
00207 static double ElapsedTime(void)
00208 {
00209 #if defined(WIZARDSTOOLKIT_HAVE_TIMES)
00210   struct tms
00211     timer;
00212 
00213   return((double) times(&timer)/CLK_TCK);
00214 #else
00215 #if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00216   return(NTElapsedTime());
00217 #else
00218   return((double) clock()/CLK_TCK);
00219 #endif
00220 #endif
00221 }
00222 
00223 /*
00224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00225 %                                                                             %
00226 %                                                                             %
00227 %                                                                             %
00228 %   G e t E l a p s e d T i m e                                               %
00229 %                                                                             %
00230 %                                                                             %
00231 %                                                                             %
00232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00233 %
00234 %  GetElapsedTime() returns the elapsed time (in seconds) passed between the
00235 %  start and stop events. If the stopwatch is still running, it is stopped
00236 %  first.
00237 %
00238 %  The format of the GetElapsedTime method is:
00239 %
00240 %      double GetElapsedTime(TimerInfo *timer_info)
00241 %
00242 %  A description of each parameter follows.
00243 %
00244 %    o  timer_info: Timer statistics structure.
00245 %
00246 */
00247 WizardExport double GetElapsedTime(TimerInfo *timer_info)
00248 {
00249   assert(timer_info != (TimerInfo *) NULL);
00250   assert(timer_info->signature == WizardSignature);
00251   if (timer_info->state == UndefinedTimerState)
00252     return(0.0);
00253   if (timer_info->state == RunningTimerState)
00254     StopTimer(timer_info);
00255   return(timer_info->elapsed.total);
00256 }
00257 
00258 /*
00259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00260 %                                                                             %
00261 %                                                                             %
00262 %                                                                             %
00263 +   G e t T i m e r I n f o                                                   %
00264 %                                                                             %
00265 %                                                                             %
00266 %                                                                             %
00267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00268 %
00269 %  GetTimerInfo() initializes the TimerInfo structure.  It effectively creates
00270 %  a stopwatch and starts it.
00271 %
00272 %  The format of the GetTimerInfo method is:
00273 %
00274 %      void GetTimerInfo(TimerInfo *timer_info)
00275 %
00276 %  A description of each parameter follows.
00277 %
00278 %    o  timer_info: Timer statistics structure.
00279 %
00280 */
00281 WizardExport void GetTimerInfo(TimerInfo *timer_info)
00282 {
00283   /*
00284     Create a stopwatch and start it.
00285   */
00286   assert(timer_info != (TimerInfo *) NULL);
00287   timer_info->state=UndefinedTimerState;
00288   StartTimer(timer_info,WizardTrue);
00289 }
00290 
00291 /*
00292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00293 %                                                                             %
00294 %                                                                             %
00295 %                                                                             %
00296 %   G e t U s e r T i m e                                                     %
00297 %                                                                             %
00298 %                                                                             %
00299 %                                                                             %
00300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00301 %
00302 %  GetUserTime() returns the User time (user and system) by the operating
00303 %  system (in seconds) between the start and stop events. If the stopwatch is
00304 %  still running, it is stopped first.
00305 %
00306 %  The format of the GetUserTime method is:
00307 %
00308 %      double GetUserTime(TimerInfo *timer_info)
00309 %
00310 %  A description of each parameter follows.
00311 %
00312 %    o  timer_info: Timer statistics structure.
00313 %
00314 */
00315 WizardExport double GetUserTime(TimerInfo *timer_info)
00316 {
00317   assert(timer_info != (TimerInfo *) NULL);
00318   assert(timer_info->signature == WizardSignature);
00319   if (timer_info->state == UndefinedTimerState)
00320     return(0.0);
00321   if (timer_info->state == RunningTimerState)
00322     StopTimer(timer_info);
00323   return(timer_info->user.total);
00324 }
00325 
00326 /*
00327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00328 %                                                                             %
00329 %                                                                             %
00330 %                                                                             %
00331 %   R e s e t T i m e r                                                       %
00332 %                                                                             %
00333 %                                                                             %
00334 %                                                                             %
00335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00336 %
00337 %  ResetTimer() resets the stopwatch.
00338 %
00339 %  The format of the ResetTimer method is:
00340 %
00341 %      void ResetTimer(TimerInfo *timer_info)
00342 %
00343 %  A description of each parameter follows.
00344 %
00345 %    o  timer_info: Timer statistics structure.
00346 %
00347 */
00348 WizardExport void ResetTimer(TimerInfo *timer_info)
00349 {
00350   assert(timer_info != (TimerInfo *) NULL);
00351   assert(timer_info->signature == WizardSignature);
00352   StopTimer(timer_info);
00353   timer_info->elapsed.stop=0.0;
00354   timer_info->user.stop=0.0;
00355 }
00356 
00357 /*
00358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00359 %                                                                             %
00360 %                                                                             %
00361 %                                                                             %
00362 +   S t a r t T i m e r                                                       %
00363 %                                                                             %
00364 %                                                                             %
00365 %                                                                             %
00366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00367 %
00368 %  StartTimer() starts the stopwatch.
00369 %
00370 %  The format of the StartTimer method is:
00371 %
00372 %      void StartTimer(TimerInfo *timer_info,const WizardBooleanType reset)
00373 %
00374 %  A description of each parameter follows.
00375 %
00376 %    o  timer_info: Timer statistics structure.
00377 %
00378 %    o  reset: If reset is WizardTrue, then the stopwatch is reset prior to
00379 %       starting.  If reset is WizardFalse, then timing is continued without
00380 %       resetting the stopwatch.
00381 %
00382 */
00383 WizardExport void StartTimer(TimerInfo *timer_info,
00384   const WizardBooleanType reset)
00385 {
00386   assert(timer_info != (TimerInfo *) NULL);
00387   assert(timer_info->signature == WizardSignature);
00388   if (reset != WizardFalse)
00389     {
00390       /*
00391         Reset the stopwatch before starting it.
00392       */
00393       timer_info->user.total=0.0;
00394       timer_info->elapsed.total=0.0;
00395     }
00396   if (timer_info->state != RunningTimerState)
00397     {
00398       timer_info->elapsed.start=ElapsedTime();
00399       timer_info->user.start=UserTime();
00400     }
00401   timer_info->state=RunningTimerState;
00402 }
00403 
00404 /*
00405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00406 %                                                                             %
00407 %                                                                             %
00408 %                                                                             %
00409 +   S t o p T i m e r                                                         %
00410 %                                                                             %
00411 %                                                                             %
00412 %                                                                             %
00413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00414 %
00415 %  StopTimer() stops the stopwatch.
00416 %
00417 %  The format of the StopTimer method is:
00418 %
00419 %      void StopTimer(TimerInfo *timer_info)
00420 %
00421 %  A description of each parameter follows.
00422 %
00423 %    o  timer_info: Timer statistics structure.
00424 %
00425 */
00426 static void StopTimer(TimerInfo *timer_info)
00427 {
00428   assert(timer_info != (TimerInfo *) NULL);
00429   assert(timer_info->signature == WizardSignature);
00430   timer_info->elapsed.stop=ElapsedTime();
00431   timer_info->user.stop=UserTime();
00432   if (timer_info->state == RunningTimerState)
00433     {
00434       timer_info->user.total+=
00435         timer_info->user.stop-timer_info->user.start+WizardEpsilon;
00436       timer_info->elapsed.total+=
00437         timer_info->elapsed.stop-timer_info->elapsed.start+WizardEpsilon;
00438     }
00439   timer_info->state=StoppedTimerState;
00440 }
00441 
00442 /*
00443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00444 %                                                                             %
00445 %                                                                             %
00446 %                                                                             %
00447 +   U s e r T i m e                                                           %
00448 %                                                                             %
00449 %                                                                             %
00450 %                                                                             %
00451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00452 %
00453 %  UserTime() returns the total time the process has been scheduled (in
00454 %  seconds) since the last call to StartTimer().
00455 %
00456 %  The format of the UserTime method is:
00457 %
00458 %      double UserTime()
00459 %
00460 */
00461 static double UserTime(void)
00462 {
00463 #if defined(WIZARDSTOOLKIT_HAVE_TIMES)
00464   struct tms
00465     timer;
00466 
00467   (void) times(&timer);
00468   return((double) (timer.tms_utime+timer.tms_stime)/CLK_TCK);
00469 #else
00470 #if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00471   return(NTUserTime());
00472 #else
00473   return((double) clock()/CLK_TCK);
00474 #endif
00475 #endif
00476 }
Generated by  doxygen 1.6.2-20100208