mirror of
https://github.com/kopia/kopia.git
synced 2026-01-04 12:37:52 -05:00
This is mostly mechanical and changes how loggers are instantiated. Logger is now associated with a context, passed around all methods, (most methods had ctx, but had to add it in a few missing places). By default Kopia does not produce any logs, but it can be overridden, either locally for a nested context, by calling ctx = logging.WithLogger(ctx, newLoggerFunc) To override logs globally, call logging.SetDefaultLogger(newLoggerFunc) This refactoring allowed removing dependency from Kopia repo and go-logging library (the CLI still uses it, though). It is now also possible to have all test methods emit logs using t.Logf() so that they show up in failure reports, which should make debugging of test failures suck less.
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"runtime"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/kopia/kopia/repo/logging"
|
|
)
|
|
|
|
var trackMemoryUsage = app.Flag("track-memory-usage", "Periodically force GC and log current memory usage").Hidden().Duration()
|
|
|
|
var memlog = logging.GetContextLoggerFunc("kopia/memory")
|
|
|
|
var (
|
|
memoryTrackerMutex sync.Mutex
|
|
lastHeapUsage, lastStackInUse uint64
|
|
maxHeapUsage, maxStackInUse uint64
|
|
)
|
|
|
|
func dumpMemoryUsage(ctx context.Context) {
|
|
runtime.GC()
|
|
|
|
var ms runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&ms)
|
|
|
|
memoryTrackerMutex.Lock()
|
|
defer memoryTrackerMutex.Unlock()
|
|
memlog(ctx).Debugf("in use heap %v (delta %v max %v) stack %v (delta %v max %v)", ms.HeapInuse, int64(ms.HeapInuse-lastHeapUsage), maxHeapUsage, ms.StackInuse, int64(ms.StackInuse-lastStackInUse), maxStackInUse)
|
|
|
|
if ms.HeapInuse > maxHeapUsage {
|
|
maxHeapUsage = ms.HeapInuse
|
|
}
|
|
|
|
if ms.StackInuse > maxStackInUse {
|
|
maxStackInUse = ms.StackInuse
|
|
}
|
|
|
|
lastHeapUsage = ms.HeapInuse
|
|
lastStackInUse = ms.StackInuse
|
|
}
|
|
|
|
func startMemoryTracking(ctx context.Context) {
|
|
if *trackMemoryUsage > 0 {
|
|
go func() {
|
|
for {
|
|
dumpMemoryUsage(ctx)
|
|
time.Sleep(*trackMemoryUsage)
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
func finishMemoryTracking(ctx context.Context) {
|
|
dumpMemoryUsage(ctx)
|
|
}
|