aboutsummaryrefslogtreecommitdiff
path: root/internal/inomap/inomap_test.go
blob: ce5b8805024a56abf710a9a04885cbd1e0ea7402 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package inomap

import (
	"sync"
	"testing"
)

const (
	// bit 63 is used as the spill bit
	spillBit = 1 << 63
)

func TestTranslate(t *testing.T) {
	m := New(0)
	q := QIno{Ino: 1}
	out := m.Translate(q)
	if out != 1 {
		t.Errorf("expected 1, got %d", out)
	}
	q.Ino = maxPassthruIno
	out = m.Translate(q)
	if out < maxPassthruIno {
		t.Errorf("got %d", out)
	}
	out2 := m.Translate(q)
	if out2 != out {
		t.Errorf("unstable mapping: %d %d", out2, out)
	}
}

func TestTranslateStress(t *testing.T) {
	const baseDev = 12345
	m := New(baseDev)

	var wg sync.WaitGroup
	wg.Add(4)
	go func() {
		// Some normal inode numbers on baseDev
		var q QIno
		q.Dev = baseDev
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = i
			out := m.Translate(q)
			if out != i {
				t.Errorf("i=%d out=%d", i, out)
				break
			}
		}
		wg.Done()
	}()
	go func() {
		// Very high (>maxPassthruIno) inode numbers on baseDev
		var q QIno
		q.Dev = baseDev
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = maxPassthruIno + i
			out := m.Translate(q)
			if out < maxPassthruIno {
				t.Errorf("out=%d", out)
				break
			}
		}
		wg.Done()
	}()
	go func() {
		// Device 9999999
		var q QIno
		q.Dev = 9999999
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = i
			out := m.Translate(q)
			if out < maxPassthruIno {
				t.Errorf("out=%d", out)
				break
			}
		}
		wg.Done()
	}()
	go func() {
		// Device 4444444
		var q QIno
		q.Dev = 4444444
		for i := uint64(1); i <= 10000; i++ {
			q.Ino = i
			out := m.Translate(q)
			if out < maxPassthruIno {
				t.Errorf("out=%d", out)
				break
			}
		}
		wg.Done()
	}()
	wg.Wait()
	if len(m.spillMap) != 10000 {
		t.Errorf("len=%d", len(m.spillMap))
	}
	if len(m.namespaceMap) != 3 {
		t.Errorf("len=%d", len(m.namespaceMap))
	}
}

func TestSpill(t *testing.T) {
	m := New(0)
	var q QIno
	q.Ino = maxPassthruIno + 1
	out1 := m.Translate(q)
	if out1&spillBit == 0 {
		t.Error("spill bit not set")
	}
	if out1 != spillSpaceStart {
		t.Errorf("unexpected first spill inode number %d", out1)
	}
	out2 := m.Translate(q)
	if out2&spillBit == 0 {
		t.Error("spill bit not set")
	}
	if out1 != out2 {
		t.Errorf("unstable mapping: %d vs %d", out1, out2)
	}
	q.Ino = maxPassthruIno + 2
	out3 := m.Translate(q)
	if out3 != out1+1 {
		t.Errorf("unexpected 2nd spill inode number %d", out1)
	}
}

// TestUniqueness checks that unique (Dev, Flags, Ino) tuples get unique inode
// numbers
func TestUniqueness(t *testing.T) {
	m := New(0)
	var q QIno
	outMap := make(map[uint64]struct{})
	for q.Dev = 0; q.Dev < 10; q.Dev++ {
		for q.Tag = 0; q.Tag < 10; q.Tag++ {
			// some go into spill
			for q.Ino = maxPassthruIno - 100; q.Ino < maxPassthruIno+100; q.Ino++ {
				out := m.Translate(q)
				_, found := outMap[out]
				if found {
					t.Fatalf("inode number %d already used", out)
				}
				outMap[out] = struct{}{}
			}
		}
	}
	if len(outMap) != 10*10*200 {
		t.Errorf("%d", len(outMap))
	}
}

func BenchmarkTranslateSingleDev(b *testing.B) {
	m := New(0)
	var q QIno
	for n := 0; n < b.N; n++ {
		q.Ino = uint64(n % 1000)
		m.Translate(q)
	}
}

func BenchmarkTranslateManyDevs(b *testing.B) {
	m := New(0)
	var q QIno
	for n := 0; n < b.N; n++ {
		q.Dev = uint64(n % 10)
		q.Ino = uint64(n % 1000)
		m.Translate(q)
	}
}