From owner-freebsd-current@FreeBSD.ORG Mon Aug 19 19:33:05 2013 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 8CAFBA8C for ; Mon, 19 Aug 2013 19:33:05 +0000 (UTC) (envelope-from vitja.makarov@gmail.com) Received: from mail-ve0-x22d.google.com (mail-ve0-x22d.google.com [IPv6:2607:f8b0:400c:c01::22d]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 5354323FA for ; Mon, 19 Aug 2013 19:33:05 +0000 (UTC) Received: by mail-ve0-f173.google.com with SMTP id cy12so3269556veb.18 for ; Mon, 19 Aug 2013 12:33:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=YXFZD6y6VVZmCMst3SQe7UVZwmmrxkRpritVF4UUvA4=; b=qZl0VqM+RwobDwxEccg81oBFpEN321bUhbpN6dbzxVgbWU6F1flEtuJuog5fpyQrSX HlhNnC6EZaaf8nK8v+Yw9DetwSgVmkZg8it0SvwsLBLbZCQzv2NJ4AchUrWg+qBRbAAY jArP2Q9NbTi5Z5e5ofJo4jrRm7Q5cMCqzff9JeZOlfgy0VrvpSp2GrCQ1B7OQl5F27TI rRtkePcymu76IjHSPR2f/t6lSYnJlfeYuACXnSx3Ua61RlEs0R2EqgRloAptNrJLYsXU hksjyIyX361g+R0tJKIRiiIEejEElhKxvMtN4rP/2xY4xQFcElanEvNogh4S2HhF096p ItcA== MIME-Version: 1.0 X-Received: by 10.221.40.10 with SMTP id to10mr3980293vcb.22.1376940784437; Mon, 19 Aug 2013 12:33:04 -0700 (PDT) Received: by 10.52.175.198 with HTTP; Mon, 19 Aug 2013 12:33:04 -0700 (PDT) Date: Mon, 19 Aug 2013 23:33:04 +0400 Message-ID: Subject: Question about socket timeouts From: Vitja Makarov To: freebsd-current@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 X-Mailman-Approved-At: Tue, 20 Aug 2013 02:02:46 +0000 X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Aug 2013 19:33:05 -0000 Hi! Recently I was playing with small socket timeouts. setsockopt(2) SO_RCVTIMEO and found a problem with it: if timeout is small enough read(2) may return before timeout is actually expired. I was unable to reproduce this on linux box. I found that kernel uses a timer with 1/HZ precision so it converts time in microseconds to ticks that's ok linux does it as well. The problem is in details: freebsd uses floor() approach while linux uses ceil(): from FreeBSD's sys/kern/uipc_socket.c: val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; if (val == 0 && tv.tv_usec != 0) val = 1; /* at least one tick if tv > 0 */ from Linux's net/core/sock.c: *timeo_p = tv.tv_sec*HZ + (tv.tv_usec+(1000000/HZ-1))/(1000000/HZ); So, for instance, we have a freebsd system running with kern.hz set 100 and set receive timeout to 25ms that is converted to 2 ticks which is 20ms. In my test program read(2) returns with EAGAIN set in 0.019ms. So the question is: is that a problem or not? -- vitja.