wget: use monotonic_sec instead of gettimeofday

zcip: use monotonic_us instead of gettimeofday
udhcpcd: simpler, shorter random_xid()

function                                             old     new   delta
monotonic_sec                                          -      41     +41
find_pair                                            164     180     +16
run_list_real                                       2018    2028     +10
cmp_main                                             547     555      +8
collect_ctx                                          112     119      +7
singlemount                                         4544    4549      +5
time_main                                           1124    1128      +4
static.start_sec                                       -       4      +4
static.lastupdate_sec                                  -       4      +4
sock                                                   -       4      +4
read_package_field                                   253     257      +4
pick                                                  38      40      +2
get_next_line                                        145     147      +2
count_lines                                           59      61      +2
process_stdin                                        435     433      -2
xstrtoul_range_sfx                                   229     226      -3
static.initialized                                     4       1      -3
dhcprelay_main                                      1125    1122      -3
catcher                                              380     377      -3
arping_main                                         1969    1966      -3
s                                                      8       4      -4
cfg                                                    4       -      -4
static.lastupdate                                      8       -      -8
start                                                  8       -      -8
random_xid                                            95      33     -62
.rodata                                           129114  129050     -64
zcip_main                                           1731    1576    -155
progressmeter                                       1035     867    -168
------------------------------------------------------------------------------
(add/remove: 4/3 grow/shrink: 10/11 up/down: 113/-490)       Total: -377 bytes
This commit is contained in:
Denis Vlasenko
2007-06-17 23:40:26 +00:00
parent 459be35234
commit bd7bb299c0
8 changed files with 154 additions and 147 deletions

View File

@ -38,7 +38,6 @@ static bool chunked; /* chunked transfer encoding */
#if ENABLE_FEATURE_WGET_STATUSBAR
static void progressmeter(int flag);
static const char *curfile; /* Name of current file being transferred */
static struct timeval start; /* Time a transfer started */
enum {
STALLTIME = 5 /* Seconds when xfer considered "stalled" */
};
@ -683,30 +682,30 @@ static void alarmtimer(int iwait)
setitimer(ITIMER_REAL, &itv, NULL);
}
static void
progressmeter(int flag)
{
static struct timeval lastupdate;
static unsigned lastupdate_sec;
static unsigned start_sec;
static off_t lastsize, totalsize;
struct timeval now, td, tvwait;
off_t abbrevsize;
int elapsed, ratio, barlength, i;
unsigned since_last_update, elapsed;
unsigned ratio;
int barlength, i;
if (flag == -1) { /* first call to progressmeter */
gettimeofday(&start, NULL);
lastupdate = start;
start_sec = monotonic_sec();
lastupdate_sec = start_sec;
lastsize = 0;
totalsize = content_len + beg_range; /* as content_len changes.. */
}
gettimeofday(&now, NULL);
ratio = 100;
if (totalsize != 0 && !chunked) {
/* long long helps to have working ETA even if !LFS */
ratio = (int) (100ULL * (transferred+beg_range) / totalsize);
ratio = MIN(ratio, 100);
/* long long helps to have it working even if !LFS */
ratio = (unsigned) (100ULL * (transferred+beg_range) / totalsize);
if (ratio > 100) ratio = 100;
}
fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
@ -714,12 +713,13 @@ progressmeter(int flag)
barlength = getttywidth() - 51;
if (barlength > 0) {
/* god bless gcc for variable arrays :) */
char buf[barlength+1];
i = barlength * ratio / 100;
memset(buf, '*', i);
memset(buf + i, ' ', barlength - i);
buf[barlength] = '\0';
fprintf(stderr, "|%s|", buf);
{
char buf[i+1];
memset(buf, '*', i);
buf[i] = '\0';
fprintf(stderr, "|%s%*s|", buf, barlength - i, "");
}
}
i = 0;
abbrevsize = transferred + beg_range;
@ -730,22 +730,28 @@ progressmeter(int flag)
/* see http://en.wikipedia.org/wiki/Tera */
fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
timersub(&now, &lastupdate, &tvwait);
if (transferred > lastsize) {
lastupdate = now;
lastsize = transferred;
if (tvwait.tv_sec >= STALLTIME)
timeradd(&start, &tvwait, &start);
tvwait.tv_sec = 0;
}
timersub(&now, &start, &td);
elapsed = td.tv_sec;
// Nuts! Ain't it easier to update progress meter ONLY when we transferred++?
// FIXME: get rid of alarmtimer + updateprogressmeter mess
if (tvwait.tv_sec >= STALLTIME) {
elapsed = monotonic_sec();
since_last_update = elapsed - lastupdate_sec;
if (transferred > lastsize) {
lastupdate_sec = elapsed;
lastsize = transferred;
if (since_last_update >= STALLTIME) {
/* We "cut off" these seconds from elapsed time
* by adjusting start time */
start_sec += since_last_update;
}
since_last_update = 0; /* we are un-stalled now */
}
elapsed -= start_sec; /* now it's "elapsed since start" */
if (since_last_update >= STALLTIME) {
fprintf(stderr, " - stalled -");
} else {
off_t to_download = totalsize - beg_range;
if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) {
if (transferred <= 0 || (int)elapsed <= 0 || transferred > to_download || chunked) {
fprintf(stderr, "--:--:-- ETA");
} else {
/* to_download / (transferred/elapsed) - elapsed: */