From: Shenghou Ma Date: Tue, 1 Jul 2014 22:24:43 +0000 (-0400) Subject: misc/nacl, syscall: lazily initialize fs on nacl. X-Git-Tag: go1.4beta1~1181 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bbe5c93e93922154d330cc4b7eecf148c830515c;p=gostls13.git misc/nacl, syscall: lazily initialize fs on nacl. On amd64, the real time is reduced from 176.76s to 140.26s. On ARM, the real time is reduced from 921.61s to 726.30s. LGTM=rsc R=rsc CC=golang-codereviews https://golang.org/cl/101580043 --- diff --git a/misc/nacl/mkzip.go b/misc/nacl/mkzip.go index 7b2de7d472..aaf37f120f 100644 --- a/misc/nacl/mkzip.go +++ b/misc/nacl/mkzip.go @@ -71,7 +71,13 @@ func main() { var w io.Writer = zf if *gopackage != "" { - fmt.Fprintf(zf, "package %s\n\nfunc init() {\n\tunzip(\"", *gopackage) + fmt.Fprintf(zf, `package %s +import "sync" +func init() { + var once sync.Once + fsinit = func() { + once.Do(func() { + unzip("`, *gopackage) gw := &goWriter{b: bufio.NewWriter(w)} defer func() { if err := gw.Close(); err != nil { @@ -214,7 +220,7 @@ func (w *goWriter) Write(b []byte) (int, error) { } func (w *goWriter) Close() error { - fmt.Fprintf(w.b, "\")\n}\n") + fmt.Fprintf(w.b, "\")\n\t\t})\n\t}\n}") w.b.Flush() return nil } diff --git a/src/pkg/syscall/fs_nacl.go b/src/pkg/syscall/fs_nacl.go index ac9239483b..657bc9d245 100644 --- a/src/pkg/syscall/fs_nacl.go +++ b/src/pkg/syscall/fs_nacl.go @@ -79,8 +79,13 @@ func newFsys() *fsys { } var fs = newFsys() +var fsinit = func() {} func init() { + // do not trigger loading of zipped file system here + oldFsinit := fsinit + defer func() { fsinit = oldFsinit }() + fsinit = func() {} Mkdir("/dev", 0555) Mkdir("/tmp", 0777) mkdev("/dev/null", 0666, openNull) @@ -93,7 +98,7 @@ func init() { func chdirEnv() { pwd, ok := Getenv("NACLPWD") if ok { - Chdir(pwd) + chdir(pwd) } } @@ -465,6 +470,7 @@ func (f *fsysFile) pwriteLocked(b []byte, offset int64) (int, error) { // Standard Unix system calls. func Open(path string, openmode int, perm uint32) (fd int, err error) { + fsinit() fs.mu.Lock() defer fs.mu.Unlock() f, err := fs.open(path, openmode, perm&0777|S_IFREG) @@ -487,6 +493,7 @@ func Getcwd(buf []byte) (n int, err error) { } func Stat(path string, st *Stat_t) error { + fsinit() fs.mu.Lock() defer fs.mu.Unlock() ip, _, err := fs.namei(path, false) @@ -502,6 +509,7 @@ func Lstat(path string, st *Stat_t) error { } func unlink(path string, isdir bool) error { + fsinit() fs.mu.Lock() defer fs.mu.Unlock() dp, elem, err := fs.namei(path, true) @@ -543,6 +551,7 @@ func Rmdir(path string) error { } func Chmod(path string, mode uint32) error { + fsinit() fs.mu.Lock() defer fs.mu.Unlock() ip, _, err := fs.namei(path, false) @@ -565,6 +574,7 @@ func Fchmod(fd int, mode uint32) error { } func Chown(path string, uid, gid int) error { + fsinit() fs.mu.Lock() defer fs.mu.Unlock() ip, _, err := fs.namei(path, false) @@ -598,6 +608,7 @@ func UtimesNano(path string, ts []Timespec) error { if len(ts) != 2 { return EINVAL } + fsinit() fs.mu.Lock() defer fs.mu.Unlock() ip, _, err := fs.namei(path, false) @@ -612,6 +623,7 @@ func UtimesNano(path string, ts []Timespec) error { } func Link(path, link string) error { + fsinit() ip, _, err := fs.namei(path, false) if err != nil { return err @@ -628,6 +640,7 @@ func Link(path, link string) error { } func Rename(from, to string) error { + fsinit() fdp, felem, err := fs.namei(from, true) if err != nil { return err @@ -664,6 +677,7 @@ func (fs *fsys) truncate(ip *inode, length int64) error { } func Truncate(path string, length int64) error { + fsinit() fs.mu.Lock() defer fs.mu.Unlock() ip, _, err := fs.namei(path, false) @@ -684,6 +698,11 @@ func Ftruncate(fd int, length int64) error { } func Chdir(path string) error { + fsinit() + return chdir(path) +} + +func chdir(path string) error { fs.mu.Lock() defer fs.mu.Unlock() ip, _, err := fs.namei(path, false) @@ -723,8 +742,6 @@ func Fsync(fd int) error { // Special devices. func mkdev(path string, mode uint32, open func() (devFile, error)) error { - fs.mu.Lock() - fs.mu.Unlock() f, err := fs.open(path, O_CREATE|O_RDONLY|O_EXCL, S_IFCHR|mode) if err != nil { return err