]> wimlib.net Git - wimlib/commitdiff
Add helper function for rate-limiting progress messages
authorEric Biggers <ebiggers3@gmail.com>
Mon, 1 Jun 2015 03:36:56 +0000 (22:36 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Fri, 5 Jun 2015 03:45:35 +0000 (22:45 -0500)
include/wimlib/progress.h
src/extract.c
src/verify.c
src/write.c

index 8dbb4c48885f31b2e71bef93c48cf64ee246c65f..b2754445df323a5867bce8f228fccc28b50c2ad0 100644 (file)
@@ -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 */
index b200e18cae7496df4f88e365085d3abf918c0341..6b35a90367d0b106a9a0152804481c47c27c24e1 100644 (file)
@@ -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))) {
index 1ca5be46d57a9c3c41696e4291e2765f5346f9b1..fbe48708e4289b8db6735a407f49ba72aab7da60 100644 (file)
@@ -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;
 }
 
index bb8f185623bf660e933689bc69c2bc64c05de855..47579b549c5a8123f872b5f7bc8b19652e60f5dd 100644 (file)
@@ -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;
 }