-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdirty-bitmasks.go
61 lines (53 loc) · 1.32 KB
/
dirty-bitmasks.go
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
package main
import (
"math/bits"
"sync"
)
// This will need 270MB~ per TB of disk being tracked
type DirtySectorTracker struct {
TotalSizeOfDevice uint64
Sectors uint64
dirtyTracker []uint64
// DirtySectors: CountDirty() must be called for this number to be updated
DirtySectors int
lock *sync.Mutex
}
func (d *DirtySectorTracker) Setup(diskSize uint64) {
d.lock = &sync.Mutex{}
d.dirtyTracker = make([]uint64, ((diskSize)/64)+2)
}
func (d *DirtySectorTracker) SetDirty(sector uint64) {
arrayTarget := sector / 64
bitTarget := sector % 64
d.lock.Lock()
defer d.lock.Unlock()
block := d.dirtyTracker[arrayTarget]
block |= (1 << bitTarget)
d.dirtyTracker[arrayTarget] = block
}
func (d *DirtySectorTracker) CountDirty() {
dirty := 0
for i := 0; i < len(d.dirtyTracker); i++ {
if d.dirtyTracker[i] != 0 {
dirty += bits.OnesCount64(d.dirtyTracker[i])
}
}
d.DirtySectors = dirty
}
// GetDirtySectors Gives a full list of sectors (in order) that have been marked as dirty
func (d *DirtySectorTracker) GetDirtySectors() chan uint64 {
o := make(chan uint64)
go func() {
for i := 0; i < len(d.dirtyTracker); i++ {
if d.dirtyTracker[i] != 0 {
for b := 0; b < 64; b++ {
if d.dirtyTracker[i]&(1<<b) > 0 {
o <- uint64((i * 64) + b)
}
}
}
}
close(o)
}()
return o
}