Hi, A while ago I wrote a sleep builtin mainly to test its impact in the boot process. The non-scientific results were not very impressive: around 1 second. So, I was cleaning up some directories and found the builtin. Instead of just nuking it, I'm forwarding it in case anyone wants to play with it or even merge it. If merged it will annoy those who are used to GNU's sleep(1) which supports the s/m/h/d suffixes, but not those using fractions as I added support for them. Back then, I also payed attention to the resulting size of the binary, which obviously increased by a few KBs. However, I do remember that when switching from gcc 4.4 to 4.6 the resulting binary with the sleep builtin was smaller than the binary built with 4.4 and without sleep. (interestingly, Debian's 0.5.7-3 dash is bigger than in 0.5.5.1-7) Cheers, -- Raphael Geissert - Debian Developer www.debian.org - get.debian.net
From 2dbe1edd6d355de98569175f7f76319778496d5d Mon Sep 17 00:00:00 2001 From: Raphael Geissert <atomo64@xxxxxxxxx> Date: Tue, 23 Nov 2010 21:47:20 -0600 Subject: [PATCH 1/2] [BUILTIN] Add sleep(1) built-in, similar to GNU's Using an external command results in an unecessary number of syscalls. While at it, provide an implementation that allows sleeping for fractions of seconds (pretty much like GNU coreutil's sleep(1).) Along with the reduced overhead, this provides a more fine-grained sleep period. Signed-off-by: Raphael Geissert <atomo64@xxxxxxxxx> --- src/Makefile.am | 3 +- src/bltin/sleep.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/builtins.def.in | 1 + src/dash.1 | 8 +++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/bltin/sleep.c diff --git a/src/Makefile.am b/src/Makefile.am index ba68d55..3289805 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,8 @@ dash_CFILES = \ alias.c arith_yacc.c arith_yylex.c cd.c error.c eval.c exec.c expand.c \ histedit.c input.c jobs.c mail.c main.c memalloc.c miscbltin.c \ mystring.c options.c parser.c redir.c show.c trap.c output.c \ - bltin/printf.c system.c bltin/test.c bltin/times.c var.c + bltin/printf.c system.c bltin/test.c bltin/times.c var.c \ + bltin/sleep.c dash_SOURCES = \ $(dash_CFILES) \ alias.h arith_yacc.h bltin/bltin.h cd.h error.h eval.h exec.h \ diff --git a/src/bltin/sleep.c b/src/bltin/sleep.c new file mode 100644 index 0000000..5a167d3 --- /dev/null +++ b/src/bltin/sleep.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2010 Raphael Geissert <geissert@xxxxxxxxxx> + * + * 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. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#include <errno.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <time.h> +#include "error.h" +#include "system.h" + +/* + * Built-in implementation of the sleep utility + * Conforms to POSIX-2001/SUSv3 and is still valid for POSIX-2008 + * Extra features: support fractions of seconds, + * just like GNU coreutil's sleep(1). + + * Exit status: 0: ok, 1: user error, 2: sys error + */ + +int sleepcmd(int argc, char **argv) { + struct timespec t; + double dt, diff; + uint64_t nsecs; + char *end; + + if (argc != 2) + goto usage; + + errno = 0; + dt = strtod(argv[1], &end); + + /* parsing errors */ + if (argv[1] == end || (argv[1]+strlen(argv[1])) > end) + goto invalid; + /* invalid numeric values */ + if (dt < 0 || errno != 0) + goto invalid; + + /* optimisation, avoid the syscall */ + if (dt == 0) + return 0; + + /* no attempt to detect an overflow is made, let nanosleep fail */ + t.tv_sec = dt; + t.tv_nsec = 0; + diff = (dt - (double)t.tv_sec); + if (diff < 0) { + diff *= -1; + t.tv_sec--; + } + t.tv_nsec = nsecs = diff * 1000000000; + /* + * a double's fraction is 52 bits, so we need a variable of 64bits + * to look for "would-overflows" without assuming that vars can + * in fact overflow. Since 1 billion fits in 32 bits, we only + * look for it to find invalid values (10^9 ns == 1s) + */ + if (nsecs >= 1000000000) + goto invalid; + + if (nanosleep(&t, NULL) != 0) + return 2; + return 0; + +invalid: + sh_warnx("invalid sleep interval"); + return 1; +usage: + sh_warnx("usage: sleep seconds"); + return 1; +} diff --git a/src/builtins.def.in b/src/builtins.def.in index 4441fe4..88f9cc9 100644 --- a/src/builtins.def.in +++ b/src/builtins.def.in @@ -92,3 +92,4 @@ ulimitcmd ulimit #endif testcmd test [ killcmd -u kill +sleepcmd -u sleep diff --git a/src/dash.1 b/src/dash.1 index ddeb52b..0398e50 100644 --- a/src/dash.1 +++ b/src/dash.1 @@ -1816,6 +1816,14 @@ by one. If n is greater than the number of positional parameters, .Ic shift will issue an error message, and exit with return status 2. +.It sleep Ar seconds +.It sleep Ar seconds.fraction +Sleep for at least the given number of seconds. +.Va fraction +has a precision of 1s-1ns. +The number of seconds it can sleep is up to the system's limit. The +current implementation does not attempt to sleep as most as the system +allows when the requested amount is greater than the limit. .It test Ar expression .It \&[ Ar expression Cm ] The -- 1.7.10
From 01eacc35c00da455f3e5d3a3d2e26e6fceb952e3 Mon Sep 17 00:00:00 2001 From: Raphael Geissert <atomo64@xxxxxxxxx> Date: Tue, 7 Dec 2010 19:53:53 -0600 Subject: [PATCH 2/2] [BUILTIN] Optimise sleep(1) built-in Avoid an extra variable and a useless call to strlen(3) Signed-off-by: Raphael Geissert <atomo64@xxxxxxxxx> --- src/bltin/sleep.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/bltin/sleep.c b/src/bltin/sleep.c index 5a167d3..b8b10b9 100644 --- a/src/bltin/sleep.c +++ b/src/bltin/sleep.c @@ -29,7 +29,6 @@ #include <errno.h> #include <stdlib.h> #include <stdint.h> -#include <string.h> #include <time.h> #include "error.h" #include "system.h" @@ -45,7 +44,7 @@ int sleepcmd(int argc, char **argv) { struct timespec t; - double dt, diff; + double dt; uint64_t nsecs; char *end; @@ -55,8 +54,11 @@ int sleepcmd(int argc, char **argv) { errno = 0; dt = strtod(argv[1], &end); - /* parsing errors */ - if (argv[1] == end || (argv[1]+strlen(argv[1])) > end) + /* + * parsing errors + * the first condition is needed to err out on *argv[1]=='\0' + */ + if (argv[1] == end || *end != '\0') goto invalid; /* invalid numeric values */ if (dt < 0 || errno != 0) @@ -64,17 +66,17 @@ int sleepcmd(int argc, char **argv) { /* optimisation, avoid the syscall */ if (dt == 0) - return 0; + return 0; /* no attempt to detect an overflow is made, let nanosleep fail */ - t.tv_sec = dt; - t.tv_nsec = 0; - diff = (dt - (double)t.tv_sec); - if (diff < 0) { - diff *= -1; + t.tv_sec = (time_t)dt; + dt -= (double)t.tv_sec; + /* detect if it was rounded up */ + if (dt < 0) { + dt *= -1; t.tv_sec--; } - t.tv_nsec = nsecs = diff * 1000000000; + t.tv_nsec = nsecs = dt * 1000000000; /* * a double's fraction is 52 bits, so we need a variable of 64bits * to look for "would-overflows" without assuming that vars can -- 1.7.10