Skip to content

[202411] Optimize counter polling interval by making it more accurate (Backport PR #1457) #1534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

stephenxs
Copy link
Contributor

What I did

This is to backport PR #1457 to 202411 branch

Optimize the counter-polling performance in terms of polling interval accuracy

Enable bulk counter-polling to run at a smaller chunk size There is one counter-polling thread for each counter group. All such threads can compete for the critical sections at the vendor SAI level, which means a counter-polling thread can wait for a critical section if another thread has been in it, which introduces latency for the waiting counter group. An example is the competition between the PFC watchdog and the port counter groups. The port counter group contains many counters and is polled in a bulk mode which takes a relatively longer time. The PFC watchdog counter group contains only a few counters but is polled at a short interval. Sometimes, PFC watchdog counters need to wait before polling, which makes the polling interval inaccurate and prevents the PFC storm from being detected in time. To resolve this issue, we can reduce the chunk size of the port counter group. The port counter group polls the counters of all ports in a single bulk operation by default. By using a smaller chunk size, it polls the counters in several bulk operations with each polling counter of a subset (whose size <= chunk size) of all ports. By doing so, the port counter group stays in the critical section for a shorter time and the PFC watchdog is more likely to be scheduled to poll counters and detect the PFC storm in time.

Collect the time stamp immediately after vendor SAI API returns. Currently, many counter groups require a Lua plugin to execute based on polling interval, to calculate rates, detect certain events, etc. Eg. For PFC watchdog counter group to PFC storm. In this case, the polling interval is calculated based on the difference of time stamps between the current and last poll to avoid deviation due to scheduling latency. However, the timestamp is collected in the Lua plugin which is several steps after the SAI API returns and is executed in a different context (redis-server). Both introduce even larger deviations. To overcome this, we collect the timestamp immediately after the SAI API returns.

@mssonicbld
Copy link
Collaborator

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs stephenxs requested review from kcudnik and keboliu February 19, 2025 02:43
@stephenxs stephenxs force-pushed the 202411-counter-optimization-all-in-one branch from 2c700ce to d95ef16 Compare February 19, 2025 03:01
@mssonicbld
Copy link
Collaborator

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs
Copy link
Contributor Author

Need to cherry-pick #1514 to resolve Arm64 build issue.

@stephenxs stephenxs marked this pull request as ready for review February 20, 2025 02:56
@stephenxs stephenxs marked this pull request as draft February 20, 2025 02:56
…et#1457)

What I did

Optimize the counter-polling performance in terms of polling interval accuracy

Enable bulk counter-polling to run at a smaller chunk size
There is one counter-polling thread for each counter group. All such threads can compete for the critical sections at the vendor SAI level, which means a counter-polling thread can wait for a critical section if another thread has been in it, which introduces latency for the waiting counter group.
An example is the competition between the PFC watchdog and the port counter groups.
The port counter group contains many counters and is polled in a bulk mode which takes a relatively longer time. The PFC watchdog counter group contains only a few counters but is polled at a short interval. Sometimes, PFC watchdog counters need to wait before polling, which makes the polling interval inaccurate and prevents the PFC storm from being detected in time.
To resolve this issue, we can reduce the chunk size of the port counter group. The port counter group polls the counters of all ports in a single bulk operation by default. By using a smaller chunk size, it polls the counters in several bulk operations with each polling counter of a subset (whose size <= chunk size) of all ports.
By doing so, the port counter group stays in the critical section for a shorter time and the PFC watchdog is more likely to be scheduled to poll counters and detect the PFC storm in time.

Collect the time stamp immediately after vendor SAI API returns.
Currently, many counter groups require a Lua plugin to execute based on polling interval, to calculate rates, detect certain events, etc.
Eg. For PFC watchdog counter group to PFC storm. In this case, the polling interval is calculated based on the difference of time stamps between the current and last poll to avoid deviation due to scheduling latency. However, the timestamp is collected in the Lua plugin which is several steps after the SAI API returns and is executed in a different context (redis-server). Both introduce even larger deviations. To overcome this, we collect the timestamp immediately after the SAI API returns.
@stephenxs stephenxs force-pushed the 202411-counter-optimization-all-in-one branch from d95ef16 to de928d8 Compare February 20, 2025 02:57
@mssonicbld
Copy link
Collaborator

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs
Copy link
Contributor Author

Rebased to have #1514 in the branch to resolve test issue

@stephenxs
Copy link
Contributor Author

Failed due to an unstable test which also fails sporadically in other PR checkers. Retrying

[ RUN      ] VendorSaiTest.bulk_flow_entry
Assertion failed: _owned.empty () (src/own.cpp:197)
/bin/bash: line 6: 25446 Aborted                 ${dir}$tst
FAIL: tests

@stephenxs
Copy link
Contributor Author

/azpw run

@mssonicbld
Copy link
Collaborator

/AzurePipelines run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs
Copy link
Contributor Author

/azpw run

@mssonicbld
Copy link
Collaborator

/AzurePipelines run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs
Copy link
Contributor Author

/azpw run

@mssonicbld
Copy link
Collaborator

/AzurePipelines run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs stephenxs marked this pull request as ready for review February 26, 2025 02:46
@stephenxs
Copy link
Contributor Author

@kcudnik could you please merge this one

@kcudnik kcudnik merged commit 3df03e1 into sonic-net:202411 Feb 26, 2025
15 checks passed
@stephenxs stephenxs deleted the 202411-counter-optimization-all-in-one branch February 26, 2025 08:39
stephenxs pushed a commit to stephenxs/sonic-sairedis that referenced this pull request Feb 27, 2025
…#24)

```<br>* f83634d9 - (HEAD -> 202412) Merge branch '202411' of https://github.com/sonic-net/sonic-sairedis into 202412 (2025-02-27) [Sonic Automation]
* 8c17d4b - (origin/202411) Revert "Do not enter vendor SAI critical section for counter polling/clearing operations (sonic-net#1450)" (sonic-net#1541) (2025-02-27) [mssonicbld]
* 3df03e1 - Optimize counter polling interval by making it more accurate (sonic-net#1457) (sonic-net#1534) (2025-02-26) [Stephen Sun]
* d884ff9 - [syncd] Move logSet logGet under mutex to prevent race condition (sonic-net#1520) (sonic-net#1538) (2025-02-20) [Kamil Cudnik]
* ec8b3c3 - Fix pipeline errors related to rsyslogd and libswsscommon installation (sonic-net#1535) (2025-02-19) [mssonicbld]
* 6b263b8 - [FC] Support Policer Counter (sonic-net#1533) (2025-02-19) [mssonicbld]
* e53489e - [syncd] Update log level for bulk api (sonic-net#1532) (2025-02-18) [Jianyue Wu]
* 7ae00e5 - Define bulk chunk size and bulk chunk size per counter ID (sonic-net#1528) (2025-02-11) [mssonicbld]
* f35e743 - [nvidia] Skip SAI discovery on ports (sonic-net#1524) (2025-02-07) [mssonicbld]
* bf049ed - Use sonictest pool instead of sonic-common and fix arm64 issue. (sonic-net#1516) (2025-02-05) [mssonicbld]
* ffe371d - [syncd] Support bulk set in INIT_VIEW mode (sonic-net#1517) (2025-02-05) [mssonicbld]<br>```
DavidZagury pushed a commit to DavidZagury/sonic-sairedis that referenced this pull request Apr 28, 2025
* [syncd] Support bulk set in INIT_VIEW mode (sonic-net#1517)

Support bulk set in INIT_VIEW mode.

* Use sonictest pool instead of sonic-common and fix arm64 issue. (sonic-net#1516)

1. Use sonictest pool instead of sonic-common
2. Fix arm64 build error.

* [nvidia] Skip SAI discovery on ports (sonic-net#1524)

Given that modern systems have lots of ports, performing SAI discovery takes very long time, e.g. (8 sec) for 256 port system. This has a big impact of fast-boot downtime and the discovery itself is not required for Nvidia platform fast-boot.

Same applies to Nvidia fastfast-boot (aka warm-boot), yet needs to be tested separately.

* Define bulk chunk size and bulk chunk size per counter ID (sonic-net#1528)

Define bulk chunk size and bulk chunk size per counter ID.
This is to resolve the VS test failure in sonic-net#1457, which is caused by loop dependency.
In PR sonic-net#1457, new fields `bulk_chunk_size` and `bulk_chunk_size_per_prefix` have been introduced to `sai_redis_flex_counter_group_parameter_t` whose instances are initialized by orchagent.
However, the orchagent is still compiled with the old sairedis header, which prevents both new fields from being uninitialized which in turn fails vs test.

We have to split this PR into two:
1. sonic-net#1519 which updates the header sairedis.h only. the motivation is to compile swss(orchagent) with both new fields initiated.
2. sonic-net#1457 contains all the rest of code

The order to merge:
1. sonic-net#1519
2. sonic-net/sonic-swss#3391
3. sonic-net#1457

* [syncd] Update log level for bulk api (sonic-net#1532)

[syncd] Update log level for bulk api

* [FC] Support Policer Counter (sonic-net#1533)

Added the implantation for policer counter -
Support in POLICER group and sai_serialize functions
Unit Tests: Included unit tests to add and remove policer counter.

* Fix pipeline errors related to rsyslogd and libswsscommon installation (sonic-net#1535)

On arm64 (and maybe sometimes amd64), rsyslogd appears to need a second or two to actually fully exit. The current code expects it to exit practically instantly. Add a sleep of 2 seconds to give it some time. Also enable some logging so that the commands being run can be seen.

Also, fix an error related to libswsscommon not getting installed due to new dependencies being added. Solve this by using apt install to install the package, which brings in any necessary dependencies.

* [syncd] Move logSet logGet under mutex to prevent race condition (sonic-net#1520) (sonic-net#1538)

[syncd] Move logSet logGet under mutex to prevent race condition

* Optimize counter polling interval by making it more accurate (sonic-net#1457) (sonic-net#1534)

What I did

Optimize the counter-polling performance in terms of polling interval accuracy

Enable bulk counter-polling to run at a smaller chunk size
There is one counter-polling thread for each counter group. All such threads can compete for the critical sections at the vendor SAI level, which means a counter-polling thread can wait for a critical section if another thread has been in it, which introduces latency for the waiting counter group.
An example is the competition between the PFC watchdog and the port counter groups.
The port counter group contains many counters and is polled in a bulk mode which takes a relatively longer time. The PFC watchdog counter group contains only a few counters but is polled at a short interval. Sometimes, PFC watchdog counters need to wait before polling, which makes the polling interval inaccurate and prevents the PFC storm from being detected in time.
To resolve this issue, we can reduce the chunk size of the port counter group. The port counter group polls the counters of all ports in a single bulk operation by default. By using a smaller chunk size, it polls the counters in several bulk operations with each polling counter of a subset (whose size <= chunk size) of all ports.
By doing so, the port counter group stays in the critical section for a shorter time and the PFC watchdog is more likely to be scheduled to poll counters and detect the PFC storm in time.

Collect the time stamp immediately after vendor SAI API returns.
Currently, many counter groups require a Lua plugin to execute based on polling interval, to calculate rates, detect certain events, etc.
Eg. For PFC watchdog counter group to PFC storm. In this case, the polling interval is calculated based on the difference of time stamps between the current and last poll to avoid deviation due to scheduling latency. However, the timestamp is collected in the Lua plugin which is several steps after the SAI API returns and is executed in a different context (redis-server). Both introduce even larger deviations. To overcome this, we collect the timestamp immediately after the SAI API returns.

* Revert "Do not enter vendor SAI critical section for counter polling/clearing operations (sonic-net#1450)" (sonic-net#1541)

Revert "Do not enter vendor SAI critical section for counter polling/clearing operations (sonic-net#1450)"

This reverts commit 0317b16.

* [vslib] SAI_KEY_VS_OPER_SPEED_IS_CONFIGURED_SPEED, SAI_PORT_ATTR_HOST_TX_READY_STATUS support (sonic-net#1553)

This PR adds two features to `vslib`.

- `SAI_KEY_VS_OPER_SPEED_IS_CONFIGURED_SPEED`: when `true`, `SAI_PORT_ATTR_SPEED` returns the configured speed instead of the value retrieved via [`/sys/class/net/<name>/speed`](https://github.com/sonic-net/sonic-sairedis/blob/master/vslib/SwitchStateBaseHostif.cpp#L892-L893).
 - fixes sonic-net/sonic-buildimage#19735

- `SAI_PORT_ATTR_HOST_TX_READY_STATUS`: always returns `true`. Required to support running `xcvrd` in the VS env.
 - ref: https://github.com/sonic-net/SONiC/pull/1849/files#diff-6f3e95e6c57a3edc2e30e1f13edb9fd9a32a0db44e1035ac1f0b1b9a191762a5R46

* Update build_and_install_module.sh to match newer Linux kernel version (sonic-net#1561)

sonic-sairedis will checkout sonic-swss to do vstest but using local build_and_install_module.sh to setup test environment, which is out of date with newer Linux kernel version.
The build_and_install_module.sh in sonic-swss is up to date with latest Ubuntu 20.04, so we need to update the build sh file with the file in sonic-swss.
In a long term, we may need to do some automatically sync, but now we have some azure agent security issue need to fix immediately, so just update the build_and_install_module.sh manually.

* Revert "Optimize counter polling interval by making it more accurate (sonic-net#1457) …" (sonic-net#1570)

Revert "Optimize counter polling interval by making it more accurate

---------

Co-authored-by: mssonicbld <[email protected]>
Co-authored-by: Jianyue Wu <[email protected]>
Co-authored-by: Kamil Cudnik <[email protected]>
Co-authored-by: Stephen Sun <[email protected]>
Co-authored-by: Kumaresh Perumal <[email protected]>
DavidZagury pushed a commit to DavidZagury/sonic-sairedis that referenced this pull request Apr 28, 2025
Code sync sonic-net/sonic-sairedis:202411 => 202412

```
*   0fcc968 (HEAD -> code-sync-202412, origin/code-sync-202412) r12f 250417:0536 - Merge remote-tracking branch 'base/202411' into code-sync-202412
|\  
| * 4048483 (base/202411) Kumaresh Perumal 250403:0905 - Revert "Optimize counter polling interval by making it more accurate (sonic-net#1457) …" (sonic-net#1570)
| * 420d92f (user/riffjiang/last-202411) mssonicbld 250319:1302 - Update build_and_install_module.sh to match newer Linux kernel version (sonic-net#1561)
| * e2d2ca6 mssonicbld 250312:1602 - [vslib] SAI_KEY_VS_OPER_SPEED_IS_CONFIGURED_SPEED, SAI_PORT_ATTR_HOST_TX_READY_STATUS support (sonic-net#1553)
| * 8c17d4b mssonicbld 250227:0702 - Revert "Do not enter vendor SAI critical section for counter polling/clearing operations (sonic-net#1450)" (sonic-net#1541)
| * 3df03e1 Stephen Sun 250226:1737 - Optimize counter polling interval by making it more accurate (sonic-net#1457) (sonic-net#1534)
| * d884ff9 Kamil Cudnik 250221:0109 - [syncd] Move logSet logGet under mutex to prevent race condition (sonic-net#1520) (sonic-net#1538)
| * ec8b3c3 mssonicbld 250219:1902 - Fix pipeline errors related to rsyslogd and libswsscommon installation (sonic-net#1535)
| * 6b263b8 mssonicbld 250219:0702 - [FC] Support Policer Counter (sonic-net#1533)
| * e53489e Jianyue Wu 250219:0217 - [syncd] Update log level for bulk api (sonic-net#1532)
| * 7ae00e5 mssonicbld 250211:0703 - Define bulk chunk size and bulk chunk size per counter ID (sonic-net#1528)
| * f35e743 mssonicbld 250207:1005 - [nvidia] Skip SAI discovery on ports (sonic-net#1524)
| * bf049ed mssonicbld 250205:1328 - Use sonictest pool instead of sonic-common and fix arm64 issue. (sonic-net#1516)
| * ffe371d mssonicbld 250205:1327 - [syncd] Support bulk set in INIT_VIEW mode (sonic-net#1517)
```
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