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