mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 01:07:57 +00:00
Make fromStartup compile on mac
This commit is contained in:
@@ -257,70 +257,93 @@ std::string RelativeTime::to_string () const
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
#if BEAST_WINDOWS
|
#if BEAST_WINDOWS
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
namespace beast {
|
uint32 millisecondsSinceStartup()
|
||||||
|
|
||||||
RelativeTime RelativeTime::fromStartup ()
|
|
||||||
{
|
{
|
||||||
ULONGLONG ticks (GetTickCount64());
|
return (uint32) GetTickCount64();
|
||||||
|
|
||||||
return RelativeTime (ticks / 1000.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
#elif BEAST_MAC || BEAST_IOS
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
namespace beast {
|
uint32 millisecondsSinceStartup()
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
// Converts a timespec to a RelativeTme
|
|
||||||
static RelativeTime toRelativeTime (timespec const& ts)
|
|
||||||
{
|
{
|
||||||
return RelativeTime (ts.tv_sec +
|
uint64 numerator, denominator;
|
||||||
ts.tv_nsec / 1000000000.0);
|
|
||||||
|
mach_timebase_info_data_t timebase;
|
||||||
|
(void) mach_timebase_info (&timebase);
|
||||||
|
|
||||||
|
if (timebase.numer % 1000000 == 0)
|
||||||
|
{
|
||||||
|
numerator = timebase.numer / 1000000;
|
||||||
|
denominator = timebase.denom;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
numerator = timebase.numer;
|
||||||
|
denominator = timebase.denom * (uint64) 1000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (uint32) ((mach_absolute_time() * numerator) / denominator);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
uint32 millisecondsSinceStartup()
|
||||||
|
{
|
||||||
|
timespec t;
|
||||||
|
clock_gettime (CLOCK_MONOTONIC, &t);
|
||||||
|
return t.tv_sec * 1000 + t.tv_nsec / 1000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Converts milliseconds to a RelativeTme
|
||||||
|
static RelativeTime toRelativeTime (uint32 ms)
|
||||||
|
{
|
||||||
|
return RelativeTime (ms / 1000.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Records and returns the time from process startup
|
// Records and returns the time from process startup
|
||||||
static RelativeTime getStartupTime()
|
static RelativeTime getStartupTime()
|
||||||
{
|
{
|
||||||
struct StartupTime
|
struct StartupTime
|
||||||
{
|
{
|
||||||
StartupTime ()
|
StartupTime ()
|
||||||
{ clock_gettime (CLOCK_MONOTONIC, &ts); }
|
{ s = toRelativeTime (detail::millisecondsSinceStartup()); }
|
||||||
timespec ts;
|
RelativeTime s;
|
||||||
};
|
};
|
||||||
|
|
||||||
static StartupTime startupTime;
|
static StartupTime startupTime;
|
||||||
|
|
||||||
return toRelativeTime (startupTime.ts);
|
return startupTime.s;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to call getStartupTime as early as possible
|
// Used to call getStartupTime as early as possible
|
||||||
struct StartupTimeStaticInitializer
|
struct StartupTimeStaticInitializer
|
||||||
{
|
{
|
||||||
StartupTimeStaticInitializer ()
|
StartupTimeStaticInitializer ()
|
||||||
{ getStartupTime(); }
|
{ getStartupTime(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
static StartupTimeStaticInitializer startupTimeStaticInitializer;
|
static StartupTimeStaticInitializer startupTimeStaticInitializer;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RelativeTime RelativeTime::fromStartup ()
|
RelativeTime RelativeTime::fromStartup ()
|
||||||
{
|
{
|
||||||
timespec ts;
|
return detail::toRelativeTime (detail::millisecondsSinceStartup()) - detail::getStartupTime();
|
||||||
clock_gettime (CLOCK_MONOTONIC, &ts);
|
|
||||||
|
|
||||||
return detail::toRelativeTime (ts) - detail::getStartupTime();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user