132 lines
5.4 KiB
C++
132 lines
5.4 KiB
C++
/***
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AuClocks.cpp
|
|
Date: 2023-3-21
|
|
Author: Reece
|
|
***/
|
|
#include <RuntimeInternal.hpp>
|
|
#include "AuClocks.hpp"
|
|
|
|
namespace Aurora::Time
|
|
{
|
|
#define ADD_CLOCK(name, enum, Frequency, getNs, getMs) \
|
|
struct name ## Clock_t : IClock \
|
|
{ \
|
|
EClock GetType() override \
|
|
{ \
|
|
return EClock::enum; \
|
|
} \
|
|
\
|
|
AuUInt64 GetHertz() override \
|
|
{ \
|
|
return Frequency(); \
|
|
} \
|
|
\
|
|
AuUInt64 NowNS() override \
|
|
{ \
|
|
return getNs(); \
|
|
} \
|
|
\
|
|
AuUInt64 NowMS() override \
|
|
{ \
|
|
return getMs(); \
|
|
} \
|
|
}; \
|
|
\
|
|
static name ## Clock_t gClock## name ## Instance; \
|
|
\
|
|
AUKN_SYM AuSPtr<IClock> Get ## name ## Clock() \
|
|
{ \
|
|
return AuUnsafeRaiiToShared(&gClock## name ## Instance); \
|
|
}
|
|
|
|
#define FILE_AND_USR_DIR_STEP (AuMSToNS<AuUInt64>(AuSToMS<AuUInt64>(1)) / 100ull)
|
|
|
|
ADD_CLOCK(Steady, eSteady, SteadyClockFrequency, SteadyClockNS, SteadyClockMS);
|
|
ADD_CLOCK(Wall, eWall, FILE_AND_USR_DIR_STEP + AuUInt64, CurrentClockNS, CurrentClockMS);
|
|
ADD_CLOCK(Process, eProcessTime, ProcessClockFrequency, ProcessClockNS, ProcessClockMS);
|
|
ADD_CLOCK(ProcessKernel, eProcessKernelTime, ProcessKernelClockFrequency, ProcessKernelClockNS, ProcessKernelClockMS);
|
|
ADD_CLOCK(ProcessUser, eProcessUserTime, ProcessUserClockFrequency, ProcessUserClockNS, ProcessUserClockMS);
|
|
ADD_CLOCK(Thread, eThreadTime, ThreadClockFrequency, ThreadClockNS, ThreadClockMS);
|
|
ADD_CLOCK(ThreadKernel, eThreadKernelTime, ThreadKernelClockFrequency, ThreadKernelClockNS, ThreadKernelClockMS);
|
|
ADD_CLOCK(ThreadUser, eThreadUserTime, ThreadUserClockFrequency, ThreadUserClockNS, ThreadUserClockMS);
|
|
|
|
#undef ADD_CLOCK
|
|
|
|
AUKN_SYM AuSPtr<IClock> GetClockFromEnum(EClock clock)
|
|
{
|
|
switch (clock)
|
|
{
|
|
case EClock::eWall:
|
|
return GetWallClock();
|
|
case EClock::eSteady:
|
|
return GetSteadyClock();
|
|
case EClock::eProcessTime:
|
|
return GetProcessClock();
|
|
case EClock::eProcessUserTime:
|
|
return GetProcessUserClock();
|
|
case EClock::eProcessKernelTime:
|
|
return GetProcessKernelClock();
|
|
case EClock::eThreadTime:
|
|
return GetThreadClock();
|
|
case EClock::eThreadUserTime:
|
|
return GetThreadUserClock();
|
|
case EClock::eThreadKernelTime:
|
|
return GetThreadKernelClock();
|
|
default:
|
|
SysPushErrorArg("Invalid clock");
|
|
return {};
|
|
}
|
|
}
|
|
|
|
AUKN_SYM AuUInt64 QueryClockTimeNS(EClock clock)
|
|
{
|
|
switch (clock)
|
|
{
|
|
case EClock::eWall:
|
|
return CurrentClockNS();
|
|
case EClock::eSteady:
|
|
return SteadyClockNS();
|
|
case EClock::eProcessTime:
|
|
return ProcessClockNS();
|
|
case EClock::eProcessUserTime:
|
|
return ProcessUserClockNS();
|
|
case EClock::eProcessKernelTime:
|
|
return ProcessKernelClockNS();
|
|
case EClock::eThreadTime:
|
|
return ThreadClockNS();
|
|
case EClock::eThreadUserTime:
|
|
return ThreadUserClockNS();
|
|
case EClock::eThreadKernelTime:
|
|
return ThreadKernelClockNS();
|
|
default:
|
|
SysPushErrorArg("Invalid clock");
|
|
return {};
|
|
}
|
|
}
|
|
|
|
AUKN_SYM AuUInt64 QueryClockFrequency(EClock clock)
|
|
{
|
|
switch (clock)
|
|
{
|
|
case EClock::eWall:
|
|
return FILE_AND_USR_DIR_STEP;
|
|
case EClock::eSteady:
|
|
return SteadyClockFrequency();
|
|
case EClock::eProcessTime:
|
|
case EClock::eProcessUserTime:
|
|
case EClock::eProcessKernelTime:
|
|
return ProcessClockFrequency();
|
|
case EClock::eThreadTime:
|
|
case EClock::eThreadUserTime:
|
|
case EClock::eThreadKernelTime:
|
|
return ThreadClockFrequency();
|
|
default:
|
|
SysPushErrorArg("Invalid clock");
|
|
return {};
|
|
}
|
|
}
|
|
|
|
#undef FILE_AND_USR_DIR_STEP
|
|
} |