gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/usage/io.go (about) 1 // Copyright 2018 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 usage 16 17 import "gvisor.dev/gvisor/pkg/atomicbitops" 18 19 // IO contains I/O-related statistics. 20 // 21 // +stateify savable 22 type IO struct { 23 // CharsRead is the number of bytes read by read syscalls. 24 CharsRead atomicbitops.Uint64 25 26 // CharsWritten is the number of bytes written by write syscalls. 27 CharsWritten atomicbitops.Uint64 28 29 // ReadSyscalls is the number of read syscalls. 30 ReadSyscalls atomicbitops.Uint64 31 32 // WriteSyscalls is the number of write syscalls. 33 WriteSyscalls atomicbitops.Uint64 34 35 // The following counter is only meaningful when Sentry has internal 36 // pagecache. 37 38 // BytesRead is the number of bytes actually read into pagecache. 39 BytesRead atomicbitops.Uint64 40 41 // BytesWritten is the number of bytes actually written from pagecache. 42 BytesWritten atomicbitops.Uint64 43 44 // BytesWriteCancelled is the number of bytes not written out due to 45 // truncation. 46 BytesWriteCancelled atomicbitops.Uint64 47 } 48 49 // Clone turns other into a clone of i. 50 func (i *IO) Clone(other *IO) { 51 other.CharsRead.Store(i.CharsRead.Load()) 52 other.CharsWritten.Store(i.CharsWritten.Load()) 53 other.ReadSyscalls.Store(i.ReadSyscalls.Load()) 54 other.WriteSyscalls.Store(i.WriteSyscalls.Load()) 55 other.BytesRead.Store(i.BytesRead.Load()) 56 other.BytesWritten.Store(i.BytesWritten.Load()) 57 other.BytesWriteCancelled.Store(i.BytesWriteCancelled.Load()) 58 } 59 60 // AccountReadSyscall does the accounting for a read syscall. 61 func (i *IO) AccountReadSyscall(bytes int64) { 62 i.ReadSyscalls.Add(1) 63 if bytes > 0 { 64 i.CharsRead.Add(uint64(bytes)) 65 } 66 } 67 68 // AccountWriteSyscall does the accounting for a write syscall. 69 func (i *IO) AccountWriteSyscall(bytes int64) { 70 i.WriteSyscalls.Add(1) 71 if bytes > 0 { 72 i.CharsWritten.Add(uint64(bytes)) 73 } 74 } 75 76 // AccountReadIO does the accounting for a read IO into the file system. 77 func (i *IO) AccountReadIO(bytes int64) { 78 if bytes > 0 { 79 i.BytesRead.Add(uint64(bytes)) 80 } 81 } 82 83 // AccountWriteIO does the accounting for a write IO into the file system. 84 func (i *IO) AccountWriteIO(bytes int64) { 85 if bytes > 0 { 86 i.BytesWritten.Add(uint64(bytes)) 87 } 88 } 89 90 // Accumulate adds up io usages. 91 func (i *IO) Accumulate(io *IO) { 92 i.CharsRead.Add(io.CharsRead.Load()) 93 i.CharsWritten.Add(io.CharsWritten.Load()) 94 i.ReadSyscalls.Add(io.ReadSyscalls.Load()) 95 i.WriteSyscalls.Add(io.WriteSyscalls.Load()) 96 i.BytesRead.Add(io.BytesRead.Load()) 97 i.BytesWritten.Add(io.BytesWritten.Load()) 98 i.BytesWriteCancelled.Add(io.BytesWriteCancelled.Load()) 99 }