mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Update of obipcr and homogenization of logging
Former-commit-id: 46abf47c19ace5248042c02cf1f81d9f6c12eb10
This commit is contained in:
47
pkg/obiiter/limitmemory.go
Normal file
47
pkg/obiiter/limitmemory.go
Normal file
@ -0,0 +1,47 @@
|
||||
package obiiter
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/pbnjay/memory"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (iterator IBioSequence) LimitMemory(fraction float64) IBioSequence {
|
||||
newIter := MakeIBioSequence()
|
||||
|
||||
fracLoad := func() float64 {
|
||||
var mem runtime.MemStats
|
||||
runtime.ReadMemStats(&mem)
|
||||
return float64(mem.Alloc) / float64(memory.TotalMemory())
|
||||
}
|
||||
|
||||
newIter.Add(1)
|
||||
go func() {
|
||||
|
||||
for iterator.Next() {
|
||||
nwait := 0
|
||||
for fracLoad() > fraction {
|
||||
runtime.Gosched()
|
||||
nwait++
|
||||
if nwait%1000 == 0 {
|
||||
log.Warnf("Wait for memory limit %f/%f", fracLoad(), fraction)
|
||||
|
||||
}
|
||||
if nwait > 10000 {
|
||||
log.Warnf("Very long wait for memory limit %f/%f", fracLoad(), fraction)
|
||||
break
|
||||
}
|
||||
}
|
||||
newIter.Push(iterator.Get())
|
||||
}
|
||||
|
||||
newIter.Done()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
newIter.WaitAndClose()
|
||||
}()
|
||||
|
||||
return newIter
|
||||
}
|
Reference in New Issue
Block a user