diff --git a/fboss/agent/hw/sai/switch/SaiPortManager.cpp b/fboss/agent/hw/sai/switch/SaiPortManager.cpp index 5708a09967..2ad95e4864 100644 --- a/fboss/agent/hw/sai/switch/SaiPortManager.cpp +++ b/fboss/agent/hw/sai/switch/SaiPortManager.cpp @@ -1986,6 +1986,7 @@ void SaiPortManager::removePort(const std::shared_ptr& swPort) { addRemovedHandle(itr->first); handles_.erase(itr); portStats_.erase(swId); + disabledPortStats_.erase(swId); port2SupportedStats_.erase(swId); port2PortType_.erase(swId); port2ClmEnabled_.erase(swId); @@ -3356,9 +3357,19 @@ void SaiPortManager::clearStats(PortID port) { auto now = duration_cast(system_clock::now().time_since_epoch()); portStatItr->second->updateStats(curPortStats, now); } + auto savedItr = disabledPortStats_.find(port); + if (savedItr != disabledPortStats_.end()) { + savedItr->second.inDiscards_() = 0; + } } void SaiPortManager::clearInterfacePhyCounters(const PortID& portId) { + auto savedItr = disabledPortStats_.find(portId); + if (savedItr != disabledPortStats_.end()) { + savedItr->second.fecCorrectableErrors() = 0; + savedItr->second.fecUncorrectableErrors() = 0; + savedItr->second.fecCorrectedSymbols_() = 0; + } auto portStatItr = portStats_.find(portId); if (portStatItr == portStats_.end()) { return; diff --git a/fboss/agent/hw/sai/switch/SaiPortManager.h b/fboss/agent/hw/sai/switch/SaiPortManager.h index 0c7b2d45bc..8a3fd15dc8 100644 --- a/fboss/agent/hw/sai/switch/SaiPortManager.h +++ b/fboss/agent/hw/sai/switch/SaiPortManager.h @@ -628,6 +628,10 @@ class SaiPortManager { // retain removed port handle so it does not invoke remove port api. Handles removedHandles_; Stats portStats_; + // Last stats of admin-disabled ports, used to seed portStats_ on re-enable so + // software-accumulated counters (inDiscards_, clear-on-read FEC/PFC duration) + // stay monotonic like the HW-cumulative ones. + std::unordered_map disabledPortStats_; std::map portAsicPrbsStats_; std::optional getPtpMode() const; diff --git a/fboss/agent/hw/sai/switch/npu/SaiPortManager.cpp b/fboss/agent/hw/sai/switch/npu/SaiPortManager.cpp index 67c23741f9..273a84f38f 100644 --- a/fboss/agent/hw/sai/switch/npu/SaiPortManager.cpp +++ b/fboss/agent/hw/sai/switch/npu/SaiPortManager.cpp @@ -5,6 +5,7 @@ #include "fboss/agent/AgentFeatures.h" #include "fboss/agent/FbossError.h" #include "fboss/agent/hw/HwPortFb303Stats.h" +#include "fboss/agent/hw/gen-cpp2/hardware_stats_constants.h" #include "fboss/agent/hw/sai/store/SaiStore.h" #include "fboss/agent/hw/sai/switch/ConcurrentIndices.h" #include "fboss/agent/hw/sai/switch/SaiAclTableManager.h" @@ -571,6 +572,15 @@ void SaiPortManager::changePortImpl( HwAsic::Feature::SLL_HLL_DISCARD_COUNTERS), platform_->getAsic()->isSupported( HwAsic::Feature::PORT_LATCH_STATUS))); + auto savedItr = disabledPortStats_.find(newPort->getID()); + if (savedItr != disabledPortStats_.end()) { + auto& savedStats = savedItr->second; + savedStats.portName_() = newPort->getName(); + portStats_.find(newPort->getID()) + ->second->updateStats( + savedStats, std::chrono::seconds(*savedStats.timestamp_())); + disabledPortStats_.erase(savedItr); + } } else if (oldPort->getName() != newPort->getName()) { // Port was already enabled, but Port name changed - update stats portStats_.find(newPort->getID()) @@ -583,7 +593,15 @@ void SaiPortManager::changePortImpl( } } else if (oldPort->isEnabled()) { // Port transitioned from enabled to disabled, remove stats - portStats_.erase(newPort->getID()); + auto statsItr = portStats_.find(newPort->getID()); + if (statsItr != portStats_.end()) { + const auto& lastStats = statsItr->second->portStats(); + if (*lastStats.timestamp_() != + hardware_stats_constants::STAT_UNINITIALIZED()) { + disabledPortStats_[newPort->getID()] = lastStats; + } + portStats_.erase(statsItr); + } } // Before the qos policy: on TU1, VC create on a second port of an input // buffer fails with INVALID PARAMETER once QUEUE_TO_VC is bound to it. diff --git a/fboss/agent/hw/sai/switch/tests/PortManagerTest.cpp b/fboss/agent/hw/sai/switch/tests/PortManagerTest.cpp index 642db5a485..4b64bc00a7 100644 --- a/fboss/agent/hw/sai/switch/tests/PortManagerTest.cpp +++ b/fboss/agent/hw/sai/switch/tests/PortManagerTest.cpp @@ -843,6 +843,58 @@ TEST_F(PortManagerTest, collectStatsAfterPortDisable) { checkCounterExport(swPort->getName(), ExpectExport::NO_EXPORT); } +class PortManagerAccumulatedStatsTest : public PortManagerTest { + protected: + // Seeds the software-accumulated counters, then admin-disables the port. + std::shared_ptr addPortAndDisable() { + std::shared_ptr swPort = makePort(p0); + saiManagerTable->portManager().addPort(swPort); + saiManagerTable->portManager().updateStats(swPort->getID()); + auto* portStat = const_cast( + saiManagerTable->portManager().getLastPortStat(swPort->getID())); + CHECK(portStat); + auto stats = portStat->portStats(); + stats.inDiscards_() = 1000; + stats.fecCorrectableErrors() = 20; + portStat->updateStats(stats, std::chrono::seconds{*stats.timestamp_()}); + auto disabledPort = swPort->clone(); + disabledPort->setAdminState(cfg::PortState::DISABLED); + saiManagerTable->portManager().changePort(swPort, disabledPort); + return disabledPort; + } + + void enable(const std::shared_ptr& disabledPort) { + auto enabledPort = disabledPort->clone(); + enabledPort->setAdminState(cfg::PortState::ENABLED); + saiManagerTable->portManager().changePort(disabledPort, enabledPort); + } + + const HwPortStats& lastStats(PortID port) { + auto* portStat = saiManagerTable->portManager().getLastPortStat(port); + CHECK(portStat); + return portStat->portStats(); + } +}; + +TEST_F(PortManagerAccumulatedStatsTest, preservedAcrossDisableEnable) { + auto port = addPortAndDisable(); + enable(port); + EXPECT_EQ(*lastStats(port->getID()).inDiscards_(), 1000); + EXPECT_EQ(*lastStats(port->getID()).fecCorrectableErrors(), 20); + saiManagerTable->portManager().updateStats(port->getID()); + EXPECT_EQ(*lastStats(port->getID()).inDiscards_(), 1000); + EXPECT_EQ(*lastStats(port->getID()).fecCorrectableErrors(), 20); +} + +TEST_F(PortManagerAccumulatedStatsTest, clearWhileDisabled) { + auto port = addPortAndDisable(); + saiManagerTable->portManager().clearStats(port->getID()); + saiManagerTable->portManager().clearInterfacePhyCounters(port->getID()); + enable(port); + EXPECT_EQ(*lastStats(port->getID()).inDiscards_(), 0); + EXPECT_EQ(*lastStats(port->getID()).fecCorrectableErrors(), 0); +} + TEST_F(PortManagerTest, subsumedPorts) { // Port P0 has a port ID 0 and only be configured with all speeds. checkSubsumedPorts(p0, cfg::PortSpeed::XG, {});