1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfsutil
import (
"fmt"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
"git.lukeshu.com/btrfs-progs-ng/lib/diskio"
"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)
type skinnyItem struct {
Node btrfsvol.LogicalAddr
Item int
}
type SkinnyPath struct {
Root btrfsvol.LogicalAddr
Items []int
}
type SkinnyPathArena struct {
FS diskio.File[btrfsvol.LogicalAddr]
SB btrfstree.Superblock
fatRoots map[btrfsvol.LogicalAddr]btrfstree.TreePathElem
fatItems *containers.LRUCache[skinnyItem, btrfstree.TreePathElem]
}
func (a *SkinnyPathArena) init() {
if a.fatRoots == nil {
a.fatRoots = make(map[btrfsvol.LogicalAddr]btrfstree.TreePathElem)
// This cache size is sorta arbitrary. At first I figured
// "let's allow 1GB of cached items", and figured 67bytes per
// item, that's about 16M items. But with overhead of the
// LRUCache, it's actually a lot higher than that. So then I
// cut it to .5M, and that cut my total memory use to ~8GB,
// which is a good number for me. Then I tought it to do a
// better job of recovering trees, and so the memory grew, and I
// cut it to 64K. Then to 8K. Then grew it to 128K.
a.fatItems = containers.NewLRUCache[skinnyItem, btrfstree.TreePathElem](textui.Tunable(128 * 1024))
}
}
func (a *SkinnyPathArena) getItem(parent btrfstree.TreePath, itemIdx int) (btrfstree.TreePathElem, error) {
if itemIdx < 0 {
panic("should not happen")
}
a.init()
ret, ok := a.fatItems.Get(skinnyItem{
Node: parent.Node(-1).ToNodeAddr,
Item: itemIdx,
})
if ok {
return ret, nil
}
node, err := btrfstree.ReadNode(a.FS, a.SB, parent.Node(-1).ToNodeAddr, btrfstree.NodeExpectations{})
if err != nil {
return btrfstree.TreePathElem{}, err
}
if node.Data.Head.Level > 0 {
if itemIdx >= len(node.Data.BodyInternal) {
panic("should not happen")
}
for i, item := range node.Data.BodyInternal {
toMaxKey := parent.Node(-1).ToMaxKey
if i+1 < len(node.Data.BodyInternal) {
toMaxKey = node.Data.BodyInternal[i+1].Key.Mm()
}
elem := btrfstree.TreePathElem{
FromTree: node.Data.Head.Owner,
FromItemIdx: i,
ToNodeAddr: item.BlockPtr,
ToNodeGeneration: item.Generation,
ToNodeLevel: node.Data.Head.Level - 1,
ToKey: item.Key,
ToMaxKey: toMaxKey,
}
a.fatItems.Add(skinnyItem{Node: parent.Node(-1).ToNodeAddr, Item: i}, elem)
if i == itemIdx {
ret = elem
}
}
} else {
if itemIdx >= len(node.Data.BodyLeaf) {
panic("should not happen")
}
for i, item := range node.Data.BodyLeaf {
elem := btrfstree.TreePathElem{
FromTree: node.Data.Head.Owner,
FromItemIdx: i,
ToKey: item.Key,
ToMaxKey: item.Key,
}
a.fatItems.Add(skinnyItem{Node: parent.Node(-1).ToNodeAddr, Item: i}, elem)
if i == itemIdx {
ret = elem
}
}
}
return ret, nil
}
func (a *SkinnyPathArena) Deflate(fat btrfstree.TreePath) SkinnyPath {
a.init()
var ret SkinnyPath
var prevNode btrfsvol.LogicalAddr
for i, elem := range fat {
if i == 0 {
a.fatRoots[elem.ToNodeAddr] = elem
ret.Root = elem.ToNodeAddr
} else {
a.fatItems.Add(skinnyItem{Node: prevNode, Item: elem.FromItemIdx}, elem)
ret.Items = append(ret.Items, elem.FromItemIdx)
}
prevNode = elem.ToNodeAddr
}
return ret
}
func (a *SkinnyPathArena) Inflate(skinny SkinnyPath) btrfstree.TreePath {
a.init()
ret := make(btrfstree.TreePath, 0, 1+len(skinny.Items))
root, ok := a.fatRoots[skinny.Root]
if !ok {
panic(fmt.Errorf("SkinnyPathArena.Inflate: no stored TreePathElem for root->%v",
skinny.Root))
}
ret = append(ret, root)
for _, itemIdx := range skinny.Items {
elem, err := a.getItem(ret, itemIdx)
if err != nil {
panic(err)
}
ret = append(ret, elem)
}
return ret
}
|