From owner-svn-src-head@FreeBSD.ORG Wed Oct 17 07:58:28 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 3D0F261D for ; Wed, 17 Oct 2012 07:58:28 +0000 (UTC) (envelope-from andrey@zonov.org) Received: from mail-la0-f54.google.com (mail-la0-f54.google.com [209.85.215.54]) by mx1.freebsd.org (Postfix) with ESMTP id 9A3F58FC19 for ; Wed, 17 Oct 2012 07:58:27 +0000 (UTC) Received: by mail-la0-f54.google.com with SMTP id e12so5917774lag.13 for ; Wed, 17 Oct 2012 00:58:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:x-enigmail-version:content-type :x-gm-message-state; bh=7tmtnCXHQ4y5XeMNMHR6XJBiGcFR0X2u66nA/8NZYB0=; b=ojVCyHJ+t8cI5vlykFgXByBMJZ9rapmZKW9TWh4MZV1noH/mXJkuAQv7O4PC4Q8wiu eh027nToPSR2/ZTcccpAoc3YQCgm4jkJCC2tHxJVTc7Gip8mIJyXzqYBSIPpU9HRxO3u Z/1zvaDXZcthbsFVj7ff3ANKM5v4SZBfPJ5ic8usxBh1FgwR3SaGUBOG7qwaSJsG0s5b U+FkF7+TGDsgYFqSQAzrsuemMYf5wJ2cv+SzVVN+mC6QlD/7Clj8q3T0yWQQz9z2YVZC xb6wDw6gDSMS8cz7Nf3NcNNWkiO4i4FlYEfkESbqO/4YZJ3wa+AzePlH853yRfUH2BQK a2vg== Received: by 10.112.44.198 with SMTP id g6mr6300838lbm.66.1350460705124; Wed, 17 Oct 2012 00:58:25 -0700 (PDT) Received: from zont-osx.local (ppp95-165-155-241.pppoe.spdop.ru. [95.165.155.241]) by mx.google.com with ESMTPS id jk8sm6188649lab.7.2012.10.17.00.58.23 (version=SSLv3 cipher=OTHER); Wed, 17 Oct 2012 00:58:24 -0700 (PDT) Sender: Andrey Zonov Message-ID: <507E651D.2060809@FreeBSD.org> Date: Wed, 17 Oct 2012 11:58:21 +0400 From: Andrey Zonov User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: Maxim Sobolev Subject: Re: svn commit: r241625 - head/usr.sbin/cron/cron References: <201210170044.q9H0iZHo055977@svn.freebsd.org> In-Reply-To: <201210170044.q9H0iZHo055977@svn.freebsd.org> X-Enigmail-Version: 1.4.5 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig31F401DC21F5684E424370A8" X-Gm-Message-State: ALoCoQntfShXI/0Iqeb8SED1ijqOW4e5tcah1/MX1OML7ij1Ad4gsTWgod6RnF1taSWZEfw6wSpe Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Oct 2012 07:58:28 -0000 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig31F401DC21F5684E424370A8 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 10/17/12 4:44 AM, Maxim Sobolev wrote: > Author: sobomax > Date: Wed Oct 17 00:44:34 2012 > New Revision: 241625 > URL: http://svn.freebsd.org/changeset/base/241625 >=20 > Log: > o Use nanosleep(2) to sleep exact amount of time till the next second= , > not multiple of 1 second, which results in actual time to drift back > and forth every run within 1 second of the actual action has > been set for. > =20 > Suggested by: Ian Lepore > =20 > o Schedule the first run in 1 second after starting up, not on the > boundary of the next minute, which results in the every_second jobs > not being run. >=20 > Modified: > head/usr.sbin/cron/cron/cron.c >=20 > Modified: head/usr.sbin/cron/cron/cron.c > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D > --- head/usr.sbin/cron/cron/cron.c Wed Oct 17 00:33:10 2012 (r241624) > +++ head/usr.sbin/cron/cron/cron.c Wed Oct 17 00:44:34 2012 (r241625) > @@ -341,37 +341,73 @@ cron_tick(db) > */ > static void > cron_sync() { > +#if 0 > register struct tm *tm; > +#endif > =20 > - TargetTime =3D time((time_t*)0); > + TargetTime =3D time((time_t*)0) + 1; > +#if 0 > tm =3D localtime(&TargetTime); > TargetTime +=3D (60 - tm->tm_sec); > +#endif > } > =20 > +static int This should be type of time_t. > +timeval_subtract(struct timespec *result, struct timeval *x, struct ti= meval *y) > +{ > + int nsec; And this should be suseconds_t. > + > + /* Perform the carry for the later subtraction by updating y. */ > + if (x->tv_usec < y->tv_usec) { > + nsec =3D (y->tv_usec - x->tv_usec) / 1000000 + 1; > + y->tv_usec -=3D 1000000 * nsec; > + y->tv_sec +=3D nsec; > + } > + if (x->tv_usec - y->tv_usec > 1000000) { > + nsec =3D (x->tv_usec - y->tv_usec) / 1000000; > + y->tv_usec +=3D 1000000 * nsec; > + y->tv_sec -=3D nsec; > + } > + =20 > + /* tv_nsec is certainly positive. */ > + result->tv_sec =3D x->tv_sec - y->tv_sec; > + result->tv_nsec =3D (x->tv_usec - y->tv_usec) * 1000; > + =20 > + /* Return difference in seconds */ > + return (x->tv_sec - y->tv_sec); > +} May be it's better to use timersub() and TIMEVAL_TO_TIMESPEC()? > =20 > static void > cron_sleep(db) > cron_db *db; > { > - int seconds_to_wait =3D 0; > + int seconds_to_wait; > + int rval; > + struct timeval ctime, ttime; > + struct timespec stime, remtime; > =20 > /* > * Loop until we reach the top of the next minute, sleep when possibl= e. > */ > =20 > for (;;) { > - seconds_to_wait =3D (int) (TargetTime - time((time_t*)0)); > + gettimeofday(&ctime, NULL); > + ttime.tv_sec =3D TargetTime; > + ttime.tv_usec =3D 0; > + timeval_subtract(&stime, &ttime, &ctime); > =20 > /* > * If the seconds_to_wait value is insane, jump the cron > */ > =20 > - if (seconds_to_wait < -600 || seconds_to_wait > 600) { > + if (stime.tv_sec < -600 || stime.tv_sec > 600) { > cron_clean(db); > cron_sync(); > continue; > } > =20 > + seconds_to_wait =3D (stime.tv_nsec > 0) ? stime.tv_sec + 1 : stime.t= v_sec; > + > Debug(DSCH, ("[%d] TargetTime=3D%ld, sec-to-wait=3D%d\n", > getpid(), (long)TargetTime, seconds_to_wait)) > =20 > @@ -380,13 +416,19 @@ cron_sleep(db) > * to run, break > */ > =20 > - if (seconds_to_wait <=3D 0) > + if (stime.tv_sec < 0) > break; > if (job_runqueue() =3D=3D 0) { > Debug(DSCH, ("[%d] sleeping for %d seconds\n", > getpid(), seconds_to_wait)) > =20 > - sleep(seconds_to_wait); > + for (;;) { > + rval =3D nanosleep(&stime, &remtime); > + if (rval =3D=3D 0 || errno !=3D EINTR) > + break; > + stime.tv_sec =3D remtime.tv_sec; > + stime.tv_nsec =3D remtime.tv_nsec; > + } > } > } > } >=20 --=20 Andrey Zonov --------------enig31F401DC21F5684E424370A8 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.18 (Darwin) Comment: GPGTools - http://gpgtools.org iQEcBAEBAgAGBQJQfmUhAAoJEBWLemxX/CvTiXkH/jqGbd/nVeCJw4h2yKvXhL8y 4SMPIWJ+mHwtgPm4uhYNJ0MrZzVpmt1VwOzgyCz1SKPVvLDFP5KF2RJDs2hQFJn7 uMTaazdK6zOirNWjXj2YsFSERz/LdvxEF4aOPOqe6qQYtlZE7L8cF7rMCWPgPhm0 RFFVqX7eUS3yJOMfSG/hInnyt0RSPXnhw3i3531mnQKJloq3G36sr9McS11IYsaC YhUn4h+/HIDeHi9UN2Nl4n1Obmm9zOiSyiZcn+8w/4wAsv7O7qsxqyq1Y0SLDQlL 2IuQ8GR1qCc/lfT9oQjLHH04u5hwmpcogyImojRXh/XZDXSpirIVG6mKmfoC+5A= =SufR -----END PGP SIGNATURE----- --------------enig31F401DC21F5684E424370A8--