2022-08-28 11:21:57 +00:00
|
|
|
// Package memory provides the memory test command.
|
2020-08-08 17:02:18 +00:00
|
|
|
package memory
|
2016-08-05 16:12:27 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2016-08-05 16:12:27 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2020-08-08 17:02:18 +00:00
|
|
|
"github.com/rclone/rclone/cmd/test"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-08-05 16:12:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-08-08 17:02:18 +00:00
|
|
|
test.Command.AddCommand(commandDefinition)
|
2016-08-05 16:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:58:11 +00:00
|
|
|
var commandDefinition = &cobra.Command{
|
2020-08-08 17:02:18 +00:00
|
|
|
Use: "memory remote:path",
|
|
|
|
Short: `Load all the objects at remote:path into memory and report memory stats.`,
|
2016-08-05 16:12:27 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2016-12-04 16:52:24 +00:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2019-06-17 08:34:30 +00:00
|
|
|
ctx := context.Background()
|
2021-04-06 12:10:11 +00:00
|
|
|
ci := fs.GetConfig(context.Background())
|
2022-04-06 12:15:07 +00:00
|
|
|
objects, _, _, err := operations.Count(ctx, fsrc)
|
2016-08-05 16:12:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objs := make([]fs.Object, 0, objects)
|
|
|
|
var before, after runtime.MemStats
|
|
|
|
runtime.GC()
|
|
|
|
runtime.ReadMemStats(&before)
|
|
|
|
var mu sync.Mutex
|
2019-06-17 08:34:30 +00:00
|
|
|
err = operations.ListFn(ctx, fsrc, func(o fs.Object) {
|
2016-08-05 16:12:27 +00:00
|
|
|
mu.Lock()
|
|
|
|
objs = append(objs, o)
|
|
|
|
mu.Unlock()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
runtime.GC()
|
|
|
|
runtime.ReadMemStats(&after)
|
2021-04-06 12:10:11 +00:00
|
|
|
var allocChange int64
|
|
|
|
if after.Alloc >= before.Alloc {
|
|
|
|
allocChange = int64(after.Alloc - before.Alloc)
|
|
|
|
} else {
|
|
|
|
allocChange = -int64(before.Alloc - after.Alloc)
|
|
|
|
}
|
|
|
|
var sysChange int64
|
|
|
|
if after.Sys >= before.Sys {
|
|
|
|
sysChange = int64(after.Sys - before.Sys)
|
|
|
|
} else {
|
|
|
|
sysChange = -int64(before.Sys - after.Sys)
|
|
|
|
}
|
|
|
|
if ci.HumanReadable {
|
|
|
|
objString := fs.CountSuffix(int64(len(objs)))
|
|
|
|
var usedString string
|
|
|
|
if after.Alloc >= before.Alloc {
|
|
|
|
usedString = fs.SizeSuffix(int64(after.Alloc - before.Alloc)).ByteUnit()
|
|
|
|
} else {
|
|
|
|
usedString = "-" + fs.SizeSuffix(int64(before.Alloc-after.Alloc)).ByteUnit()
|
|
|
|
}
|
|
|
|
avgString := fs.SizeSuffix(allocChange / int64(len(objs))).ByteUnit()
|
|
|
|
fs.Logf(nil, "%s objects took %s, %s/object", objString, usedString, avgString)
|
|
|
|
|
|
|
|
var sysBeforeString string
|
|
|
|
if before.Sys <= fs.SizeSuffixMaxValue {
|
|
|
|
sysBeforeString = fs.SizeSuffix(int64(before.Sys)).String()
|
|
|
|
} else {
|
|
|
|
sysBeforeString = ">" + fs.SizeSuffixMax.String()
|
|
|
|
}
|
|
|
|
var sysAfterString string
|
|
|
|
if after.Sys <= fs.SizeSuffixMaxValue {
|
|
|
|
sysAfterString = fs.SizeSuffix(int64(after.Sys)).ByteUnit()
|
|
|
|
} else {
|
|
|
|
sysAfterString = ">" + fs.SizeSuffixMax.ByteUnit()
|
|
|
|
}
|
|
|
|
var sysUsedString string
|
|
|
|
if after.Sys >= before.Sys {
|
|
|
|
sysUsedString = fs.SizeSuffix(int64(after.Sys - before.Sys)).ByteUnit()
|
|
|
|
} else {
|
|
|
|
sysUsedString = "-" + fs.SizeSuffix(int64(before.Sys-after.Sys)).ByteUnit()
|
|
|
|
}
|
|
|
|
fs.Logf(nil, "System memory changed from %s to %s a change of %s", sysBeforeString, sysAfterString, sysUsedString)
|
|
|
|
} else {
|
|
|
|
fs.Logf(nil, "%d objects took %d bytes, %.1f bytes/object", len(objs), allocChange, float64(allocChange)/float64(len(objs)))
|
|
|
|
fs.Logf(nil, "System memory changed from %d to %d bytes a change of %d bytes", before.Sys, after.Sys, sysChange)
|
|
|
|
}
|
2016-08-05 16:12:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|