Skip to content

net.sendFile does not use sendfile() on linux in all the cases in which it is available #7005

Closed
@gopherbot

Description

@gopherbot

by shawnlandden:

sendfile() is backed by splice() which can do arbitrary bidirectional IO between a
limited set of fd types, including TCP UDP files AF_ALG, etc (anything with a sendpage()
handler in the kernel)

the following application connects two tcp sockets, which sendfile() supports, but the
go runtime does not use sendfile()

this patch doesn't work, and i'm not sure why, i'm being held up by 6776

diff -r c4b7c0824984 src/pkg/net/sendfile_linux.go
--- a/src/pkg/net/sendfile_linux.go Fri Dec 20 23:19:32 2013 -0800
+++ b/src/pkg/net/sendfile_linux.go Sun Dec 22 15:42:18 2013 -0800
@@ -8,6 +8,8 @@
    "io"
    "os"
    "syscall"
+   "reflect"
+   "fmt"
 )
 
 // maxSendfileSize is the largest chunk size we ask the kernel to copy
@@ -31,9 +33,17 @@
            return 0, nil, true
        }
    }
+   fmt.Println(reflect.TypeOf(r))
    f, ok := r.(*os.File)
+   var src int
    if !ok {
-       return 0, nil, false
+       t, ok := r.(*TCPConn)
+       if !ok {
+           return 0, nil, false
+       }
+       src = t.conn.fd.sysfd
+   } else {
+       src = int(f.Fd())
    }
 
    if err := c.writeLock(); err != nil {
@@ -42,7 +52,6 @@
    defer c.writeUnlock()
 
    dst := c.sysfd
-   src := int(f.Fd())
    for remain > 0 {
        n := maxSendfileSize
        if int64(n) > remain {

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1.run http://play.golang.org/p/EvOJBw-Esm in strace
2. uses read/write when sendfile() possible


Which compiler are you using (5g, 6g, 8g, gccgo)?
6g. gccgo create a program that crashes (debian sid)

Which operating system are you using?
debian sid

Which version are you using?  (run 'go version')
go version go1.2 linux/amd64

Please provide any additional information below.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions