Skip to content

Fec counters clear function#4640

Open
nkanchi-nexthop wants to merge 6 commits into
sonic-net:masterfrom
nexthop-ai:nkanchi.FEC-histogram-counters-clear
Open

Fec counters clear function#4640
nkanchi-nexthop wants to merge 6 commits into
sonic-net:masterfrom
nexthop-ai:nkanchi.FEC-histogram-counters-clear

Conversation

@nkanchi-nexthop

Copy link
Copy Markdown
Contributor

What I did

The sonic-clear counters command does not reset FEC histogram output, so show interface counters fec-histogram continued to show lifetime bin values after a clear. This fixes the upstream issue sonic-net/sonic-utilities#3741.

How I did it

  • Switched the show interface counters fec-histogram to use portstat -fh -i <iface> so it follows the cached clear-counter flow introduced upstream in sonic-net/sonic-utilities#4075.
  • Extended portstat -c to clear the FEC sliding-window aggregate fields stored in RATES:<oid>.
  • Added unit tests for histogram display, cache contents, aggregate clearing, and no-cache behavior.

How to verify it

  • Run show interface counters fec-histogram <iface> and show interface counters fec-stats <iface> on a port with existing FEC data.
  • Run sonic-clear counters then re-run the previous commands to confirm the histogram output and FEC sliding-window aggregates are reset.
  • Verify through unit tests added in tests/portstat_test.py.

Previous command output (if the output of a command-line utility has changed)

root@golf302:/home/admin# show interface counters fec-histogram Ethernet0
Last cached time was 2025-09-04T21:55:19.933197
Symbol Errors Per Codeword    Codewords      Last Updated         Relative Time
----------------------------  -------------  -------------------  ---------------
BIN0                          7,823,206,607  2025-09-04 21:56:33  1 second ago
BIN1                          314            2025-09-04 21:56:32  2 seconds ago
BIN2                          0              2025-09-03 18:39:12  1 day ago
BIN3                          0              2025-09-03 18:39:12  1 day ago
BIN4                          0              2025-09-03 18:39:12  1 day ago
BIN5                          0              2025-09-03 18:39:12  1 day ago
BIN6                          0              2025-09-03 18:39:12  1 day ago
BIN7                          0              2025-09-03 18:39:12  1 day ago
BIN8                          0              2025-09-03 18:39:12  1 day ago
BIN9                          0              2025-09-03 18:39:12  1 day ago
BIN10                         0              2025-09-03 18:39:12  1 day ago
BIN11                         0              2025-09-03 18:39:12  1 day ago
BIN12                         0              2025-09-03 18:39:12  1 day ago
BIN13                         0              2025-09-03 18:39:12  1 day ago
BIN14                         0              2025-09-03 18:39:12  1 day ago
BIN15                         0              2025-09-03 18:39:12  1 day ago

root@golf302:/home/admin# show interface counters fec-stats Ethernet0 
Last cached time was 2025-09-04T21:55:19.933197
    IFACE    STATE    FEC_CORR    FEC_UNCORR    FEC_SYMBOL_ERR    FEC_PRE_BER    FEC_POST_BER    FEC_PRE_BER_MAX    FLR(O)    FLR(P) (Accuracy)    FEC_MAX_T
---------  -------  ----------  ------------  ----------------  -------------  --------------  -----------------  --------  -------------------  -----------
Ethernet0        X         314            14               538              0               0           3.41e-11         0                    0            1

root@golf302:/home/admin# sonic-clear counters
Cleared counters

root@golf302:/home/admin# show interface counters fec-histogram Ethernet0
Last cached time was 2025-09-04T21:56:43.434346
Symbol Errors Per Codeword    Codewords      Last Updated         Relative Time
----------------------------  -------------  -------------------  ---------------
BIN0                          7,823,206,607  2025-09-04 21:56:33  1 second ago
BIN1                          314            2025-09-04 21:56:32  2 seconds ago
BIN2                          0              2025-09-03 18:39:12  1 day ago
BIN3                          0              2025-09-03 18:39:12  1 day ago
BIN4                          0              2025-09-03 18:39:12  1 day ago
BIN5                          0              2025-09-03 18:39:12  1 day ago
BIN6                          0              2025-09-03 18:39:12  1 day ago
BIN7                          0              2025-09-03 18:39:12  1 day ago
BIN8                          0              2025-09-03 18:39:12  1 day ago
BIN9                          0              2025-09-03 18:39:12  1 day ago
BIN10                         0              2025-09-03 18:39:12  1 day ago
BIN11                         0              2025-09-03 18:39:12  1 day ago
BIN12                         0              2025-09-03 18:39:12  1 day ago
BIN13                         0              2025-09-03 18:39:12  1 day ago
BIN14                         0              2025-09-03 18:39:12  1 day ago
BIN15                         0              2025-09-03 18:39:12  1 day ago

