From b0fd25e26a0319fc0f9ef157f362e6de64bfc8b2 Mon Sep 17 00:00:00 2001 From: chaitanyakn Date: Tue, 3 Nov 2026 05:43:00 +0000 Subject: [PATCH 1/2] Parallelize ReadOp for multi-core performance - Parallelize ReadOp calls in ServeOps for multi-core performance benefit. - Convert to syscall.Read to allow simultaneous reads from the FUSE connection. - Update error handling in readMessage to handle raw syscall errnos (including EBADF). --- connection.go | 21 +++++++++-------- fuseutil/file_system.go | 43 +++++++++++++++++++---------------- internal/buffer/in_message.go | 7 +++++- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/connection.go b/connection.go index 429a3b7c..014741f5 100644 --- a/connection.go +++ b/connection.go @@ -401,15 +401,17 @@ func (c *Connection) readMessage() (*buffer.InMessage, error) { // * EINTR means we should try again. (This seems to happen often on // OS X, cf. http://golang.org/issue/11180) // + var errno error = err if pe, ok := err.(*os.PathError); ok { - switch pe.Err { - case syscall.ENODEV: - err = io.EOF + errno = pe.Err + } + switch errno { + case syscall.ENODEV, syscall.EBADF: + err = io.EOF - case syscall.EINTR: - err = nil - continue - } + case syscall.EINTR: + err = nil + continue } if err != nil { @@ -460,8 +462,9 @@ func (c *Connection) writeMessage(msg []byte) error { // If err != nil, the user is responsible for later calling c.Reply with the // returned context. // -// This function delivers ops in exactly the order they are received from -// /dev/fuse. It must not be called multiple times concurrently. +// ReadOp is safe to be called concurrently by multiple goroutines. Note that +// concurrent callers may receive ops out of the order in which they were +// delivered by /dev/fuse. // // LOCKS_EXCLUDED(c.mu) func (c *Connection) ReadOp() (_ context.Context, op interface{}, _ error) { diff --git a/fuseutil/file_system.go b/fuseutil/file_system.go index b1c27f56..985e0dad 100644 --- a/fuseutil/file_system.go +++ b/fuseutil/file_system.go @@ -104,27 +104,30 @@ func (s *fileSystemServer) ServeOps(c *fuse.Connection) { s.fs.Destroy() }() - for { - ctx, op, err := c.ReadOp() - if err == io.EOF { - break - } - - if err != nil { - panic(err) - } - - s.opsInFlight.Add(1) - if _, ok := op.(*fuseops.ForgetInodeOp); ok { - // Special case: call in this goroutine for - // forget inode ops, which may come in a - // flurry from the kernel and are generally - // cheap for the file system to handle - s.handleOp(c, ctx, op) - } else { - go s.handleOp(c, ctx, op) - } + const numReaders = 8 + var readers sync.WaitGroup + for range numReaders { + readers.Add(1) + go func() { + defer readers.Done() + for { + ctx, op, err := c.ReadOp() + if err == io.EOF { + return + } + if err != nil { + panic(err) + } + s.opsInFlight.Add(1) + if _, ok := op.(*fuseops.ForgetInodeOp); ok { + s.handleOp(c, ctx, op) + } else { + go s.handleOp(c, ctx, op) + } + } + }() } + readers.Wait() } func (s *fileSystemServer) handleOp( diff --git a/internal/buffer/in_message.go b/internal/buffer/in_message.go index 60eb7633..486e0ac5 100644 --- a/internal/buffer/in_message.go +++ b/internal/buffer/in_message.go @@ -17,6 +17,7 @@ package buffer import ( "fmt" "io" + "os" "sync" "syscall" "unsafe" @@ -88,7 +89,11 @@ func (m *InMessage) Init(r io.Reader) error { if fusekernel.IsPlatformFuseT { n, err = m.ReadSingle(r) } else { - n, err = r.Read(m.storage[:]) + if f, ok := r.(*os.File); ok { + n, err = syscall.Read(int(f.Fd()), m.storage[:]) + } else { + n, err = r.Read(m.storage[:]) + } } if err != nil { From 6e8e9b446651799b5e923f740d63ee463c8cf705 Mon Sep 17 00:00:00 2001 From: chaitanyakn Date: Wed, 18 Nov 2026 09:31:47 +0000 Subject: [PATCH 2/2] Make ReadOp reader count configurable via MountConfig.NumReaders - Add NumReaders field to MountConfig (defaults to 1 when unset or <= 0). - Expose Connection.NumReaders() for fuseutil.NewFileSystemServer and custom Server implementations. - Update fileSystemServer.ServeOps to use Connection.NumReaders() instead of hardcoded 8. - Add unit and multi-reader mount tests. --- connection.go | 9 +++++++++ connection_test.go | 26 ++++++++++++++++++++++++ fuseutil/file_system.go | 45 ++++++++++++++++++++++++++--------------- mount_config.go | 9 +++++++++ mount_test.go | 33 ++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 16 deletions(-) diff --git a/connection.go b/connection.go index 014741f5..a0ef1008 100644 --- a/connection.go +++ b/connection.go @@ -455,6 +455,15 @@ func (c *Connection) writeMessage(msg []byte) error { return nil } +// NumReaders returns the configured number of concurrent goroutines that should +// read ops from /dev/fuse, defaulting to 1 if unset or non-positive. +func (c *Connection) NumReaders() int { + if c.cfg.NumReaders <= 0 { + return 1 + } + return c.cfg.NumReaders +} + // ReadOp consumes the next op from the kernel process, returning the op and a // context that should be used for work related to the op. It returns io.EOF if // the kernel has closed the connection. diff --git a/connection_test.go b/connection_test.go index c1a4f013..fa8a323d 100644 --- a/connection_test.go +++ b/connection_test.go @@ -142,3 +142,29 @@ func TestSanitizeMaxPagesAndWrite(t *testing.T) { }) } } + +func TestConnectionNumReaders(t *testing.T) { + tests := []struct { + name string + input int + want int + }{ + {name: "unset defaults to 1", input: 0, want: 1}, + {name: "negative defaults to 1", input: -4, want: 1}, + {name: "explicit 1", input: 1, want: 1}, + {name: "explicit 8", input: 8, want: 8}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + c := &Connection{ + cfg: MountConfig{ + NumReaders: tc.input, + }, + } + if got := c.NumReaders(); got != tc.want { + t.Errorf("NumReaders() = %d, want %d", got, tc.want) + } + }) + } +} diff --git a/fuseutil/file_system.go b/fuseutil/file_system.go index 985e0dad..532d50ef 100644 --- a/fuseutil/file_system.go +++ b/fuseutil/file_system.go @@ -104,32 +104,45 @@ func (s *fileSystemServer) ServeOps(c *fuse.Connection) { s.fs.Destroy() }() - const numReaders = 8 + numReaders := c.NumReaders() + if numReaders == 1 { + s.readOps(c) + return + } + var readers sync.WaitGroup for range numReaders { readers.Add(1) go func() { defer readers.Done() - for { - ctx, op, err := c.ReadOp() - if err == io.EOF { - return - } - if err != nil { - panic(err) - } - s.opsInFlight.Add(1) - if _, ok := op.(*fuseops.ForgetInodeOp); ok { - s.handleOp(c, ctx, op) - } else { - go s.handleOp(c, ctx, op) - } - } + s.readOps(c) }() } readers.Wait() } +func (s *fileSystemServer) readOps(c *fuse.Connection) { + for { + ctx, op, err := c.ReadOp() + if err == io.EOF { + return + } + if err != nil { + panic(err) + } + s.opsInFlight.Add(1) + if _, ok := op.(*fuseops.ForgetInodeOp); ok { + // Special case: call in this goroutine for + // forget inode ops, which may come in a + // flurry from the kernel and are generally + // cheap for the file system to handle + s.handleOp(c, ctx, op) + } else { + go s.handleOp(c, ctx, op) + } + } +} + func (s *fileSystemServer) handleOp( c *fuse.Connection, ctx context.Context, diff --git a/mount_config.go b/mount_config.go index d98058ad..bcc938bb 100644 --- a/mount_config.go +++ b/mount_config.go @@ -276,6 +276,15 @@ type MountConfig struct { // If unset, a default value of 1 MiB is used (or less if MaxPages is set // to a value smaller than 256 pages). MaxWrite uint32 + + // Number of concurrent goroutines reading ops from /dev/fuse in + // fuseutil.NewFileSystemServer (and available to custom Server + // implementations via Connection.NumReaders). + // + // If unset or <= 0, defaults to 1. Values > 1 can improve multi-core + // throughput on Linux, but allow concurrent ForgetInode calls, pin one + // InMessage buffer per reader, and may dispatch ops out of arrival order. + NumReaders int } type FUSEImpl uint8 diff --git a/mount_test.go b/mount_test.go index 87cc36a2..8197595a 100644 --- a/mount_test.go +++ b/mount_test.go @@ -64,6 +64,39 @@ func TestSuccessfulMount(t *testing.T) { defer fuse.Unmount(mfs.Dir()) } +func TestSuccessfulMountWithMultipleReaders(t *testing.T) { + ctx := context.Background() + + // Set up a temporary directory. + dir, err := ioutil.TempDir("", "mount_test") + if err != nil { + t.Fatalf("ioutil.TempDir: %v", err) + } + + defer os.RemoveAll(dir) + + // Mount with multiple readers. + fs := &minimalFS{} + mfs, err := fuse.Mount( + dir, + fuseutil.NewFileSystemServer(fs), + &fuse.MountConfig{ + NumReaders: 8, + }) + + if err != nil { + t.Fatalf("fuse.Mount: %v", err) + } + + defer func() { + if err := mfs.Join(ctx); err != nil { + t.Errorf("Joining: %v", err) + } + }() + + defer fuse.Unmount(mfs.Dir()) +} + func TestNonexistentMountPoint(t *testing.T) { ctx := context.Background()