-
Notifications
You must be signed in to change notification settings - Fork 2
/
shm_test.go
329 lines (300 loc) · 7.51 KB
/
shm_test.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2014 Vastech SA (PTY) LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shm
import (
"flag"
"github.com/jsgilmore/mount"
"math"
"os"
"runtime"
"sync"
"syscall"
"testing"
)
const size64MiB = 64 << 20
const size1G = 1024 << 20
func newBuffer(size, prot int) Buffer {
buf, err := NewBufferTmpfs(size, prot)
if err != nil {
panic(err)
}
return buf
}
func testShm(newBuffer func(_, _ int) (Buffer, error)) {
buf, err := newBuffer(size64MiB, PROT_RDWR)
if err != nil {
panic(err)
}
defer checkClose(buf)
fd2, err := syscall.Dup(int(buf.File().Fd()))
if err != nil {
panic(err)
}
file2 := os.NewFile(uintptr(fd2), "")
buf2, err := NewBufferFile(file2, buf.Len(), PROT_READ)
if err != nil {
panic(err)
}
defer checkClose(buf2)
// NewBufferFile duped file2
checkClose(file2)
bufSlice := buf.Bytes()
for i := 0; i < len(bufSlice); i++ {
bufSlice[i] = byte(i)
}
buf2Slice := buf2.Bytes()
if len(bufSlice) != len(buf2Slice) {
panic("slices must have the same length")
}
for i := 0; i < len(bufSlice); i++ {
if bufSlice[i] != buf2Slice[i] {
panic("slices not equal")
}
}
buf2.Lock(0, buf2.Len())
buf2.Unlock(0, buf2.Len())
}
func stat(dir string) syscall.Statfs_t {
var st syscall.Statfs_t
err := syscall.Statfs(dir, &st)
if err != nil {
panic(err)
}
return st
}
func TestShmTmpfs(t *testing.T) {
setup()
st := stat(devShm)
testShm(NewBufferTmpfs)
if st != stat(devShm) {
panic("stat mismatch")
}
}
func TestShmDevHugepages(t *testing.T) {
setup()
st := stat(devHugepages)
testShm(NewBufferHugepages)
if st != stat(devHugepages) {
panic("stat mismatch")
}
}
func TestProtReadTmpfs(t *testing.T) {
setup()
st := stat(devShm)
// this doesn't work with hugepages
buf, err := NewBufferTmpfs(size1G, PROT_READ)
if err != nil {
panic(err)
}
if err := buf.LockAll(); err != nil {
panic(err)
}
checkClose(buf)
if st != stat(devShm) {
panic("stat mismatch")
}
}
func BenchmarkAllocate(b *testing.B) {
setup()
b.SetBytes(int64(size64MiB))
origBuf := make([]byte, size64MiB)
b.ResetTimer()
b.StartTimer()
for n := b.N; n > 0; n-- {
buf := newBuffer(size64MiB, PROT_RDWR)
copy(buf.Bytes(), origBuf)
buf.Close()
}
b.StopTimer()
}
func BenchmarkCopySliceToSlice(b *testing.B) {
setup()
b.SetBytes(int64(size64MiB))
dest := make([]byte, size64MiB)
src := make([]byte, size64MiB)
b.ResetTimer()
b.StartTimer()
for n := b.N; n > 0; n-- {
copy(dest, src)
}
b.StopTimer()
}
func BenchmarkMemmoveSliceToSlice(b *testing.B) {
setup()
b.SetBytes(int64(size64MiB))
dest := make([]byte, size64MiB)
src := make([]byte, size64MiB)
b.ResetTimer()
b.StartTimer()
for n := b.N; n > 0; n-- {
memmove(dest, src)
}
b.StopTimer()
}
func BenchmarkCopySliceToShm(b *testing.B) {
setup()
b.SetBytes(int64(size64MiB))
origBuf := make([]byte, size64MiB)
buf := newBuffer(size64MiB, PROT_RDWR)
b.ResetTimer()
b.StartTimer()
for n := b.N; n > 0; n-- {
copy(buf.Bytes(), origBuf)
}
b.StopTimer()
buf.Close()
}
func BenchmarkCopyShmToShm(b *testing.B) {
setup()
b.SetBytes(int64(size64MiB))
origBuf := make([]byte, size64MiB)
buf1 := newBuffer(size64MiB, PROT_RDWR)
copy(buf1.Bytes(), origBuf)
buf2 := newBuffer(size64MiB, PROT_RDWR)
b.ResetTimer()
b.StartTimer()
for n := b.N; n > 0; n-- {
copy(buf2.Bytes(), buf1.Bytes())
}
b.StopTimer()
buf1.Close()
buf2.Close()
}
func BenchmarkMemcpyShmToShm(b *testing.B) {
setup()
b.SetBytes(int64(size64MiB))
origBuf := make([]byte, size64MiB)
buf1 := newBuffer(size64MiB, PROT_RDWR)
copy(buf1.Bytes(), origBuf)
buf2 := newBuffer(size64MiB, PROT_RDWR)
b.ResetTimer()
b.StartTimer()
for n := b.N; n > 0; n-- {
memcpy(buf2.Bytes(), buf1.Bytes())
}
b.StopTimer()
buf1.Close()
buf2.Close()
}
func BenchmarkMemmoveShmToShm(b *testing.B) {
setup()
b.SetBytes(int64(size64MiB))
origBuf := make([]byte, size64MiB)
buf1 := newBuffer(size64MiB, PROT_RDWR)
copy(buf1.Bytes(), origBuf)
buf2 := newBuffer(size64MiB, PROT_RDWR)
b.ResetTimer()
b.StartTimer()
for n := b.N; n > 0; n-- {
memmove(buf2.Bytes(), buf1.Bytes())
}
b.StopTimer()
buf1.Close()
buf2.Close()
}
func TestUnlinkError(t *testing.T) {
err := syscall.Unlink("/this/does/not/exist")
if !os.IsNotExist(err) {
panic("expected an IsNotExist error")
}
}
var tmpfsSize *int64 = flag.Int64("tmpfs.size", 4<<30, "tmpfs size")
var hugeSize *int64 = flag.Int64("huge.size", 4<<30, "hugetlbfs size")
var setupDone = false
func setup() {
if setupDone {
return
}
mount.MountNamespace()
if err := mount.MountTmpfs("/dev/shm", *tmpfsSize); err != nil {
panic(err)
}
if err := mount.MountHugetlbfs("/dev/hugepages", 2<<20, *hugeSize); err != nil {
panic(err)
}
setupDone = true
}
func checkNil(err error) {
if err != nil {
panic("expected nil error")
}
}
func checkNotNil(err error) {
if err == nil {
panic("expected non-nil error")
}
}
func testOffsetLength(lenb, offset, length int) error {
// negative checks, zero/overflow check, bounds check
if offset < 0 || length < 0 || offset+length <= offset || offset+length > lenb {
return errOffsetLength
}
return nil
}
func TestCheckOffsetLength(t *testing.T) {
checkNotNil(checkOffsetLength(make([]byte, 0), 0, 0))
checkNotNil(checkOffsetLength(make([]byte, 1), 0, 0))
checkNil(checkOffsetLength(make([]byte, 1), 0, 1))
checkNotNil(checkOffsetLength(make([]byte, 1), 0, 0))
checkNotNil(checkOffsetLength(make([]byte, 1), 0, -1))
checkNil(checkOffsetLength(make([]byte, 2), 0, 1))
checkNil(checkOffsetLength(make([]byte, 2), 0, 2))
checkNil(checkOffsetLength(make([]byte, 2), 1, 1))
checkNotNil(checkOffsetLength(make([]byte, 2), 1, 2))
checkNotNil(checkOffsetLength(make([]byte, 1), math.MaxInt32, 1))
checkNotNil(checkOffsetLength(make([]byte, 1), math.MinInt32, 1))
checkNotNil(checkOffsetLength(make([]byte, 1), 0, math.MinInt32))
checkNil(testOffsetLength(math.MaxInt32, math.MaxInt32-1, 1))
checkNotNil(testOffsetLength(math.MaxInt32, math.MaxInt32, 0))
checkNotNil(testOffsetLength(math.MaxInt32, math.MaxInt32, math.MinInt32))
checkNotNil(testOffsetLength(math.MaxInt32, math.MaxInt32-10, 11))
checkNil(testOffsetLength(math.MaxInt32, 0, math.MaxInt32))
checkNotNil(testOffsetLength(math.MaxInt32, 0, math.MinInt32))
checkNotNil(testOffsetLength(math.MaxInt32-1, 0, math.MaxInt32))
}
func BenchmarkSyscalls(b *testing.B) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(8))
buf := newBuffer(size64MiB, PROT_RDWR)
defer checkClose(buf)
var wg sync.WaitGroup
workChans := make([]chan struct{}, 0, 8)
for i := 0; i < cap(workChans); i++ {
wg.Add(1)
workChan := make(chan struct{})
workChans = append(workChans, workChan)
go func() {
<-workChan
for i := 0; i < b.N; i++ {
fd2, err := syscall.Dup(int(buf.File().Fd()))
if err != nil {
panic(err)
}
file2 := os.NewFile(uintptr(fd2), "")
buf2, err := NewBufferFile(file2, buf.Len(), PROT_READ)
if err != nil {
panic(err)
}
checkClose(buf2)
checkClose(file2)
}
wg.Done()
}()
}
b.StartTimer()
for _, workChan := range workChans {
close(workChan)
}
wg.Wait()
b.StopTimer()
}