Files
Jamie LiuandgVisor bot 406172e308 pgalloc,stateio: implement async page saving
Aside from improving checkpointing performance by using AIO to better utilize
disk bandwidth, this makes it possible to parallelize writing pages with
scanning for zero pages (in a following CL).

As part of this, update and document ownership semantics for the state file,
pages metadata file, and pages file, with the following goals:

- Since io.WriteCloser.Close() may flush buffers, it must return a nil error
  before write success is reported

- The pages metadata file is closed while async page saving is in progress (to
  hide the latency of the former)

PiperOrigin-RevId: 814315485
2025-10-02 11:32:53 -07:00

61 lines
1.6 KiB
Go

// Copyright 2025 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stateio
import (
"bufio"
"errors"
"io"
)
// BufioReadCloser is a wrapper around bufio.Reader that implements io.Closer
// by closing the underlying io.ReadCloser.
type BufioReadCloser struct {
bufio.Reader
io.Closer
}
// NewBufioReadCloser returns a new BufioReadCloser whose buffer has the
// default size.
func NewBufioReadCloser(rc io.ReadCloser) *BufioReadCloser {
brc := &BufioReadCloser{
Closer: rc,
}
brc.Reader.Reset(rc)
return brc
}
// BufioWriteCloser is a wrapper around bufio.Writer that implements io.Closer
// by closing the underlying io.WriteCloser.
type BufioWriteCloser struct {
bufio.Writer
Closer io.Closer
}
// NewBufioWriteCloser returns a new BufioWriteCloser whose buffer has the
// default size.
func NewBufioWriteCloser(wc io.WriteCloser) *BufioWriteCloser {
bwc := &BufioWriteCloser{
Closer: wc,
}
bwc.Writer.Reset(wc)
return bwc
}
// Close implements io.Closer.Close.
func (bwc *BufioWriteCloser) Close() error {
return errors.Join(bwc.Writer.Flush(), bwc.Closer.Close())
}