diff mbox series

[1/2] Removed redundant if/else statement

Message ID 20250207023302.311829-2-racz.zoli@gmail.com (mailing list archive)
State New
Headers show
Series btrfs-progs: scrub status: add json output format | expand

Commit Message

Racz Zoltan Feb. 7, 2025, 2:33 a.m. UTC
Removed unnecesary if/else statement in the print_scrub_summary
function. If unit_mode == UNITS_RAW, bytes_per_sec and limit get converted to
UNITS_RAW, otherwise to unit_mode, but in both cases the exact same message is 
written to the output

 cmds/scrub.c | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)

Comments

David Sterba Feb. 11, 2025, 7:14 p.m. UTC | #1
On Fri, Feb 07, 2025 at 04:33:01AM +0200, Racz Zoltan wrote:
> Removed unnecesary if/else statement in the print_scrub_summary
> function. If unit_mode == UNITS_RAW, bytes_per_sec and limit get converted to
> UNITS_RAW, otherwise to unit_mode, but in both cases the exact same message is 
> written to the output
> 
>  cmds/scrub.c | 28 +++++++++-------------------
>  1 file changed, 9 insertions(+), 19 deletions(-)
> 
> diff --git a/cmds/scrub.c b/cmds/scrub.c
> index b2cdc924..3507c9d8 100644
> --- a/cmds/scrub.c
> +++ b/cmds/scrub.c
> @@ -207,25 +207,15 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
>  	 * Rate and size units are disproportionate so they are affected only
>  	 * by --raw, otherwise it's human readable
>  	 */
> -	if (unit_mode == UNITS_RAW) {
> -		pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
> -			pretty_size_mode(bytes_per_sec, UNITS_RAW));
> -		if (limit > 1)
> -			pr_verbose(LOG_DEFAULT, " (limit %s/s)",
> -				   pretty_size_mode(limit, UNITS_RAW));
> -		else if (limit == 1)
> -			pr_verbose(LOG_DEFAULT, " (some device limits set)");
> -		pr_verbose(LOG_DEFAULT, "\n");
> -	} else {
> -		pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
> -			pretty_size_mode(bytes_per_sec, unit_mode));
> -		if (limit > 1)
> -			pr_verbose(LOG_DEFAULT, " (limit %s/s)",
> -				   pretty_size_mode(limit, unit_mode));
> -		else if (limit == 1)
> -			pr_verbose(LOG_DEFAULT, " (some device limits set)");
> -		pr_verbose(LOG_DEFAULT, "\n");
> -	}
> +	
> +	pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
> +		pretty_size_mode(bytes_per_sec, unit_mode));
> +	if (limit > 1)
> +		pr_verbose(LOG_DEFAULT, " (limit %s/s)",
> +			pretty_size_mode(limit, unit_mode));
> +	else if (limit == 1)
> +		pr_verbose(LOG_DEFAULT, " (some device limits set)");
> +	pr_verbose(LOG_DEFAULT, "\n");

