]> Cypherpunks repositories - gostls13.git/commitdiff
misc/nacl, syscall: lazily initialize fs on nacl.
authorShenghou Ma <minux@golang.org>
Tue, 1 Jul 2014 22:24:43 +0000 (18:24 -0400)
committerShenghou Ma <minux@golang.org>
Tue, 1 Jul 2014 22:24:43 +0000 (18:24 -0400)
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

misc/nacl/mkzip.go
src/pkg/syscall/fs_nacl.go

index 7b2de7d472016261f43f99b6b69b962b3b8fbdad..aaf37f120fccd6d8d2f81c26efec2253c21e6cf9 100644 (file)
@@ -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
 }
index ac9239483b65a59ecb7026931f518c8d1c1636e2..657bc9d245f048d2d72f49dd2e5d600d096cbbfc 100644 (file)
@@ -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