43#include "MagickCore/studio.h"
44#include "MagickCore/exception.h"
45#include "MagickCore/exception-private.h"
46#include "MagickCore/image-private.h"
47#include "MagickCore/locale_.h"
48#include "MagickCore/log.h"
49#include "MagickCore/memory_.h"
50#include "MagickCore/memory-private.h"
51#include "MagickCore/nt-base-private.h"
52#include "MagickCore/registry.h"
53#include "MagickCore/resource_.h"
54#include "MagickCore/string-private.h"
55#include "MagickCore/timer.h"
56#include "MagickCore/timer-private.h"
61#if !defined(CLOCKS_PER_SEC)
62#define CLOCKS_PER_SEC 100
99MagickExport
TimerInfo *AcquireTimerInfo(
void)
104 timer_info=(
TimerInfo *) AcquireCriticalMemory(
sizeof(*timer_info));
105 (void) memset(timer_info,0,
sizeof(*timer_info));
106 timer_info->signature=MagickCoreSignature;
107 GetTimerInfo(timer_info);
134MagickExport MagickBooleanType ContinueTimer(
TimerInfo *time_info)
137 assert(time_info->signature == MagickCoreSignature);
138 if (time_info->state == UndefinedTimerState)
140 if (time_info->state == StoppedTimerState)
142 time_info->user.total-=time_info->user.stop-time_info->user.start;
143 time_info->elapsed.total-=time_info->elapsed.stop-
144 time_info->elapsed.start;
146 time_info->state=RunningTimerState;
174 assert(timer_info != (
TimerInfo *) NULL);
175 assert(timer_info->signature == MagickCoreSignature);
176 timer_info->signature=(~MagickCoreSignature);
177 timer_info=(
TimerInfo *) RelinquishMagickMemory(timer_info);
200static double ElapsedTime(
void)
202#if defined(MAGICKCORE_HAVE_CLOCK_GETTIME)
203#define NANOSECONDS_PER_SECOND 1000000000.0
204#if defined(CLOCK_HIGHRES)
205# define CLOCK_ID CLOCK_HIGHRES
206#elif defined(CLOCK_MONOTONIC_RAW)
207# define CLOCK_ID CLOCK_MONOTONIC_RAW
208#elif defined(CLOCK_MONOTONIC_PRECISE)
209# define CLOCK_ID CLOCK_MONOTONIC_PRECISE
210#elif defined(CLOCK_MONOTONIC)
211# define CLOCK_ID CLOCK_MONOTONIC
213# define CLOCK_ID CLOCK_REALTIME
219 (void) clock_gettime(CLOCK_ID,&timer);
220 return((
double) timer.tv_sec+timer.tv_nsec/NANOSECONDS_PER_SECOND);
221#elif defined(MAGICKCORE_HAVE_TIMES) && defined(MAGICKCORE_HAVE_SYSCONF)
225 return((
double) times(&timer)/sysconf(_SC_CLK_TCK));
227#if defined(MAGICKCORE_WINDOWS_SUPPORT)
228 return(NTElapsedTime());
230 return((
double) clock()/CLOCKS_PER_SEC);
264MagickExport ssize_t FormatMagickTime(
const time_t time,
const size_t length,
273 assert(timestamp != (
char *) NULL);
274 if (date_precision == -1)
280 limit=GetEnvironmentValue(
"MAGICK_DATE_PRECISION");
281 if (limit != (
char *) NULL)
283 date_precision=StringToInteger(limit);
284 limit=DestroyString(limit);
287 GetMagickUTCTime(&time,&utc_time);
288 count=FormatLocaleString(timestamp,length,
289 "%04d-%02d-%02dT%02d:%02d:%02d%+03d:00",utc_time.tm_year+1900,
290 utc_time.tm_mon+1,utc_time.tm_mday,utc_time.tm_hour,utc_time.tm_min,
292 if ((date_precision > 0) && (date_precision < (ssize_t) strlen(timestamp)))
293 timestamp[date_precision]=
'\0';
321MagickExport
double GetElapsedTime(
TimerInfo *time_info)
324 assert(time_info->signature == MagickCoreSignature);
325 if (time_info->state == UndefinedTimerState)
327 if (time_info->state == RunningTimerState)
328 StopTimer(time_info);
329 return(time_info->elapsed.total);
350MagickExport time_t GetMagickTime(
void)
353 magick_epoch = (time_t) 0;
355 static MagickBooleanType
356 epoch_initialized = MagickFalse;
358 if (epoch_initialized == MagickFalse)
363 source_date_epoch=getenv(
"SOURCE_DATE_EPOCH");
364 if (source_date_epoch != (
const char *) NULL)
369 epoch=(time_t) StringToMagickOffsetType(source_date_epoch,100.0);
370 if ((epoch > 0) && (epoch <= time((time_t *) NULL)))
373 epoch_initialized=MagickTrue;
375 if (magick_epoch != 0)
376 return(magick_epoch);
377 return(time((time_t *) NULL));
402MagickExport
void GetTimerInfo(
TimerInfo *time_info)
408 (void) memset(time_info,0,
sizeof(*time_info));
409 time_info->state=UndefinedTimerState;
410 time_info->signature=MagickCoreSignature;
411 StartTimer(time_info,MagickTrue);
438MagickExport
double GetUserTime(
TimerInfo *time_info)
441 assert(time_info->signature == MagickCoreSignature);
442 if (time_info->state == UndefinedTimerState)
444 if (time_info->state == RunningTimerState)
445 StopTimer(time_info);
446 return(time_info->user.total);
471MagickExport
void ResetTimer(
TimerInfo *time_info)
474 assert(time_info->signature == MagickCoreSignature);
475 StopTimer(time_info);
476 time_info->elapsed.stop=0.0;
477 time_info->user.stop=0.0;
502MagickPrivate
void SetMagickDatePrecision(
const unsigned long precision)
504 date_precision=(ssize_t) precision;
533MagickExport
void StartTimer(
TimerInfo *time_info,
const MagickBooleanType reset)
536 assert(time_info->signature == MagickCoreSignature);
537 if (reset != MagickFalse)
542 time_info->user.total=0.0;
543 time_info->elapsed.total=0.0;
545 if (time_info->state != RunningTimerState)
547 time_info->elapsed.start=ElapsedTime();
548 time_info->user.start=UserTime();
550 time_info->state=RunningTimerState;
575static void StopTimer(
TimerInfo *time_info)
578 assert(time_info->signature == MagickCoreSignature);
579 time_info->elapsed.stop=ElapsedTime();
580 time_info->user.stop=UserTime();
581 if (time_info->state == RunningTimerState)
583 time_info->user.total+=time_info->user.stop-
584 time_info->user.start+MagickEpsilon;
585 time_info->elapsed.total+=time_info->elapsed.stop-
586 time_info->elapsed.start+MagickEpsilon;
588 time_info->state=StoppedTimerState;
610static double UserTime(
void)
612#if defined(MAGICKCORE_HAVE_TIMES) && defined(MAGICKCORE_HAVE_SYSCONF)
616 (void) times(&timer);
617 return((
double) (timer.tms_utime+timer.tms_stime)/sysconf(_SC_CLK_TCK));
619#if defined(MAGICKCORE_WINDOWS_SUPPORT)
620 return(NTElapsedTime());
622 return((
double) clock()/CLOCKS_PER_SEC);