Skip to contentSkip to conversation

tcp: use likely() for srtt check in tcp_rtt_estimator()

PATCH In review Ziran Zhang 2 participants 3 messages Aug 8 – Aug 10

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: use likely() for srtt check in tcp_rtt_estimator()

In tcp_rtt_estimator(), srtt is initialized to a non-zero value after the first RTT measurement (srtt = m << 3) and never returns to zero for the lifetime of the connection. Thus, the "srtt != 0" branch is almost always true.

Marking it with likely() helps the compiler generate better code for this hot path.

No functional change is introduced.

---
 net/ipv4/tcp_input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_input.c +1 -1
index 5b6378b94..a4c4d4760 100644
@@ -1089,7 +1089,7 @@ static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
 	 * does not matter how to _calculate_ it. Seems, it was trap
 	 * that VJ failed to avoid. 8)
 	 */
-	if (srtt != 0) {
+	if (likely(srtt != 0)) {
 		m -= (srtt >> 3);	/* m is now error in rtt est */
 		srtt += m;		/* rtt = 7/8 rtt + 1/8 new */
 		if (m < 0) {
Signature · 1 line
2.51.0
#1 lore raw
Quoted 21 lines · Ziran Zhang

In tcp_rtt_estimator(), srtt is initialized to a non-zero value after the first RTT measurement (srtt = m << 3) and never returns to zero for the lifetime of the connection. Thus, the "srtt != 0" branch is almost always true.

Marking it with likely() helps the compiler generate better code for this hot path.

No functional change is introduced.

---
 net/ipv4/tcp_input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_input.c +0 -0
index 5b6378b94..a4c4d4760 100644

net/ipv4/tcp_input.c static void tcp_rtt_estimator(struct sock *sk, long mrtt_us) · line 1095

Quoted 8 lines
net/ipv4/tcp_input.c +1 -1
@@ -1089,7 +1089,7 @@ static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
         * does not matter how to _calculate_ it. Seems, it was trap
         * that VJ failed to avoid. 8)
         */
-       if (srtt != 0) {
+       if (likely(srtt != 0)) {
                m -= (srtt >> 3);       /* m is now error in rtt est */
                srtt += m;              /* rtt = 7/8 rtt + 1/8 new */
                if (m < 0) {

Many flows are short lived, unfortunately.

I would suggest not adding a likely() here, and let FDO/LTO decide here on a case by case.

#2 lore raw
Quoted 4 lines · Eric Dumazet

Many flows are short lived, unfortunately.

I would suggest not adding a likely() here, and let FDO/LTO decide here on a case by case.

Hi Eric,

Thank you for your review.

I didn't fully take into account the large number of short-lived flows in real-world networks. I agree with your assessment, so I will drop this patch.

Thanks,
Ziran Zhang