public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Racz Zoltan <racz.zoli@gmail.com>
To: dsterba@suse.cz
Cc: linux-btrfs@vger.kernel.org, Racz Zoltan <racz.zoli@gmail.com>
Subject: [PATCH 3/6] btrfs-progs: Added json output format for print_scrub_summary
Date: Fri, 14 Feb 2025 18:47:06 +0200	[thread overview]
Message-ID: <20250214164709.51465-4-racz.zoli@gmail.com> (raw)
In-Reply-To: <20250214164709.51465-1-racz.zoli@gmail.com>

---
 cmds/scrub.c | 110 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 76 insertions(+), 34 deletions(-)

diff --git a/cmds/scrub.c b/cmds/scrub.c
index caada704..5c05d00f 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -216,6 +216,8 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 	u64 sec_left = 0;
 	time_t sec_eta;
 
+	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
+
 	bytes_scrubbed = p->data_bytes_scrubbed + p->tree_bytes_scrubbed;
 	/*
 	 * If duration is zero seconds (rounded down), then the Rate metric
@@ -236,8 +238,12 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 
 	err_cnt2 = p->corrected_errors + p->uncorrectable_errors;
 
+	if (json_output) 
+		fmt_print_start_group(&fctx, "scrub", JSON_TYPE_MAP);
+
 	if (p->malloc_errors)
-		pr_verbose(LOG_DEFAULT, "*** WARNING: memory allocation failed while scrubbing. "
+		if (!json_output)
+			pr_verbose(LOG_DEFAULT, "*** WARNING: memory allocation failed while scrubbing. "
 		       "results may be inaccurate\n");
 
 	if (s->in_progress) {
@@ -246,21 +252,33 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 
 		sec_eta = time(NULL);
 		sec_eta += sec_left;
-		localtime_r(&sec_eta, &tm);
-		t[sizeof(t) - 1] = '\0';
-		strftime(t, sizeof(t), "%c", &tm);
 
-		pr_verbose(LOG_DEFAULT, "Time left:        %llu:%02llu:%02llu\n",
-			sec_left / 3600, (sec_left / 60) % 60, sec_left % 60);
-		pr_verbose(LOG_DEFAULT, "ETA:              %s\n", t);
-		pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
-			pretty_size_mode(bytes_total, unit_mode));
-		pr_verbose(LOG_DEFAULT, "Bytes scrubbed:   %s  (%.2f%%)\n",
-			pretty_size_mode(bytes_scrubbed, unit_mode),
-			100.0 * bytes_scrubbed / bytes_total);
+		if (json_output) {
+			fmt_print(&fctx, "time-left", sec_left);
+			fmt_print(&fctx, "eta", sec_eta);
+			fmt_print(&fctx, "total-bytes-to-scrub", bytes_total);
+			fmt_print(&fctx, "bytes-scrubbed", bytes_scrubbed);
+		}
+		else {
+			localtime_r(&sec_eta, &tm);
+			t[sizeof(t) - 1] = '\0';
+			strftime(t, sizeof(t), "%c", &tm);
+
+			pr_verbose(LOG_DEFAULT, "Time left:        %llu:%02llu:%02llu\n",
+				sec_left / 3600, (sec_left / 60) % 60, sec_left % 60);
+			pr_verbose(LOG_DEFAULT, "ETA:              %s\n", t);
+			pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
+				pretty_size_mode(bytes_total, unit_mode));
+			pr_verbose(LOG_DEFAULT, "Bytes scrubbed:   %s  (%.2f%%)\n",
+				pretty_size_mode(bytes_scrubbed, unit_mode),
+				100.0 * bytes_scrubbed / bytes_total);
+		}
 	} else {
-		pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
-			pretty_size_mode(bytes_total, unit_mode));
+		if (json_output) 
+			fmt_print(&fctx, "total-bytes-to-scrub", bytes_total);
+		else
+			pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
+				pretty_size_mode(bytes_total, unit_mode));
 	}
 
 	/*
@@ -271,28 +289,52 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 	if (unit_mode != UNITS_RAW) 
 		mode = unit_mode & UNITS_BINARY ? UNITS_HUMAN_BINARY : UNITS_HUMAN_DECIMAL;
 
-	pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
-		pretty_size_mode(bytes_per_sec, mode));
-	if (limit > 1)
-		pr_verbose(LOG_DEFAULT, " (limit %s/s)",
-				pretty_size_mode(limit, 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) {
-		PRINT_SCRUB_ERROR(p->read_errors, "read");
-		PRINT_SCRUB_ERROR(p->super_errors, "super");
-		PRINT_SCRUB_ERROR(p->verify_errors, "verify");
-		PRINT_SCRUB_ERROR(p->csum_errors, "csum");
-		pr_verbose(LOG_DEFAULT, "\n");
-		pr_verbose(LOG_DEFAULT, "  Corrected:      %llu\n", p->corrected_errors);
-		pr_verbose(LOG_DEFAULT, "  Uncorrectable:  %llu\n", p->uncorrectable_errors);
-		pr_verbose(LOG_DEFAULT, "  Unverified:     %llu\n", p->unverified_errors);
+	if (json_output) {
+		fmt_print(&fctx, "rate", bytes_per_sec);
+		if (limit > 1)
+			fmt_print(&fctx, "limit", limit);
 	} else {
-		pr_verbose(LOG_DEFAULT, " no errors found\n");
+		pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
+			pretty_size_mode(bytes_per_sec, mode));
+		if (limit > 1)
+			pr_verbose(LOG_DEFAULT, " (limit %s/s)",
+					pretty_size_mode(limit, mode));
+		else if (limit == 1)
+			pr_verbose(LOG_DEFAULT, " (some device limits set)");
+		pr_verbose(LOG_DEFAULT, "\n");
 	}
+
+	if (json_output) {
+		if (err_cnt || err_cnt2) {
+			fmt_print_start_group(&fctx, "error-summary", JSON_TYPE_MAP);
+			fmt_print(&fctx, "read-errors", p->read_errors);
+			fmt_print(&fctx, "super-errors", p->super_errors);
+			fmt_print(&fctx, "verify-errors", p->verify_errors);
+			fmt_print(&fctx, "csum-errors", p->csum_errors);
+			fmt_print(&fctx, "corrected-errors", p->corrected_errors);
+			fmt_print(&fctx, "uncorrectable-errors", p->uncorrectable_errors);
+			fmt_print(&fctx, "unverified-errors", p->unverified_errors);
+			fmt_print_end_group(&fctx, "error-summary");
+		}
+	}
+	else {
+		pr_verbose(LOG_DEFAULT, "Error summary:   ");
+		if (err_cnt || err_cnt2) {
+			PRINT_SCRUB_ERROR(p->read_errors, "read");
+			PRINT_SCRUB_ERROR(p->super_errors, "super");
+			PRINT_SCRUB_ERROR(p->verify_errors, "verify");
+			PRINT_SCRUB_ERROR(p->csum_errors, "csum");
+			pr_verbose(LOG_DEFAULT, "\n");
+			pr_verbose(LOG_DEFAULT, "  Corrected:      %llu\n", p->corrected_errors);
+			pr_verbose(LOG_DEFAULT, "  Uncorrectable:  %llu\n", p->uncorrectable_errors);
+			pr_verbose(LOG_DEFAULT, "  Unverified:     %llu\n", p->unverified_errors);
+		} else {
+			pr_verbose(LOG_DEFAULT, " no errors found\n");
+		}
+	}
+
+	if (json_output)
+		fmt_print_end_group(&fctx, "scrub");
 }
 
 #define _SCRUB_FS_STAT(p, name, fs_stat) do {	\
-- 
2.48.1


  parent reply	other threads:[~2025-02-14 16:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
2025-02-14 16:47 ` [PATCH 1/6] btrfs-progs: Added rowspec struct and neccesary include file for " Racz Zoltan
2025-02-14 16:47 ` [PATCH 2/6] btrfs-progs: Added json output format for print_scrub_full Racz Zoltan
2025-02-14 16:47 ` Racz Zoltan [this message]
2025-02-14 16:47 ` [PATCH 4/6] btrfs-progs: Added json output format for _print_scrub_ss and print_scrub_dev Racz Zoltan
2025-02-14 16:47 ` [PATCH 5/6] btrfs-progs: Added json output format for cmd_scrub_status Racz Zoltan
2025-02-14 16:47 ` [PATCH 6/6] btrfs-progs: Corrected a minor JSON string error in print_scrub_dev Racz Zoltan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250214164709.51465-4-racz.zoli@gmail.com \
    --to=racz.zoli@gmail.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox