sleep builtin
PATCH 1/1 sleep builtin
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,
Signature · 2 lines
Raphael Geissert - Debian Developer www.debian.org - get.debian.net
From 2dbe1edd6d355de98569175f7f76319778496d5d Mon Sep 17 00:00:00 2001
From: Raphael Geissert <[email protected]>
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.
--- 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
src/Makefile.am +2 -1
@@ -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 \
src/bltin/sleep.c new file +97 -0
97 lines not on this page · raw message
src/builtins.def.in +1 -0
@@ -92,3 +92,4 @@ ulimitcmd ulimit #endif testcmd test [ killcmd -u kill +sleepcmd -u sleep
src/dash.1 +8 -0
@@ -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
Signature · 1 line
1.7.10
From 01eacc35c00da455f3e5d3a3d2e26e6fceb952e3 Mon Sep 17 00:00:00 2001
From: Raphael Geissert <[email protected]>
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)
--- src/bltin/sleep.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
src/bltin/sleep.c +13 -11
@@ -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
Signature · 1 line
1.7.10
Quoted 3 lines · Raphael Geissert
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.
Anything statistically significant can be impressive, taking into account how many other things also happen and are unaffected by the change.
If it is not statistically significant it can just be noise.
Quoted 6 lines
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.
Quoted 8 lines
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)
[patch snipped]
POSIX says any utility may be provided as a builtin as long as this is not detectable apart from performance (and {ARG_MAX} I guess).
Some ksh variants (ksh93 and mksh) have a sleep builtin, so it is likely fairly safe to do this.
However, it will need better handling of [EINTR]. The included patch simply exits with status 2 when it happens, which differs from what an instance of /bin/sleep would do (assuming the signal is sent to parent and child). If the signal is one that is ignored by default such as SIGCHLD, /bin/sleep ignores it and the shell only takes the trap when sleep is done. If the signal is one that causes termination by default, /bin/sleep terminates on it and the shell returns exit status 128 plus the signal number after which it takes the trap. Unfortunately, determining the default action of a non-standard signal is annoying in a portable program.
It is probably acceptable that an interactive sleep command cannot be ^Z'ed.
Signature · 1 line
Jilles Tjoelker