From 68b60499d7dca73dd94fe6f01a4f2e0fd3481282 Mon Sep 17 00:00:00 2001 From: Gilbert Chen Date: Fri, 15 Oct 2021 20:45:53 -0400 Subject: [PATCH] Add a global option to print memory usage This option, -print-memory-usage, will print memory usage every second while the program is running. --- duplicacy/duplicacy_main.go | 8 ++++++++ src/duplicacy_utils.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/duplicacy/duplicacy_main.go b/duplicacy/duplicacy_main.go index 9fe4ce0..88cad5d 100644 --- a/duplicacy/duplicacy_main.go +++ b/duplicacy/duplicacy_main.go @@ -147,6 +147,10 @@ func setGlobalOptions(context *cli.Context) { duplicacy.SetLoggingLevel(duplicacy.DEBUG) } + if context.GlobalBool("print-memory-usage") { + go duplicacy.PrintMemoryUsage() + } + ScriptEnabled = true if context.GlobalBool("no-script") { ScriptEnabled = false @@ -2180,6 +2184,10 @@ func main() { Usage: "suppress logs with the specified id", Argument: "", }, + cli.BoolFlag{ + Name: "print-memory-usage", + Usage: "print memory usage every second", + }, } app.HideVersion = true diff --git a/src/duplicacy_utils.go b/src/duplicacy_utils.go index 3b3cc5a..7f6754b 100644 --- a/src/duplicacy_utils.go +++ b/src/duplicacy_utils.go @@ -14,6 +14,7 @@ import ( "strconv" "strings" "time" + "runtime" "github.com/gilbertchen/gopass" "golang.org/x/crypto/pbkdf2" @@ -460,3 +461,16 @@ func AtoSize(sizeString string) int { return size } + +func PrintMemoryUsage() { + + for { + var m runtime.MemStats + runtime.ReadMemStats(&m) + + LOG_INFO("MEMORY_STATS", "Currently allocated: %s, total allocated: %s, system memory: %s, number of GCs: %d", + PrettySize(int64(m.Alloc)), PrettySize(int64(m.TotalAlloc)), PrettySize(int64(m.Sys)), m.NumGC) + + time.Sleep(time.Second) + } +}