Last active
September 26, 2016 20:37
-
-
Save tumdum/a10e1fea3c8edf9e7f04719ce4714214 to your computer and use it in GitHub Desktop.
Cost of stating files while walking linux kernel tree 4.7.5
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" | |
"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 | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment