Skip to content

Commit a522c59

Browse files
author
DocMAX
committed
btrfs-progs: add ETA display for device replace operations
Add estimated time of arrival (ETA) calculation for device replace operations using progress percentage and elapsed time. The ETA is calculated by determining the total estimated completion time based on current progress ratio and displays the expected completion time in a user-friendly format.
1 parent c03e29f commit a522c59

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cmds/replace.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ static char *progress2string(char *buf, size_t s, int progress_1000);
4646

4747
/* Used to separate internal errors from actual dev replace ioctl results. */
4848
#define BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT -1
49+
/* Buffer size for ETA string display */
50+
#define ETA_STR_SIZE 80
4951

5052
static const char *replace_dev_result2string(__u64 result)
5153
{
@@ -371,6 +373,7 @@ static DEFINE_SIMPLE_COMMAND(replace_start, "start");
371373
static const char *const cmd_replace_status_usage[] = {
372374
"btrfs replace status [-1] <mount_point>",
373375
"Print status and progress information of a running device replace operation",
376+
"Shows current progress percentage and estimated time of arrival (ETA) for completion.",
374377
"",
375378
OPTLINE("-1", "print once instead of print continuously until the replace operation finishes (or is canceled)"),
376379
NULL
@@ -421,6 +424,8 @@ static int print_replace_status(int fd, const char *path, int once)
421424
char string1[80];
422425
char string2[80];
423426
char string3[80];
427+
char eta_str[ETA_STR_SIZE];
428+
struct tm eta_tm_buf;
424429

425430
for (;;) {
426431
args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS;
@@ -454,6 +459,27 @@ static int print_replace_status(int fd, const char *path, int once)
454459
progress2string(string3,
455460
sizeof(string3),
456461
status->progress_1000));
462+
/* Calculate and display ETA if progress is available */
463+
if (status->progress_1000 > 0 && status->time_started > 0) {
464+
time_t current_time = time(NULL);
465+
time_t elapsed_time = current_time - status->time_started;
466+
time_t total_time, remaining_time, eta_time;
467+
468+
/* Avoid division by zero */
469+
if (elapsed_time > 0) {
470+
total_time = (elapsed_time * 1000) / status->progress_1000;
471+
remaining_time = total_time - elapsed_time;
472+
473+
/* Only show ETA if we have a reasonable estimate */
474+
if (remaining_time > 0 && total_time > elapsed_time) {
475+
eta_time = current_time + remaining_time;
476+
struct tm *eta_tm = localtime_r(&eta_time, &eta_tm_buf);
477+
if (strftime(eta_str, sizeof(eta_str), "ETA: %a %b %d %H:%M:%S %Y", eta_tm) > 0) {
478+
num_chars += printf(", %s", eta_str);
479+
}
480+
}
481+
}
482+
}
457483
break;
458484
case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
459485
prevent_loop = 1;

0 commit comments

Comments
 (0)