1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// Copyright 2018 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 linux
import (
"math"
"time"
)
const (
// ClockTick is the length of time represented by a single clock tick, as
// used by times(2) and /proc/[pid]/stat.
ClockTick = time.Second / CLOCKS_PER_SEC
// CLOCKS_PER_SEC is the number of ClockTicks per second.
//
// Linux defines this to be 100 on most architectures, irrespective of
// CONFIG_HZ. Userspace obtains the value through sysconf(_SC_CLK_TCK),
// which uses the AT_CLKTCK entry in the auxiliary vector if one is
// provided, and assumes 100 otherwise (glibc:
// sysdeps/posix/sysconf.c:__sysconf() =>
// sysdeps/unix/sysv/linux/getclktck.c, elf/dl-support.c:_dl_aux_init()).
//
// Not to be confused with POSIX CLOCKS_PER_SEC, as used by clock(3); "XSI
// requires that [POSIX] CLOCKS_PER_SEC equals 1000000 independent of the
// actual resolution" - clock(3).
CLOCKS_PER_SEC = 100
)
// CPU clock types for use with clock_gettime(2) et al.
//
// The 29 most significant bits of a 32 bit clock ID are either a PID or a FD.
//
// Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
//
// Bit 2 indicates whether a cpu clock refers to a thread or a process.
const (
CPUCLOCK_PROF = 0
CPUCLOCK_VIRT = 1
CPUCLOCK_SCHED = 2
CPUCLOCK_MAX = 3
CLOCKFD = CPUCLOCK_MAX
CPUCLOCK_CLOCK_MASK = 3
CPUCLOCK_PERTHREAD_MASK = 4
)
// Clock identifiers for use with clock_gettime(2), clock_getres(2),
// clock_nanosleep(2).
const (
CLOCK_REALTIME = 0
CLOCK_MONOTONIC = 1
CLOCK_PROCESS_CPUTIME_ID = 2
CLOCK_THREAD_CPUTIME_ID = 3
CLOCK_MONOTONIC_RAW = 4
CLOCK_REALTIME_COARSE = 5
CLOCK_MONOTONIC_COARSE = 6
CLOCK_BOOTTIME = 7
CLOCK_REALTIME_ALARM = 8
CLOCK_BOOTTIME_ALARM = 9
)
// Flags for clock_nanosleep(2).
const (
TIMER_ABSTIME = 1
)
// Flags for timerfd syscalls (timerfd_create(2), timerfd_settime(2)).
const (
// TFD_CLOEXEC is a timerfd_create flag.
TFD_CLOEXEC = O_CLOEXEC
// TFD_NONBLOCK is a timerfd_create flag.
TFD_NONBLOCK = O_NONBLOCK
// TFD_TIMER_ABSTIME is a timerfd_settime flag.
TFD_TIMER_ABSTIME = 1
)
// The safe number of seconds you can represent by int64.
const maxSecInDuration = math.MaxInt64 / int64(time.Second)
// TimeT represents time_t in <time.h>. It represents time in seconds.
//
// +marshal
type TimeT int64
// NsecToTimeT translates nanoseconds to TimeT (seconds).
func NsecToTimeT(nsec int64) TimeT {
return TimeT(nsec / 1e9)
}
// Timespec represents struct timespec in <time.h>.
//
// +marshal slice:TimespecSlice
type Timespec struct {
Sec int64
Nsec int64
}
// Unix returns the second and nanosecond.
func (ts Timespec) Unix() (sec int64, nsec int64) {
return int64(ts.Sec), int64(ts.Nsec)
}
// ToTime returns the Go time.Time representation.
func (ts Timespec) ToTime() time.Time {
return time.Unix(ts.Sec, ts.Nsec)
}
// ToNsec returns the nanosecond representation.
func (ts Timespec) ToNsec() int64 {
return int64(ts.Sec)*1e9 + int64(ts.Nsec)
}
// ToNsecCapped returns the safe nanosecond representation.
func (ts Timespec) ToNsecCapped() int64 {
if ts.Sec > maxSecInDuration {
return math.MaxInt64
}
return ts.ToNsec()
}
// ToDuration returns the safe nanosecond representation as time.Duration.
func (ts Timespec) ToDuration() time.Duration {
return time.Duration(ts.ToNsecCapped())
}
// Valid returns whether the timespec contains valid values.
func (ts Timespec) Valid() bool {
return !(ts.Sec < 0 || ts.Nsec < 0 || ts.Nsec >= int64(time.Second))
}
// NsecToTimespec translates nanoseconds to Timespec.
func NsecToTimespec(nsec int64) (ts Timespec) {
ts.Sec = nsec / 1e9
ts.Nsec = nsec % 1e9
return
}
// DurationToTimespec translates time.Duration to Timespec.
func DurationToTimespec(dur time.Duration) Timespec {
return NsecToTimespec(dur.Nanoseconds())
}
// SizeOfTimeval is the size of a Timeval struct in bytes.
const SizeOfTimeval = 16
// Timeval represents struct timeval in <time.h>.
//
// +marshal slice:TimevalSlice
type Timeval struct {
Sec int64
Usec int64
}
// ToNsecCapped returns the safe nanosecond representation.
func (tv Timeval) ToNsecCapped() int64 {
if tv.Sec > maxSecInDuration {
return math.MaxInt64
}
return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3
}
// ToDuration returns the safe nanosecond representation as a time.Duration.
func (tv Timeval) ToDuration() time.Duration {
return time.Duration(tv.ToNsecCapped())
}
// ToTime returns the Go time.Time representation.
func (tv Timeval) ToTime() time.Time {
return time.Unix(tv.Sec, tv.Usec*1e3)
}
// NsecToTimeval translates nanosecond to Timeval.
func NsecToTimeval(nsec int64) (tv Timeval) {
nsec += 999 // round up to microsecond
tv.Sec = nsec / 1e9
tv.Usec = nsec % 1e9 / 1e3
return
}
// DurationToTimeval translates time.Duration to Timeval.
func DurationToTimeval(dur time.Duration) Timeval {
return NsecToTimeval(dur.Nanoseconds())
}
// Itimerspec represents struct itimerspec in <time.h>.
//
// +marshal
type Itimerspec struct {
Interval Timespec
Value Timespec
}
// ItimerVal mimics the following struct in <sys/time.h>
// struct itimerval {
// struct timeval it_interval; /* next value */
// struct timeval it_value; /* current value */
// };
//
// +marshal
type ItimerVal struct {
Interval Timeval
Value Timeval
}
// ClockT represents type clock_t.
//
// +marshal
type ClockT int64
// ClockTFromDuration converts time.Duration to clock_t.
func ClockTFromDuration(d time.Duration) ClockT {
return ClockT(d / ClockTick)
}
// Tms represents struct tms, used by times(2).
//
// +marshal
type Tms struct {
UTime ClockT
STime ClockT
CUTime ClockT
CSTime ClockT
}
// TimerID represents type timer_t, which identifies a POSIX per-process
// interval timer.
//
// +marshal
type TimerID int32
// StatxTimestamp represents struct statx_timestamp.
//
// +marshal
type StatxTimestamp struct {
Sec int64
Nsec uint32
_ int32
}
// ToNsec returns the nanosecond representation.
func (sxts StatxTimestamp) ToNsec() int64 {
return int64(sxts.Sec)*1e9 + int64(sxts.Nsec)
}
// ToNsecCapped returns the safe nanosecond representation.
func (sxts StatxTimestamp) ToNsecCapped() int64 {
if sxts.Sec > maxSecInDuration {
return math.MaxInt64
}
return sxts.ToNsec()
}
// NsecToStatxTimestamp translates nanoseconds to StatxTimestamp.
func NsecToStatxTimestamp(nsec int64) (ts StatxTimestamp) {
return StatxTimestamp{
Sec: nsec / 1e9,
Nsec: uint32(nsec % 1e9),
}
}
// Utime represents struct utimbuf used by utimes(2).
//
// +marshal
type Utime struct {
Actime int64
Modtime int64
}
|