From owner-freebsd-bugs@FreeBSD.ORG Tue Mar 29 18:30:07 2005 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B389916A4CE for ; Tue, 29 Mar 2005 18:30:07 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 494C143D3F for ; Tue, 29 Mar 2005 18:30:07 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.3/8.13.3) with ESMTP id j2TIU7Rh070715 for ; Tue, 29 Mar 2005 18:30:07 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.3/8.13.1/Submit) id j2TIU7t7070711; Tue, 29 Mar 2005 18:30:07 GMT (envelope-from gnats) Resent-Date: Tue, 29 Mar 2005 18:30:07 GMT Resent-Message-Id: <200503291830.j2TIU7t7070711@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Joshua Coombs Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7B85B16A4CE for ; Tue, 29 Mar 2005 18:25:18 +0000 (GMT) Received: from www.freebsd.org (www.freebsd.org [216.136.204.117]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2282B43D45 for ; Tue, 29 Mar 2005 18:25:18 +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 j2TIPHT1084231 for ; Tue, 29 Mar 2005 18:25:17 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.13.1/8.13.1/Submit) id j2TIPHXl084230; Tue, 29 Mar 2005 18:25:17 GMT (envelope-from nobody) Message-Id: <200503291825.j2TIPHXl084230@www.freebsd.org> Date: Tue, 29 Mar 2005 18:25:17 GMT From: Joshua Coombs To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-2.3 Subject: kern/79339: Kernel time code sync with improvements from DragonFly X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Mar 2005 18:30:07 -0000 >Number: 79339 >Category: kern >Synopsis: Kernel time code sync with improvements from DragonFly >Confidential: no >Severity: serious >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: update >Submitter-Id: current-users >Arrival-Date: Tue Mar 29 18:30:06 GMT 2005 >Closed-Date: >Last-Modified: >Originator: Joshua Coombs >Release: 4.11-Release >Organization: >Environment: FreeBSD salyavin.gwi.net 4.11-RELEASE FreeBSD 4.11-RELEASE #1: Tue Mar 29 12:43:42 EST 2005 root@salyavin.gwi.net:/usr/src/sys/compile/GWI i386 >Description: Based on: http://www.dragonflybsd.org/docs/nanosleep/ The improvements to kernel timekeeping appear to apply directly to FreeBSD 4.x based on my own quick testing. >How-To-Repeat: /* * Copyright (c) 2003 Paul Herman * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * $DragonFly: site/data/docs/nanosleep/wakeup_latency.c,v 1.1 2004/01/22 21:55:58 justin Exp $ */ #include #include #include #include #include #include #define ONE_SECOND 1000000L int count = 200; int debug = 0; int main (int ac, char **av) { long s; double diff; struct timeval tv1, tv2; if (ac > 1 && av[1]) count = strtol(av[1], NULL, 10); while(count--) { gettimeofday(&tv1, NULL); /* * Calculate the number of microseconds to sleep so we * can wakeup right when the second hand hits zero. * * The latency for the following two statements is minimal. * On a > 1.0GHz machine, the subtraction is done in a few * nanoseconds, and the syscall to usleep/nanosleep is usualy * less than 800 ns or 0.8 us. */ s = ONE_SECOND - tv1.tv_usec; usleep(s); gettimeofday(&tv2, NULL); diff = (double)(tv2.tv_usec - (tv1.tv_usec + s))/1e6; diff += (double)(tv2.tv_sec - tv1.tv_sec); if (debug) printf("(%ld.%.6ld) ", tv2.tv_sec, tv2.tv_usec); printf("%.6f\n", diff); } return 0; } 4.11 returns the following sample data: 0.016126 0.016146 0.016162 0.016181 0.016199 0.016218 0.016238 0.016259 0.016274 0.016292 0.016310 0.016342 0.016359 0.016366 >Fix: /usr/src/sys/kern/kern_clock.c 325c325 < / tick + 1; --- > / tick; 328c328 < + ((unsigned long)usec + (tick - 1)) / tick + 1; --- > + ((unsigned long)usec + (tick - 1)) / tick; /usr/src/sys/kern/kern_time.c 232c232 < int error; --- > int error, sleepticks; 241a242 > sleepticks = tvtohz(&tv); 243c244 < tvtohz(&tv)); --- > (sleepticks < 1)? 1 : sleepticks); 252c253,254 < *rmt = ts; --- > rmt->tv_sec = ts.tv_sec; > rmt->tv_nsec = ts.tv_nsec; 258c260,261 < ts3 = ts; --- > ts3.tv_sec = ts.tv_sec; > ts3.tv_nsec = ts.tv_nsec; 260a264,265 > if (tv.tv_sec == 0 && tv.tv_usec < tick) > return (0); /usr/src/sys/i386/isa/clock.c 113c113,114 < #define TIMER_DIV(x) ((timer_freq + (x) / 2) / (x)) --- > #define TIMER_DIV(x) (timer_freq / (x)) > #define FRAC_ADJUST(x) (timer_freq - ((timer freq / (x)) * (x))) 141a143 > u_int timer0_frac_freq; 204a207,209 > int phase; > int delta; > 215a221,236 > > phase = 1000000 / timer0_frac_freq; > delta = timecounter->tc_microtime.tv_usec % phase; > #if 1 > disable_intr(); > if (delta < (phase >> 1)) { > outb(TIMER_CNTR0, timer0_max_count & 0xff); > outb(TIMER_CNTR0, timer0_max_count >> 8); > } else { > outb(TIMER_CNTR0, (timer0_max_count +1) & 0xff); > outb(TIMER_CNTR0, (timer0_max_count +1) >> 8); > ++i8254_offset; > } > enable_intr(); > #endif > 236a258 > timer0_frac_freq = new_rate; 247,248c269,270 < if ((timer0_prescaler_count += timer0_max_count) < >= hardclock_max_count) { --- > timer0_prescaler_count += timer0_max_count; > if (timer0_prescaler_count >= hardclock_max_count) { 689a712 > timer0_frac_freq = intr_freq; 1221c1244 < count = timer0_max_count - ((high << 8) | low); --- > count = timer0_max_count + 1 - ((high << 8) | low); Note, the diffs above are just the code, proper credit must be given to Paul Herman and Matt Dillon, in addition the DFly patches listed at the source url contain comments indicating what the code is doing. Sample post patch test data: 0.000020 0.000226 0.000336 0.000284 0.000234 0.000187 0.000132 0.000082 0.000035 0.000242 0.000348 0.000295 0.000246 0.000192 0.000137 0.000090 0.000043 0.000252 0.000361 0.000307 A sawtooth is still present, but the accuracy is MUCH better. I suspect my hack application of the PLL function isn't correct or my P133 is slow enough that I'm observing some other latencies. I have observed occasional negative offsets, which according to the article are strictly forbidden by RFCs, so please check my work. I believe they were the result of my playing with a hz value too high for the machine to reasonably handle, and are not occuring with saner values for hz. >Release-Note: >Audit-Trail: >Unformatted: