mirror of
https://github.com/hanzoai/visor.git
synced 2026-09-24 17:04:14 +00:00
When a pages file is provided to `runsc restore`, reads from that file are asynchronous (via statefile.AsyncReader) in order to maximize throughput. However, all such reads must complete before Kernel.LoadFrom() returns, so applications cannot execute before MemoryFile loading is complete. The main objective of this CL is to allow reads to continue after Kernel.LoadFrom() returns, allowing applications to execute while MemoryFile loading is still in progress. This behavior is user-visible: it affects whether deleting the pages file frees disk space immediately on POSIX filesystems, may affect whether deletion is possible on non-POSIX filesystems, and prevents unmounting regardless. Thus it is flag-guarded as `runsc restore --background`. MemoryFile ranges that have yet to be loaded, but that are being waited-for by applications, should be prioritized over ranges for which no application is waiting. This requires that application requests for data (calls to MemoryFile.(memmap.File).DataFD/MapInternal()) are able to determine which ranges have not yet been loaded, request reads for such ranges with elevated priority, and wait for only those reads to be completed; none of these are supported by the existing statefile.AsyncReader. Thus: - Add //pkg/sentry/pgalloc/aio, which provides an async I/O API that is designed to be easily implementable using a goroutine pool, Linux native AIO, or io_uring, though only includes a goroutine pool implementation. (io_uring is widely disabled due to security vulnerabilities. In my testing, Linux native AIO is slower than the goroutine pool, but this may change with lower GOMAXPROCS which needs further testing.) - Move I/O scheduling into pgalloc: introduce an async page loader goroutine that is started by MemoryFile.LoadFrom() when async page loading is requested (implicitly, via the existence of a pages file), which is responsible for driving submission of read requests and handling their completions. PiperOrigin-RevId: 679321884
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
// Copyright 2024 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 aio
|
|
|
|
import (
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// Request provides inputs to an asynchronous I/O operation.
|
|
type Request struct {
|
|
ID uint64 // copied to Completion.ID in the corresponding Completion
|
|
Op Op
|
|
FD int32 // host file descriptor
|
|
Off int64 // offset into FD
|
|
Buf unsafe.Pointer // depends on Op
|
|
Len int // depends on Op
|
|
}
|
|
|
|
// Read enqueues a read. The caller must ensure that the memory referred to by
|
|
// dst remains valid until the read is complete.
|
|
//
|
|
// Preconditions: As for q.Add().
|
|
func Read(q Queue, id uint64, fd int32, off int64, dst []byte) {
|
|
q.Add(Request{
|
|
ID: id,
|
|
Op: OpRead,
|
|
FD: fd,
|
|
Off: off,
|
|
Buf: unsafe.Pointer(unsafe.SliceData(dst)),
|
|
Len: len(dst),
|
|
})
|
|
}
|
|
|
|
// Write enqueues a write. The caller must ensure that the memory referred to
|
|
// by src remains valid until the write is complete.
|
|
//
|
|
// Preconditions: As for q.Add().
|
|
func Write(q Queue, id uint64, fd int32, off int64, src []byte) {
|
|
q.Add(Request{
|
|
ID: id,
|
|
Op: OpWrite,
|
|
FD: fd,
|
|
Off: off,
|
|
Buf: unsafe.Pointer(unsafe.SliceData(src)),
|
|
Len: len(src),
|
|
})
|
|
}
|
|
|
|
// Readv enqueues a vectored read. The caller must ensure that the struct iovec
|
|
// array referred to by dst, and the memory that those struct iovecs refer to,
|
|
// remain valid until the read is complete.
|
|
//
|
|
// Preconditions: As for q.Add().
|
|
func Readv(q Queue, id uint64, fd int32, off int64, dst []unix.Iovec) {
|
|
q.Add(Request{
|
|
ID: id,
|
|
Op: OpReadv,
|
|
FD: fd,
|
|
Off: off,
|
|
Buf: unsafe.Pointer(unsafe.SliceData(dst)),
|
|
Len: len(dst),
|
|
})
|
|
}
|
|
|
|
// Writev enqueues a vectored write. The caller must ensure that the struct
|
|
// iovec array referred to by src, and the memory that those struct iovecs
|
|
// refer to, remain valid until the write is complete.
|
|
//
|
|
// Preconditions: As for q.Add().
|
|
func Writev(q Queue, id uint64, fd int32, off int64, src []unix.Iovec) {
|
|
q.Add(Request{
|
|
ID: id,
|
|
Op: OpWritev,
|
|
FD: fd,
|
|
Off: off,
|
|
Buf: unsafe.Pointer(unsafe.SliceData(src)),
|
|
Len: len(src),
|
|
})
|
|
}
|