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 %                         MagickCore Timing Methods                           %
00014 %                                                                             %
00015 %                             Software Design                                 %
00016 %                               John Cristy                                   %
00017 %                              January 1993                                   %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2009 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.imagemagick.org/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 "magick/studio.h"
00044 #include "magick/exception.h"
00045 #include "magick/exception-private.h"
00046 #include "magick/log.h"
00047 #include "magick/memory_.h"
00048 #include "magick/timer.h"
00049 
00050 /*
00051   Define declarations.
00052 */
00053 #if defined(macintosh)
00054 #define CLK_TCK  CLOCKS_PER_SEC
00055 #endif
00056 #if !defined(CLK_TCK)
00057 #define CLK_TCK  sysconf(_SC_CLK_TCK)
00058 #endif
00059 
00060 /*
00061   Forward declarations.
00062 */
00063 static double
00064   UserTime(void);
00065 
00066 static void
00067   StopTimer(TimerInfo *);
00068 
00069 /*
00070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00071 %                                                                             %
00072 %                                                                             %
00073 %                                                                             %
00074 %   A c q u i r e T i m e r I n f o                                           %
00075 %                                                                             %
00076 %                                                                             %
00077 %                                                                             %
00078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00079 %
00080 %  AcquireTimerInfo() initializes the TimerInfo structure.  It effectively
00081 %  creates a stopwatch and starts it.
00082 %
00083 %  The format of the AcquireTimerInfo method is:
00084 %
00085 %      TimerInfo *AcquireTimerInfo(void)
00086 %
00087 */
00088 MagickExport TimerInfo *AcquireTimerInfo(void)
00089 {
00090   TimerInfo
00091     *timer_info;
00092 
00093   timer_info=(TimerInfo *) AcquireMagickMemory(sizeof(*timer_info));
00094   if (timer_info == (TimerInfo *) NULL)
00095     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
00096   (void) ResetMagickMemory(timer_info,0,sizeof(*timer_info));
00097   timer_info->signature=MagickSignature;
00098   GetTimerInfo(timer_info);
00099   return(timer_info);
00100 }
00101 
00102 /*
00103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00104 %                                                                             %
00105 %                                                                             %
00106 %                                                                             %
00107 %   C o n t i n u e T i m e r                                                 %
00108 %                                                                             %
00109 %                                                                             %
00110 %                                                                             %
00111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00112 %
00113 %  ContinueTimer() resumes a stopped stopwatch. The stopwatch continues
00114 %  counting from the last StartTimer() onwards.
00115 %
00116 %  The format of the ContinueTimer method is:
00117 %
00118 %      MagickBooleanType ContinueTimer(TimerInfo *time_info)
00119 %
00120 %  A description of each parameter follows.
00121 %
00122 %    o  time_info: Time statistics structure.
00123 %
00124 */
00125 MagickExport MagickBooleanType ContinueTimer(TimerInfo *time_info)
00126 {
00127   assert(time_info != (TimerInfo *) NULL);
00128   assert(time_info->signature == MagickSignature);
00129   if (time_info->state == UndefinedTimerState)
00130     return(MagickFalse);
00131   if (time_info->state == StoppedTimerState)
00132     {
00133       time_info->user.total-=time_info->user.stop-time_info->user.start;
00134       time_info->elapsed.total-=time_info->elapsed.stop-
00135         time_info->elapsed.start;
00136     }
00137   time_info->state=RunningTimerState;
00138   return(MagickTrue);
00139 }
00140 
00141 /*
00142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00143 %                                                                             %
00144 %                                                                             %
00145 %                                                                             %
00146 %   D e s t r o y T i m e r I n f o                                           %
00147 %                                                                             %
00148 %                                                                             %
00149 %                                                                             %
00150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00151 %
00152 %  DestroyTimerInfo() zeros memory associated with the TimerInfo structure.
00153 %
00154 %  The format of the DestroyTimerInfo method is:
00155 %
00156 %      TimerInfo *DestroyTimerInfo(TimerInfo *timer_info)
00157 %
00158 %  A description of each parameter follows:
00159 %
00160 %    o timer_info: The cipher context.
00161 %
00162 */
00163 MagickExport TimerInfo *DestroyTimerInfo(TimerInfo *timer_info)
00164 {
00165   assert(timer_info != (TimerInfo *) NULL);
00166   assert(timer_info->signature == MagickSignature);
00167   timer_info->signature=(~MagickSignature);
00168   timer_info=(TimerInfo *) RelinquishMagickMemory(timer_info);
00169   return(timer_info);
00170 }
00171 
00172 /*
00173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00174 %                                                                             %
00175 %                                                                             %
00176 %                                                                             %
00177 +   E l a p s e d T i m e                                                     %
00178 %                                                                             %
00179 %                                                                             %
00180 %                                                                             %
00181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00182 %
00183 %  ElapsedTime() returns the elapsed time (in seconds) since the last call to
00184 %  StartTimer().
00185 %
00186 %  The format of the ElapsedTime method is:
00187 %
00188 %      double ElapsedTime()
00189 %
00190 */
00191 static double ElapsedTime(void)
00192 {
00193 #if defined(MAGICKCORE_HAVE_TIMES)
00194   struct tms
00195     timer;
00196 
00197   return((double) times(&timer)/CLK_TCK);
00198 #else
00199 #if defined(__WINDOWS__)
00200   return(NTElapsedTime());
00201 #else
00202   return((double) clock()/CLK_TCK);
00203 #endif
00204 #endif
00205 }
00206 
00207 /*
00208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00209 %                                                                             %
00210 %                                                                             %
00211 %                                                                             %
00212 %   G e t E l a p s e d T i m e                                               %
00213 %                                                                             %
00214 %                                                                             %
00215 %                                                                             %
00216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00217 %
00218 %  GetElapsedTime() returns the elapsed time (in seconds) passed between the
00219 %  start and stop events. If the stopwatch is still running, it is stopped
00220 %  first.
00221 %
00222 %  The format of the GetElapsedTime method is:
00223 %
00224 %      double GetElapsedTime(TimerInfo *time_info)
00225 %
00226 %  A description of each parameter follows.
00227 %
00228 %    o  time_info: Timer statistics structure.
00229 %
00230 */
00231 MagickExport double GetElapsedTime(TimerInfo *time_info)
00232 {
00233   assert(time_info != (TimerInfo *) NULL);
00234   assert(time_info->signature == MagickSignature);
00235   if (time_info->state == UndefinedTimerState)
00236     return(0.0);
00237   if (time_info->state == RunningTimerState)
00238     StopTimer(time_info);
00239   return(time_info->elapsed.total);
00240 }
00241 
00242 /*
00243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00244 %                                                                             %
00245 %                                                                             %
00246 %                                                                             %
00247 +   G e t T i m e r I n f o                                                   %
00248 %                                                                             %
00249 %                                                                             %
00250 %                                                                             %
00251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00252 %
00253 %  GetTimerInfo() initializes the TimerInfo structure.
00254 %
00255 %  The format of the GetTimerInfo method is:
00256 %
00257 %      void GetTimerInfo(TimerInfo *time_info)
00258 %
00259 %  A description of each parameter follows.
00260 %
00261 %    o  time_info: Timer statistics structure.
00262 %
00263 */
00264 MagickExport void GetTimerInfo(TimerInfo *time_info)
00265 {
00266   /*
00267     Create a stopwatch and start it.
00268   */
00269   assert(time_info != (TimerInfo *) NULL);
00270   (void) ResetMagickMemory(time_info,0,sizeof(*time_info));
00271   time_info->state=UndefinedTimerState;
00272   time_info->signature=MagickSignature;
00273   StartTimer(time_info,MagickTrue);
00274 }
00275 
00276 /*
00277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00278 %                                                                             %
00279 %                                                                             %
00280 %                                                                             %
00281 %   G e t U s e r T i m e                                                     %
00282 %                                                                             %
00283 %                                                                             %
00284 %                                                                             %
00285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00286 %
00287 %  GetUserTime() returns the User time (user and system) by the operating
00288 %  system (in seconds) between the start and stop events. If the stopwatch is
00289 %  still running, it is stopped first.
00290 %
00291 %  The format of the GetUserTime method is:
00292 %
00293 %      double GetUserTime(TimerInfo *time_info)
00294 %
00295 %  A description of each parameter follows.
00296 %
00297 %    o  time_info: Timer statistics structure.
00298 %
00299 */
00300 MagickExport double GetUserTime(TimerInfo *time_info)
00301 {
00302   assert(time_info != (TimerInfo *) NULL);
00303   assert(time_info->signature == MagickSignature);
00304   if (time_info->state == UndefinedTimerState)
00305     return(0.0);
00306   if (time_info->state == RunningTimerState)
00307     StopTimer(time_info);
00308   return(time_info->user.total);
00309 }
00310 
00311 /*
00312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00313 %                                                                             %
00314 %                                                                             %
00315 %                                                                             %
00316 %   R e s e t T i m e r                                                       %
00317 %                                                                             %
00318 %                                                                             %
00319 %                                                                             %
00320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00321 %
00322 %  ResetTimer() resets the stopwatch.
00323 %
00324 %  The format of the ResetTimer method is:
00325 %
00326 %      void ResetTimer(TimerInfo *time_info)
00327 %
00328 %  A description of each parameter follows.
00329 %
00330 %    o  time_info: Timer statistics structure.
00331 %
00332 */
00333 MagickExport void ResetTimer(TimerInfo *time_info)
00334 {
00335   assert(time_info != (TimerInfo *) NULL);
00336   assert(time_info->signature == MagickSignature);
00337   StopTimer(time_info);
00338   time_info->elapsed.stop=0.0;
00339   time_info->user.stop=0.0;
00340 }
00341 
00342 /*
00343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00344 %                                                                             %
00345 %                                                                             %
00346 %                                                                             %
00347 +   S t a r t T i m e r                                                       %
00348 %                                                                             %
00349 %                                                                             %
00350 %                                                                             %
00351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00352 %
00353 %  StartTimer() starts the stopwatch.
00354 %
00355 %  The format of the StartTimer method is:
00356 %
00357 %      void StartTimer(TimerInfo *time_info,const MagickBooleanType reset)
00358 %
00359 %  A description of each parameter follows.
00360 %
00361 %    o  time_info: Timer statistics structure.
00362 %
00363 %    o  reset: If reset is MagickTrue, then the stopwatch is reset prior to
00364 %       starting.  If reset is MagickFalse, then timing is continued without
00365 %       resetting the stopwatch.
00366 %
00367 */
00368 MagickExport void StartTimer(TimerInfo *time_info,const MagickBooleanType reset)
00369 {
00370   assert(time_info != (TimerInfo *) NULL);
00371   assert(time_info->signature == MagickSignature);
00372   if (reset != MagickFalse)
00373     {
00374       /*
00375         Reset the stopwatch before starting it.
00376       */
00377       time_info->user.total=0.0;
00378       time_info->elapsed.total=0.0;
00379     }
00380   if (time_info->state != RunningTimerState)
00381     {
00382       time_info->elapsed.start=ElapsedTime();
00383       time_info->user.start=UserTime();
00384     }
00385   time_info->state=RunningTimerState;
00386 }
00387 
00388 /*
00389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00390 %                                                                             %
00391 %                                                                             %
00392 %                                                                             %
00393 +   S t o p T i m e r                                                         %
00394 %                                                                             %
00395 %                                                                             %
00396 %                                                                             %
00397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00398 %
00399 %  StopTimer() stops the stopwatch.
00400 %
00401 %  The format of the StopTimer method is:
00402 %
00403 %      void StopTimer(TimerInfo *time_info)
00404 %
00405 %  A description of each parameter follows.
00406 %
00407 %    o  time_info: Timer statistics structure.
00408 %
00409 */
00410 static void StopTimer(TimerInfo *time_info)
00411 {
00412   assert(time_info != (TimerInfo *) NULL);
00413   assert(time_info->signature == MagickSignature);
00414   time_info->elapsed.stop=ElapsedTime();
00415   time_info->user.stop=UserTime();
00416   if (time_info->state == RunningTimerState)
00417     {
00418       time_info->user.total+=time_info->user.stop-
00419         time_info->user.start+MagickEpsilon;
00420       time_info->elapsed.total+=time_info->elapsed.stop-
00421         time_info->elapsed.start+MagickEpsilon;
00422     }
00423   time_info->state=StoppedTimerState;
00424 }
00425 
00426 /*
00427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00428 %                                                                             %
00429 %                                                                             %
00430 %                                                                             %
00431 +   U s e r T i m e                                                           %
00432 %                                                                             %
00433 %                                                                             %
00434 %                                                                             %
00435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00436 %
00437 %  UserTime() returns the total time the process has been scheduled (in
00438 %  seconds) since the last call to StartTimer().
00439 %
00440 %  The format of the UserTime method is:
00441 %
00442 %      double UserTime()
00443 %
00444 */
00445 static double UserTime(void)
00446 {
00447 #if defined(MAGICKCORE_HAVE_TIMES)
00448   struct tms
00449     timer;
00450 
00451   (void) times(&timer);
00452   return((double) (timer.tms_utime+timer.tms_stime)/CLK_TCK);
00453 #else
00454 #if defined(__WINDOWS__)
00455   return(NTUserTime());
00456 #else
00457   return((double) clock()/CLK_TCK);
00458 #endif
00459 #endif
00460 }

Generated on 19 Nov 2009 for MagickCore by  doxygen 1.6.1