Files
kopia/cli/memory_tracking.go
Jarek Kowalski 35d0f31c0d huge: replaced the use of allocated byte slices with populating gather.WriteBuffer in the repository (#1244)
This helps recycle buffers more efficiently during snapshots.
Also, improved memory tracking, enabled profiling flags and added pprof
by default.
2021-08-20 08:45:10 -07:00

56 lines
935 B
Go

package cli
import (
"context"
"sync"
"time"
"github.com/alecthomas/kingpin"
"github.com/kopia/kopia/internal/memtrack"
)
type memoryTracker struct {
trackMemoryUsage time.Duration
}
func (c *memoryTracker) setup(app *kingpin.Application) {
app.Flag("track-memory-usage", "Periodically force GC and log current memory usage").Hidden().DurationVar(&c.trackMemoryUsage)
}
func (c *memoryTracker) startMemoryTracking(ctx context.Context) (context.Context, context.CancelFunc) {
ctx = memtrack.Attach(ctx, "memory")
var (
closed = make(chan struct{})
wg sync.WaitGroup
)
if c.trackMemoryUsage > 0 {
ticker := time.NewTicker(c.trackMemoryUsage)
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-closed:
return
case <-ticker.C:
memtrack.Dump(ctx, "periodic")
}
}
}()
}
return ctx, func() {
close(closed)
wg.Wait()
memtrack.Dump(ctx, "final")
}
}