From 1f685b635b882df22019ba9397da50b65928a621 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Mon, 23 Oct 2023 09:04:36 +0100 Subject: [PATCH] [+] ISocketStats::GetUptimeNS [*] Some socket stat cleanup+fixes --- Include/Aurora/IO/Net/ISocketStats.hpp | 6 ++++ Source/IO/Net/AuNetSocket.cpp | 10 ++++++ Source/IO/Net/AuNetSocketChannel.cpp | 13 +++++++ Source/IO/Net/AuNetSocketChannel.hpp | 3 ++ Source/IO/Net/AuSocketStats.cpp | 47 ++++++++++++++++++++------ Source/IO/Net/AuSocketStats.hpp | 11 ++++-- 6 files changed, 76 insertions(+), 14 deletions(-) diff --git a/Include/Aurora/IO/Net/ISocketStats.hpp b/Include/Aurora/IO/Net/ISocketStats.hpp index 347f025d..92b00bc7 100644 --- a/Include/Aurora/IO/Net/ISocketStats.hpp +++ b/Include/Aurora/IO/Net/ISocketStats.hpp @@ -34,5 +34,11 @@ namespace Aurora::IO::Net * @return bytes / second */ virtual double GetApproximatedThroughput() = 0; + + /** + * @brief + * @return + */ + virtual AuUInt64 GetUptimeNS() = 0; }; } \ No newline at end of file diff --git a/Source/IO/Net/AuNetSocket.cpp b/Source/IO/Net/AuNetSocket.cpp index 49fa93d2..1a0f201a 100644 --- a/Source/IO/Net/AuNetSocket.cpp +++ b/Source/IO/Net/AuNetSocket.cpp @@ -339,6 +339,8 @@ namespace Aurora::IO::Net this->RejectAllListeners(); + this->socketChannel_.StopTime(); + if (this->pSocketDriver_) { try @@ -363,6 +365,8 @@ namespace Aurora::IO::Net this->error_ = error; + this->socketChannel_.StopTime(); + if (this->pSocketDriver_) { try @@ -544,6 +548,8 @@ namespace Aurora::IO::Net this->socketChannel_.uBytesInputBuffer = pServer->uDefaultInputStreamSize; } + this->socketChannel_.StartTime(); + this->socketChannel_.inputChannel.WarmOnEstablish(); // Allocate stream resources, in case we need to start working with the source buffer // think: setting up protocol stacks, accessing the base bytebuffer, without the pipe being // allocated already @@ -570,6 +576,8 @@ namespace Aurora::IO::Net return; } + this->socketChannel_.StopTime(); + if (this->pSocketDriver_) { try @@ -592,6 +600,8 @@ namespace Aurora::IO::Net return; } + this->socketChannel_.StopTime(); + this->pWorker_->RemoveSocket(this); if (this->bHasErrored_) diff --git a/Source/IO/Net/AuNetSocketChannel.cpp b/Source/IO/Net/AuNetSocketChannel.cpp index df0e6415..625b67a4 100644 --- a/Source/IO/Net/AuNetSocketChannel.cpp +++ b/Source/IO/Net/AuNetSocketChannel.cpp @@ -525,9 +525,22 @@ namespace Aurora::IO::Net return true; } + void SocketChannel::StopTime() + { + this->sendStats_.End(); + this->recvStats_.End(); + } + + void SocketChannel::StartTime() + { + this->sendStats_.Start(); + this->recvStats_.Start(); + } void SocketChannel::Release() { + this->StopTime(); + this->PrivateUserDataClear(); this->pCachedReader.reset(); diff --git a/Source/IO/Net/AuNetSocketChannel.hpp b/Source/IO/Net/AuNetSocketChannel.hpp index 12692799..04fa0f2d 100644 --- a/Source/IO/Net/AuNetSocketChannel.hpp +++ b/Source/IO/Net/AuNetSocketChannel.hpp @@ -119,6 +119,9 @@ namespace Aurora::IO::Net AuThreadPrimitives::SpinLock spinLock; AuList> eventListeners; + void StopTime(); + void StartTime(); + void Release(); private: diff --git a/Source/IO/Net/AuSocketStats.cpp b/Source/IO/Net/AuSocketStats.cpp index 3771956b..f2d719ef 100644 --- a/Source/IO/Net/AuSocketStats.cpp +++ b/Source/IO/Net/AuSocketStats.cpp @@ -12,26 +12,18 @@ namespace Aurora::IO::Net { void SocketStats::AddBytes(AuUInt32 uBytes) { + this->Start(); this->calculator.AddData(uBytes); - - auto uNow = AuTime::CurrentClockMS(); - if (!this->iFirstTime) - { - this->iFirstTime = uNow; - } - - this->iLastTime = uNow; - this->uLastTimeSteadyMS = AuTime::SteadyClockMS(); } AuInt64 SocketStats::GetFirstTickTimeMS() { - return this->iFirstTime; + return this->uFirstTime; } AuInt64 SocketStats::GetLastTickTimeMS() { - return this->iLastTime; + return this->calculator.GetLastFrameTimeWall(); } AuUInt64 SocketStats::GetTotalBytesTransferred() @@ -43,4 +35,37 @@ namespace Aurora::IO::Net { return this->calculator.GetEstimatedHertz(); } + + AuUInt64 SocketStats::GetUptimeNS() + { + if (auto uStartTime = this->uStartSteadyTime) + { + if (auto uEndTime = this->uEndSteadyTime) + { + return uEndTime - uStartTime; + } + else + { + return AuTime::SteadyClockNS() - uStartTime; + } + } + else + { + return 0; + } + } + + void SocketStats::Start() + { + if (!this->uFirstTime) + { + this->uFirstTime = AuTime::CurrentClockMS(); + this->uStartSteadyTime = AuTime::SteadyClockNS(); + } + } + + void SocketStats::End() + { + this->uEndSteadyTime = AuTime::SteadyClockNS(); + } } \ No newline at end of file diff --git a/Source/IO/Net/AuSocketStats.hpp b/Source/IO/Net/AuSocketStats.hpp index bcce7f71..0e294381 100644 --- a/Source/IO/Net/AuSocketStats.hpp +++ b/Source/IO/Net/AuSocketStats.hpp @@ -23,11 +23,16 @@ namespace Aurora::IO::Net virtual double GetApproximatedThroughput() override; + virtual AuUInt64 GetUptimeNS() override; + + void Start(); + void End(); + AuUInt64 uLastTimeSteadyMS {}; private: Aurora::Utility::ThroughputCalculator calculator; - AuInt64 iFirstTime {}; - AuInt64 iLastTime {}; - AuUInt64 uTotalTransferred {}; + AuUInt64 uFirstTime {}; + AuUInt64 uStartSteadyTime {}; + AuUInt64 uEndSteadyTime {}; }; } \ No newline at end of file