mirror of
https://github.com/scratchfoundation/golangci-lint.git
synced 2025-08-28 22:28:43 -04:00
Reduce memory usage of go/analysis
This commit is contained in:
parent
358ce7c20c
commit
3aade55e05
15 changed files with 369 additions and 184 deletions
|
@ -451,21 +451,30 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
|
|||
startedAt := time.Now()
|
||||
debugf("Started tracking time")
|
||||
|
||||
var rssValues []uint64
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
var maxRSSMB, totalRSSMB float64
|
||||
var iterationsCount int
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
logEveryRecord := os.Getenv("GL_MEM_LOG_EVERY") == "1"
|
||||
const MB = 1024 * 1024
|
||||
|
||||
track := func() {
|
||||
debugf("Starting memory tracing iteration ...")
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
|
||||
if logEveryRecord {
|
||||
debugf("Stopping memory tracing iteration, printing ...")
|
||||
printMemStats(&m, logger)
|
||||
}
|
||||
|
||||
rssValues = append(rssValues, m.Sys)
|
||||
rssMB := float64(m.Sys) / MB
|
||||
if rssMB > maxRSSMB {
|
||||
maxRSSMB = rssMB
|
||||
}
|
||||
totalRSSMB += rssMB
|
||||
iterationsCount++
|
||||
}
|
||||
|
||||
for {
|
||||
|
@ -476,7 +485,7 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
|
|||
case <-ctx.Done():
|
||||
stop = true
|
||||
debugf("Stopped resources tracking")
|
||||
case <-ticker.C: // track every second
|
||||
case <-ticker.C:
|
||||
}
|
||||
|
||||
if stop {
|
||||
|
@ -485,19 +494,10 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
|
|||
}
|
||||
track()
|
||||
|
||||
var avg, max uint64
|
||||
for _, v := range rssValues {
|
||||
avg += v
|
||||
if v > max {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
avg /= uint64(len(rssValues))
|
||||
avgRSSMB := totalRSSMB / float64(iterationsCount)
|
||||
|
||||
const MB = 1024 * 1024
|
||||
maxMB := float64(max) / MB
|
||||
logger.Infof("Memory: %d samples, avg is %.1fMB, max is %.1fMB",
|
||||
len(rssValues), float64(avg)/MB, maxMB)
|
||||
iterationsCount, avgRSSMB, maxRSSMB)
|
||||
logger.Infof("Execution took %s", time.Since(startedAt))
|
||||
close(done)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue