mirror of
https://github.com/sheumann/hush.git
synced 2024-08-20 19:29:02 +00:00
libbb: introduce and use monotonic_ms
function old new delta monotonic_ms - 60 +60 process_stdin 433 443 +10 display_speed 85 90 +5 nmeter_main 672 674 +2 builtin_type 114 116 +2 bb__parsespent 117 119 +2 ifplugd_main 1110 1109 -1 acpid_main 441 440 -1 chat_main 1361 1359 -2 doCommands 2458 2449 -9 arpping 466 450 -16 run_command 268 234 -34 readcmd 1072 1034 -38 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 5/7 up/down: 81/-101) Total: -20 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
cb23d93c68
commit
f2c8aa6676
@ -249,6 +249,7 @@ extern int *const bb_errno;
|
|||||||
|
|
||||||
unsigned long long monotonic_ns(void) FAST_FUNC;
|
unsigned long long monotonic_ns(void) FAST_FUNC;
|
||||||
unsigned long long monotonic_us(void) FAST_FUNC;
|
unsigned long long monotonic_us(void) FAST_FUNC;
|
||||||
|
unsigned long long monotonic_ms(void) FAST_FUNC;
|
||||||
unsigned monotonic_sec(void) FAST_FUNC;
|
unsigned monotonic_sec(void) FAST_FUNC;
|
||||||
|
|
||||||
extern void chomp(char *s) FAST_FUNC;
|
extern void chomp(char *s) FAST_FUNC;
|
||||||
|
12
libbb/time.c
12
libbb/time.c
@ -175,6 +175,12 @@ unsigned long long FAST_FUNC monotonic_us(void)
|
|||||||
get_mono(&ts);
|
get_mono(&ts);
|
||||||
return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
|
return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
|
||||||
}
|
}
|
||||||
|
unsigned long long FAST_FUNC monotonic_ms(void)
|
||||||
|
{
|
||||||
|
struct timespec ts;
|
||||||
|
get_mono(&ts);
|
||||||
|
return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
|
||||||
|
}
|
||||||
unsigned FAST_FUNC monotonic_sec(void)
|
unsigned FAST_FUNC monotonic_sec(void)
|
||||||
{
|
{
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
@ -196,6 +202,12 @@ unsigned long long FAST_FUNC monotonic_us(void)
|
|||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
return tv.tv_sec * 1000000ULL + tv.tv_usec;
|
return tv.tv_sec * 1000000ULL + tv.tv_usec;
|
||||||
}
|
}
|
||||||
|
unsigned long long FAST_FUNC monotonic_ms(void)
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
|
||||||
|
}
|
||||||
unsigned FAST_FUNC monotonic_sec(void)
|
unsigned FAST_FUNC monotonic_sec(void)
|
||||||
{
|
{
|
||||||
return time(NULL);
|
return time(NULL);
|
||||||
|
@ -70,7 +70,7 @@ static void resuse_end(pid_t pid, resource_t *resp)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp->elapsed_ms = (monotonic_us() / 1000) - resp->elapsed_ms;
|
resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void printargv(char *const *argv)
|
static void printargv(char *const *argv)
|
||||||
@ -371,7 +371,7 @@ static void run_command(char *const *cmd, resource_t *resp)
|
|||||||
void (*interrupt_signal)(int);
|
void (*interrupt_signal)(int);
|
||||||
void (*quit_signal)(int);
|
void (*quit_signal)(int);
|
||||||
|
|
||||||
resp->elapsed_ms = monotonic_us() / 1000;
|
resp->elapsed_ms = monotonic_ms();
|
||||||
pid = vfork(); /* Run CMD as child process. */
|
pid = vfork(); /* Run CMD as child process. */
|
||||||
if (pid < 0)
|
if (pid < 0)
|
||||||
bb_perror_msg_and_die("fork");
|
bb_perror_msg_and_die("fork");
|
||||||
|
@ -88,7 +88,7 @@ int FAST_FUNC arpping(uint32_t test_nip,
|
|||||||
timeout_ms = 2000;
|
timeout_ms = 2000;
|
||||||
do {
|
do {
|
||||||
int r;
|
int r;
|
||||||
unsigned prevTime = monotonic_us();
|
unsigned prevTime = monotonic_ms();
|
||||||
|
|
||||||
pfd[0].events = POLLIN;
|
pfd[0].events = POLLIN;
|
||||||
r = safe_poll(pfd, 1, timeout_ms);
|
r = safe_poll(pfd, 1, timeout_ms);
|
||||||
@ -119,7 +119,7 @@ int FAST_FUNC arpping(uint32_t test_nip,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timeout_ms -= ((unsigned)monotonic_us() - prevTime) / 1000;
|
timeout_ms -= (unsigned)monotonic_ms() - prevTime;
|
||||||
} while (timeout_ms > 0);
|
} while (timeout_ms > 0);
|
||||||
|
|
||||||
ret:
|
ret:
|
||||||
|
@ -12619,7 +12619,7 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
|||||||
backslash = 0;
|
backslash = 0;
|
||||||
#if ENABLE_ASH_READ_TIMEOUT
|
#if ENABLE_ASH_READ_TIMEOUT
|
||||||
if (timeout) /* NB: ensuring end_ms is nonzero */
|
if (timeout) /* NB: ensuring end_ms is nonzero */
|
||||||
end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1;
|
end_ms = ((unsigned)monotonic_ms() + timeout) | 1;
|
||||||
#endif
|
#endif
|
||||||
STARTSTACKSTR(p);
|
STARTSTACKSTR(p);
|
||||||
do {
|
do {
|
||||||
@ -12630,7 +12630,7 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
|||||||
struct pollfd pfd[1];
|
struct pollfd pfd[1];
|
||||||
pfd[0].fd = fd;
|
pfd[0].fd = fd;
|
||||||
pfd[0].events = POLLIN;
|
pfd[0].events = POLLIN;
|
||||||
timeout = end_ms - (unsigned)(monotonic_us() / 1000);
|
timeout = end_ms - (unsigned)monotonic_ms();
|
||||||
if ((int)timeout <= 0 /* already late? */
|
if ((int)timeout <= 0 /* already late? */
|
||||||
|| safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
|
|| safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
|
||||||
) { /* timed out! */
|
) { /* timed out! */
|
||||||
|
Loading…
Reference in New Issue
Block a user