Skip to content

Commit 1fde010

Browse files
findleyrgopherbot
authored andcommitted
internal/mmap: add a test for atomic operations on mmapped files
While investigating golang/go#68311, we became concerned that assumptions of cross process atomic operations may not hold on all platforms. While this logic should be exercised indirectly via telemetry integration tests, add an explicit test to the mmap package for this core behavior. Updates golang/go#68311 Change-Id: Ibf5049e8a2fe03335e394af17f611207ca0f2016 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/597336 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent af73eac commit 1fde010

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

internal/mmap/mmap_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mmap_test
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
13+
"sync"
14+
"sync/atomic"
15+
"testing"
16+
"unsafe"
17+
18+
"golang.org/x/telemetry/internal/mmap"
19+
"golang.org/x/telemetry/internal/testenv"
20+
)
21+
22+
// If the sharedFileEnv environment variable is set,
23+
// increment an atomic value in that file rather than
24+
// run the test.
25+
const sharedFileEnv = "MMAP_TEST_SHARED_FILE"
26+
27+
func TestMain(m *testing.M) {
28+
if name := os.Getenv(sharedFileEnv); name != "" {
29+
_, mapping, err := openMapped(name)
30+
if err != nil {
31+
log.Fatalf("openMapped failed: %v", err)
32+
}
33+
34+
v := (*atomic.Uint64)(unsafe.Pointer(&mapping.Data[0]))
35+
v.Add(1)
36+
// Exit without explicitly calling munmap/close.
37+
os.Exit(0)
38+
}
39+
os.Exit(m.Run())
40+
}
41+
42+
func openMapped(name string) (*os.File, mmap.Data, error) {
43+
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0666)
44+
if err != nil {
45+
return nil, mmap.Data{}, fmt.Errorf("open failed: %v", err)
46+
}
47+
data, err := mmap.Mmap(f, nil)
48+
if err != nil {
49+
return nil, mmap.Data{}, fmt.Errorf("Mmap failed: %v", err)
50+
}
51+
return f, data, nil
52+
}
53+
54+
func TestSharedMemory(t *testing.T) {
55+
testenv.SkipIfUnsupportedPlatform(t)
56+
57+
// This test verifies that Mmap'ed files are usable for concurrent
58+
// cross-process atomic operations.
59+
60+
dir := t.TempDir()
61+
name := filepath.Join(dir, "shared.count")
62+
63+
var zero [8]byte
64+
if err := os.WriteFile(name, zero[:], 0666); err != nil {
65+
t.Fatal(err)
66+
}
67+
68+
// Fork+exec the current test process.
69+
// Child processes atomically increment the counter file in shared memory.
70+
71+
exe, err := os.Executable()
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
const concurrency = 100
77+
var wg sync.WaitGroup
78+
env := append(os.Environ(), sharedFileEnv+"="+name)
79+
for i := 0; i < concurrency; i++ {
80+
i := i
81+
wg.Add(1)
82+
go func() {
83+
defer wg.Done()
84+
cmd := exec.Command(exe)
85+
cmd.Env = env
86+
87+
if err := cmd.Run(); err != nil {
88+
t.Errorf("subcommand #%d failed: %v", i, err)
89+
}
90+
}()
91+
}
92+
93+
wg.Wait()
94+
95+
data, err := os.ReadFile(name)
96+
if err != nil {
97+
t.Fatalf("final read failed: %v", err)
98+
}
99+
v := (*atomic.Uint64)(unsafe.Pointer(&data[0]))
100+
if got := v.Load(); got != concurrency {
101+
t.Errorf("incremented %d times, want %d", got, concurrency)
102+
}
103+
}

0 commit comments

Comments
 (0)