github.com/moontrade/nogc@v0.1.7/stats.go (about)

     1  package nogc
     2  
     3  import "math"
     4  
     5  // HeapStats provides the metrics of an Allocator
     6  type HeapStats struct {
     7  	HeapSize        int64
     8  	AllocSize       int64
     9  	PeakAllocSize   int64
    10  	FreeSize        int64
    11  	Allocs          int32
    12  	InitialPages    int32
    13  	ConsecutiveLow  int32
    14  	ConsecutiveHigh int32
    15  	Pages           int32
    16  	Grows           int32
    17  	fragmentation   float32
    18  }
    19  
    20  func (s *HeapStats) Fragmentation() float32 {
    21  	if s.fragmentation != 0 {
    22  		return s.fragmentation
    23  	}
    24  	if s.HeapSize == 0 || s.PeakAllocSize == 0 {
    25  		return 0
    26  	}
    27  	pct := float64(s.HeapSize-s.PeakAllocSize) / float64(s.HeapSize)
    28  	s.fragmentation = float32(math.Floor(pct*100) / 100)
    29  	return s.fragmentation
    30  }