This is an automated email from the ASF dual-hosted git repository.
lordgamez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 36f1a2d3f MINIFICPP-2899 procfs: Fix CpuStat and CPUStatTests
inaccuracy (#2260)
36f1a2d3f is described below
commit 36f1a2d3f91462236e9d9145501c62ea4a922565
Author: Márton Szász <[email protected]>
AuthorDate: Mon Sep 14 10:40:15 2026 +0000
MINIFICPP-2899 procfs: Fix CpuStat and CPUStatTests inaccuracy (#2260)
Signed-off-by: Marton Szasz <[email protected]>
---
extensions/procfs/CpuStat.h | 2 +-
extensions/procfs/tests/CPUStatTests.cpp | 22 ++++++++++------------
2 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/extensions/procfs/CpuStat.h b/extensions/procfs/CpuStat.h
index 2cf118a41..0ccb51841 100644
--- a/extensions/procfs/CpuStat.h
+++ b/extensions/procfs/CpuStat.h
@@ -51,7 +51,7 @@ class CpuStatData {
[[nodiscard]] SystemClockDuration getIdleAll() const noexcept { return idle_
+ io_wait_; }
[[nodiscard]] SystemClockDuration getSystemAll() const noexcept { return
system_ + irq_ + soft_irq_; }
[[nodiscard]] SystemClockDuration getVirtAll() const noexcept { return
guest_ + guest_nice_; }
- [[nodiscard]] std::chrono::duration<double> getTotal() const noexcept {
return user_ + nice_ + getSystemAll() + getIdleAll() + steal_; } // VirtAll is
already included in User and Nice
+ [[nodiscard]] SystemClockDuration getTotal() const noexcept { return user_ +
nice_ + getSystemAll() + getIdleAll() + steal_; } // VirtAll is already
included in User and Nice
private:
SystemClockDuration user_;
diff --git a/extensions/procfs/tests/CPUStatTests.cpp
b/extensions/procfs/tests/CPUStatTests.cpp
index be4d47842..413c60659 100644
--- a/extensions/procfs/tests/CPUStatTests.cpp
+++ b/extensions/procfs/tests/CPUStatTests.cpp
@@ -25,18 +25,16 @@
namespace org::apache::nifi::minifi::extensions::procfs::tests {
void cpu_stat_period_total_should_be_one(const CpuStatData& cpu_stat) {
- double percentage = 0;
- percentage += cpu_stat.getUser() / cpu_stat.getTotal();
- percentage += cpu_stat.getNice() / cpu_stat.getTotal();
- percentage += cpu_stat.getSystem() / cpu_stat.getTotal();
- percentage += cpu_stat.getIdle() / cpu_stat.getTotal();
- percentage += cpu_stat.getIoWait() / cpu_stat.getTotal();
- percentage += cpu_stat.getIrq() / cpu_stat.getTotal();
- percentage += cpu_stat.getSoftIrq() / cpu_stat.getTotal();
- percentage += cpu_stat.getSteal() / cpu_stat.getTotal();
- percentage += cpu_stat.getGuest() / cpu_stat.getTotal();
- percentage += cpu_stat.getGuestNice() / cpu_stat.getTotal();
- REQUIRE(percentage == Catch::Approx(1.0));
+ // according to the comment of getTotal, User and Nice already includes
VirtAll (Guest + GuestNice), so adding them again would push us above 1
+ const auto sum_of_parts = cpu_stat.getUser()
+ + cpu_stat.getNice()
+ + cpu_stat.getSystem()
+ + cpu_stat.getIdle()
+ + cpu_stat.getIoWait()
+ + cpu_stat.getIrq()
+ + cpu_stat.getSoftIrq()
+ + cpu_stat.getSteal();
+ REQUIRE(sum_of_parts == cpu_stat.getTotal());
}
TEST_CASE("ProcFSTest stat test with mock", "[procfsstatmockabsolutetest]") {