AuroraRuntime/Source/HWInfo/AuCpuLoadSampler.cpp

190 lines
5.7 KiB
C++
Raw Normal View History

2023-10-28 15:00:57 +00:00
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuCpuLoadSampler.cpp
2023-10-28 15:00:57 +00:00
Date: 2023-10-28
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuCpuLoadSampler.hpp"
namespace Aurora::HWInfo
{
static thread_local ProcessLoadSamplerImpl tlsThreadLocalUsageSamplers[2] {
ProcessLoadSamplerImpl(AuSToMS<AuUInt32>(1), true, false),
ProcessLoadSamplerImpl(AuSToMS<AuUInt32>(1), true, true)
};
static ProcessLoadSamplerImpl gProcessUsageSampler(AuSToMS<AuUInt32>(1), false, true);
ProcessLoadSamplerImpl::ProcessLoadSamplerImpl(AuUInt32 uMinSamplePeriodMS,
bool bThreadMode,
bool bCountKernelUsage) :
2023-10-28 15:00:57 +00:00
uMinSamplePeriod(uMinSamplePeriodMS),
bThreadMode(bThreadMode),
bCountKernelUsage(bCountKernelUsage)
2023-10-28 15:00:57 +00:00
{
}
ProcessLoadSamplerImpl::~ProcessLoadSamplerImpl()
2023-10-28 15:00:57 +00:00
{
}
double ProcessLoadSamplerImpl::GetLoad()
2023-10-28 15:00:57 +00:00
{
if (!this->bThreadMode)
{
return this->processState.GetLoad(this->uMinSamplePeriod, false, this->bCountKernelUsage);
2023-10-28 15:00:57 +00:00
}
else
{
return this->threadState->GetLoad(this->uMinSamplePeriod, true, this->bCountKernelUsage);
2023-10-28 15:00:57 +00:00
}
}
double ProcessLoadSamplerState::GetLoad(AuUInt32 uMinSamplePeriod, bool bThread, bool bCountKernelUsage)
2023-10-28 15:00:57 +00:00
{
AuUInt64 now[2] = {
AuTime::SteadyClockNS(),
bThread ?
(bCountKernelUsage ? AuTime::ThreadClockNS() : AuTime::ThreadUserClockNS()) :
(bCountKernelUsage ? AuTime::ProcessClockNS() : AuTime::ProcessUserClockNS())
2023-10-28 15:00:57 +00:00
};
double dDeltaSteady = now[0] - this->uPrevTimes[0];
double dDeltaProcess = now[1] - this->uPrevTimes[1];
double dUsage = 0;
double dMinSamplePeriod = double(AuMSToNS<AuUInt64>(uMinSamplePeriod));
2023-12-08 10:26:12 +00:00
double dDeltaSteady2;
double dThreads = 1.0;
if (bThread)
{
dThreads = GetCPUInfo().uThreads;
dDeltaSteady2 = dDeltaSteady * dThreads;
}
else
{
dDeltaSteady2 = dDeltaSteady;
}
2023-10-28 15:00:57 +00:00
2023-10-28 18:14:30 +00:00
if (!bool(this->uPrevTimes[0]))
{
this->uPrevTimes[0] = now[0];
2023-12-08 10:26:12 +00:00
this->uPrevTimes[1] = now[1];
2023-10-28 18:14:30 +00:00
return 0;
}
2023-10-28 15:00:57 +00:00
if (!uMinSamplePeriod ||
2023-12-08 10:26:12 +00:00
(dDeltaSteady >= dMinSamplePeriod))
2023-10-28 15:00:57 +00:00
{
2023-12-08 10:26:12 +00:00
dUsage = dDeltaProcess / dDeltaSteady2;
2023-10-28 15:00:57 +00:00
this->uPrevTimes[1] = now[1];
this->uPrevTimes[0] = now[0];
2023-12-08 10:26:12 +00:00
if (uMinSamplePeriod)
{
if (dUsage > AuNumericLimits<double>::epsilon() &&
this->dPrevLoad > AuNumericLimits<double>::epsilon())
{
dUsage = AuMin(dUsage, dUsage + this->dPrevLoad / 2.0);
}
else
{
dUsage = AuMax(this->dPrevLoad / 2.0, 0.0001);
}
}
else if (!dUsage)
{
dUsage = AuMax(this->dPrevLoad / 2.0, 0.0001);
}
2023-10-28 15:00:57 +00:00
this->dPrevLoad = dUsage;
2023-12-08 10:26:12 +00:00
this->bSet = true;
2023-10-28 15:00:57 +00:00
}
2023-12-08 10:26:12 +00:00
else if (!dDeltaProcess &&
!this->bSet)
{
// well, we obviously have some uptime. but how much?
auto uDeltaNS = AuMin(dDeltaSteady, dMinSamplePeriod);
if (uDeltaNS > AuMSToNS<double>(1000 / 250))
{
uDeltaNS = 100;
}
dUsage = uDeltaNS / dDeltaSteady2;
}
else if (dDeltaProcess &&
!this->bSet)
{
dUsage = dDeltaProcess / dDeltaSteady2;
}
else if (dDeltaProcess)
2023-10-28 15:00:57 +00:00
{
2023-12-08 10:26:12 +00:00
dUsage = dDeltaProcess / dDeltaSteady2;
2023-10-28 15:00:57 +00:00
#if 0
2023-12-08 10:26:12 +00:00
dUsage *= dDeltaSteady2 / double(dMinSamplePeriod);
2023-10-28 15:00:57 +00:00
if (this->dPrevLoad) dUsage = dUsage + this->dPrevLoad / 2.0;
#else
if (this->dPrevLoad)
{
2023-12-08 10:26:12 +00:00
auto dFrameDelta = dDeltaSteady / dMinSamplePeriod;
auto dFrameDeltaInverse = 1.0 - dFrameDelta;
2023-10-28 15:00:57 +00:00
dUsage *= dFrameDelta;
dUsage += this->dPrevLoad * dFrameDeltaInverse;
}
else
{
2023-12-08 10:26:12 +00:00
dUsage *= dDeltaSteady / dMinSamplePeriod;
2023-10-28 15:00:57 +00:00
}
#endif
}
2023-12-08 10:26:12 +00:00
else
{
dUsage = this->dPrevLoad;
}
2023-10-28 15:00:57 +00:00
{
dUsage = dUsage * 100.0;
if (dUsage > 100.0)
{
return 100.0;
}
else if (dUsage < 0.0)
{
return 0.0;
}
else
{
return dUsage;
}
}
}
2023-10-28 15:10:25 +00:00
AUKN_SYM double GetProcessCPUUtilization()
{
return gProcessUsageSampler.GetLoad();
}
AUKN_SYM double GetThreadCPUUtilization(AuOptional<bool> optIncludeKernel)
{
return tlsThreadLocalUsageSamplers[optIncludeKernel.value_or(true)].GetLoad();
2023-10-28 15:10:25 +00:00
}
AUKN_SYM IProcessLoadSampler *ProcessLoadSamplerNew(AuUInt32 uMinSamplePeriodMS,
bool bThreadMode,
bool bCountKernelUsage)
2023-10-28 15:00:57 +00:00
{
return _new ProcessLoadSamplerImpl(uMinSamplePeriodMS, bThreadMode, bCountKernelUsage);
2023-10-28 15:00:57 +00:00
}
AUKN_SYM void ProcessLoadSamplerRelease(IProcessLoadSampler *pEvent)
2023-10-28 15:00:57 +00:00
{
AuSafeDelete<ProcessLoadSamplerImpl *>(pEvent);
2023-10-28 15:00:57 +00:00
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, ProcessLoadSampler, ProcessLoadSamplerImpl, (AuUInt32, uMinSamplePeriod), (bool, bThreadMode), (bool, bCountKernelUsage))
2023-10-28 15:00:57 +00:00
}