2007-02-20 07:13:14 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-02-19 01:53:54 -05:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2007-02-20 07:13:14 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2007-02-20 07:13:14 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2007-02-20 07:13:14 -05:00
|
|
|
|
2011-10-06 11:39:00 -04:00
|
|
|
#include "tool_util.h"
|
2007-02-20 07:13:14 -05:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-14 05:27:12 -04:00
|
|
|
|
2008-05-09 12:31:51 -04:00
|
|
|
#if defined(WIN32) && !defined(MSDOS)
|
2007-02-20 07:13:14 -05:00
|
|
|
|
2020-01-24 03:34:52 -05:00
|
|
|
/* set in win32_init() */
|
2020-04-13 17:46:18 -04:00
|
|
|
extern LARGE_INTEGER tool_freq;
|
|
|
|
extern bool tool_isVistaOrGreater;
|
2020-01-24 03:34:52 -05:00
|
|
|
|
|
|
|
/* In case of bug fix this function has a counterpart in timeval.c */
|
2017-06-02 08:13:02 -04:00
|
|
|
struct timeval tvnow(void)
|
2007-02-20 07:13:14 -05:00
|
|
|
{
|
2008-05-09 12:31:51 -04:00
|
|
|
struct timeval now;
|
2020-04-13 17:46:18 -04:00
|
|
|
if(tool_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
|
2020-01-24 03:34:52 -05:00
|
|
|
LARGE_INTEGER count;
|
|
|
|
QueryPerformanceCounter(&count);
|
2020-04-13 17:46:18 -04:00
|
|
|
now.tv_sec = (long)(count.QuadPart / tool_freq.QuadPart);
|
|
|
|
now.tv_usec = (long)((count.QuadPart % tool_freq.QuadPart) * 1000000 /
|
|
|
|
tool_freq.QuadPart);
|
2020-01-24 03:34:52 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Disable /analyze warning that GetTickCount64 is preferred */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:28159)
|
2014-12-14 12:32:41 -05:00
|
|
|
#endif
|
2020-01-24 03:34:52 -05:00
|
|
|
DWORD milliseconds = GetTickCount();
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
now.tv_sec = (long)(milliseconds / 1000);
|
|
|
|
now.tv_usec = (long)((milliseconds % 1000) * 1000);
|
|
|
|
}
|
2008-05-09 12:31:51 -04:00
|
|
|
return now;
|
2007-02-20 07:13:14 -05:00
|
|
|
}
|
2008-05-09 12:31:51 -04:00
|
|
|
|
2008-05-11 22:04:21 -04:00
|
|
|
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
|
2008-05-09 12:31:51 -04:00
|
|
|
|
2017-06-02 08:13:02 -04:00
|
|
|
struct timeval tvnow(void)
|
2007-02-20 07:13:14 -05:00
|
|
|
{
|
2008-05-09 12:31:51 -04:00
|
|
|
/*
|
|
|
|
** clock_gettime() is granted to be increased monotonically when the
|
|
|
|
** monotonic clock is queried. Time starting point is unspecified, it
|
|
|
|
** could be the system start-up time, the Epoch, or something else,
|
|
|
|
** in any case the time starting point does not change once that the
|
|
|
|
** system has started up.
|
|
|
|
*/
|
|
|
|
struct timeval now;
|
|
|
|
struct timespec tsnow;
|
2008-07-10 03:16:45 -04:00
|
|
|
if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
|
|
|
|
now.tv_sec = tsnow.tv_sec;
|
|
|
|
now.tv_usec = tsnow.tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
** Even when the configure process has truly detected monotonic clock
|
|
|
|
** availability, it might happen that it is not actually available at
|
|
|
|
** run-time. When this occurs simply fallback to other time source.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_GETTIMEOFDAY
|
|
|
|
else
|
|
|
|
(void)gettimeofday(&now, NULL);
|
|
|
|
#else
|
|
|
|
else {
|
|
|
|
now.tv_sec = (long)time(NULL);
|
|
|
|
now.tv_usec = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-09 12:31:51 -04:00
|
|
|
return now;
|
2007-02-20 07:13:14 -05:00
|
|
|
}
|
|
|
|
|
2008-05-09 12:31:51 -04:00
|
|
|
#elif defined(HAVE_GETTIMEOFDAY)
|
|
|
|
|
2017-06-02 08:13:02 -04:00
|
|
|
struct timeval tvnow(void)
|
2007-02-20 07:13:14 -05:00
|
|
|
{
|
2008-05-09 12:31:51 -04:00
|
|
|
/*
|
|
|
|
** gettimeofday() is not granted to be increased monotonically, due to
|
|
|
|
** clock drifting and external source time synchronization it can jump
|
|
|
|
** forward or backward in time.
|
|
|
|
*/
|
2007-02-20 07:13:14 -05:00
|
|
|
struct timeval now;
|
|
|
|
(void)gettimeofday(&now, NULL);
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
2008-05-09 12:31:51 -04:00
|
|
|
#else
|
|
|
|
|
2017-06-02 08:13:02 -04:00
|
|
|
struct timeval tvnow(void)
|
2008-05-09 12:31:51 -04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
** time() returns the value of time in seconds since the Epoch.
|
|
|
|
*/
|
|
|
|
struct timeval now;
|
|
|
|
now.tv_sec = (long)time(NULL);
|
|
|
|
now.tv_usec = 0;
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-02-20 07:13:14 -05:00
|
|
|
/*
|
|
|
|
* Make sure that the first argument is the more recent time, as otherwise
|
|
|
|
* we'll get a weird negative time-diff back...
|
|
|
|
*
|
|
|
|
* Returns: the time difference in number of milliseconds.
|
|
|
|
*/
|
2017-06-02 08:13:02 -04:00
|
|
|
long tvdiff(struct timeval newer, struct timeval older)
|
2007-02-20 07:13:14 -05:00
|
|
|
{
|
2017-05-20 13:39:51 -04:00
|
|
|
return (long)(newer.tv_sec-older.tv_sec)*1000+
|
|
|
|
(long)(newer.tv_usec-older.tv_usec)/1000;
|
2007-02-20 07:13:14 -05:00
|
|
|
}
|