This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathmain.go
70 lines (61 loc) · 1.7 KB
/
main.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
/*
Copyright 2017 The Kubernetes Authors.
Copyright 2018 Intel Coporation.
SPDX-License-Identifier: Apache-2.0
*/
/*
This helper program is meant to cause a page fault event for
dax-mounted hugepage related testing. This program runs in a pod.
It creates a file and accesses data in it so that kernel gets a page fault,
while worker host has enabled tracing showing paging events.
This program prints out inode number and page address of data accessed,
(catenated together without whitespace) which is then used by e2e testing
code to verify that traced event was related to the action made here.
*/
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
"unsafe"
)
func handleError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runCommand(cmd string, args ...string) (string, error) {
output, err := exec.Command(cmd, args...).CombinedOutput()
handleError(err)
strOutput := string(output)
return strOutput, err
}
func main() {
const fname = "/mnt/hugepagedata"
var stat syscall.Stat_t
const size = 2*1024*1024 + 4
runPid := os.Getpid()
// Create the file
map_file, err := os.Create(fname)
handleError(err)
_, err = map_file.Seek(int64(size-1), 0)
handleError(err)
_, err = map_file.Write([]byte(" "))
handleError(err)
// Get inode number of the file
err = syscall.Stat(fname, &stat)
handleError(err)
mmap, err := syscall.Mmap(int(map_file.Fd()), 0, size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
handleError(err)
mapped := (*[size]byte)(unsafe.Pointer(&mmap[0]))
for i := 1; i < size; i++ {
mapped[i] = byte(runPid)
}
err = syscall.Munmap(mmap)
handleError(err)
err = map_file.Close()
handleError(err)
fmt.Printf("0x%x%p", stat.Ino, mapped)
}