| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package tickstate |
| 4 | |
| 5 | import ( |
| 6 | "sync" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // SkipSnapshot is the skip-state view returned from MarkSkipped(). |
| 11 | type SkipSnapshot struct { |
| 12 | Count int |
| 13 | RunStarted time.Time |
| 14 | } |
| 15 | |
| 16 | // ResumeSnapshot is the resume-state view returned from MarkRunStart(). |
| 17 | type ResumeSnapshot struct { |
| 18 | Skipped int |
| 19 | RunStarted time.Time |
| 20 | RunStopped time.Time |
| 21 | } |
| 22 | |
| 23 | // SkipTracker tracks skipped scheduler ticks and run timing in a thread-safe way. |
| 24 | type SkipTracker struct { |
| 25 | mu sync.Mutex |
| 26 | skipped int |
| 27 | runStarted time.Time |
| 28 | runStopped time.Time |
| 29 | } |
| 30 | |
| 31 | // MarkSkipped records one dropped tick and returns current skip state. |
| 32 | func (t *SkipTracker) MarkSkipped() SkipSnapshot { |
| 33 | t.mu.Lock() |
| 34 | defer t.mu.Unlock() |
| 35 | |
| 36 | t.skipped++ |
| 37 | return SkipSnapshot{ |
| 38 | Count: t.skipped, |
| 39 | RunStarted: t.runStarted, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // MarkRunStart records run start and returns previous skip/resume state. |
| 44 | func (t *SkipTracker) MarkRunStart(now time.Time) ResumeSnapshot { |
| 45 | t.mu.Lock() |
| 46 | defer t.mu.Unlock() |
| 47 | |
| 48 | snapshot := ResumeSnapshot{ |
| 49 | Skipped: t.skipped, |
| 50 | RunStarted: t.runStarted, |
| 51 | RunStopped: t.runStopped, |
| 52 | } |
| 53 | t.skipped = 0 |
| 54 | t.runStarted = now |
| 55 | return snapshot |
| 56 | } |
| 57 | |
| 58 | // MarkRunStop records run completion time. |
| 59 | func (t *SkipTracker) MarkRunStop(now time.Time) { |
| 60 | t.mu.Lock() |
| 61 | t.runStopped = now |
| 62 | t.mu.Unlock() |
| 63 | } |