github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/usage/cpu.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 ( 18 "time" 19 ) 20 21 // CPUStats contains the subset of struct rusage fields that relate to CPU 22 // scheduling. 23 // 24 // +stateify savable 25 type CPUStats struct { 26 // UserTime is the amount of time spent executing application code. 27 UserTime time.Duration 28 29 // SysTime is the amount of time spent executing sentry code. 30 SysTime time.Duration 31 32 // VoluntarySwitches is the number of times control has been voluntarily 33 // ceded due to blocking, etc. 34 VoluntarySwitches uint64 35 36 // InvoluntarySwitches (struct rusage::ru_nivcsw) is unsupported, since 37 // "preemptive" scheduling is managed by the Go runtime, which doesn't 38 // provide this information. 39 } 40 41 // Accumulate adds s2 to s. 42 func (s *CPUStats) Accumulate(s2 CPUStats) { 43 s.UserTime += s2.UserTime 44 s.SysTime += s2.SysTime 45 s.VoluntarySwitches += s2.VoluntarySwitches 46 } 47 48 // DifferenceSince computes s - earlierSample. 49 // 50 // Precondition: s >= earlierSample. 51 func (s *CPUStats) DifferenceSince(earlierSample CPUStats) CPUStats { 52 return CPUStats{ 53 UserTime: s.UserTime - earlierSample.UserTime, 54 SysTime: s.SysTime - earlierSample.SysTime, 55 VoluntarySwitches: s.VoluntarySwitches - earlierSample.VoluntarySwitches, 56 } 57 }