root@golf302:/home/admin# show interface counters fec-stats Ethernet0 
Last cached time was 2025-09-04T21:56:43.434346
    IFACE    STATE    FEC_CORR    FEC_UNCORR    FEC_SYMBOL_ERR    FEC_PRE_BER    FEC_POST_BER    FEC_PRE_BER_MAX    FLR(O)    FLR(P) (Accuracy)    FEC_MAX_T
---------  -------  ----------  ------------  ----------------  -------------  --------------  -----------------  --------  -------------------  -----------
Ethernet0        X         314            14               538              0               0           3.41e-11         0                    0            1

New command output (if the output of a command-line utility has changed)

root@golf302:/home/admin# show interface counters fec-histogram Ethernet0
Last cached time was 2025-09-04T21:55:19.933197
Symbol Errors Per Codeword    Codewords      Last Updated         Relative Time
----------------------------  -------------  -------------------  ---------------
BIN0                          7,823,206,607  2025-09-04 21:56:33  1 second ago
BIN1                          314            2025-09-04 21:56:32  2 seconds ago
BIN2                          0              2025-09-03 18:39:12  1 day ago
BIN3                          0              2025-09-03 18:39:12  1 day ago
BIN4                          0              2025-09-03 18:39:12  1 day ago
BIN5                          0              2025-09-03 18:39:12  1 day ago
BIN6                          0              2025-09-03 18:39:12  1 day ago
BIN7                          0              2025-09-03 18:39:12  1 day ago
BIN8                          0              2025-09-03 18:39:12  1 day ago
BIN9                          0              2025-09-03 18:39:12  1 day ago
BIN10                         0              2025-09-03 18:39:12  1 day ago
BIN11                         0              2025-09-03 18:39:12  1 day ago
BIN12                         0              2025-09-03 18:39:12  1 day ago
BIN13                         0              2025-09-03 18:39:12  1 day ago
BIN14                         0              2025-09-03 18:39:12  1 day ago
BIN15                         0              2025-09-03 18:39:12  1 day ago

root@golf302:/home/admin# show interface counters fec-stats Ethernet0 
Last cached time was 2025-09-04T21:55:19.933197
    IFACE    STATE    FEC_CORR    FEC_UNCORR    FEC_SYMBOL_ERR    FEC_PRE_BER    FEC_POST_BER    FEC_PRE_BER_MAX    FLR(O)    FLR(P) (Accuracy)    FEC_MAX_T
---------  -------  ----------  ------------  ----------------  -------------  --------------  -----------------  --------  -------------------  -----------
Ethernet0        X         314            14               538              0               0           3.41e-11         0                    0            1

root@golf302:/home/admin# sonic-clear counters
Cleared counters

root@golf302:/home/admin# show interface counters fec-histogram Ethernet0
Last cached time was 2025-09-04T21:56:43.434346
Symbol Errors Per Codeword      Codewords  Last Updated         Relative Time
----------------------------  -----------  -------------------  ---------------
BIN0                                    0  2025-09-04 21:56:33  13 seconds ago
BIN1                                    0  2025-09-04 21:56:32  14 seconds ago
BIN2                                    0  2025-09-03 18:39:12  1 day ago
BIN3                                    0  2025-09-03 18:39:12  1 day ago
BIN4                                    0  2025-09-03 18:39:12  1 day ago
BIN5                                    0  2025-09-03 18:39:12  1 day ago
BIN6                                    0  2025-09-03 18:39:12  1 day ago
BIN7                                    0  2025-09-03 18:39:12  1 day ago
BIN8                                    0  2025-09-03 18:39:12  1 day ago
BIN9                                    0  2025-09-03 18:39:12  1 day ago
BIN10                                   0  2025-09-03 18:39:12  1 day ago
BIN11                                   0  2025-09-03 18:39:12  1 day ago
BIN12                                   0  2025-09-03 18:39:12  1 day ago
BIN13                                   0  2025-09-03 18:39:12  1 day ago
BIN14                                   0  2025-09-03 18:39:12  1 day ago
BIN15                                   0  2025-09-03 18:39:12  1 day ago

root@golf302:/home/admin# show interface counters fec-stats Ethernet0 
Last cached time was 2025-09-04T21:56:43.434346
    IFACE    STATE    FEC_CORR    FEC_UNCORR    FEC_SYMBOL_ERR    FEC_PRE_BER    FEC_POST_BER    FEC_PRE_BER_MAX    FLR(O)    FLR(P) (Accuracy)    FEC_MAX_T
