Add a global option to print memory usage

This option, -print-memory-usage, will print memory usage every second while
the program is running.
This commit is contained in:
Gilbert Chen
2021-10-15 20:45:53 -04:00
parent cacf6618d2
commit 68b60499d7
2 changed files with 22 additions and 0 deletions

View File

@@ -147,6 +147,10 @@ func setGlobalOptions(context *cli.Context) {
duplicacy.SetLoggingLevel(duplicacy.DEBUG) duplicacy.SetLoggingLevel(duplicacy.DEBUG)
} }
if context.GlobalBool("print-memory-usage") {
go duplicacy.PrintMemoryUsage()
}
ScriptEnabled = true ScriptEnabled = true
if context.GlobalBool("no-script") { if context.GlobalBool("no-script") {
ScriptEnabled = false ScriptEnabled = false
@@ -2180,6 +2184,10 @@ func main() {
Usage: "suppress logs with the specified id", Usage: "suppress logs with the specified id",
Argument: "<id>", Argument: "<id>",
}, },
cli.BoolFlag{
Name: "print-memory-usage",
Usage: "print memory usage every second",
},
} }
app.HideVersion = true app.HideVersion = true

View File

@@ -14,6 +14,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"runtime"
"github.com/gilbertchen/gopass" "github.com/gilbertchen/gopass"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
@@ -460,3 +461,16 @@ func AtoSize(sizeString string) int {
return size 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)
}
}