From 9bd0659477b3a1ebad90b46106f743d72689bbd3 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sun, 31 May 2015 22:36:56 -0500 Subject: [PATCH] Add helper function for rate-limiting progress messages --- include/wimlib/progress.h | 22 ++++++++++++++++++++++ src/extract.c | 27 +++------------------------ src/verify.c | 37 +++++++++---------------------------- src/write.c | 33 +++++---------------------------- 4 files changed, 39 insertions(+), 80 deletions(-) diff --git a/include/wimlib/progress.h b/include/wimlib/progress.h index 8dbb4c48..b2754445 100644 --- a/include/wimlib/progress.h +++ b/include/wimlib/progress.h @@ -33,4 +33,26 @@ extern int report_error(wimlib_progress_func_t progfunc, void *progctx, int error_code, const tchar *path); +/* Rate-limiting of byte-count based progress messages: update *next_progress_p + * to the value that completed_bytes needs to reach before the next progress + * message will be sent. */ +static inline void +set_next_progress(u64 completed_bytes, u64 total_bytes, u64 *next_progress_p) +{ + if (*next_progress_p < total_bytes) { + /* + * Send the next message as soon as: + * - another 1/128 of the total has been processed; + * - OR another 5000000 bytes have been processed; + * - OR all bytes have been processed. + */ + *next_progress_p = min(min(completed_bytes + total_bytes / 128, + completed_bytes + 5000000), + total_bytes); + } else { + /* Last message has been sent. */ + *next_progress_p = ~0; + } +} + #endif /* _WIMLIB_PROGRESS_H */ diff --git a/src/extract.c b/src/extract.c index b200e18c..6b35a903 100644 --- a/src/extract.c +++ b/src/extract.c @@ -341,30 +341,9 @@ extract_chunk_wrapper(const void *chunk, size_t size, void *_ctx) if (ret) return ret; - if (progress->extract.completed_bytes >= - progress->extract.total_bytes) - { - ctx->next_progress = UINT64_MAX; - } else { - /* Send new message as soon as another 1/128 of the - * total has been extracted. (Arbitrary number.) */ - ctx->next_progress = - progress->extract.completed_bytes + - progress->extract.total_bytes / 128; - - /* ... Unless that would be more than 5000000 bytes, in - * which case send the next after the next 5000000 - * bytes. (Another arbitrary number.) */ - if (progress->extract.completed_bytes + 5000000 < - ctx->next_progress) - ctx->next_progress = - progress->extract.completed_bytes + 5000000; - - /* ... But always send a message as soon as we're - * completely done. */ - if (progress->extract.total_bytes < ctx->next_progress) - ctx->next_progress = progress->extract.total_bytes; - } + set_next_progress(progress->extract.completed_bytes, + progress->extract.total_bytes, + &ctx->next_progress); } if (unlikely(filedes_valid(&ctx->tmpfile_fd))) { diff --git a/src/verify.c b/src/verify.c index 1ca5be46..fbe48708 100644 --- a/src/verify.c +++ b/src/verify.c @@ -58,37 +58,18 @@ end_verify_blob(struct blob_descriptor *blob, int status, void *_ctx) progress->verify_streams.completed_streams++; progress->verify_streams.completed_bytes += blob->size; - /* Handle rate-limiting of progress messages */ + if (progress->verify_streams.completed_bytes >= ctx->next_progress) { - if (progress->verify_streams.completed_bytes < ctx->next_progress) - return 0; + status = call_progress(ctx->progfunc, + WIMLIB_PROGRESS_MSG_VERIFY_STREAMS, + progress, ctx->progctx); + if (status) + return status; - /* Time for another progress message. */ - - status = call_progress(ctx->progfunc, WIMLIB_PROGRESS_MSG_VERIFY_STREAMS, - progress, ctx->progctx); - if (status) - return status; - - if (ctx->next_progress == progress->verify_streams.total_bytes) { - ctx->next_progress = ~(u64)0; - return 0; + set_next_progress(progress->verify_streams.completed_bytes, + progress->verify_streams.total_bytes, + &ctx->next_progress); } - - /* Send new message as soon as another 1/128 of the total has - * been verified. (Arbitrary number.) */ - ctx->next_progress = progress->verify_streams.completed_bytes + - progress->verify_streams.total_bytes / 128; - - /* ... Unless that would be more than 5000000 bytes, in which case send - * the next after the next 5000000 bytes. (Another arbitrary number.) */ - if (progress->verify_streams.completed_bytes + 5000000 < ctx->next_progress) - ctx->next_progress = progress->verify_streams.completed_bytes + 5000000; - - /* ... But always send a message as soon as we're completely - * done. */ - if (progress->verify_streams.total_bytes < ctx->next_progress) - ctx->next_progress = progress->verify_streams.total_bytes; return 0; } diff --git a/src/write.c b/src/write.c index bb8f1856..47579b54 100644 --- a/src/write.c +++ b/src/write.c @@ -300,8 +300,8 @@ do_write_blobs_progress(struct write_blobs_progress_data *progress_data, progress->write_streams.completed_streams += complete_count; } - if (progress->write_streams.completed_bytes >= progress_data->next_progress) - { + if (progress->write_streams.completed_bytes >= progress_data->next_progress) { + ret = call_progress(progress_data->progfunc, WIMLIB_PROGRESS_MSG_WRITE_STREAMS, progress, @@ -309,32 +309,9 @@ do_write_blobs_progress(struct write_blobs_progress_data *progress_data, if (ret) return ret; - if (progress_data->next_progress == progress->write_streams.total_bytes) { - progress_data->next_progress = ~(u64)0; - } else { - /* Handle rate-limiting of messages */ - - /* Send new message as soon as another 1/128 of the - * total has been written. (Arbitrary number.) */ - progress_data->next_progress = - progress->write_streams.completed_bytes + - progress->write_streams.total_bytes / 128; - - /* ... Unless that would be more than 5000000 bytes, in - * which case send the next after the next 5000000 - * bytes. (Another arbitrary number.) */ - if (progress->write_streams.completed_bytes + 5000000 < - progress_data->next_progress) - progress_data->next_progress = - progress->write_streams.completed_bytes + 5000000; - - /* ... But always send a message as soon as we're - * completely done. */ - if (progress->write_streams.total_bytes < - progress_data->next_progress) - progress_data->next_progress = - progress->write_streams.total_bytes; - } + set_next_progress(progress->write_streams.completed_bytes, + progress->write_streams.total_bytes, + &progress_data->next_progress); } return 0; } -- 2.43.0