From owner-freebsd-current@FreeBSD.ORG Fri Oct 28 20:44:38 2005 Return-Path: X-Original-To: current@freebsd.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2A34616A41F for ; Fri, 28 Oct 2005 20:44:38 +0000 (GMT) (envelope-from cswiger@mac.com) Received: from pi.codefab.com (pi.codefab.com [199.103.21.227]) by mx1.FreeBSD.org (Postfix) with ESMTP id 56EC243D53 for ; Fri, 28 Oct 2005 20:44:37 +0000 (GMT) (envelope-from cswiger@mac.com) Received: from localhost (localhost [127.0.0.1]) by pi.codefab.com (Postfix) with ESMTP id 3A13D5CB6; Fri, 28 Oct 2005 16:44:36 -0400 (EDT) Received: from pi.codefab.com ([127.0.0.1]) by localhost (pi.codefab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 29026-04; Fri, 28 Oct 2005 16:44:35 -0400 (EDT) Received: from [192.168.1.3] (pool-68-161-122-227.ny325.east.verizon.net [68.161.122.227]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by pi.codefab.com (Postfix) with ESMTP id 2C2715C21; Fri, 28 Oct 2005 16:44:35 -0400 (EDT) Message-ID: <43628DB3.6070402@mac.com> Date: Fri, 28 Oct 2005 16:44:35 -0400 From: Chuck Swiger Organization: The Courts of Chaos User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Benjamin Lutz References: <26845.1130452524@critter.freebsd.dk> <436167D5.2060104@mac.com> <43627CCB.2060508@datacomm.ch> In-Reply-To: <43627CCB.2060508@datacomm.ch> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: amavisd-new at codefab.com Cc: current@freebsd.org Subject: Re: Timers and timing, was: MySQL Performance 6.0rc1 X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 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: Fri, 28 Oct 2005 20:44:38 -0000 Benjamin Lutz wrote: > Chuck Swiger wrote: >>For one case, I have some code which needs to update statistics like >>"packets sent per second" (or "per minute" or "per hour") on a periodic >>basis. I use a reasonable timeout-- ~50ms-- for a call to select() (or >>pcap_dispatch(), etc) so I check time() perhaps 20 times a second, and >>then update my per-second stats when I notice that time(&now) returns a >>different value. >> >>Is there a better way of running code once a second, as close to the >>time the clock ticks? > > Once you've seen the clock tick, you can then probably use a longer > timeout in the next select, something in the 700ms range? I use alarm() to schedule period wakeups at one-minute, hourly etc intervals, after doing a shuffle to sync with localtime() and the fractional second part so that they are neatly aligned. Well, the issue is that these calls don't always return within the time you give them, and the pcap_dispatch() timer doesn't even start counting until it sees a packet, so my choice of timeout length is driven more by how much off am I willing to be if the call returns late. Giving them a half-second timeout sometimes seems to mean up to a 1.5 or 2 second pause if the network is completely quiet. [1] Take care, -- -Chuck [1]: If I don't use threaded DNS resolver code that was foisted upon me and avoid -pthreads it's better. :-)