|
| 1 | +package pebble |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +const decayRate = 0.99 |
| 12 | +const sampleRate = 10 * time.Millisecond |
| 13 | +const numSamples = 100 |
| 14 | +const minUtilization = 0.1 |
| 15 | + |
| 16 | +// Smoother will attempt to smooth out a process that runs multiple iterations. |
| 17 | +// The goal is not to have it run faster or slower, but simply with pace itself |
| 18 | +// to run evenly over time rather than being bunchy. |
| 19 | +// |
| 20 | +// Each "thread" can be in one of three states, Running, Idle, Sleeping. |
| 21 | +// * Running means the thread is currently working (and writing to disk). |
| 22 | +// * Idle means this thread is not doing anything as there is no work to do. |
| 23 | +// * Sleeping means the thread has active work to do, but is sleeping to smooth load. |
| 24 | +// |
| 25 | +// The goal of the smoother is to turn all Idle work into Sleeping work while |
| 26 | +// preserving the amount of Running work that is being done. The sleeping work |
| 27 | +// can be somewhat evenly placed between small Running tasks. The perfect |
| 28 | +// smoother would result in the flush / compaction write bandwidth to be a |
| 29 | +// constant. |
| 30 | +// |
| 31 | +// Some simplifying assumptions are made which could be improved upon. |
| 32 | +// * All tasks runs for a similar amount of time (estimatedIterDurationNs). |
| 33 | +// * All tasks create a similar amount of IO. |
| 34 | +// * Different types of tasks (compaction vs flush) are similar cost. |
| 35 | +// |
| 36 | +// After a Smoother is created, it must be started by calling start. |
| 37 | +type Smoother struct { |
| 38 | + enabled bool |
| 39 | + runningCount int32 |
| 40 | + sleepingCount int32 |
| 41 | + stopper chan struct{} |
| 42 | + |
| 43 | + mu struct { |
| 44 | + sync.Mutex |
| 45 | + estimatedIterDurationNs float64 |
| 46 | + estimatedUtilization float64 |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (s *Smoother) start() { |
| 51 | + s.mu.estimatedUtilization = 1.0 |
| 52 | + s.stopper = make(chan struct{}) |
| 53 | + |
| 54 | + go func() { |
| 55 | + ticker := time.NewTicker(sampleRate) |
| 56 | + defer ticker.Stop() |
| 57 | + |
| 58 | + var sampleRunning, sampleSleeping int32 |
| 59 | + var totalSamples int32 |
| 60 | + |
| 61 | + for { |
| 62 | + select { |
| 63 | + case <-s.stopper: |
| 64 | + return |
| 65 | + |
| 66 | + case <-ticker.C: |
| 67 | + totalSamples++ |
| 68 | + // NB: The number of running and sleeping can be >1. This allows |
| 69 | + // utilization to also be greater than 1. Once util is >1 the smoother |
| 70 | + // is disabled. In a single-threaded system this would never occur, |
| 71 | + // however our flushing and compaction is multi-threaded, however our |
| 72 | + // flushing and compaction is multi-threaded. |
| 73 | + sampleRunning += atomic.LoadInt32(&s.runningCount) |
| 74 | + // We only care if at least 1 job is sleeping. |
| 75 | + if atomic.LoadInt32(&s.sleepingCount) > 1 { |
| 76 | + sampleSleeping++ |
| 77 | + } |
| 78 | + |
| 79 | + // Every 100 iterations, update the estimated utilization under lock. |
| 80 | + if totalSamples == numSamples { |
| 81 | + // utilRunning may be bigger than 1, utilSleeping is always less than 1. |
| 82 | + utilRunning := float64(sampleRunning) / float64(totalSamples) |
| 83 | + utilSleeping := float64(sampleSleeping) / float64(totalSamples) |
| 84 | + |
| 85 | + s.mu.Lock() |
| 86 | + // The sleep time is multiplied by the estimated prior utilization |
| 87 | + // because sleep work should not change the utilization either up or |
| 88 | + // down. |
| 89 | + util := utilRunning + utilSleeping*s.mu.estimatedUtilization |
| 90 | + updatedEstimate := float64(s.mu.estimatedUtilization)*decayRate + float64(util)*(1-decayRate) |
| 91 | + // Prevent the utilization from getting too low. At 10% utilization |
| 92 | + // there should already be sufficient smoothing. If it gets lower the |
| 93 | + // sleeps can get too long, and it may take too long to recover. On |
| 94 | + // most systems it will stay above this. |
| 95 | + s.mu.estimatedUtilization = math.Max(updatedEstimate, minUtilization) |
| 96 | + s.mu.Unlock() |
| 97 | + |
| 98 | + sampleRunning = 0 |
| 99 | + sampleSleeping = 0 |
| 100 | + totalSamples = 0 |
| 101 | + } |
| 102 | + time.Sleep(sampleRate) |
| 103 | + } |
| 104 | + } |
| 105 | + }() |
| 106 | +} |
| 107 | + |
| 108 | +// stop is called to stop them main thread running. |
| 109 | +func (s *Smoother) stop() { |
| 110 | + close(s.stopper) |
| 111 | +} |
| 112 | + |
| 113 | +// startWork is called before work is started so the smoother can tell if there |
| 114 | +// is active work being done. |
| 115 | +func (s *Smoother) startWork() { |
| 116 | + atomic.AddInt32(&s.runningCount, 1) |
| 117 | +} |
| 118 | + |
| 119 | +// finishWork records that work has completed and returns the amount of time the |
| 120 | +// process should sleep after this work. Typically, shouldSleep should be set to |
| 121 | +// true, but in error cases or at the end of a larger iteration loop it can be |
| 122 | +// set to false. Setting to false means to use the measurements in calculations, |
| 123 | +// but don't actually sleep. |
| 124 | +func (s *Smoother) finishWork(workDuration time.Duration, shouldSleep bool) time.Duration { |
| 125 | + atomic.AddInt32(&s.runningCount, -1) |
| 126 | + s.mu.Lock() |
| 127 | + s.mu.estimatedIterDurationNs = float64(s.mu.estimatedIterDurationNs)*decayRate + float64(workDuration)*(1-decayRate) |
| 128 | + sleepTime := time.Duration(0) |
| 129 | + if s.mu.estimatedUtilization < 1 { |
| 130 | + sleepTime = time.Duration(s.mu.estimatedIterDurationNs * ((1 / s.mu.estimatedUtilization) - 1)) |
| 131 | + } |
| 132 | + s.mu.Unlock() |
| 133 | + fmt.Println(sleepTime, s.mu.estimatedIterDurationNs, s.mu.estimatedUtilization) |
| 134 | + |
| 135 | + // Duration to sleep if the caller wants to sleep after a call. |
| 136 | + // FIXME |
| 137 | + // if !s.enabled { |
| 138 | + // return time.Duration(0) |
| 139 | + // } |
| 140 | + if shouldSleep && sleepTime > 0 { |
| 141 | + atomic.AddInt32(&s.sleepingCount, 1) |
| 142 | + time.Sleep(sleepTime) |
| 143 | + atomic.AddInt32(&s.sleepingCount, -1) |
| 144 | + } |
| 145 | + // Returned for tests or logging. |
| 146 | + return sleepTime |
| 147 | +} |
0 commit comments