📄 progress.c
字号:
log_set_flush (false); if (dp->dots == 0) logprintf (LOG_VERBOSE, "\n%6sK", number_to_static_string (dp->rows * ROW_BYTES / 1024)); for (i = dp->dots; i < opt.dots_in_line; i++) { if (i % opt.dot_spacing == 0) logputs (LOG_VERBOSE, " "); logputs (LOG_VERBOSE, " "); } print_row_stats (dp, dltime, true); logputs (LOG_VERBOSE, "\n\n"); log_set_flush (false); xfree (dp);}/* This function interprets the progress "parameters". For example, if Wget is invoked with --progress=dot:mega, it will set the "dot-style" to "mega". Valid styles are default, binary, mega, and giga. */static voiddot_set_params (const char *params){ if (!params || !*params) params = opt.dot_style; if (!params) return; /* We use this to set the retrieval style. */ if (!strcasecmp (params, "default")) { /* Default style: 1K dots, 10 dots in a cluster, 50 dots in a line. */ opt.dot_bytes = 1024; opt.dot_spacing = 10; opt.dots_in_line = 50; } else if (!strcasecmp (params, "binary")) { /* "Binary" retrieval: 8K dots, 16 dots in a cluster, 48 dots (384K) in a line. */ opt.dot_bytes = 8192; opt.dot_spacing = 16; opt.dots_in_line = 48; } else if (!strcasecmp (params, "mega")) { /* "Mega" retrieval, for retrieving very long files; each dot is 64K, 8 dots in a cluster, 6 clusters (3M) in a line. */ opt.dot_bytes = 65536L; opt.dot_spacing = 8; opt.dots_in_line = 48; } else if (!strcasecmp (params, "giga")) { /* "Giga" retrieval, for retrieving very very *very* long files; each dot is 1M, 8 dots in a cluster, 4 clusters (32M) in a line. */ opt.dot_bytes = (1L << 20); opt.dot_spacing = 8; opt.dots_in_line = 32; } else fprintf (stderr, _("Invalid dot style specification `%s'; leaving unchanged.\n"), params);}/* "Thermometer" (bar) progress. *//* Assumed screen width if we can't find the real value. */#define DEFAULT_SCREEN_WIDTH 80/* Minimum screen width we'll try to work with. If this is too small, create_image will overflow the buffer. */#define MINIMUM_SCREEN_WIDTH 45/* The last known screen width. This can be updated by the code that detects that SIGWINCH was received (but it's never updated from the signal handler). */static int screen_width;/* A flag that, when set, means SIGWINCH was received. */static volatile sig_atomic_t received_sigwinch;/* Size of the download speed history ring. */#define DLSPEED_HISTORY_SIZE 20/* The minimum time length of a history sample. By default, each sample is at least 150ms long, which means that, over the course of 20 samples, "current" download speed spans at least 3s into the past. */#define DLSPEED_SAMPLE_MIN 0.15/* The time after which the download starts to be considered "stalled", i.e. the current bandwidth is not printed and the recent download speeds are scratched. */#define STALL_START_TIME 5/* Time between screen refreshes will not be shorter than this, so that Wget doesn't swamp the TTY with output. */#define REFRESH_INTERVAL 0.2/* Don't refresh the ETA too often to avoid jerkiness in predictions. This allows ETA to change approximately once per second. */#define ETA_REFRESH_INTERVAL 0.99struct bar_progress { wgint initial_length; /* how many bytes have been downloaded previously. */ wgint total_length; /* expected total byte count when the download finishes */ wgint count; /* bytes downloaded so far */ double last_screen_update; /* time of the last screen update, measured since the beginning of download. */ int width; /* screen width we're using at the time the progress gauge was created. this is different from the screen_width global variable in that the latter can be changed by a signal. */ char *buffer; /* buffer where the bar "image" is stored. */ int tick; /* counter used for drawing the progress bar where the total size is not known. */ /* The following variables (kept in a struct for namespace reasons) keep track of recent download speeds. See bar_update() for details. */ struct bar_progress_hist { int pos; double times[DLSPEED_HISTORY_SIZE]; wgint bytes[DLSPEED_HISTORY_SIZE]; /* The sum of times and bytes respectively, maintained for efficiency. */ double total_time; wgint total_bytes; } hist; double recent_start; /* timestamp of beginning of current position. */ wgint recent_bytes; /* bytes downloaded so far. */ bool stalled; /* set when no data arrives for longer than STALL_START_TIME, then reset when new data arrives. */ /* create_image() uses these to make sure that ETA information doesn't flicker. */ double last_eta_time; /* time of the last update to download speed and ETA, measured since the beginning of download. */ int last_eta_value;};static void create_image (struct bar_progress *, double, bool);static void display_image (char *);static void *bar_create (wgint initial, wgint total){ struct bar_progress *bp = xnew0 (struct bar_progress); /* In theory, our callers should take care of this pathological case, but it can sometimes happen. */ if (initial > total) total = initial; bp->initial_length = initial; bp->total_length = total; /* Initialize screen_width if this hasn't been done or if it might have changed, as indicated by receiving SIGWINCH. */ if (!screen_width || received_sigwinch) { screen_width = determine_screen_width (); if (!screen_width) screen_width = DEFAULT_SCREEN_WIDTH; else if (screen_width < MINIMUM_SCREEN_WIDTH) screen_width = MINIMUM_SCREEN_WIDTH; received_sigwinch = 0; } /* - 1 because we don't want to use the last screen column. */ bp->width = screen_width - 1; /* + enough space for the terminating zero, and hopefully enough room * for multibyte characters. */ bp->buffer = xmalloc (bp->width + 100); logputs (LOG_VERBOSE, "\n"); create_image (bp, 0, false); display_image (bp->buffer); return bp;}static void update_speed_ring (struct bar_progress *, wgint, double);static voidbar_update (void *progress, wgint howmuch, double dltime){ struct bar_progress *bp = progress; bool force_screen_update = false; bp->count += howmuch; if (bp->total_length > 0 && bp->count + bp->initial_length > bp->total_length) /* We could be downloading more than total_length, e.g. when the server sends an incorrect Content-Length header. In that case, adjust bp->total_length to the new reality, so that the code in create_image() that depends on total size being smaller or equal to the expected size doesn't abort. */ bp->total_length = bp->initial_length + bp->count; update_speed_ring (bp, howmuch, dltime); /* If SIGWINCH (the window size change signal) been received, determine the new screen size and update the screen. */ if (received_sigwinch) { int old_width = screen_width; screen_width = determine_screen_width (); if (!screen_width) screen_width = DEFAULT_SCREEN_WIDTH; else if (screen_width < MINIMUM_SCREEN_WIDTH) screen_width = MINIMUM_SCREEN_WIDTH; if (screen_width != old_width) { bp->width = screen_width - 1; bp->buffer = xrealloc (bp->buffer, bp->width + 100); force_screen_update = true; } received_sigwinch = 0; } if (dltime - bp->last_screen_update < REFRESH_INTERVAL && !force_screen_update) /* Don't update more often than five times per second. */ return; create_image (bp, dltime, false); display_image (bp->buffer); bp->last_screen_update = dltime;}static voidbar_finish (void *progress, double dltime){ struct bar_progress *bp = progress; if (bp->total_length > 0 && bp->count + bp->initial_length > bp->total_length) /* See bar_update() for explanation. */ bp->total_length = bp->initial_length + bp->count; create_image (bp, dltime, true); display_image (bp->buffer); logputs (LOG_VERBOSE, "\n\n"); xfree (bp->buffer); xfree (bp);}/* This code attempts to maintain the notion of a "current" download speed, over the course of no less than 3s. (Shorter intervals produce very erratic results.) To do so, it samples the speed in 150ms intervals and stores the recorded samples in a FIFO history ring. The ring stores no more than 20 intervals, hence the history covers the period of at least three seconds and at most 20 reads into the past. This method should produce reasonable results for downloads ranging from very slow to very fast. The idea is that for fast downloads, we get the speed over exactly the last three seconds. For slow downloads (where a network read takes more than 150ms to complete), we get the speed over a larger time period, as large as it takes to complete thirty reads. This is good because slow downloads tend to fluctuate more and a 3-second average would be too erratic. */static voidupdate_speed_ring (struct bar_progress *bp, wgint howmuch, double dltime){ struct bar_progress_hist *hist = &bp->hist; double recent_age = dltime - bp->recent_start; /* Update the download count. */ bp->recent_bytes += howmuch; /* For very small time intervals, we return after having updated the "recent" download count. When its age reaches or exceeds minimum sample time, it will be recorded in the history ring. */ if (recent_age < DLSPEED_SAMPLE_MIN) return; if (howmuch == 0) { /* If we're not downloading anything, we might be stalling, i.e. not downloading anything for an extended period of time. Since 0-reads do not enter the history ring, recent_age effectively measures the time since last read. */ if (recent_age >= STALL_START_TIME) { /* If we're stalling, reset the ring contents because it's stale and because it will make bar_update stop printing the (bogus) current bandwidth. */ bp->stalled = true; xzero (*hist); bp->recent_bytes = 0; } return; } /* We now have a non-zero amount of to store to the speed ring. */ /* If the stall status was acquired, reset it. */ if (bp->stalled) { bp->stalled = false; /* "recent_age" includes the the entired stalled period, which could be very long. Don't update the speed ring with that value because the current bandwidth would start too small. Start with an arbitrary (but more reasonable) time value and let it level out. */ recent_age = 1; } /* Store "recent" bytes and download time to history ring at the position POS. */ /* To correctly maintain the totals, first invalidate existing data (least recent in time) at this position. */ hist->total_time -= hist->times[hist->pos]; hist->total_bytes -= hist->bytes[hist->pos]; /* Now store the new data and update the totals. */ hist->times[hist->pos] = recent_age; hist->bytes[hist->pos] = bp->recent_bytes; hist->total_time += recent_age; hist->total_bytes += bp->recent_bytes; /* Start a new "recent" period. */ bp->recent_start = dltime; bp->recent_bytes = 0; /* Advance the current ring position. */ if (++hist->pos == DLSPEED_HISTORY_SIZE) hist->pos = 0;#if 0 /* Sledgehammer check to verify that the totals are accurate. */ { int i; double sumt = 0, sumb = 0; for (i = 0; i < DLSPEED_HISTORY_SIZE; i++) { sumt += hist->times[i]; sumb += hist->bytes[i]; } assert (sumb == hist->total_bytes); /* We can't use assert(sumt==hist->total_time) because some precision is lost by adding and subtracting floating-point numbers. But during a download this precision should not be detectable, i.e. no larger than 1ns. */ double diff = sumt - hist->total_time; if (diff < 0) diff = -diff; assert (diff < 1e-9); }#endif}#if USE_NLS_PROGRESS_BARint
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -