summaryrefslogtreecommitdiff
path: root/lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-12-14 16:19:07 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2022-12-20 20:03:44 -0700
commit94a86a763157327ac969c98e19d7770db477a6a3 (patch)
treef2395593d4d4cfd12b26ba2030bb94930fe4ba56 /lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go
parent1674500388d1e0837103a870f0b96f1f1dab2206 (diff)
Migrate existing progress logs to textui.NewProgress
Diffstat (limited to 'lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go')
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go60
1 files changed, 43 insertions, 17 deletions
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go
index 55cf95b..51313a9 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/orphans.go
@@ -6,6 +6,8 @@ package rebuildnodes
import (
"context"
+ "fmt"
+ "time"
"github.com/datawire/dlib/dlog"
@@ -16,6 +18,7 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
"git.lukeshu.com/btrfs-progs-ng/lib/diskio"
"git.lukeshu.com/btrfs-progs-ng/lib/maps"
+ "git.lukeshu.com/btrfs-progs-ng/lib/textui"
)
func listRoots(graph graph.Graph, leaf btrfsvol.LogicalAddr) containers.Set[btrfsvol.LogicalAddr] {
@@ -54,6 +57,31 @@ func (a keyAndTree) Cmp(b keyAndTree) int {
return containers.NativeCmp(a.TreeID, b.TreeID)
}
+type crawlStats struct {
+ DoneOrphans int
+ TotalOrphans int
+
+ VisitedNodes int
+ FoundLeafs int
+}
+
+func (s crawlStats) String() string {
+ return fmt.Sprintf("... crawling orphans %v%% (%v/%v); visited %d nodes, found %d leaf nodes",
+ int(100*float64(s.DoneOrphans)/float64(s.TotalOrphans)),
+ s.DoneOrphans, s.TotalOrphans, s.VisitedNodes, s.FoundLeafs)
+}
+
+type readStats struct {
+ DoneLeafNodes int
+ TotalLeafNodes int
+}
+
+func (s readStats) String() string {
+ return fmt.Sprintf("... reading leafs %v%% (%v/%v)",
+ int(100*float64(s.DoneLeafNodes)/float64(s.TotalLeafNodes)),
+ s.DoneLeafNodes, s.TotalLeafNodes)
+}
+
func indexOrphans(ctx context.Context, fs diskio.File[btrfsvol.LogicalAddr], sb btrfstree.Superblock, graph graph.Graph) (
orphans containers.Set[btrfsvol.LogicalAddr],
leaf2orphans map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr],
@@ -71,18 +99,15 @@ func indexOrphans(ctx context.Context, fs diskio.File[btrfsvol.LogicalAddr], sb
leaf2orphans = make(map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr])
visited := make(containers.Set[btrfsvol.LogicalAddr])
- lastPct, lastVisited, lastLeafs := -1, 0, 0
- total := len(orphans)
done := 0
+ crawlProgressWriter := textui.NewProgress[crawlStats](ctx, dlog.LogLevelInfo, 1*time.Second)
progress := func() {
- pct := int(100 * float64(done) / float64(total))
- if pct != lastPct || (len(visited) != lastVisited && len(visited)%500 == 0) || len(leaf2orphans) != lastLeafs || done == total {
- dlog.Infof(ctx, "... crawling orphans %v%% (%v/%v); visited %d nodes, found %d leaf nodes",
- pct, done, total, len(visited), len(leaf2orphans))
- lastPct = pct
- lastVisited = len(visited)
- lastLeafs = len(leaf2orphans)
- }
+ crawlProgressWriter.Set(crawlStats{
+ DoneOrphans: done,
+ TotalOrphans: len(orphans),
+ VisitedNodes: len(visited),
+ FoundLeafs: len(leaf2orphans),
+ })
}
progress()
for _, orphan := range maps.SortedKeys(orphans) {
@@ -102,17 +127,16 @@ func indexOrphans(ctx context.Context, fs diskio.File[btrfsvol.LogicalAddr], sb
done++
progress()
}
+ crawlProgressWriter.Done()
key2leaf = new(containers.SortedMap[keyAndTree, btrfsvol.LogicalAddr])
- total = len(leaf2orphans)
done = 0
+ readProgressWriter := textui.NewProgress[readStats](ctx, dlog.LogLevelInfo, 1*time.Second)
progress = func() {
- pct := int(100 * float64(done) / float64(total))
- if pct != lastPct || done == total {
- dlog.Infof(ctx, "... reading leafs %v%% (%v/%v)",
- pct, done, total)
- lastPct = pct
- }
+ readProgressWriter.Set(readStats{
+ DoneLeafNodes: done,
+ TotalLeafNodes: len(leaf2orphans),
+ })
}
progress()
for _, laddr := range maps.SortedKeys(leaf2orphans) {
@@ -136,5 +160,7 @@ func indexOrphans(ctx context.Context, fs diskio.File[btrfsvol.LogicalAddr], sb
done++
progress()
}
+ readProgressWriter.Done()
+
return orphans, leaf2orphans, key2leaf, nil
}