---------  -------  ----------  ------------  ----------------  -------------  --------------  -----------------  --------  -------------------  -----------
Ethernet0        X           0             0                 0              0               0           3.41e-11         0                    0            1

Signed-off-by: nkanchi-nexthop <nkanchi@nexthop.ai>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@nkanchi-nexthop nkanchi-nexthop marked this pull request as ready for review June 23, 2026 20:56
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@nkanchi-nexthop nkanchi-nexthop marked this pull request as draft June 23, 2026 20:56
@nkanchi-nexthop nkanchi-nexthop marked this pull request as ready for review June 26, 2026 17:06
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment thread show/interfaces/__init__.py Outdated


=======
>>>>>>> 6009f7ce (NOS-8001: Clear all FEC counters in SONiC properly including histogram (#591))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nkanchi-nexthop is this properly rebased?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes sonic-clear counters not resetting FEC histogram/sliding-window outputs by aligning show interfaces counters fec-histogram with the portstat -fh cached clear-counter flow and extending portstat -c to clear additional FEC aggregate fields in RATES:<oid>.

Changes:

  • Add a single-interface “vertical” FEC histogram view in utilities_common/portstat.py, including per-bin “Last Updated” timestamps and optional relative time.
  • Extend scripts/portstat -c to clear FEC sliding-window aggregate fields from RATES:<oid>.
  • Add/extend unit tests to validate the updated fec-histogram command behavior, cache contents, and aggregate clearing behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
utilities_common/portstat.py Adds vertical FEC histogram rendering and timestamp/relative-time support; updates cnstat printing flow.
scripts/portstat Adds --relative-timestamp flag; clears FEC aggregate fields on -c; wires relative-timestamp through to portstat output.
show/interfaces/init.py Updates show interfaces counters fec-histogram to delegate to portstat -fh (but currently contains merge-conflict artifacts).
tests/portstat_test.py Adds tests for the new flow and clearing behavior (but currently contains merge-conflict artifacts).
Comments suppressed due to low confidence (1)

tests/portstat_test.py:75

  • Unresolved merge-conflict markers (<<<<<<< / ======= / >>>>>>>) are present in this expected-output string, which will break test collection. Remove the conflict markers and keep the intended baseline string definition.
<<<<<<< HEAD
intf_fec_counters_fec_hist = """\
Symbol Errors Per Codeword      Codewords
----------------------------  -----------
BIN0                              1000000

Comment thread show/interfaces/__init__.py Outdated
Comment on lines +916 to +920
<<<<<<< HEAD
@clicommon.pass_db
def fec_histogram(db, interfacename, namespace, display):
=======
@click.pass_context
Comment thread tests/portstat_test.py
Comment on lines 519 to +523
def test_show_intf_counters_fec_histogram(self):
runner = CliRunner()
result = runner.invoke(
show.cli.commands["interfaces"].commands["counters"].commands["fec-histogram"], ["Ethernet0"])
with mock.patch('show.interfaces.clicommon.run_command') as mock_run:
result = runner.invoke(
show.cli.commands["interfaces"].commands["counters"].commands["fec-histogram"], ["Ethernet0"])
Comment on lines 11 to 13
from utilities_common import constants
from utilities_common.general import format_relative_time
import utilities_common.multi_asic as multi_asic_util
Comment thread utilities_common/portstat.py Outdated
Comment on lines +442 to +451
ts_ms = asic_kvp.get(SAI_FEC_BIN_TIMESTAMP_KEY.format(i)) if asic_kvp else None
if ts_ms:
ts_str = datetime.datetime.fromtimestamp(int(ts_ms) / 1000.0) \
.strftime('%Y-%m-%d %H:%M:%S')
else:
ts_str = STATUS_NA
row = ['BIN{}'.format(i), codewords, ts_str]
if relative_timestamp:
row.append(format_relative_time(ts_ms) if ts_ms else STATUS_NA)
table.append(row)
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Nikhil Kanchi <nkanchi@nexthop.ai>
@nkanchi-nexthop nkanchi-nexthop force-pushed the nkanchi.FEC-histogram-counters-clear branch from a99f80d to d828ca9 Compare July 9, 2026 18:54
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Nikhil Kanchi <nkanchi@nexthop.ai>
@nkanchi-nexthop nkanchi-nexthop force-pushed the nkanchi.FEC-histogram-counters-clear branch from edc104e to eefe794 Compare July 10, 2026 09:23
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Nikhil Kanchi <nkanchi@nexthop.ai>
@nkanchi-nexthop nkanchi-nexthop force-pushed the nkanchi.FEC-histogram-counters-clear branch from f1bb371 to c7cbaf3 Compare July 10, 2026 16:39
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants