switch result pointer with channel

This commit is contained in:
2025-03-22 23:13:15 +01:00
parent f4f64e1999
commit bcfaccfec5
2 changed files with 12 additions and 11 deletions

View File

@@ -220,16 +220,14 @@ func (p *ProtoPackage) build(ctx context.Context) (time.Duration, error) {
log.Errorf("error getting PGID: %v", err) log.Errorf("error getting PGID: %v", err)
} }
var peakMem *int64
done := make(chan bool) done := make(chan bool)
go pollMemoryUsage(pgid, 1*time.Second, done, peakMem) result := make(chan int64)
go pollMemoryUsage(pgid, 1*time.Second, done, result)
err = cmd.Wait() err = cmd.Wait()
close(done) close(done)
close(result)
if peakMem == nil { peakMem := <-result
log.Warningf("memory reading failed")
}
Rusage, ok := cmd.ProcessState.SysUsage().(*syscall.Rusage) Rusage, ok := cmd.ProcessState.SysUsage().(*syscall.Rusage)
if !ok { if !ok {
@@ -329,7 +327,7 @@ func (p *ProtoPackage) build(ctx context.Context) (time.Duration, error) {
SetBuildTimeStart(start). SetBuildTimeStart(start).
SetLastVersionBuild(p.Version). SetLastVersionBuild(p.Version).
SetTagRev(p.State.TagRev). SetTagRev(p.State.TagRev).
SetMaxRss(*peakMem). SetMaxRss(peakMem).
SetIoOut(Rusage.Oublock). SetIoOut(Rusage.Oublock).
SetIoIn(Rusage.Inblock). SetIoIn(Rusage.Inblock).
SetUTime(Rusage.Utime.Sec). SetUTime(Rusage.Utime.Sec).

View File

@@ -849,10 +849,13 @@ func getMemoryStats(pid int) (MemStats, error) {
return stats, nil return stats, nil
} }
func pollMemoryUsage(pid int, interval time.Duration, done chan bool, peakMem *int64) { func pollMemoryUsage(pid int, interval time.Duration, done chan bool, result chan int64) {
var totalMemory int64
for { for {
select { select {
case <-done: case <-done:
result <- totalMemory
return return
default: default:
var totalRSS, totalSwap int64 var totalRSS, totalSwap int64
@@ -878,9 +881,9 @@ func pollMemoryUsage(pid int, interval time.Duration, done chan bool, peakMem *i
} }
} }
totalMemory := totalRSS + totalSwap newMemory := totalRSS + totalSwap
if peakMem == nil || totalMemory > *peakMem { if newMemory > totalMemory {
peakMem = &totalMemory totalMemory = newMemory
} }
time.Sleep(interval) time.Sleep(interval)