It's true that the branch is redundant and it's been like since the
first commit d60d48fce5d32a ("btrfs-progs: scrub status: add unit mode
options") where I added. The idea is to separate the other size options
from the rate, e.g. a 20TB sized device may not give more than 200MB/s
of rate and selecting --tbytes as size option will print number like
0.000MB/s

How it works now:

$ sudo btrfs scrub status /data
...
Total to scrub:   5.14TiB
Bytes scrubbed:   7.08GiB  (0.13%)
Rate:             207.07MiB/s
...


$ sudo btrfs scrub status --tbytes /data
...
Total to scrub:   5.14TiB
Bytes scrubbed:   0.00TiB  (0.09%)
Rate:             0.00TiB/s
...

but scrub is still running so the Rate is really not 0. The point is to
print sensible numbers for Rate regardless of the other options but with
the exception of --raw that will always print the raw numbers.

Digging in the log it seems it got broken in ec3c8428590e90
("btrfs-progs: scrub status: with --si, show rate in metric units"), the
fix should have been to add the SI modifier to the units, not forcing
the unit_mode from the command line.
Racz Zoltan Feb. 11, 2025, 7:31 p.m. UTC | #2
I simplified that if/else block because looking at it it seemed the
only difference between the two was that in the if block
pretty_size_mode received UNITS_RAW and in else it received unit_mode.
Didn`t know about the underlying reason you mentioned.
But if the second patch containing the json output implementation is
ok, I can rewrite it so it uses the function as it was before my
commit.

Thank you.

On Tue, Feb 11, 2025 at 9:15 PM David Sterba <dsterba@suse.cz> wrote:
>
> On Fri, Feb 07, 2025 at 04:33:01AM +0200, Racz Zoltan wrote:
> > Removed unnecesary if/else statement in the print_scrub_summary
> > function. If unit_mode == UNITS_RAW, bytes_per_sec and limit get converted to
> > UNITS_RAW, otherwise to unit_mode, but in both cases the exact same message is
> > written to the output
> >
> >  cmds/scrub.c | 28 +++++++++-------------------
> >  1 file changed, 9 insertions(+), 19 deletions(-)
> >
> > diff --git a/cmds/scrub.c b/cmds/scrub.c
> > index b2cdc924..3507c9d8 100644
> > --- a/cmds/scrub.c
> > +++ b/cmds/scrub.c
> > @@ -207,25 +207,15 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
> >        * Rate and size units are disproportionate so they are affected only
> >        * by --raw, otherwise it's human readable
> >        */
> > -     if (unit_mode == UNITS_RAW) {
> > -             pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
> > -                     pretty_size_mode(bytes_per_sec, UNITS_RAW));
> > -             if (limit > 1)
> > -                     pr_verbose(LOG_DEFAULT, " (limit %s/s)",
> > -                                pretty_size_mode(limit, UNITS_RAW));
> > -             else if (limit == 1)
> > -                     pr_verbose(LOG_DEFAULT, " (some device limits set)");
> > -             pr_verbose(LOG_DEFAULT, "\n");
> > -     } else {
> > -             pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
> > -                     pretty_size_mode(bytes_per_sec, unit_mode));
> > -             if (limit > 1)
> > -                     pr_verbose(LOG_DEFAULT, " (limit %s/s)",
> > -                                pretty_size_mode(limit, unit_mode));
> > -             else if (limit == 1)
> > -                     pr_verbose(LOG_DEFAULT, " (some device limits set)");
> > -             pr_verbose(LOG_DEFAULT, "\n");
> > -     }
> > +
> > +     pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
> > +             pretty_size_mode(bytes_per_sec, unit_mode));
> > +     if (limit > 1)
> > +             pr_verbose(LOG_DEFAULT, " (limit %s/s)",
> > +                     pretty_size_mode(limit, unit_mode));
> > +     else if (limit == 1)
> > +             pr_verbose(LOG_DEFAULT, " (some device limits set)");
> > +     pr_verbose(LOG_DEFAULT, "\n");
>
> It's true that the branch is redundant and it's been like since the
> first commit d60d48fce5d32a ("btrfs-progs: scrub status: add unit mode
> options") where I added. The idea is to separate the other size options
> from the rate, e.g. a 20TB sized device may not give more than 200MB/s
> of rate and selecting --tbytes as size option will print number like
> 0.000MB/s
>
> How it works now:
>
> $ sudo btrfs scrub status /data
> ...
> Total to scrub:   5.14TiB
> Bytes scrubbed:   7.08GiB  (0.13%)
> Rate:             207.07MiB/s
> ...
>
>
> $ sudo btrfs scrub status --tbytes /data
> ...
> Total to scrub:   5.14TiB
> Bytes scrubbed:   0.00TiB  (0.09%)
> Rate:             0.00TiB/s
> ...
>
> but scrub is still running so the Rate is really not 0. The point is to
> print sensible numbers for Rate regardless of the other options but with
> the exception of --raw that will always print the raw numbers.
>
> Digging in the log it seems it got broken in ec3c8428590e90
> ("btrfs-progs: scrub status: with --si, show rate in metric units"), the
> fix should have been to add the SI modifier to the units, not forcing
> the unit_mode from the command line.
David Sterba Feb. 11, 2025, 11:42 p.m. UTC | #3
On Tue, Feb 11, 2025 at 09:31:33PM +0200, Racz Zoli wrote:
> I simplified that if/else block because looking at it it seemed the
> only difference between the two was that in the if block
> pretty_size_mode received UNITS_RAW and in else it received unit_mode.
> Didn`t know about the underlying reason you mentioned.
> But if the second patch containing the json output implementation is
> ok, I can rewrite it so it uses the function as it was before my
> commit.

I have fixed the unit printing and found another bug. Please base your
commit on current devel branch. The json patch won't apply cleanly but
it's only in the rate printing part.
Racz Zoltan Feb. 13, 2025, 7:49 p.m. UTC | #4
I looked over the changes you made and I was thinking, can`t we
isolate the part which checks unit_mode, and then leave the block that
prints rate and limit unique?
Asking because this way when adding the json formatting option we
wouldn't need to repeat the print logic after.

My suggestion would be something like this:

unsigned int mode = UNITS_RAW;
if (unit_mode != UNITS_RAW)
    mode = unit_mode & UNITS_BINARY ? UNITS_HUMAN_BINARY : UNITS_HUMAN_DECIMAL;
...

What do you think, should i create a commit on this, and continue with
the json implementation?




On Wed, Feb 12, 2025 at 1:43 AM David Sterba <dsterba@suse.cz> wrote:
>
> On Tue, Feb 11, 2025 at 09:31:33PM +0200, Racz Zoli wrote:
> > I simplified that if/else block because looking at it it seemed the
> > only difference between the two was that in the if block
> > pretty_size_mode received UNITS_RAW and in else it received unit_mode.
> > Didn`t know about the underlying reason you mentioned.
> > But if the second patch containing the json output implementation is
> > ok, I can rewrite it so it uses the function as it was before my
> > commit.
>
> I have fixed the unit printing and found another bug. Please base your
> commit on current devel branch. The json patch won't apply cleanly but
> it's only in the rate printing part.
diff mbox series

Patch

diff --git a/cmds/scrub.c b/cmds/scrub.c
index b2cdc924..3507c9d8 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -207,25 +207,15 @@  static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 	 * Rate and size units are disproportionate so they are affected only
 	 * by --raw, otherwise it's human readable
 	 */
-	if (unit_mode == UNITS_RAW) {
-		pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
-			pretty_size_mode(bytes_per_sec, UNITS_RAW));
-		if (limit > 1)
-			pr_verbose(LOG_DEFAULT, " (limit %s/s)",
-				   pretty_size_mode(limit, UNITS_RAW));
-		else if (limit == 1)
-			pr_verbose(LOG_DEFAULT, " (some device limits set)");
-		pr_verbose(LOG_DEFAULT, "\n");
-	} else {
-		pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
-			pretty_size_mode(bytes_per_sec, unit_mode));
-		if (limit > 1)
-			pr_verbose(LOG_DEFAULT, " (limit %s/s)",
-				   pretty_size_mode(limit, unit_mode));
-		else if (limit == 1)
-			pr_verbose(LOG_DEFAULT, " (some device limits set)");
-		pr_verbose(LOG_DEFAULT, "\n");
-	}
+	
+	pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
+		pretty_size_mode(bytes_per_sec, unit_mode));
+	if (limit > 1)
+		pr_verbose(LOG_DEFAULT, " (limit %s/s)",
+			pretty_size_mode(limit, unit_mode));
+	else if (limit == 1)
+		pr_verbose(LOG_DEFAULT, " (some device limits set)");
+	pr_verbose(LOG_DEFAULT, "\n");
 
 	pr_verbose(LOG_DEFAULT, "Error summary:   ");
 	if (err_cnt || err_cnt2) {