Compare commits

...

1 Commits

Author SHA1 Message Date
Vaibhav Kamra
f5360b0074 Memory profiling 2022-11-29 21:54:47 -08:00
2 changed files with 30 additions and 1 deletions

View File

@ -19,6 +19,8 @@ COPY --from=builder /go/src/app/corso /corso
# Pull tls certs directly from latest upstream image
COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
RUN apk add --no-cache --update binutils util-linux logrotate
ENV CORSO_HOME=/app/corso
ENV CORSO_CONFIG_DIR=$CORSO_HOME \
KOPIA_CONFIG_PATH=$CORSO_HOME/kopia/config/repository.config \
@ -28,4 +30,4 @@ ENV CORSO_CONFIG_DIR=$CORSO_HOME \
KOPIA_PERSIST_CREDENTIALS_ON_CONNECT=false \
KOPIA_CHECK_FOR_UPDATES=false
ENTRYPOINT ["/corso"]
ENTRYPOINT ["tail", "-f", "/dev/null"]

View File

@ -2,8 +2,11 @@ package cli
import (
"context"
"fmt"
"os"
"regexp"
"runtime"
"runtime/pprof"
"strings"
"github.com/spf13/cobra"
@ -95,6 +98,16 @@ func Handle() {
ctx, log := logger.Seed(ctx, logger.PreloadLogLevel())
defer func() {
_ = log.Sync() // flush all logs in the buffer
f, err := os.Create("mem.prof")
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
PrintMemUsage()
}()
if err := corsoCmd.ExecuteContext(ctx); err != nil {
@ -113,3 +126,17 @@ func indentExamplesTemplate(template string) string {
return e.ReplaceAllString(template, "{{.Example | indent 2}}")
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}