From nobody@FreeBSD.org  Mon Apr 24 16:19:45 2006
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 1BA0A16A404
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 24 Apr 2006 16:19:45 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id D2B1343D45
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 24 Apr 2006 16:19:44 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id k3OGJiOi071635
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 24 Apr 2006 16:19:44 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id k3OGJiNI071632;
	Mon, 24 Apr 2006 16:19:44 GMT
	(envelope-from nobody)
Message-Id: <200604241619.k3OGJiNI071632@www.freebsd.org>
Date: Mon, 24 Apr 2006 16:19:44 GMT
From: Jost Boekemeier <jost2345@users.sourceforge.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: TCP socket performance drops by 3000% if packets are split at the first byte
X-Send-Pr-Version: www-2.3

>Number:         96268
>Category:       kern
>Synopsis:       [socket] TCP socket performance drops by 3000% if packets are split at the first byte
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-net
>State:          feedback
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 24 16:20:22 GMT 2006
>Closed-Date:    
>Last-Modified:  Sun Mar 22 03:00:06 UTC 2009
>Originator:     Jost Boekemeier
>Release:        6.0
>Organization:
>Environment:
Unknown, should be reproduceable on all FreeBSD kernels
>Description:
See test below.  The test is written in java, but it it likely a kernel bug.  

The time needed to complete Test#2 is independent of the architecture, for
200 iterations it needs ~20 seconds, for 40 interations ~40 seconds etc.

It is not a configuration problem, for example socket waiting for a timeout,
because the similar Test#1 completes in ~1.2 seconds.



>How-To-Repeat:
import java.io.*;
import java.net.*;
import java.util.*;

public class TestServ implements Runnable {
    static final int SIZE=8192;
    static final int PORT=9767;

    private String getID(byte[] b, int n) {
	String str = (new String(b, 0, n));
	//System.out.println(str);
	int idx = str.lastIndexOf("=", 0); idx+=2;
	int idx2= str.indexOf("\"", idx);
	return str.substring(idx, idx2);
    }    

    public void run() {
	try {
	    doRun();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    public void doRun() throws Exception {
	int n;
	ServerSocket ss = new ServerSocket(PORT, 1, InetAddress.getByName("127.0.0.1"));
	while(true) {
	    byte[] b = new byte[SIZE];
	    Socket s = ss.accept();

	    InputStream in = s.getInputStream();
	    in.read(b, 0, 1); //options
	    n = in.read(b, 0, 51);
	    
	    OutputStream out = s.getOutputStream();
	    byte[] x = ("<N i=\""+getID(b, n)+"\"/>").getBytes();
	    out.write(x, 0, x.length);

	    s.close();
	}
    }

    private static void runTest1() throws Exception {
	for(int i=0; i<200; i++) {
	    Socket s = new Socket("127.0.0.1", PORT);
	    InputStream in = s.getInputStream();
	    OutputStream out = s.getOutputStream();
	    out.write(("@<I v=\"0\" m=\"lastException\" p=\"P\" i=\"136070284\"></I>").getBytes());
	    byte[] b = new byte[1024];
	    in.read(b);
	    s.close();
	}
    }

    // same as above, buf send two packets
    private static void runTest2() throws Exception {
	for(int i=0; i<200; i++) {
	    Socket s = new Socket("127.0.0.1", PORT);
	    InputStream in = s.getInputStream();
	    OutputStream out = s.getOutputStream();
	    out.write('@');
	    out.write(("<I v=\"0\" m=\"lastException\" p=\"P\" i=\"136070284\"></I>").getBytes());
	    byte[] b = new byte[1024];
	    in.read(b);
	    s.close();
	}
    }	
    public static void main(String _s[]) throws Exception {
	Thread t = new Thread(new TestServ());
	t.start();
	Thread.sleep(100);

	long T1 = System.currentTimeMillis();
	runTest1();
	long T2 = System.currentTimeMillis();
	runTest2();
	long T3 = System.currentTimeMillis();
	    
	System.out.println("The following test demonstrates a bug in the FreeBSD 6 kernel:");
	System.out.println("Both tests transfer the same amount of data.");
	System.out.println("But the second test splits the packet after the first byte.");
	System.out.println("Test1: "+(T2-T1));
	System.out.println("Test2: "+(T3-T2));
	short c = (short)((T3-T2)/(T2-T1));
	System.out.println("Test2/Test1: "+c);
	if(c>1) {
	    System.out.println("Test failed");
	    System.exit(1);
	}
	System.out.println("Test okay");
	System.exit(0);
    }
}

>Fix:

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: vwe 
State-Changed-When: Wed Jan 14 23:24:23 UTC 2009 
State-Changed-Why:  
Jost, 
do you still see this issue with recent releases? 


Responsible-Changed-From-To: freebsd-bugs->freebsd-net 
Responsible-Changed-By: vwe 
Responsible-Changed-When: Wed Jan 14 23:24:23 UTC 2009 
Responsible-Changed-Why:  

Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=96268 

From: Tom Judge <tom@tomjudge.com>
To: bug-followup@FreeBSD.org, jost2345@users.sourceforge.net
Cc:  
Subject: Re: kern/96268: [socket] TCP socket performance drops by 3000% if
 packets are split at the first byte
Date: Wed, 14 Jan 2009 19:31:11 -0600

 I have seen this issue on 7.0-RELEASE. 
 
 It seems to be related to the nagle algorithm being enable by default on 
 connections on the loop back interface.
 
 One application level work around is to set the TCP_NODELAY option on 
 the socket so that the nagle algorithem is disabled.
 
 This bug affects users of php java bridge as newer release talk to a JVM 
 over a tcp socket bound to localhost, and the typical packets passing 
 back and forward are very small.
 
 It is also possible to reproduce this bug using PHP 5.  I can attach 
 test scripts if required.
 
 Tom

From: Mark Linimon <linimon@lonesome.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/96268: [socket] TCP socket performance drops by 3000% if
	packets are split at the first byte
Date: Sat, 21 Mar 2009 21:57:07 -0500

 ----- Forwarded message from Jost Boekemeier <jostb2345@yahoo.de> -----
 
 From: Jost Boekemeier <jostb2345@yahoo.de>
 To: vwe@FreeBSD.org, freebsd-bugs@FreeBSD.org, freebsd-net@FreeBSD.org
 Subject: Re: kern/96268: [socket] TCP socket performance drops by 3000% if
 	packets are split at the first byte
 
 Hi,
 
 from my point of view this issue can be closed. 
 
 TCP write/write/read sequences are bad on any operating system, it's just that other OS are a little bit smarter. -- I think Jon Nagle has had a proposal to fix/remove this unconditional delay, but I don't know if it has been implemented.
 
 Furthermore this problem has been fixed on application level. And I think Patrick van Staveren maintains a FreeBSD port which uses unix domain- instead of TCP socket communication.
 
 Regards,
 Jost Bkemeier
 
 _______________________________________________
 freebsd-bugs@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-bugs
 To unsubscribe, send any mail to "freebsd-bugs-unsubscribe@freebsd.org"
 
 
 ----- End forwarded message -----
>Unformatted:
