From owner-freebsd-arch@FreeBSD.ORG Thu Mar 1 00:44:04 2012 Return-Path: Delivered-To: arch@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 61B8F106564A for ; Thu, 1 Mar 2012 00:44:04 +0000 (UTC) (envelope-from luigi@onelab2.iet.unipi.it) Received: from onelab2.iet.unipi.it (onelab2.iet.unipi.it [131.114.59.238]) by mx1.freebsd.org (Postfix) with ESMTP id 228918FC0A for ; Thu, 1 Mar 2012 00:44:03 +0000 (UTC) Received: by onelab2.iet.unipi.it (Postfix, from userid 275) id 0B3937300A; Thu, 1 Mar 2012 02:02:19 +0100 (CET) Date: Thu, 1 Mar 2012 02:02:19 +0100 From: Luigi Rizzo To: Bruce Evans Message-ID: <20120301010219.GA14508@onelab2.iet.unipi.it> References: <20120229194042.GA10921@onelab2.iet.unipi.it> <20120301071145.O879@besplex.bde.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120301071145.O879@besplex.bde.org> User-Agent: Mutt/1.4.2.3i Cc: arch@FreeBSD.org Subject: Re: select/poll/usleep precision on FreeBSD vs Linux vs OSX X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Mar 2012 00:44:04 -0000 On Thu, Mar 01, 2012 at 11:33:46AM +1100, Bruce Evans wrote: > On Wed, 29 Feb 2012, Luigi Rizzo wrote: > > >I have always been annoyed by the fact that FreeBSD rounds timeouts > >in select/usleep/poll in very conservative ways, so i decided to > >try how other systems behave in this respect. Attached is a simple > >program that you should be able to compile and run on various OS > >and see what happens. > > Many are broken, indeed. > > The simple program isn't attached. attachment stripped by the mailing list, retrying to put it inline (and comments on a followup email) ---- /* * test minimum select time * * ./prog usec [method [duration]] */ #include #include #include #include #include #include enum { M_SELECT =0 , M_POLL, M_USLEEP }; static const char *names[] = { "select", "poll", "usleep" }; int main(int argc, char *argv[]) { struct timeval ta, tb; int usec = 1, total = 0, method = M_SELECT, count = 0; if (argc > 1) usec = atoi(argv[1]); if (usec <= 0) usec = 1; else if (usec > 500000) usec = 500000; if (argc > 2) { if (!strcmp(argv[2], "poll")) method = M_POLL; else if (!strcmp(argv[2], "usleep")) method = M_USLEEP; } if (argc > 3) total = atoi(argv[3]); if (total < 1) total = 1; else if (total > 10) total = 10; fprintf(stderr, "testing %s for %dus over %ds\n", names[method], usec, total); gettimeofday(&ta, NULL); for (;;) { if (method == M_SELECT) { struct timeval to = { 0, usec }; select(0, NULL, NULL, NULL, &to); } else if (method == M_POLL) { poll(NULL, 0, usec/1000); } else { usleep(usec); } count++; gettimeofday(&tb, NULL); timersub(&tb, &ta, &tb); if (tb.tv_sec > total) break; } fprintf(stderr, "%dus actually took %dus\n", usec, (int)(tb.tv_sec * 1000000 + tb.tv_usec) / count ); return 0; } -----