Created
May 9, 2017 16:00
-
-
Save abg/ab5fe37530f863fe78c631d3dbda3551 to your computer and use it in GitHub Desktop.
Quick & Dirty implementation of fincore in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"syscall" | |
"unsafe" | |
) | |
func fincore(path string) { | |
file, err := os.Open(path) | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
fileInfo, err := file.Stat() | |
if err != nil { | |
panic(err) | |
} | |
fileOffset := int64(0) | |
fileSize := int(fileInfo.Size()) | |
mmap, err := syscall.Mmap(int(file.Fd()), fileOffset, fileSize, syscall.PROT_NONE, syscall.MAP_SHARED) | |
if err != nil { | |
panic(err) | |
} | |
defer syscall.Munmap(mmap) | |
vecsz := (int64(fileSize) + int64(os.Getpagesize()) - 1) / int64(os.Getpagesize()) | |
vec := make([]byte, vecsz) | |
mmap_ptr := uintptr(unsafe.Pointer(&mmap[0])) | |
size_ptr := uintptr(fileSize) | |
vec_ptr := uintptr(unsafe.Pointer(&vec[0])) | |
r1, _, err := syscall.Syscall(syscall.SYS_MINCORE, mmap_ptr, size_ptr, vec_ptr) | |
if r1 != 0 { | |
panic("Oh god, mincore failed.") | |
} | |
cachedPages := 0 | |
for _, page := range vec { | |
if page&1 != 0 { | |
cachedPages++ | |
} | |
} | |
fmt.Printf("%s: %d of %d pages cached\n", path, cachedPages, len(vec)) | |
} | |
func main() { | |
for _, location := range os.Args[1:] { | |
fincore(location) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment