]> wimlib.net Git - wimlib/blobdiff - include/wimlib/progress.h
Add helper function for rate-limiting progress messages
[wimlib] / include / wimlib / progress.h
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 */