| 1 | package tq |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "sync" |
| 9 | "sync/atomic" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/git-lfs/git-lfs/v3/config" |
| 13 | "github.com/git-lfs/git-lfs/v3/tasklog" |
| 14 | "github.com/git-lfs/git-lfs/v3/tools" |
| 15 | "github.com/git-lfs/git-lfs/v3/tools/humanize" |
| 16 | "github.com/git-lfs/git-lfs/v3/tr" |
| 17 | ) |
| 18 | |
| 19 | // Meter provides a progress bar type output for the TransferQueue. It |
| 20 | // is given an estimated file count and size up front and tracks the number of |
| 21 | // files and bytes transferred as well as the number of files and bytes that |
| 22 | // get skipped because the transfer is unnecessary. |
| 23 | type Meter struct { |
| 24 | finishedFiles int64 // int64s must come first for struct alignment |
| 25 | transferringFiles int64 |
| 26 | estimatedBytes int64 |
| 27 | lastBytes int64 |
| 28 | currentBytes int64 |
| 29 | sampleCount uint64 |
| 30 | avgBytes float64 |
| 31 | lastAvg time.Time |
| 32 | estimatedFiles int32 |
| 33 | paused uint32 |
| 34 | fileIndex map[string]int64 // Maps a file name to its transfer number |
| 35 | fileIndexMutex *sync.Mutex |
| 36 | updates chan *tasklog.Update |
| 37 | cfg *config.Configuration |
| 38 | |
| 39 | DryRun bool |
| 40 | Logger *tools.SyncWriter |
| 41 | Direction Direction |
| 42 | } |
| 43 | |
| 44 | type env interface { |
| 45 | Get(key string) (val string, ok bool) |
| 46 | } |
| 47 | |
| 48 | func (m *Meter) LoggerFromEnv(os env) *tools.SyncWriter { |
| 49 | name, _ := os.Get("GIT_LFS_PROGRESS") |
| 50 | if len(name) < 1 { |
| 51 | return nil |
| 52 | } |
| 53 | return m.LoggerToFile(name) |
| 54 | } |
| 55 | |
| 56 | func (m *Meter) LoggerToFile(name string) *tools.SyncWriter { |
| 57 | printErr := func(err string) { |
| 58 | fmt.Fprintln(os.Stderr, tr.Tr.Get("Error creating progress logger: %s", err)) |
| 59 | } |
| 60 | |
| 61 | if !filepath.IsAbs(name) { |
| 62 | printErr(tr.Tr.Get("GIT_LFS_PROGRESS must be an absolute path")) |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | if err := tools.MkdirAll(filepath.Dir(name), m.cfg); err != nil { |
| 67 | printErr(err.Error()) |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | file, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) |
| 72 | if err != nil { |
| 73 | printErr(err.Error()) |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | return tools.NewSyncWriter(file) |
| 78 | } |
| 79 | |
| 80 | // NewMeter creates a new Meter. |
| 81 | func NewMeter(cfg *config.Configuration) *Meter { |
| 82 | m := &Meter{ |
| 83 | fileIndex: make(map[string]int64), |
| 84 | fileIndexMutex: &sync.Mutex{}, |
| 85 | updates: make(chan *tasklog.Update), |
| 86 | cfg: cfg, |
| 87 | } |
| 88 | |
| 89 | return m |
| 90 | } |
| 91 | |
| 92 | // Start begins sending status updates to the optional log file, and stdout. |
| 93 | func (m *Meter) Start() { |
| 94 | if m == nil { |
| 95 | return |
| 96 | } |
| 97 | atomic.StoreUint32(&m.paused, 0) |
| 98 | } |
| 99 | |
| 100 | // Pause stops sending status updates temporarily, until Start() is called again. |
| 101 | func (m *Meter) Pause() { |
| 102 | if m == nil { |
| 103 | return |
| 104 | } |
| 105 | atomic.StoreUint32(&m.paused, 1) |
| 106 | } |
| 107 | |
| 108 | // Add tells the progress meter that a single file of the given size will |
| 109 | // possibly be transferred. If a file doesn't need to be transferred for some |
| 110 | // reason, be sure to call Skip(int64) with the same size. |
| 111 | func (m *Meter) Add(size int64) { |
| 112 | if m == nil { |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | defer m.update(false) |
| 117 | atomic.AddInt32(&m.estimatedFiles, 1) |
| 118 | atomic.AddInt64(&m.estimatedBytes, size) |
| 119 | } |
| 120 | |
| 121 | // Skip tells the progress meter that a file of size `size` is being skipped |
| 122 | // because the transfer is unnecessary. |
| 123 | func (m *Meter) Skip(size int64) { |
| 124 | if m == nil { |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | defer m.update(false) |
| 129 | atomic.AddInt64(&m.finishedFiles, 1) |
| 130 | atomic.AddInt64(&m.currentBytes, size) |
| 131 | } |
| 132 | |
| 133 | // StartTransfer tells the progress meter that a transferring file is being |
| 134 | // added to the TransferQueue. |
| 135 | func (m *Meter) StartTransfer(name string) { |
| 136 | if m == nil { |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | defer m.update(false) |
| 141 | idx := atomic.AddInt64(&m.transferringFiles, 1) |
| 142 | m.fileIndexMutex.Lock() |
| 143 | m.fileIndex[name] = idx |
| 144 | m.fileIndexMutex.Unlock() |
| 145 | } |
| 146 | |
| 147 | // TransferBytes increments the number of bytes transferred |
| 148 | func (m *Meter) TransferBytes(direction, name string, read, total int64, current int) { |
| 149 | if m == nil { |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | defer m.update(false) |
| 154 | |
| 155 | now := time.Now() |
| 156 | since := now.Sub(m.lastAvg) |
| 157 | atomic.AddInt64(&m.currentBytes, int64(current)) |
| 158 | atomic.AddInt64(&m.lastBytes, int64(current)) |
| 159 | |
| 160 | if since > time.Second { |
| 161 | m.lastAvg = now |
| 162 | |
| 163 | bps := float64(m.lastBytes) / since.Seconds() |
| 164 | |
| 165 | m.avgBytes = (m.avgBytes*float64(m.sampleCount) + bps) / (float64(m.sampleCount) + 1.0) |
| 166 | |
| 167 | atomic.StoreInt64(&m.lastBytes, 0) |
| 168 | atomic.AddUint64(&m.sampleCount, 1) |
| 169 | } |
| 170 | |
| 171 | m.logBytes(direction, name, read, total) |
| 172 | } |
| 173 | |
| 174 | // FinishTransfer increments the finished transfer count |
| 175 | func (m *Meter) FinishTransfer(name string) { |
| 176 | if m == nil { |
| 177 | return |
| 178 | } |
| 179 | |
| 180 | defer m.update(false) |
| 181 | atomic.AddInt64(&m.finishedFiles, 1) |
| 182 | m.fileIndexMutex.Lock() |
| 183 | delete(m.fileIndex, name) |
| 184 | m.fileIndexMutex.Unlock() |
| 185 | } |
| 186 | |
| 187 | // Flush sends the latest progress update, while leaving the meter active. |
| 188 | func (m *Meter) Flush() { |
| 189 | if m == nil { |
| 190 | return |
| 191 | } |
| 192 | |
| 193 | m.update(true) |
| 194 | } |
| 195 | |
| 196 | // Finish shuts down the Meter. |
| 197 | func (m *Meter) Finish() { |
| 198 | if m == nil { |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | m.update(false) |
| 203 | close(m.updates) |
| 204 | } |
| 205 | |
| 206 | func (m *Meter) Updates() <-chan *tasklog.Update { |
| 207 | if m == nil { |
| 208 | return nil |
| 209 | } |
| 210 | return m.updates |
| 211 | } |
| 212 | |
| 213 | func (m *Meter) Throttled() bool { |
| 214 | return true |
| 215 | } |
| 216 | |
| 217 | func (m *Meter) update(force bool) { |
| 218 | if m.skipUpdate() { |
| 219 | return |
| 220 | } |
| 221 | |
| 222 | m.updates <- &tasklog.Update{ |
| 223 | S: m.str(), |
| 224 | At: time.Now(), |
| 225 | Force: force, |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func (m *Meter) skipUpdate() bool { |
| 230 | return m.DryRun || |
| 231 | m.estimatedFiles == 0 || |
| 232 | atomic.LoadUint32(&m.paused) == 1 |
| 233 | } |
| 234 | |
| 235 | func (m *Meter) str() string { |
| 236 | // (Uploading|Downloading) LFS objects: 100% (10/10) 100 MiB | 10 MiB/s |
| 237 | percentage := 100 * float64(m.finishedFiles) / float64(m.estimatedFiles) |
| 238 | |
| 239 | return fmt.Sprintf("%s: %3.f%% (%d/%d), %s | %s", |
| 240 | m.Direction.Progress(), |
| 241 | percentage, |
| 242 | m.finishedFiles, m.estimatedFiles, |
| 243 | humanize.FormatBytes(clamp(m.currentBytes)), |
| 244 | humanize.FormatByteRate(clampf(m.avgBytes), time.Second)) |
| 245 | } |
| 246 | |
| 247 | // clamp clamps the given "x" within the acceptable domain of the uint64 integer |
| 248 | // type, so as to prevent over- and underflow. |
| 249 | func clamp(x int64) uint64 { |
| 250 | if x < 0 { |
| 251 | return 0 |
| 252 | } |
| 253 | if x > math.MaxInt64 { |
| 254 | return math.MaxUint64 |
| 255 | } |
| 256 | return uint64(x) |
| 257 | } |
| 258 | |
| 259 | func clampf(x float64) uint64 { |
| 260 | if x < 0 { |
| 261 | return 0 |
| 262 | } |
| 263 | if x > math.MaxUint64 { |
| 264 | return math.MaxUint64 |
| 265 | } |
| 266 | return uint64(x) |
| 267 | } |
| 268 | |
| 269 | func (m *Meter) logBytes(direction, name string, read, total int64) { |
| 270 | m.fileIndexMutex.Lock() |
| 271 | idx := m.fileIndex[name] |
| 272 | logger := m.Logger |
| 273 | m.fileIndexMutex.Unlock() |
| 274 | if logger == nil { |
| 275 | return |
| 276 | } |
| 277 | |
| 278 | readFraction := tools.FormatFraction(read, total) |
| 279 | line := fmt.Sprintf("%s %d/%d %s %s\n", direction, idx, m.estimatedFiles, readFraction, name) |
| 280 | if err := m.Logger.Write([]byte(line)); err != nil { |
| 281 | m.fileIndexMutex.Lock() |
| 282 | m.Logger = nil |
| 283 | m.fileIndexMutex.Unlock() |
| 284 | } |
| 285 | } |