Skip to content

Instantly share code, notes, and snippets.

@tumdum
Last active September 26, 2016 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tumdum/a10e1fea3c8edf9e7f04719ce4714214 to your computer and use it in GitHub Desktop.
Save tumdum/a10e1fea3c8edf9e7f04719ce4714214 to your computer and use it in GitHub Desktop.
Cost of stating files while walking linux kernel tree 4.7.5
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime/pprof"
)
// #include <dirent.h>
import "C"
type FileInfo struct {
Name string
Type uint8
}
func (f FileInfo) IsDirectory() bool {
return f.Type&C.DT_DIR != 0
}
func (f FileInfo) IsSymbolicLink() bool {
return f.Type&C.DT_LNK != 0
}
func (f FileInfo) IsRegularFile() bool {
return f.Type&C.DT_REG != 0
}
type WalkFunc func(path string, info FileInfo)
func Walk(path string, walkFunc WalkFunc) error {
f, err := os.Open(path)
if err != nil {
return err
}
dir := C.fdopendir(C.int(f.Fd()))
for {
dirent := C.readdir(dir)
if dirent == nil {
break
}
info := FileInfo{C.GoString((*C.char)(&dirent.d_name[0])), uint8(dirent.d_type)}
if info.Name == "." || info.Name == ".." {
continue
}
fullPath := path + "/" + info.Name
walkFunc(fullPath, info)
if info.IsDirectory() {
Walk(fullPath, walkFunc)
}
}
f.Close()
return nil
}
func main() {
if len(os.Args) != 2 {
log.Fatal("need one arg")
}
f, err := os.Create(os.Args[1] + ".prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
if os.Args[1] == "cgo" {
Walk(".", func(name string, info FileInfo) {
fmt.Println(name)
})
} else if os.Args[1] == "pure" {
filepath.Walk(".", func(name string, info os.FileInfo, err error) error {
fmt.Println(name)
return nil
})
}
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment