2018-02-01 14:13:24 +01:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-11-05 17:59:59 +01:00
|
|
|
"context"
|
2023-03-08 19:40:37 +01:00
|
|
|
"errors"
|
2018-02-01 14:13:24 +01:00
|
|
|
"fmt"
|
2019-07-16 13:56:20 +02:00
|
|
|
"sort"
|
2018-02-01 14:13:24 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2020-10-06 17:34:26 +02:00
|
|
|
"github.com/rclone/rclone/lib/terminal"
|
2018-02-01 14:13:24 +01:00
|
|
|
)
|
|
|
|
|
2021-06-13 18:10:15 +02:00
|
|
|
const (
|
|
|
|
averagePeriodLength = time.Second
|
|
|
|
averageStopAfter = time.Minute
|
|
|
|
)
|
|
|
|
|
2019-11-14 14:14:40 +01:00
|
|
|
// MaxCompletedTransfers specifies maximum number of completed transfers in startedTransfers list
|
|
|
|
var MaxCompletedTransfers = 100
|
2019-10-16 21:11:11 +02:00
|
|
|
|
2018-02-01 14:13:24 +01:00
|
|
|
// StatsInfo accounts all transfers
|
2021-11-04 09:24:12 +01:00
|
|
|
// N.B.: if this struct is modified, please remember to also update sum() function in stats_groups
|
|
|
|
// to correctly count the updated fields
|
2018-02-01 14:13:24 +01:00
|
|
|
type StatsInfo struct {
|
2023-07-30 06:39:01 +02:00
|
|
|
mu sync.RWMutex
|
|
|
|
ctx context.Context
|
|
|
|
ci *fs.ConfigInfo
|
|
|
|
bytes int64
|
|
|
|
errors int64
|
|
|
|
lastError error
|
|
|
|
fatalError bool
|
|
|
|
retryError bool
|
|
|
|
retryAfter time.Time
|
|
|
|
checks int64
|
|
|
|
checking *transferMap
|
|
|
|
checkQueue int
|
|
|
|
checkQueueSize int64
|
|
|
|
transfers int64
|
|
|
|
transferring *transferMap
|
|
|
|
transferQueue int
|
|
|
|
transferQueueSize int64
|
|
|
|
renames int64
|
|
|
|
renameQueue int
|
|
|
|
renameQueueSize int64
|
|
|
|
deletes int64
|
|
|
|
deletesSize int64
|
|
|
|
deletedDirs int64
|
|
|
|
inProgress *inProgress
|
|
|
|
startedTransfers []*Transfer // currently active transfers
|
|
|
|
oldTimeRanges timeRanges // a merged list of time ranges for the transfers
|
|
|
|
oldDuration time.Duration // duration of transfers we have culled
|
|
|
|
group string
|
|
|
|
startTime time.Time // the moment these stats were initialized or reset
|
|
|
|
average averageValues
|
|
|
|
serverSideCopies int64
|
|
|
|
serverSideCopyBytes int64
|
|
|
|
serverSideMoves int64
|
|
|
|
serverSideMoveBytes int64
|
2021-06-13 18:10:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type averageValues struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
lpBytes int64
|
|
|
|
lpTime time.Time
|
|
|
|
speed float64
|
|
|
|
stop chan bool
|
|
|
|
stopped sync.WaitGroup
|
|
|
|
startOnce sync.Once
|
|
|
|
stopOnce sync.Once
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:06:24 +02:00
|
|
|
// NewStats creates an initialised StatsInfo
|
2020-11-05 17:59:59 +01:00
|
|
|
func NewStats(ctx context.Context) *StatsInfo {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci := fs.GetConfig(ctx)
|
2018-02-01 14:13:24 +01:00
|
|
|
return &StatsInfo{
|
2020-11-05 17:59:59 +01:00
|
|
|
ctx: ctx,
|
2020-11-05 12:33:32 +01:00
|
|
|
ci: ci,
|
|
|
|
checking: newTransferMap(ci.Checkers, "checking"),
|
|
|
|
transferring: newTransferMap(ci.Transfers, "transferring"),
|
2020-11-05 17:59:59 +01:00
|
|
|
inProgress: newInProgress(ctx),
|
2021-04-28 19:40:48 +02:00
|
|
|
startTime: time.Now(),
|
2021-06-13 18:10:15 +02:00
|
|
|
average: averageValues{stop: make(chan bool)},
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 21:56:40 +02:00
|
|
|
// RemoteStats returns stats for rc
|
2019-07-18 12:13:54 +02:00
|
|
|
func (s *StatsInfo) RemoteStats() (out rc.Params, err error) {
|
2021-03-15 16:50:04 +01:00
|
|
|
// NB if adding values here - make sure you update the docs in
|
|
|
|
// stats_groups.go
|
|
|
|
|
2018-08-07 21:56:40 +02:00
|
|
|
out = make(rc.Params)
|
2021-03-15 16:50:04 +01:00
|
|
|
|
|
|
|
ts := s.calculateTransferStats()
|
|
|
|
out["totalChecks"] = ts.totalChecks
|
|
|
|
out["totalTransfers"] = ts.totalTransfers
|
|
|
|
out["totalBytes"] = ts.totalBytes
|
|
|
|
out["transferTime"] = ts.transferTime
|
|
|
|
out["speed"] = ts.speed
|
|
|
|
|
2018-08-07 21:56:40 +02:00
|
|
|
s.mu.RLock()
|
|
|
|
out["bytes"] = s.bytes
|
|
|
|
out["errors"] = s.errors
|
2018-08-30 18:26:07 +02:00
|
|
|
out["fatalError"] = s.fatalError
|
|
|
|
out["retryError"] = s.retryError
|
2018-08-07 21:56:40 +02:00
|
|
|
out["checks"] = s.checks
|
|
|
|
out["transfers"] = s.transfers
|
|
|
|
out["deletes"] = s.deletes
|
2020-10-28 13:54:31 +01:00
|
|
|
out["deletedDirs"] = s.deletedDirs
|
2020-03-30 19:12:32 +02:00
|
|
|
out["renames"] = s.renames
|
2021-04-28 19:40:48 +02:00
|
|
|
out["elapsedTime"] = time.Since(s.startTime).Seconds()
|
2023-07-30 06:39:01 +02:00
|
|
|
out["serverSideCopies"] = s.serverSideCopies
|
|
|
|
out["serverSideCopyBytes"] = s.serverSideCopyBytes
|
|
|
|
out["serverSideMoves"] = s.serverSideMoves
|
|
|
|
out["serverSideMoveBytes"] = s.serverSideMoveBytes
|
2021-03-15 16:50:04 +01:00
|
|
|
eta, etaOK := eta(s.bytes, ts.totalBytes, ts.speed)
|
|
|
|
if etaOK {
|
|
|
|
out["eta"] = eta.Seconds()
|
|
|
|
} else {
|
|
|
|
out["eta"] = nil
|
|
|
|
}
|
2018-08-07 21:56:40 +02:00
|
|
|
s.mu.RUnlock()
|
2021-03-15 16:50:04 +01:00
|
|
|
|
2018-08-07 21:56:40 +02:00
|
|
|
if !s.checking.empty() {
|
2020-08-05 17:59:44 +02:00
|
|
|
out["checking"] = s.checking.remotes()
|
2018-08-07 21:56:40 +02:00
|
|
|
}
|
|
|
|
if !s.transferring.empty() {
|
2020-08-05 17:59:44 +02:00
|
|
|
out["transferring"] = s.transferring.rcStats(s.inProgress)
|
2018-08-07 21:56:40 +02:00
|
|
|
}
|
|
|
|
if s.errors > 0 {
|
2019-07-26 09:51:51 +02:00
|
|
|
out["lastError"] = s.lastError.Error()
|
2018-08-07 21:56:40 +02:00
|
|
|
}
|
2021-03-15 16:50:04 +01:00
|
|
|
|
2018-08-07 21:56:40 +02:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:30:24 +01:00
|
|
|
// _speed returns the average speed of the transfer in bytes/second
|
2021-03-15 16:50:04 +01:00
|
|
|
//
|
|
|
|
// Call with lock held
|
2024-01-16 18:30:24 +01:00
|
|
|
func (s *StatsInfo) _speed() float64 {
|
2023-06-12 18:42:29 +02:00
|
|
|
return s.average.speed
|
2020-02-26 09:34:32 +01:00
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:26 +02:00
|
|
|
// timeRange is a start and end time of a transfer
|
2019-07-16 13:56:20 +02:00
|
|
|
type timeRange struct {
|
|
|
|
start time.Time
|
|
|
|
end time.Time
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:26 +02:00
|
|
|
// timeRanges is a list of non-overlapping start and end times for
|
|
|
|
// transfers
|
|
|
|
type timeRanges []timeRange
|
|
|
|
|
|
|
|
// merge all the overlapping time ranges
|
|
|
|
func (trs *timeRanges) merge() {
|
|
|
|
Trs := *trs
|
2019-07-16 13:56:20 +02:00
|
|
|
|
|
|
|
// Sort by the starting time.
|
2019-10-16 17:05:26 +02:00
|
|
|
sort.Slice(Trs, func(i, j int) bool {
|
|
|
|
return Trs[i].start.Before(Trs[j].start)
|
2019-07-16 13:56:20 +02:00
|
|
|
})
|
|
|
|
|
2019-10-16 17:05:26 +02:00
|
|
|
// Merge overlaps and add distinctive ranges together
|
|
|
|
var (
|
|
|
|
newTrs = Trs[:0]
|
|
|
|
i, j = 0, 1
|
|
|
|
)
|
|
|
|
for i < len(Trs) {
|
|
|
|
if j < len(Trs) {
|
|
|
|
if !Trs[i].end.Before(Trs[j].start) {
|
|
|
|
if Trs[i].end.Before(Trs[j].end) {
|
|
|
|
Trs[i].end = Trs[j].end
|
2019-07-16 13:56:20 +02:00
|
|
|
}
|
|
|
|
j++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 17:05:26 +02:00
|
|
|
newTrs = append(newTrs, Trs[i])
|
2019-07-16 13:56:20 +02:00
|
|
|
i = j
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:26 +02:00
|
|
|
*trs = newTrs
|
|
|
|
}
|
|
|
|
|
2019-10-17 12:43:32 +02:00
|
|
|
// cull remove any ranges whose start and end are before cutoff
|
|
|
|
// returning their duration sum
|
|
|
|
func (trs *timeRanges) cull(cutoff time.Time) (d time.Duration) {
|
|
|
|
var newTrs = (*trs)[:0]
|
|
|
|
for _, tr := range *trs {
|
|
|
|
if cutoff.Before(tr.start) || cutoff.Before(tr.end) {
|
|
|
|
newTrs = append(newTrs, tr)
|
|
|
|
} else {
|
|
|
|
d += tr.end.Sub(tr.start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*trs = newTrs
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:26 +02:00
|
|
|
// total the time out of the time ranges
|
|
|
|
func (trs timeRanges) total() (total time.Duration) {
|
|
|
|
for _, tr := range trs {
|
|
|
|
total += tr.end.Sub(tr.start)
|
|
|
|
}
|
2019-07-16 13:56:20 +02:00
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
2019-10-16 17:05:26 +02:00
|
|
|
// Total duration is union of durations of all transfers belonging to this
|
|
|
|
// object.
|
2024-01-16 18:30:24 +01:00
|
|
|
//
|
2019-10-16 17:05:26 +02:00
|
|
|
// Needs to be protected by mutex.
|
2024-01-16 18:30:24 +01:00
|
|
|
func (s *StatsInfo) _totalDuration() time.Duration {
|
2019-10-16 17:05:26 +02:00
|
|
|
// copy of s.oldTimeRanges with extra room for the current transfers
|
|
|
|
timeRanges := make(timeRanges, len(s.oldTimeRanges), len(s.oldTimeRanges)+len(s.startedTransfers))
|
|
|
|
copy(timeRanges, s.oldTimeRanges)
|
|
|
|
|
|
|
|
// Extract time ranges of all transfers.
|
|
|
|
now := time.Now()
|
|
|
|
for i := range s.startedTransfers {
|
|
|
|
start, end := s.startedTransfers[i].TimeRange()
|
|
|
|
if end.IsZero() {
|
|
|
|
end = now
|
|
|
|
}
|
|
|
|
timeRanges = append(timeRanges, timeRange{start, end})
|
|
|
|
}
|
|
|
|
|
|
|
|
timeRanges.merge()
|
2019-10-17 12:43:32 +02:00
|
|
|
return s.oldDuration + timeRanges.total()
|
2019-10-16 17:05:26 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 10:21:00 +02:00
|
|
|
const (
|
|
|
|
etaMaxSeconds = (1<<63 - 1) / int64(time.Second) // Largest possible ETA as number of seconds
|
|
|
|
etaMax = time.Duration(etaMaxSeconds) * time.Second // Largest possible ETA, which is in second precision, representing "292y24w3d23h47m16s"
|
|
|
|
)
|
|
|
|
|
2018-08-28 12:17:05 +02:00
|
|
|
// eta returns the ETA of the current operation,
|
|
|
|
// rounded to full seconds.
|
|
|
|
// If the ETA cannot be determined 'ok' returns false.
|
|
|
|
func eta(size, total int64, rate float64) (eta time.Duration, ok bool) {
|
|
|
|
if total <= 0 || size < 0 || rate <= 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
remaining := total - size
|
|
|
|
if remaining < 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2022-08-19 10:21:00 +02:00
|
|
|
seconds := int64(float64(remaining) / rate)
|
2021-03-15 16:50:04 +01:00
|
|
|
if seconds < 0 {
|
2022-08-19 10:21:00 +02:00
|
|
|
// Got Int64 overflow
|
|
|
|
eta = etaMax
|
|
|
|
} else if seconds >= etaMaxSeconds {
|
|
|
|
// Would get Int64 overflow if converting from seconds to Duration (nanoseconds)
|
|
|
|
eta = etaMax
|
|
|
|
} else {
|
|
|
|
eta = time.Duration(seconds) * time.Second
|
2021-03-15 16:50:04 +01:00
|
|
|
}
|
2022-08-19 10:21:00 +02:00
|
|
|
return eta, true
|
2018-08-28 12:17:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// etaString returns the ETA of the current operation,
|
|
|
|
// rounded to full seconds.
|
|
|
|
// If the ETA cannot be determined it returns "-"
|
|
|
|
func etaString(done, total int64, rate float64) string {
|
|
|
|
d, ok := eta(done, total, rate)
|
|
|
|
if !ok {
|
|
|
|
return "-"
|
|
|
|
}
|
2022-08-19 10:21:00 +02:00
|
|
|
if d == etaMax {
|
|
|
|
return "-"
|
|
|
|
}
|
2022-08-19 11:40:43 +02:00
|
|
|
return fs.Duration(d).ShortReadableString()
|
2018-08-28 12:17:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// percent returns a/b as a percentage rounded to the nearest integer
|
|
|
|
// as a string
|
|
|
|
//
|
|
|
|
// if the percentage is invalid it returns "-"
|
|
|
|
func percent(a int64, b int64) string {
|
|
|
|
if a < 0 || b <= 0 {
|
|
|
|
return "-"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d%%", int(float64(a)*100/float64(b)+0.5))
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:50:04 +01:00
|
|
|
// returned from calculateTransferStats
|
|
|
|
type transferStats struct {
|
|
|
|
totalChecks int64
|
|
|
|
totalTransfers int64
|
|
|
|
totalBytes int64
|
|
|
|
transferTime float64
|
|
|
|
speed float64
|
|
|
|
}
|
|
|
|
|
2022-08-14 04:56:32 +02:00
|
|
|
// calculateTransferStats calculates some additional transfer stats not
|
2021-03-15 16:50:04 +01:00
|
|
|
// stored directly in StatsInfo
|
|
|
|
func (s *StatsInfo) calculateTransferStats() (ts transferStats) {
|
2018-08-28 12:17:05 +02:00
|
|
|
// checking and transferring have their own locking so read
|
|
|
|
// here before lock to prevent deadlock on GetBytes
|
|
|
|
transferring, checking := s.transferring.count(), s.checking.count()
|
2019-07-18 12:13:54 +02:00
|
|
|
transferringBytesDone, transferringBytesTotal := s.transferring.progress(s)
|
2018-08-28 12:17:05 +02:00
|
|
|
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.RLock()
|
2021-03-15 16:50:04 +01:00
|
|
|
defer s.mu.RUnlock()
|
|
|
|
|
|
|
|
ts.totalChecks = int64(s.checkQueue) + s.checks + int64(checking)
|
|
|
|
ts.totalTransfers = int64(s.transferQueue) + s.transfers + int64(transferring)
|
|
|
|
// note that s.bytes already includes transferringBytesDone so
|
|
|
|
// we take it off here to avoid double counting
|
|
|
|
ts.totalBytes = s.transferQueueSize + s.bytes + transferringBytesTotal - transferringBytesDone
|
2024-06-18 13:12:26 +02:00
|
|
|
s.average.mu.Lock()
|
2021-06-13 18:10:15 +02:00
|
|
|
ts.speed = s.average.speed
|
2024-06-18 13:12:26 +02:00
|
|
|
s.average.mu.Unlock()
|
2024-01-16 18:30:24 +01:00
|
|
|
dt := s._totalDuration()
|
2023-08-16 23:12:35 +02:00
|
|
|
ts.transferTime = dt.Seconds()
|
2018-05-02 18:01:39 +02:00
|
|
|
|
2021-06-13 18:10:15 +02:00
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatsInfo) averageLoop() {
|
|
|
|
var period float64
|
|
|
|
|
|
|
|
ticker := time.NewTicker(averagePeriodLength)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
a := &s.average
|
|
|
|
defer a.stopped.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case now := <-ticker.C:
|
|
|
|
a.mu.Lock()
|
|
|
|
var elapsed float64
|
|
|
|
if a.lpTime.IsZero() {
|
|
|
|
elapsed = now.Sub(startTime).Seconds()
|
|
|
|
} else {
|
|
|
|
elapsed = now.Sub(a.lpTime).Seconds()
|
|
|
|
}
|
|
|
|
avg := 0.0
|
|
|
|
if elapsed > 0 {
|
|
|
|
avg = float64(a.lpBytes) / elapsed
|
|
|
|
}
|
|
|
|
if period < averagePeriod {
|
|
|
|
period++
|
|
|
|
}
|
|
|
|
a.speed = (avg + a.speed*(period-1)) / period
|
|
|
|
a.lpBytes = 0
|
|
|
|
a.lpTime = now
|
|
|
|
a.mu.Unlock()
|
|
|
|
case <-a.stop:
|
|
|
|
return
|
|
|
|
}
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
2021-06-13 18:10:15 +02:00
|
|
|
}
|
2018-02-01 14:13:24 +01:00
|
|
|
|
2024-01-16 18:30:24 +01:00
|
|
|
// Start the average loop
|
2021-06-13 18:10:15 +02:00
|
|
|
func (s *StatsInfo) startAverageLoop() {
|
2024-01-16 18:30:24 +01:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2021-06-13 18:10:15 +02:00
|
|
|
s.average.startOnce.Do(func() {
|
|
|
|
s.average.stopped.Add(1)
|
|
|
|
go s.averageLoop()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:30:24 +01:00
|
|
|
// Stop the average loop
|
|
|
|
//
|
|
|
|
// Call with the mutex held
|
|
|
|
func (s *StatsInfo) _stopAverageLoop() {
|
2021-06-13 18:10:15 +02:00
|
|
|
s.average.stopOnce.Do(func() {
|
|
|
|
close(s.average.stop)
|
|
|
|
s.average.stopped.Wait()
|
|
|
|
})
|
2021-03-15 16:50:04 +01:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:30:24 +01:00
|
|
|
// Stop the average loop
|
|
|
|
func (s *StatsInfo) stopAverageLoop() {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
s._stopAverageLoop()
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:50:04 +01:00
|
|
|
// String convert the StatsInfo to a string for printing
|
|
|
|
func (s *StatsInfo) String() string {
|
|
|
|
// NB if adding more stats in here, remember to add them into
|
|
|
|
// RemoteStats() too.
|
|
|
|
|
|
|
|
ts := s.calculateTransferStats()
|
|
|
|
|
|
|
|
s.mu.RLock()
|
|
|
|
|
2021-04-07 12:26:29 +02:00
|
|
|
var (
|
|
|
|
buf = &bytes.Buffer{}
|
|
|
|
xfrchkString = ""
|
|
|
|
dateString = ""
|
2021-04-28 19:40:48 +02:00
|
|
|
elapsedTime = time.Since(s.startTime)
|
2021-04-07 12:26:29 +02:00
|
|
|
elapsedTimeSecondsOnly = elapsedTime.Truncate(time.Second/10) % time.Minute
|
2021-03-02 20:11:57 +01:00
|
|
|
displaySpeedString string
|
2021-04-07 12:26:29 +02:00
|
|
|
)
|
2021-03-15 16:50:04 +01:00
|
|
|
|
2020-11-05 12:33:32 +01:00
|
|
|
if s.ci.DataRateUnit == "bits" {
|
2021-03-02 20:11:57 +01:00
|
|
|
displaySpeedString = fs.SizeSuffix(ts.speed * 8).BitRateUnit()
|
2021-04-07 12:26:29 +02:00
|
|
|
} else {
|
2021-03-02 20:11:57 +01:00
|
|
|
displaySpeedString = fs.SizeSuffix(ts.speed).ByteRateUnit()
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:33:32 +01:00
|
|
|
if !s.ci.StatsOneLine {
|
2018-08-16 17:32:35 +02:00
|
|
|
_, _ = fmt.Fprintf(buf, "\nTransferred: ")
|
2018-08-28 12:17:05 +02:00
|
|
|
} else {
|
2018-08-16 17:32:35 +02:00
|
|
|
xfrchk := []string{}
|
2021-03-15 16:50:04 +01:00
|
|
|
if ts.totalTransfers > 0 && s.transferQueue > 0 {
|
|
|
|
xfrchk = append(xfrchk, fmt.Sprintf("xfr#%d/%d", s.transfers, ts.totalTransfers))
|
2018-08-16 17:32:35 +02:00
|
|
|
}
|
2021-03-15 16:50:04 +01:00
|
|
|
if ts.totalChecks > 0 && s.checkQueue > 0 {
|
|
|
|
xfrchk = append(xfrchk, fmt.Sprintf("chk#%d/%d", s.checks, ts.totalChecks))
|
2018-08-16 17:32:35 +02:00
|
|
|
}
|
|
|
|
if len(xfrchk) > 0 {
|
|
|
|
xfrchkString = fmt.Sprintf(" (%s)", strings.Join(xfrchk, ", "))
|
|
|
|
}
|
2020-11-05 12:33:32 +01:00
|
|
|
if s.ci.StatsOneLineDate {
|
2019-03-26 03:41:45 +01:00
|
|
|
t := time.Now()
|
2020-11-05 12:33:32 +01:00
|
|
|
dateString = t.Format(s.ci.StatsOneLineDateFormat) // Including the separator so people can customize it
|
2019-03-26 03:41:45 +01:00
|
|
|
}
|
2018-07-19 23:43:53 +02:00
|
|
|
}
|
2018-08-28 12:17:05 +02:00
|
|
|
|
2021-08-21 19:54:21 +02:00
|
|
|
_, _ = fmt.Fprintf(buf, "%s%13s / %s, %s, %s, ETA %s%s",
|
2019-03-26 03:41:45 +01:00
|
|
|
dateString,
|
2021-08-21 19:54:21 +02:00
|
|
|
fs.SizeSuffix(s.bytes).ByteUnit(),
|
2021-03-02 20:11:57 +01:00
|
|
|
fs.SizeSuffix(ts.totalBytes).ByteUnit(),
|
2021-03-15 16:50:04 +01:00
|
|
|
percent(s.bytes, ts.totalBytes),
|
2021-03-02 20:11:57 +01:00
|
|
|
displaySpeedString,
|
2021-03-15 16:50:04 +01:00
|
|
|
etaString(s.bytes, ts.totalBytes, ts.speed),
|
2018-08-28 12:17:05 +02:00
|
|
|
xfrchkString,
|
|
|
|
)
|
|
|
|
|
2020-11-05 12:33:32 +01:00
|
|
|
if s.ci.ProgressTerminalTitle {
|
2020-10-06 17:34:26 +02:00
|
|
|
// Writes ETA to the terminal title
|
2021-03-15 16:50:04 +01:00
|
|
|
terminal.WriteTerminalTitle("ETA: " + etaString(s.bytes, ts.totalBytes, ts.speed))
|
2020-10-06 17:34:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:33:32 +01:00
|
|
|
if !s.ci.StatsOneLine {
|
2020-09-08 15:27:42 +02:00
|
|
|
_, _ = buf.WriteRune('\n')
|
2018-08-30 18:26:07 +02:00
|
|
|
errorDetails := ""
|
|
|
|
switch {
|
|
|
|
case s.fatalError:
|
|
|
|
errorDetails = " (fatal error encountered)"
|
|
|
|
case s.retryError:
|
|
|
|
errorDetails = " (retrying may help)"
|
|
|
|
case s.errors != 0:
|
|
|
|
errorDetails = " (no need to retry)"
|
2020-09-08 15:27:42 +02:00
|
|
|
|
2018-08-30 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:15:47 +01:00
|
|
|
// Add only non zero stats
|
|
|
|
if s.errors != 0 {
|
|
|
|
_, _ = fmt.Fprintf(buf, "Errors: %10d%s\n",
|
|
|
|
s.errors, errorDetails)
|
|
|
|
}
|
2021-03-15 16:50:04 +01:00
|
|
|
if s.checks != 0 || ts.totalChecks != 0 {
|
2019-11-21 12:15:47 +01:00
|
|
|
_, _ = fmt.Fprintf(buf, "Checks: %10d / %d, %s\n",
|
2021-03-15 16:50:04 +01:00
|
|
|
s.checks, ts.totalChecks, percent(s.checks, ts.totalChecks))
|
2019-11-21 12:15:47 +01:00
|
|
|
}
|
2020-10-28 13:54:31 +01:00
|
|
|
if s.deletes != 0 || s.deletedDirs != 0 {
|
2024-04-16 19:13:38 +02:00
|
|
|
_, _ = fmt.Fprintf(buf, "Deleted: %10d (files), %d (dirs), %s (freed)\n", s.deletes, s.deletedDirs, fs.SizeSuffix(s.deletesSize).ByteUnit())
|
2019-11-21 12:15:47 +01:00
|
|
|
}
|
2020-03-30 19:12:32 +02:00
|
|
|
if s.renames != 0 {
|
|
|
|
_, _ = fmt.Fprintf(buf, "Renamed: %10d\n", s.renames)
|
|
|
|
}
|
2021-03-15 16:50:04 +01:00
|
|
|
if s.transfers != 0 || ts.totalTransfers != 0 {
|
2019-11-21 12:15:47 +01:00
|
|
|
_, _ = fmt.Fprintf(buf, "Transferred: %10d / %d, %s\n",
|
2021-03-15 16:50:04 +01:00
|
|
|
s.transfers, ts.totalTransfers, percent(s.transfers, ts.totalTransfers))
|
2019-11-21 12:15:47 +01:00
|
|
|
}
|
2023-07-30 06:39:01 +02:00
|
|
|
if s.serverSideCopies != 0 || s.serverSideCopyBytes != 0 {
|
|
|
|
_, _ = fmt.Fprintf(buf, "Server Side Copies:%6d @ %s\n",
|
|
|
|
s.serverSideCopies, fs.SizeSuffix(s.serverSideCopyBytes).ByteUnit(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if s.serverSideMoves != 0 || s.serverSideMoveBytes != 0 {
|
|
|
|
_, _ = fmt.Fprintf(buf, "Server Side Moves:%7d @ %s\n",
|
|
|
|
s.serverSideMoves, fs.SizeSuffix(s.serverSideMoveBytes).ByteUnit(),
|
|
|
|
)
|
|
|
|
}
|
2023-02-06 12:52:22 +01:00
|
|
|
_, _ = fmt.Fprintf(buf, "Elapsed time: %10ss\n", strings.TrimRight(fs.Duration(elapsedTime.Truncate(time.Minute)).ReadableString(), "0s")+fmt.Sprintf("%.1f", elapsedTimeSecondsOnly.Seconds()))
|
2018-08-16 17:32:35 +02:00
|
|
|
}
|
2018-05-02 18:01:39 +02:00
|
|
|
|
|
|
|
// checking and transferring have their own locking so unlock
|
|
|
|
// here to prevent deadlock on GetBytes
|
|
|
|
s.mu.RUnlock()
|
|
|
|
|
2018-08-28 12:17:05 +02:00
|
|
|
// Add per transfer stats if required
|
2020-11-05 12:33:32 +01:00
|
|
|
if !s.ci.StatsOneLine {
|
2018-08-16 17:32:35 +02:00
|
|
|
if !s.checking.empty() {
|
2020-11-05 17:59:59 +01:00
|
|
|
_, _ = fmt.Fprintf(buf, "Checking:\n%s\n", s.checking.String(s.ctx, s.inProgress, s.transferring))
|
2018-08-16 17:32:35 +02:00
|
|
|
}
|
|
|
|
if !s.transferring.empty() {
|
2020-11-05 17:59:59 +01:00
|
|
|
_, _ = fmt.Fprintf(buf, "Transferring:\n%s\n", s.transferring.String(s.ctx, s.inProgress, nil))
|
2018-08-16 17:32:35 +02:00
|
|
|
}
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
2018-08-28 12:17:05 +02:00
|
|
|
|
2018-02-01 14:13:24 +01:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:11:46 +02:00
|
|
|
// Transferred returns list of all completed transfers including checked and
|
|
|
|
// failed ones.
|
|
|
|
func (s *StatsInfo) Transferred() []TransferSnapshot {
|
2020-11-26 23:54:38 +01:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2019-07-22 21:11:46 +02:00
|
|
|
ts := make([]TransferSnapshot, 0, len(s.startedTransfers))
|
|
|
|
|
|
|
|
for _, tr := range s.startedTransfers {
|
|
|
|
if tr.IsDone() {
|
|
|
|
ts = append(ts, tr.Snapshot())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2018-02-01 14:13:24 +01:00
|
|
|
// Log outputs the StatsInfo to the log
|
|
|
|
func (s *StatsInfo) Log() {
|
2020-11-05 12:33:32 +01:00
|
|
|
if s.ci.UseJSONLog {
|
2020-02-15 22:21:01 +01:00
|
|
|
out, _ := s.RemoteStats()
|
2020-12-30 14:06:43 +01:00
|
|
|
fs.LogLevelPrintf(s.ci.StatsLogLevel, nil, "%v%v\n", s, fs.LogValueHide("stats", out))
|
2020-02-15 22:21:01 +01:00
|
|
|
} else {
|
2020-11-05 12:33:32 +01:00
|
|
|
fs.LogLevelPrintf(s.ci.StatsLogLevel, nil, "%v\n", s)
|
2020-02-15 22:21:01 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes updates the stats for bytes bytes
|
|
|
|
func (s *StatsInfo) Bytes(bytes int64) {
|
2021-06-13 18:10:15 +02:00
|
|
|
s.average.mu.Lock()
|
|
|
|
s.average.lpBytes += bytes
|
|
|
|
s.average.mu.Unlock()
|
|
|
|
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
s.bytes += bytes
|
|
|
|
}
|
|
|
|
|
2024-01-04 12:39:51 +01:00
|
|
|
// BytesNoNetwork updates the stats for bytes bytes but doesn't include the transfer stats
|
|
|
|
func (s *StatsInfo) BytesNoNetwork(bytes int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.bytes += bytes
|
|
|
|
}
|
|
|
|
|
2018-04-21 23:03:27 +02:00
|
|
|
// GetBytes returns the number of bytes transferred so far
|
|
|
|
func (s *StatsInfo) GetBytes() int64 {
|
2018-07-22 11:33:19 +02:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2018-04-21 23:03:27 +02:00
|
|
|
return s.bytes
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:23:17 +01:00
|
|
|
// GetBytesWithPending returns the number of bytes transferred and remaining transfers
|
|
|
|
func (s *StatsInfo) GetBytesWithPending() int64 {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
pending := int64(0)
|
|
|
|
for _, tr := range s.startedTransfers {
|
|
|
|
if tr.acc != nil {
|
|
|
|
bytes, size := tr.acc.progress()
|
|
|
|
if bytes < size {
|
|
|
|
pending += size - bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.bytes + pending
|
|
|
|
}
|
|
|
|
|
2018-02-01 14:13:24 +01:00
|
|
|
// Errors updates the stats for errors
|
|
|
|
func (s *StatsInfo) Errors(errors int64) {
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
s.errors += errors
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetErrors reads the number of errors
|
|
|
|
func (s *StatsInfo) GetErrors() int64 {
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
return s.errors
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLastError returns the lastError
|
|
|
|
func (s *StatsInfo) GetLastError() error {
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
return s.lastError
|
|
|
|
}
|
|
|
|
|
2018-12-31 12:58:55 +01:00
|
|
|
// GetChecks returns the number of checks
|
|
|
|
func (s *StatsInfo) GetChecks() int64 {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.checks
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:07 +02:00
|
|
|
// FatalError sets the fatalError flag
|
|
|
|
func (s *StatsInfo) FatalError() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.fatalError = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// HadFatalError returns whether there has been at least one FatalError
|
|
|
|
func (s *StatsInfo) HadFatalError() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.fatalError
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetryError sets the retryError flag
|
|
|
|
func (s *StatsInfo) RetryError() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.retryError = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// HadRetryError returns whether there has been at least one non-NoRetryError
|
|
|
|
func (s *StatsInfo) HadRetryError() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
return s.retryError
|
|
|
|
}
|
|
|
|
|
2023-03-08 19:40:37 +01:00
|
|
|
var (
|
|
|
|
errMaxDelete = fserrors.FatalError(errors.New("--max-delete threshold reached"))
|
|
|
|
errMaxDeleteSize = fserrors.FatalError(errors.New("--max-delete-size threshold reached"))
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteFile updates the stats for deleting a file
|
|
|
|
//
|
|
|
|
// It may return fatal errors if the threshold for --max-delete or
|
|
|
|
// --max-delete-size have been reached.
|
|
|
|
func (s *StatsInfo) DeleteFile(ctx context.Context, size int64) error {
|
|
|
|
ci := fs.GetConfig(ctx)
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2023-03-08 19:40:37 +01:00
|
|
|
if size < 0 {
|
|
|
|
size = 0
|
|
|
|
}
|
|
|
|
if ci.MaxDelete >= 0 && s.deletes+1 > ci.MaxDelete {
|
|
|
|
return errMaxDelete
|
|
|
|
}
|
|
|
|
if ci.MaxDeleteSize >= 0 && s.deletesSize+size > int64(ci.MaxDeleteSize) {
|
|
|
|
return errMaxDeleteSize
|
|
|
|
}
|
|
|
|
s.deletes++
|
|
|
|
s.deletesSize += size
|
|
|
|
return nil
|
2018-01-22 19:53:18 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 19:40:37 +01:00
|
|
|
// GetDeletes returns the number of deletes
|
|
|
|
func (s *StatsInfo) GetDeletes() int64 {
|
2022-08-03 16:53:02 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2023-03-08 19:40:37 +01:00
|
|
|
return s.deletes
|
2022-08-03 16:53:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:54:31 +01:00
|
|
|
// DeletedDirs updates the stats for deletedDirs
|
|
|
|
func (s *StatsInfo) DeletedDirs(deletedDirs int64) int64 {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.deletedDirs += deletedDirs
|
|
|
|
return s.deletedDirs
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:12:32 +02:00
|
|
|
// Renames updates the stats for renames
|
|
|
|
func (s *StatsInfo) Renames(renames int64) int64 {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.renames += renames
|
|
|
|
return s.renames
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetCounters sets the counters (bytes, checks, errors, transfers, deletes, renames) to 0 and resets lastError, fatalError and retryError
|
2018-02-01 14:13:24 +01:00
|
|
|
func (s *StatsInfo) ResetCounters() {
|
2018-07-22 11:33:19 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
s.bytes = 0
|
|
|
|
s.errors = 0
|
2018-08-30 18:26:07 +02:00
|
|
|
s.lastError = nil
|
|
|
|
s.fatalError = false
|
|
|
|
s.retryError = false
|
2019-03-21 12:24:13 +01:00
|
|
|
s.retryAfter = time.Time{}
|
2018-02-01 14:13:24 +01:00
|
|
|
s.checks = 0
|
|
|
|
s.transfers = 0
|
2018-01-22 19:53:18 +01:00
|
|
|
s.deletes = 0
|
2022-08-03 16:53:02 +02:00
|
|
|
s.deletesSize = 0
|
2020-10-28 13:54:31 +01:00
|
|
|
s.deletedDirs = 0
|
2020-03-30 19:12:32 +02:00
|
|
|
s.renames = 0
|
2019-07-22 21:11:46 +02:00
|
|
|
s.startedTransfers = nil
|
2019-10-17 12:43:32 +02:00
|
|
|
s.oldDuration = 0
|
2021-06-13 18:10:15 +02:00
|
|
|
|
2024-01-16 18:30:24 +01:00
|
|
|
s._stopAverageLoop()
|
2021-06-13 18:10:15 +02:00
|
|
|
s.average = averageValues{stop: make(chan bool)}
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:07 +02:00
|
|
|
// ResetErrors sets the errors count to 0 and resets lastError, fatalError and retryError
|
2018-02-01 14:13:24 +01:00
|
|
|
func (s *StatsInfo) ResetErrors() {
|
2018-07-22 11:33:19 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
s.errors = 0
|
2018-08-30 18:26:07 +02:00
|
|
|
s.lastError = nil
|
|
|
|
s.fatalError = false
|
|
|
|
s.retryError = false
|
2019-03-21 12:24:13 +01:00
|
|
|
s.retryAfter = time.Time{}
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errored returns whether there have been any errors
|
|
|
|
func (s *StatsInfo) Errored() bool {
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
return s.errors != 0
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:07 +02:00
|
|
|
// Error adds a single error into the stats, assigns lastError and eventually sets fatalError or retryError
|
2019-11-18 15:13:02 +01:00
|
|
|
func (s *StatsInfo) Error(err error) error {
|
|
|
|
if err == nil || fserrors.IsCounted(err) {
|
|
|
|
return err
|
2019-03-21 12:24:13 +01:00
|
|
|
}
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
s.errors++
|
|
|
|
s.lastError = err
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fserrors.FsError(err)
|
|
|
|
fserrors.Count(err)
|
2018-08-30 18:26:07 +02:00
|
|
|
switch {
|
|
|
|
case fserrors.IsFatalError(err):
|
|
|
|
s.fatalError = true
|
2019-03-21 12:24:13 +01:00
|
|
|
case fserrors.IsRetryAfterError(err):
|
|
|
|
retryAfter := fserrors.RetryAfterErrorTime(err)
|
|
|
|
if s.retryAfter.IsZero() || retryAfter.Sub(s.retryAfter) > 0 {
|
|
|
|
s.retryAfter = retryAfter
|
|
|
|
}
|
|
|
|
s.retryError = true
|
2018-08-30 18:26:07 +02:00
|
|
|
case !fserrors.IsNoRetryError(err):
|
|
|
|
s.retryError = true
|
|
|
|
}
|
2019-11-18 15:13:02 +01:00
|
|
|
return err
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 12:24:13 +01:00
|
|
|
// RetryAfter returns the time to retry after if it is set. It will
|
|
|
|
// be Zero if it isn't set.
|
|
|
|
func (s *StatsInfo) RetryAfter() time.Time {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.retryAfter
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:11:46 +02:00
|
|
|
// NewCheckingTransfer adds a checking transfer to the stats, from the object.
|
2023-02-06 11:30:22 +01:00
|
|
|
func (s *StatsInfo) NewCheckingTransfer(obj fs.DirEntry, what string) *Transfer {
|
|
|
|
tr := newCheckingTransfer(s, obj, what)
|
2020-06-13 20:37:42 +02:00
|
|
|
s.checking.add(tr)
|
|
|
|
return tr
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DoneChecking removes a check from the stats
|
|
|
|
func (s *StatsInfo) DoneChecking(remote string) {
|
2018-05-02 18:01:39 +02:00
|
|
|
s.checking.del(remote)
|
|
|
|
s.mu.Lock()
|
2018-02-01 14:13:24 +01:00
|
|
|
s.checks++
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.Unlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransfers reads the number of transfers
|
|
|
|
func (s *StatsInfo) GetTransfers() int64 {
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
return s.transfers
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:56:20 +02:00
|
|
|
// NewTransfer adds a transfer to the stats from the object.
|
2024-01-18 17:44:13 +01:00
|
|
|
//
|
|
|
|
// The obj is uses as the srcFs, the dstFs must be supplied
|
|
|
|
func (s *StatsInfo) NewTransfer(obj fs.DirEntry, dstFs fs.Fs) *Transfer {
|
|
|
|
var srcFs fs.Fs
|
|
|
|
if oi, ok := obj.(fs.ObjectInfo); ok {
|
|
|
|
if f, ok := oi.Fs().(fs.Fs); ok {
|
|
|
|
srcFs = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tr := newTransfer(s, obj, srcFs, dstFs)
|
2020-06-13 20:37:42 +02:00
|
|
|
s.transferring.add(tr)
|
2021-06-13 18:10:15 +02:00
|
|
|
s.startAverageLoop()
|
2020-06-13 20:37:42 +02:00
|
|
|
return tr
|
2019-07-16 13:56:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransferRemoteSize adds a transfer to the stats based on remote and size.
|
2024-01-18 17:44:13 +01:00
|
|
|
func (s *StatsInfo) NewTransferRemoteSize(remote string, size int64, srcFs, dstFs fs.Fs) *Transfer {
|
|
|
|
tr := newTransferRemoteSize(s, remote, size, false, "", srcFs, dstFs)
|
2020-06-13 20:37:42 +02:00
|
|
|
s.transferring.add(tr)
|
2021-06-13 18:10:15 +02:00
|
|
|
s.startAverageLoop()
|
2020-06-13 20:37:42 +02:00
|
|
|
return tr
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DoneTransferring removes a transfer from the stats
|
|
|
|
//
|
2022-06-27 18:56:03 +02:00
|
|
|
// if ok is true and it was in the transfermap (to avoid incrementing in case of nested calls, #6213) then it increments the transfers count
|
2018-02-01 14:13:24 +01:00
|
|
|
func (s *StatsInfo) DoneTransferring(remote string, ok bool) {
|
2022-06-27 18:56:03 +02:00
|
|
|
existed := s.transferring.del(remote)
|
|
|
|
if ok && existed {
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.Lock()
|
2018-02-01 14:13:24 +01:00
|
|
|
s.transfers++
|
2018-05-02 18:01:39 +02:00
|
|
|
s.mu.Unlock()
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
2023-08-16 13:25:56 +02:00
|
|
|
if s.transferring.empty() && s.checking.empty() {
|
2021-06-13 18:10:15 +02:00
|
|
|
time.AfterFunc(averageStopAfter, s.stopAverageLoop)
|
|
|
|
}
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
2018-07-19 23:41:34 +02:00
|
|
|
|
|
|
|
// SetCheckQueue sets the number of queued checks
|
|
|
|
func (s *StatsInfo) SetCheckQueue(n int, size int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.checkQueue = n
|
|
|
|
s.checkQueueSize = size
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTransferQueue sets the number of queued transfers
|
|
|
|
func (s *StatsInfo) SetTransferQueue(n int, size int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.transferQueue = n
|
|
|
|
s.transferQueueSize = size
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRenameQueue sets the number of queued transfers
|
|
|
|
func (s *StatsInfo) SetRenameQueue(n int, size int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.renameQueue = n
|
|
|
|
s.renameQueueSize = size
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
2019-07-16 13:56:20 +02:00
|
|
|
|
|
|
|
// AddTransfer adds reference to the started transfer.
|
|
|
|
func (s *StatsInfo) AddTransfer(transfer *Transfer) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.startedTransfers = append(s.startedTransfers, transfer)
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
2019-10-15 17:33:09 +02:00
|
|
|
|
2024-01-16 18:30:24 +01:00
|
|
|
// _removeTransfer removes a reference to the started transfer in
|
2019-10-16 21:11:11 +02:00
|
|
|
// position i.
|
|
|
|
//
|
|
|
|
// Must be called with the lock held
|
2024-01-16 18:30:24 +01:00
|
|
|
func (s *StatsInfo) _removeTransfer(transfer *Transfer, i int) {
|
2019-10-17 12:43:32 +02:00
|
|
|
now := time.Now()
|
|
|
|
|
2019-10-16 17:05:26 +02:00
|
|
|
// add finished transfer onto old time ranges
|
|
|
|
start, end := transfer.TimeRange()
|
|
|
|
if end.IsZero() {
|
2019-10-17 12:43:32 +02:00
|
|
|
end = now
|
2019-10-16 17:05:26 +02:00
|
|
|
}
|
|
|
|
s.oldTimeRanges = append(s.oldTimeRanges, timeRange{start, end})
|
|
|
|
s.oldTimeRanges.merge()
|
|
|
|
|
2019-10-16 21:11:11 +02:00
|
|
|
// remove the found entry
|
|
|
|
s.startedTransfers = append(s.startedTransfers[:i], s.startedTransfers[i+1:]...)
|
2019-10-17 12:43:32 +02:00
|
|
|
|
|
|
|
// Find youngest active transfer
|
|
|
|
oldestStart := now
|
|
|
|
for i := range s.startedTransfers {
|
|
|
|
start, _ := s.startedTransfers[i].TimeRange()
|
|
|
|
if start.Before(oldestStart) {
|
|
|
|
oldestStart = start
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove old entries older than that
|
|
|
|
s.oldDuration += s.oldTimeRanges.cull(oldestStart)
|
2019-10-16 21:11:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTransfer removes a reference to the started transfer.
|
|
|
|
func (s *StatsInfo) RemoveTransfer(transfer *Transfer) {
|
|
|
|
s.mu.Lock()
|
2019-10-15 17:33:09 +02:00
|
|
|
for i, tr := range s.startedTransfers {
|
|
|
|
if tr == transfer {
|
2024-01-16 18:30:24 +01:00
|
|
|
s._removeTransfer(tr, i)
|
2019-10-15 17:33:09 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
2019-10-16 21:11:11 +02:00
|
|
|
|
2019-11-14 14:14:40 +01:00
|
|
|
// PruneTransfers makes sure there aren't too many old transfers by removing
|
|
|
|
// single finished transfer.
|
2019-10-16 21:11:11 +02:00
|
|
|
func (s *StatsInfo) PruneTransfers() {
|
2019-11-14 14:14:40 +01:00
|
|
|
if MaxCompletedTransfers < 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-10-16 21:11:11 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
// remove a transfer from the start if we are over quota
|
2020-11-05 12:33:32 +01:00
|
|
|
if len(s.startedTransfers) > MaxCompletedTransfers+s.ci.Transfers {
|
2019-10-16 21:11:11 +02:00
|
|
|
for i, tr := range s.startedTransfers {
|
|
|
|
if tr.IsDone() {
|
2024-01-16 18:30:24 +01:00
|
|
|
s._removeTransfer(tr, i)
|
2019-10-16 21:11:11 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
2023-07-30 06:39:01 +02:00
|
|
|
|
|
|
|
// AddServerSideMove counts a server side move
|
|
|
|
func (s *StatsInfo) AddServerSideMove(n int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.serverSideMoves += 1
|
|
|
|
s.serverSideMoveBytes += n
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddServerSideCopy counts a server side copy
|
|
|
|
func (s *StatsInfo) AddServerSideCopy(n int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.serverSideCopies += 1
|
|
|
|
s.serverSideCopyBytes += n
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|