tools: virsh-domain: display progress with enhanced granularity

Switch from int to double for displaying job progress upto 2 decimal
places.

Signed-off-by: Shaleen Bathla <shaleen.bathla@oracle.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Shaleen Bathla 2023-04-26 16:58:33 +05:30 committed by Martin Kletzander
parent 51cfca2210
commit b4f5e6c91b

View File

@ -1722,21 +1722,22 @@ static void
virshPrintJobProgress(const char *label, unsigned long long remaining, virshPrintJobProgress(const char *label, unsigned long long remaining,
unsigned long long total) unsigned long long total)
{ {
int progress = 100; double progress = 100.00;
/* if remaining == 0 migration has completed */ /* if remaining == 0 migration has completed */
if (remaining != 0) { if (remaining != 0) {
/* use float to avoid overflow */ /* use double to avoid overflow */
progress = (int)(100.0 - remaining * 100.0 / total); progress = 100.00 - remaining * 100.00 / total;
if (progress >= 100) { if (progress >= 100.00) {
/* migration has not completed, do not print [100 %] */ /* migration has not completed, do not print [100 %] */
progress = 99; progress = 99.99;
} }
} }
/* see comments in vshError about why we must flush */ /* see comments in vshError about why we must flush */
fflush(stdout); fflush(stdout);
fprintf(stderr, "\r%s: [%3d %%]", label, progress); /* avoid auto-round-off of double by keeping only 2 decimals */
fprintf(stderr, "\r%s: [%5.2f %%]", label, (int)(progress*100)/100.0);
fflush(stderr); fflush(stderr);
} }