gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/state/statefile/async_io.go (about)

     1  // Copyright 2024 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package statefile
    16  
    17  import (
    18  	"runtime"
    19  	"sync/atomic"
    20  
    21  	"gvisor.dev/gvisor/pkg/fd"
    22  	"gvisor.dev/gvisor/pkg/sync"
    23  )
    24  
    25  type chunk struct {
    26  	dst []byte
    27  	off int64
    28  }
    29  
    30  // AsyncReader can be used to do reads asynchronously. It does not change the
    31  // underlying file's offset.
    32  type AsyncReader struct {
    33  	// in is the backing file which contains all pages.
    34  	in *fd.FD
    35  	// off is the offset being read.
    36  	off int64
    37  	// q is the work queue.
    38  	q chan chunk
    39  	// err stores the latest IO error that occured during async read.
    40  	err atomic.Pointer[error]
    41  	// wg tracks all in flight work.
    42  	wg sync.WaitGroup
    43  }
    44  
    45  // NewAsyncReader initializes a new AsyncReader.
    46  func NewAsyncReader(in *fd.FD, off int64) *AsyncReader {
    47  	workers := runtime.GOMAXPROCS(0)
    48  	r := &AsyncReader{
    49  		in:  in,
    50  		off: off,
    51  		q:   make(chan chunk, workers),
    52  	}
    53  	for i := 0; i < workers; i++ {
    54  		go r.work()
    55  	}
    56  	return r
    57  }
    58  
    59  // ReadAsync schedules a read of len(p) bytes from current offset into p.
    60  func (r *AsyncReader) ReadAsync(p []byte) {
    61  	r.wg.Add(1)
    62  	r.q <- chunk{off: r.off, dst: p}
    63  	r.off += int64(len(p))
    64  }
    65  
    66  // Wait blocks until all in flight work is complete and then returns any IO
    67  // errors that occurred since the last call to Wait().
    68  func (r *AsyncReader) Wait() error {
    69  	r.wg.Wait()
    70  	if err := r.err.Swap(nil); err != nil {
    71  		return *err
    72  	}
    73  	return nil
    74  }
    75  
    76  // Close calls Wait() and additionally cleans up all worker goroutines.
    77  func (r *AsyncReader) Close() error {
    78  	err := r.Wait()
    79  	close(r.q)
    80  	return err
    81  }
    82  
    83  func (r *AsyncReader) work() {
    84  	for {
    85  		c := <-r.q
    86  		if c.dst == nil {
    87  			return
    88  		}
    89  		if _, err := r.in.ReadAt(c.dst, c.off); err != nil {
    90  			r.err.Store(&err)
    91  		}
    92  		r.wg.Done()
    93  	}
    94  }