github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/kernel/time/time.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 time defines the Timer type, which provides a periodic timer that 16 // works by sampling a user-provided clock. 17 package time 18 19 import ( 20 "fmt" 21 "math" 22 "time" 23 24 "github.com/SagerNet/gvisor/pkg/abi/linux" 25 "github.com/SagerNet/gvisor/pkg/errors/linuxerr" 26 "github.com/SagerNet/gvisor/pkg/sync" 27 "github.com/SagerNet/gvisor/pkg/waiter" 28 ) 29 30 // Events that may be generated by a Clock. 31 const ( 32 // ClockEventSet occurs when a Clock undergoes a discontinuous change. 33 ClockEventSet waiter.EventMask = 1 << iota 34 35 // ClockEventRateIncrease occurs when the rate at which a Clock advances 36 // increases significantly, such that values returned by previous calls to 37 // Clock.WallTimeUntil may be too large. 38 ClockEventRateIncrease 39 ) 40 41 // Time represents an instant in time with nanosecond precision. 42 // 43 // Time may represent time with respect to any clock and may not have any 44 // meaning in the real world. 45 // 46 // +stateify savable 47 type Time struct { 48 ns int64 49 } 50 51 var ( 52 // MinTime is the zero time instant, the lowest possible time that can 53 // be represented by Time. 54 MinTime = Time{ns: math.MinInt64} 55 56 // MaxTime is the highest possible time that can be represented by 57 // Time. 58 MaxTime = Time{ns: math.MaxInt64} 59 60 // ZeroTime represents the zero time in an unspecified Clock's domain. 61 ZeroTime = Time{ns: 0} 62 ) 63 64 const ( 65 // MinDuration is the minimum duration representable by time.Duration. 66 MinDuration = time.Duration(math.MinInt64) 67 68 // MaxDuration is the maximum duration representable by time.Duration. 69 MaxDuration = time.Duration(math.MaxInt64) 70 ) 71 72 // FromNanoseconds returns a Time representing the point ns nanoseconds after 73 // an unspecified Clock's zero time. 74 func FromNanoseconds(ns int64) Time { 75 return Time{ns} 76 } 77 78 // FromSeconds returns a Time representing the point s seconds after an 79 // unspecified Clock's zero time. 80 func FromSeconds(s int64) Time { 81 if s > math.MaxInt64/time.Second.Nanoseconds() { 82 return MaxTime 83 } 84 return Time{s * 1e9} 85 } 86 87 // FromUnix converts from Unix seconds and nanoseconds to Time, assuming a real 88 // time Unix clock domain. 89 func FromUnix(s int64, ns int64) Time { 90 if s > math.MaxInt64/time.Second.Nanoseconds() { 91 return MaxTime 92 } 93 t := s * 1e9 94 if t > math.MaxInt64-ns { 95 return MaxTime 96 } 97 return Time{t + ns} 98 } 99 100 // FromTimespec converts from Linux Timespec to Time. 101 func FromTimespec(ts linux.Timespec) Time { 102 return Time{ts.ToNsecCapped()} 103 } 104 105 // FromTimeval converts a Linux Timeval to Time. 106 func FromTimeval(tv linux.Timeval) Time { 107 return Time{tv.ToNsecCapped()} 108 } 109 110 // Nanoseconds returns nanoseconds elapsed since the zero time in t's Clock 111 // domain. If t represents walltime, this is nanoseconds since the Unix epoch. 112 func (t Time) Nanoseconds() int64 { 113 return t.ns 114 } 115 116 // Seconds returns seconds elapsed since the zero time in t's Clock domain. If 117 // t represents walltime, this is seconds since Unix epoch. 118 func (t Time) Seconds() int64 { 119 return t.Nanoseconds() / time.Second.Nanoseconds() 120 } 121 122 // Timespec converts Time to a Linux timespec. 123 func (t Time) Timespec() linux.Timespec { 124 return linux.NsecToTimespec(t.Nanoseconds()) 125 } 126 127 // Unix returns the (seconds, nanoseconds) representation of t such that 128 // seconds*1e9 + nanoseconds = t. 129 func (t Time) Unix() (s int64, ns int64) { 130 s = t.ns / 1e9 131 ns = t.ns % 1e9 132 return 133 } 134 135 // TimeT converts Time to a Linux time_t. 136 func (t Time) TimeT() linux.TimeT { 137 return linux.NsecToTimeT(t.Nanoseconds()) 138 } 139 140 // Timeval converts Time to a Linux timeval. 141 func (t Time) Timeval() linux.Timeval { 142 return linux.NsecToTimeval(t.Nanoseconds()) 143 } 144 145 // StatxTimestamp converts Time to a Linux statx_timestamp. 146 func (t Time) StatxTimestamp() linux.StatxTimestamp { 147 return linux.NsecToStatxTimestamp(t.Nanoseconds()) 148 } 149 150 // Add adds the duration of d to t. 151 func (t Time) Add(d time.Duration) Time { 152 if t.ns > 0 && d.Nanoseconds() > math.MaxInt64-int64(t.ns) { 153 return MaxTime 154 } 155 if t.ns < 0 && d.Nanoseconds() < math.MinInt64-int64(t.ns) { 156 return MinTime 157 } 158 return Time{int64(t.ns) + d.Nanoseconds()} 159 } 160 161 // AddTime adds the duration of u to t. 162 func (t Time) AddTime(u Time) Time { 163 return t.Add(time.Duration(u.ns)) 164 } 165 166 // Equal reports whether the two times represent the same instant in time. 167 func (t Time) Equal(u Time) bool { 168 return t.ns == u.ns 169 } 170 171 // Before reports whether the instant t is before the instant u. 172 func (t Time) Before(u Time) bool { 173 return t.ns < u.ns 174 } 175 176 // After reports whether the instant t is after the instant u. 177 func (t Time) After(u Time) bool { 178 return t.ns > u.ns 179 } 180 181 // Sub returns the duration of t - u. 182 // 183 // N.B. This measure may not make sense for every Time returned by ktime.Clock. 184 // Callers who need wall time duration can use ktime.Clock.WallTimeUntil to 185 // estimate that wall time. 186 func (t Time) Sub(u Time) time.Duration { 187 dur := time.Duration(int64(t.ns)-int64(u.ns)) * time.Nanosecond 188 switch { 189 case u.Add(dur).Equal(t): 190 return dur 191 case t.Before(u): 192 return MinDuration 193 default: 194 return MaxDuration 195 } 196 } 197 198 // IsMin returns whether t represents the lowest possible time instant. 199 func (t Time) IsMin() bool { 200 return t == MinTime 201 } 202 203 // IsZero returns whether t represents the zero time instant in t's Clock domain. 204 func (t Time) IsZero() bool { 205 return t == ZeroTime 206 } 207 208 // String returns the time represented in nanoseconds as a string. 209 func (t Time) String() string { 210 return fmt.Sprintf("%dns", t.Nanoseconds()) 211 } 212 213 // A Clock is an abstract time source. 214 type Clock interface { 215 // Now returns the current time in nanoseconds according to the Clock. 216 Now() Time 217 218 // WallTimeUntil returns the estimated wall time until Now will return a 219 // value greater than or equal to t, given that a recent call to Now 220 // returned now. If t has already passed, WallTimeUntil may return 0 or a 221 // negative value. 222 // 223 // WallTimeUntil must be abstract to support Clocks that do not represent 224 // wall time (e.g. thread group execution timers). Clocks that represent 225 // wall times may embed the WallRateClock type to obtain an appropriate 226 // trivial implementation of WallTimeUntil. 227 // 228 // WallTimeUntil is used to determine when associated Timers should next 229 // check for expirations. Returning too small a value may result in 230 // spurious Timer goroutine wakeups, while returning too large a value may 231 // result in late expirations. Implementations should usually err on the 232 // side of underestimating. 233 WallTimeUntil(t, now Time) time.Duration 234 235 // Waitable methods may be used to subscribe to Clock events. Waiters will 236 // not be preserved by Save and must be re-established during restore. 237 // 238 // Since Clock events are transient, implementations of 239 // waiter.Waitable.Readiness should return 0. 240 waiter.Waitable 241 } 242 243 // WallRateClock implements Clock.WallTimeUntil for Clocks that elapse at the 244 // same rate as wall time. 245 type WallRateClock struct{} 246 247 // WallTimeUntil implements Clock.WallTimeUntil. 248 func (*WallRateClock) WallTimeUntil(t, now Time) time.Duration { 249 return t.Sub(now) 250 } 251 252 // NoClockEvents implements waiter.Waitable for Clocks that do not generate 253 // events. 254 type NoClockEvents struct{} 255 256 // Readiness implements waiter.Waitable.Readiness. 257 func (*NoClockEvents) Readiness(mask waiter.EventMask) waiter.EventMask { 258 return 0 259 } 260 261 // EventRegister implements waiter.Waitable.EventRegister. 262 func (*NoClockEvents) EventRegister(e *waiter.Entry, mask waiter.EventMask) { 263 } 264 265 // EventUnregister implements waiter.Waitable.EventUnregister. 266 func (*NoClockEvents) EventUnregister(e *waiter.Entry) { 267 } 268 269 // ClockEventsQueue implements waiter.Waitable by wrapping waiter.Queue and 270 // defining waiter.Waitable.Readiness as required by Clock. 271 type ClockEventsQueue struct { 272 waiter.Queue 273 } 274 275 // Readiness implements waiter.Waitable.Readiness. 276 func (*ClockEventsQueue) Readiness(mask waiter.EventMask) waiter.EventMask { 277 return 0 278 } 279 280 // A TimerListener receives expirations from a Timer. 281 type TimerListener interface { 282 // Notify is called when its associated Timer expires. exp is the number of 283 // expirations. setting is the next timer Setting. 284 // 285 // Notify is called with the associated Timer's mutex locked, so Notify 286 // must not take any locks that precede Timer.mu in lock order. 287 // 288 // If Notify returns true, the timer will use the returned setting 289 // rather than the passed one. 290 // 291 // Preconditions: exp > 0. 292 Notify(exp uint64, setting Setting) (newSetting Setting, update bool) 293 294 // Destroy is called when the timer is destroyed. 295 Destroy() 296 } 297 298 // Setting contains user-controlled mutable Timer properties. 299 // 300 // +stateify savable 301 type Setting struct { 302 // Enabled is true if the timer is running. 303 Enabled bool 304 305 // Next is the time in nanoseconds of the next expiration. 306 Next Time 307 308 // Period is the time in nanoseconds between expirations. If Period is 309 // zero, the timer will not automatically restart after expiring. 310 // 311 // Invariant: Period >= 0. 312 Period time.Duration 313 } 314 315 // SettingFromSpec converts a (value, interval) pair to a Setting based on a 316 // reading from c. value is interpreted as a time relative to c.Now(). 317 func SettingFromSpec(value time.Duration, interval time.Duration, c Clock) (Setting, error) { 318 return SettingFromSpecAt(value, interval, c.Now()) 319 } 320 321 // SettingFromSpecAt converts a (value, interval) pair to a Setting. value is 322 // interpreted as a time relative to now. 323 func SettingFromSpecAt(value time.Duration, interval time.Duration, now Time) (Setting, error) { 324 if value < 0 { 325 return Setting{}, linuxerr.EINVAL 326 } 327 if value == 0 { 328 return Setting{Period: interval}, nil 329 } 330 return Setting{ 331 Enabled: true, 332 Next: now.Add(value), 333 Period: interval, 334 }, nil 335 } 336 337 // SettingFromAbsSpec converts a (value, interval) pair to a Setting. value is 338 // interpreted as an absolute time. 339 func SettingFromAbsSpec(value Time, interval time.Duration) (Setting, error) { 340 if value.Before(ZeroTime) { 341 return Setting{}, linuxerr.EINVAL 342 } 343 if value.IsZero() { 344 return Setting{Period: interval}, nil 345 } 346 return Setting{ 347 Enabled: true, 348 Next: value, 349 Period: interval, 350 }, nil 351 } 352 353 // SettingFromItimerspec converts a linux.Itimerspec to a Setting. If abs is 354 // true, its.Value is interpreted as an absolute time. Otherwise, it is 355 // interpreted as a time relative to c.Now(). 356 func SettingFromItimerspec(its linux.Itimerspec, abs bool, c Clock) (Setting, error) { 357 if abs { 358 return SettingFromAbsSpec(FromTimespec(its.Value), its.Interval.ToDuration()) 359 } 360 return SettingFromSpec(its.Value.ToDuration(), its.Interval.ToDuration(), c) 361 } 362 363 // SpecFromSetting converts a timestamp and a Setting to a (relative value, 364 // interval) pair, as used by most Linux syscalls that return a struct 365 // itimerval or struct itimerspec. 366 func SpecFromSetting(now Time, s Setting) (value, period time.Duration) { 367 if !s.Enabled { 368 return 0, s.Period 369 } 370 return s.Next.Sub(now), s.Period 371 } 372 373 // ItimerspecFromSetting converts a Setting to a linux.Itimerspec. 374 func ItimerspecFromSetting(now Time, s Setting) linux.Itimerspec { 375 val, iv := SpecFromSetting(now, s) 376 return linux.Itimerspec{ 377 Interval: linux.DurationToTimespec(iv), 378 Value: linux.DurationToTimespec(val), 379 } 380 } 381 382 // At returns an updated Setting and a number of expirations after the 383 // associated Clock indicates a time of now. 384 // 385 // Settings may be created by successive calls to At with decreasing 386 // values of now (i.e. time may appear to go backward). Supporting this is 387 // required to support non-monotonic clocks, as well as allowing 388 // Timer.clock.Now() to be called without holding Timer.mu. 389 func (s Setting) At(now Time) (Setting, uint64) { 390 if !s.Enabled { 391 return s, 0 392 } 393 if s.Next.After(now) { 394 return s, 0 395 } 396 if s.Period == 0 { 397 s.Enabled = false 398 return s, 1 399 } 400 exp := 1 + uint64(now.Sub(s.Next).Nanoseconds())/uint64(s.Period) 401 s.Next = s.Next.Add(time.Duration(uint64(s.Period) * exp)) 402 return s, exp 403 } 404 405 // Timer is an optionally-periodic timer driven by sampling a user-specified 406 // Clock. Timer's semantics support the requirements of Linux's interval timers 407 // (setitimer(2), timer_create(2), timerfd_create(2)). 408 // 409 // Timers should be created using NewTimer and must be cleaned up by calling 410 // Timer.Destroy when no longer used. 411 // 412 // +stateify savable 413 type Timer struct { 414 // clock is the time source. clock is immutable. 415 clock Clock 416 417 // listener is notified of expirations. listener is immutable. 418 listener TimerListener 419 420 // mu protects the following mutable fields. 421 mu sync.Mutex `state:"nosave"` 422 423 // setting is the timer setting. setting is protected by mu. 424 setting Setting 425 426 // paused is true if the Timer is paused. paused is protected by mu. 427 paused bool 428 429 // kicker is used to wake the Timer goroutine. The kicker pointer is 430 // immutable, but its state is protected by mu. 431 kicker *time.Timer `state:"nosave"` 432 433 // entry is registered with clock.EventRegister. entry is immutable. 434 // 435 // Per comment in Clock, entry must be re-registered after restore; per 436 // comment in Timer.Load, this is done in Timer.Resume. 437 entry waiter.Entry `state:"nosave"` 438 439 // events is the channel that will be notified whenever entry receives an 440 // event. It is also closed by Timer.Destroy to instruct the Timer 441 // goroutine to exit. 442 events chan struct{} `state:"nosave"` 443 } 444 445 // timerTickEvents are Clock events that require the Timer goroutine to Tick 446 // prematurely. 447 const timerTickEvents = ClockEventSet | ClockEventRateIncrease 448 449 // NewTimer returns a new Timer that will obtain time from clock and send 450 // expirations to listener. The Timer is initially stopped and has no first 451 // expiration or period configured. 452 func NewTimer(clock Clock, listener TimerListener) *Timer { 453 t := &Timer{ 454 clock: clock, 455 listener: listener, 456 } 457 t.init() 458 return t 459 } 460 461 // init initializes Timer state that is not preserved across save/restore. If 462 // init has already been called, calling it again is a no-op. 463 // 464 // Preconditions: t.mu must be locked, or the caller must have exclusive access 465 // to t. 466 func (t *Timer) init() { 467 if t.kicker != nil { 468 return 469 } 470 // If t.kicker is nil, the Timer goroutine can't be running, so we can't 471 // race with it. 472 t.kicker = time.NewTimer(0) 473 t.entry, t.events = waiter.NewChannelEntry(nil) 474 t.clock.EventRegister(&t.entry, timerTickEvents) 475 go t.runGoroutine() // S/R-SAFE: synchronized by t.mu 476 } 477 478 // Destroy releases resources owned by the Timer. A Destroyed Timer must not be 479 // used again; in particular, a Destroyed Timer should not be Saved. 480 func (t *Timer) Destroy() { 481 // Stop the Timer, ensuring that the Timer goroutine will not call 482 // t.kicker.Reset, before calling t.kicker.Stop. 483 t.mu.Lock() 484 t.setting.Enabled = false 485 t.mu.Unlock() 486 t.kicker.Stop() 487 // Unregister t.entry, ensuring that the Clock will not send to t.events, 488 // before closing t.events to instruct the Timer goroutine to exit. 489 t.clock.EventUnregister(&t.entry) 490 close(t.events) 491 t.listener.Destroy() 492 } 493 494 func (t *Timer) runGoroutine() { 495 for { 496 select { 497 case <-t.kicker.C: 498 case _, ok := <-t.events: 499 if !ok { 500 // Channel closed by Destroy. 501 return 502 } 503 } 504 t.Tick() 505 } 506 } 507 508 // Tick requests that the Timer immediately check for expirations and 509 // re-evaluate when it should next check for expirations. 510 func (t *Timer) Tick() { 511 now := t.clock.Now() 512 t.mu.Lock() 513 defer t.mu.Unlock() 514 if t.paused { 515 return 516 } 517 s, exp := t.setting.At(now) 518 t.setting = s 519 if exp > 0 { 520 if newS, ok := t.listener.Notify(exp, t.setting); ok { 521 t.setting = newS 522 } 523 } 524 t.resetKickerLocked(now) 525 } 526 527 // Pause pauses the Timer, ensuring that it does not generate any further 528 // expirations until Resume is called. If the Timer is already paused, Pause 529 // has no effect. 530 func (t *Timer) Pause() { 531 t.mu.Lock() 532 defer t.mu.Unlock() 533 t.paused = true 534 // t.kicker may be nil if we were restored but never resumed. 535 if t.kicker != nil { 536 t.kicker.Stop() 537 } 538 } 539 540 // Resume ends the effect of Pause. If the Timer is not paused, Resume has no 541 // effect. 542 func (t *Timer) Resume() { 543 t.mu.Lock() 544 defer t.mu.Unlock() 545 if !t.paused { 546 return 547 } 548 t.paused = false 549 550 // Lazily initialize the Timer. We can't call Timer.init until Timer.Resume 551 // because save/restore will restore Timers before 552 // kernel.Timekeeper.SetClocks() has been called, so if t.clock is backed 553 // by a kernel.Timekeeper then the Timer goroutine will panic if it calls 554 // t.clock.Now(). 555 t.init() 556 557 // Kick the Timer goroutine in case it was already initialized, but the 558 // Timer goroutine was sleeping. 559 t.kicker.Reset(0) 560 } 561 562 // Get returns a snapshot of the Timer's current Setting and the time 563 // (according to the Timer's Clock) at which the snapshot was taken. 564 // 565 // Preconditions: The Timer must not be paused (since its Setting cannot 566 // be advanced to the current time while it is paused.) 567 func (t *Timer) Get() (Time, Setting) { 568 now := t.clock.Now() 569 t.mu.Lock() 570 defer t.mu.Unlock() 571 if t.paused { 572 panic(fmt.Sprintf("Timer.Get called on paused Timer %p", t)) 573 } 574 s, exp := t.setting.At(now) 575 t.setting = s 576 if exp > 0 { 577 if newS, ok := t.listener.Notify(exp, t.setting); ok { 578 t.setting = newS 579 } 580 } 581 t.resetKickerLocked(now) 582 return now, s 583 } 584 585 // Swap atomically changes the Timer's Setting and returns the Timer's previous 586 // Setting and the time (according to the Timer's Clock) at which the snapshot 587 // was taken. Setting s.Enabled to true starts the Timer, while setting 588 // s.Enabled to false stops it. 589 // 590 // Preconditions: The Timer must not be paused. 591 func (t *Timer) Swap(s Setting) (Time, Setting) { 592 return t.SwapAnd(s, nil) 593 } 594 595 // SwapAnd atomically changes the Timer's Setting, calls f if it is not nil, 596 // and returns the Timer's previous Setting and the time (according to the 597 // Timer's Clock) at which the Setting was changed. Setting s.Enabled to true 598 // starts the timer, while setting s.Enabled to false stops it. 599 // 600 // Preconditions: 601 // * The Timer must not be paused. 602 // * f cannot call any Timer methods since it is called with the Timer mutex 603 // locked. 604 func (t *Timer) SwapAnd(s Setting, f func()) (Time, Setting) { 605 now := t.clock.Now() 606 t.mu.Lock() 607 defer t.mu.Unlock() 608 if t.paused { 609 panic(fmt.Sprintf("Timer.SwapAnd called on paused Timer %p", t)) 610 } 611 oldS, oldExp := t.setting.At(now) 612 if oldExp > 0 { 613 t.listener.Notify(oldExp, oldS) 614 // N.B. The returned Setting doesn't matter because we're about 615 // to overwrite. 616 } 617 if f != nil { 618 f() 619 } 620 newS, newExp := s.At(now) 621 t.setting = newS 622 if newExp > 0 { 623 if newS, ok := t.listener.Notify(newExp, t.setting); ok { 624 t.setting = newS 625 } 626 } 627 t.resetKickerLocked(now) 628 return now, oldS 629 } 630 631 // Atomically invokes f atomically with respect to expirations of t; that is, t 632 // cannot generate expirations while f is being called. 633 // 634 // Preconditions: f cannot call any Timer methods since it is called with the 635 // Timer mutex locked. 636 func (t *Timer) Atomically(f func()) { 637 t.mu.Lock() 638 defer t.mu.Unlock() 639 f() 640 } 641 642 // Preconditions: t.mu must be locked. 643 func (t *Timer) resetKickerLocked(now Time) { 644 if t.setting.Enabled { 645 // Clock.WallTimeUntil may return a negative value. This is fine; 646 // time.when treats negative Durations as 0. 647 t.kicker.Reset(t.clock.WallTimeUntil(t.setting.Next, now)) 648 } 649 // We don't call t.kicker.Stop if !t.setting.Enabled because in most cases 650 // resetKickerLocked will be called from the Timer goroutine itself, in 651 // which case t.kicker has already fired and t.kicker.Stop will be an 652 // expensive no-op (time.Timer.Stop => time.stopTimer => runtime.stopTimer 653 // => runtime.deltimer). 654 } 655 656 // Clock returns the Clock used by t. 657 func (t *Timer) Clock() Clock { 658 return t.clock 659 } 660 661 // ChannelNotifier is a TimerListener that sends a message on an empty struct 662 // channel. 663 // 664 // ChannelNotifier cannot be saved or loaded. 665 type ChannelNotifier struct { 666 // tchan must be a buffered channel. 667 tchan chan struct{} 668 } 669 670 // NewChannelNotifier creates a new channel notifier. 671 // 672 // If the notifier is used with a timer, Timer.Destroy will close the channel 673 // returned here. 674 func NewChannelNotifier() (TimerListener, <-chan struct{}) { 675 tchan := make(chan struct{}, 1) 676 return &ChannelNotifier{tchan}, tchan 677 } 678 679 // Notify implements ktime.TimerListener.Notify. 680 func (c *ChannelNotifier) Notify(uint64, Setting) (Setting, bool) { 681 select { 682 case c.tchan <- struct{}{}: 683 default: 684 } 685 686 return Setting{}, false 687 } 688 689 // Destroy implements ktime.TimerListener.Destroy and will close the channel. 690 func (c *ChannelNotifier) Destroy() { 691 close(c.tchan) 692 }