percentiles.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Utility methods to calculate percentiles.
  15. package summary
  16. import (
  17. "fmt"
  18. "math"
  19. "sort"
  20. info "github.com/google/cadvisor/info/v2"
  21. )
  22. const secondsToMilliSeconds = 1000
  23. const milliSecondsToNanoSeconds = 1000000
  24. const secondsToNanoSeconds = secondsToMilliSeconds * milliSecondsToNanoSeconds
  25. type Uint64Slice []uint64
  26. func (a Uint64Slice) Len() int { return len(a) }
  27. func (a Uint64Slice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  28. func (a Uint64Slice) Less(i, j int) bool { return a[i] < a[j] }
  29. // Get percentile of the provided samples. Round to integer.
  30. func (self Uint64Slice) GetPercentile(d float64) uint64 {
  31. if d < 0.0 || d > 1.0 {
  32. return 0
  33. }
  34. count := self.Len()
  35. if count == 0 {
  36. return 0
  37. }
  38. sort.Sort(self)
  39. n := float64(d * (float64(count) + 1))
  40. idx, frac := math.Modf(n)
  41. index := int(idx)
  42. percentile := float64(self[index-1])
  43. if index > 1 && index < count {
  44. percentile += frac * float64(self[index]-self[index-1])
  45. }
  46. return uint64(percentile)
  47. }
  48. type mean struct {
  49. // current count.
  50. count uint64
  51. // current mean.
  52. Mean float64
  53. }
  54. func (self *mean) Add(value uint64) {
  55. self.count++
  56. if self.count == 1 {
  57. self.Mean = float64(value)
  58. return
  59. }
  60. c := float64(self.count)
  61. v := float64(value)
  62. self.Mean = (self.Mean*(c-1) + v) / c
  63. }
  64. type resource struct {
  65. // list of samples being tracked.
  66. samples Uint64Slice
  67. // average from existing samples.
  68. mean mean
  69. // maximum value seen so far in the added samples.
  70. max uint64
  71. }
  72. // Adds a new percentile sample.
  73. func (self *resource) Add(p info.Percentiles) {
  74. if !p.Present {
  75. return
  76. }
  77. if p.Max > self.max {
  78. self.max = p.Max
  79. }
  80. self.mean.Add(p.Mean)
  81. // Selecting 90p of 90p :(
  82. self.samples = append(self.samples, p.Ninety)
  83. }
  84. // Add a single sample. Internally, we convert it to a fake percentile sample.
  85. func (self *resource) AddSample(val uint64) {
  86. sample := info.Percentiles{
  87. Present: true,
  88. Mean: val,
  89. Max: val,
  90. Fifty: val,
  91. Ninety: val,
  92. NinetyFive: val,
  93. }
  94. self.Add(sample)
  95. }
  96. // Get max, average, and 90p from existing samples.
  97. func (self *resource) GetAllPercentiles() info.Percentiles {
  98. p := info.Percentiles{}
  99. p.Mean = uint64(self.mean.Mean)
  100. p.Max = self.max
  101. p.Fifty = self.samples.GetPercentile(0.5)
  102. p.Ninety = self.samples.GetPercentile(0.9)
  103. p.NinetyFive = self.samples.GetPercentile(0.95)
  104. p.Present = true
  105. return p
  106. }
  107. func NewResource(size int) *resource {
  108. return &resource{
  109. samples: make(Uint64Slice, 0, size),
  110. mean: mean{count: 0, Mean: 0},
  111. }
  112. }
  113. // Return aggregated percentiles from the provided percentile samples.
  114. func GetDerivedPercentiles(stats []*info.Usage) info.Usage {
  115. cpu := NewResource(len(stats))
  116. memory := NewResource(len(stats))
  117. for _, stat := range stats {
  118. cpu.Add(stat.Cpu)
  119. memory.Add(stat.Memory)
  120. }
  121. usage := info.Usage{}
  122. usage.Cpu = cpu.GetAllPercentiles()
  123. usage.Memory = memory.GetAllPercentiles()
  124. return usage
  125. }
  126. // Calculate part of a minute this sample set represent.
  127. func getPercentComplete(stats []*secondSample) (percent int32) {
  128. numSamples := len(stats)
  129. if numSamples > 1 {
  130. percent = 100
  131. timeRange := stats[numSamples-1].Timestamp.Sub(stats[0].Timestamp).Nanoseconds()
  132. // allow some slack
  133. if timeRange < 58*secondsToNanoSeconds {
  134. percent = int32((timeRange * 100) / 60 * secondsToNanoSeconds)
  135. }
  136. }
  137. return
  138. }
  139. // Calculate cpurate from two consecutive total cpu usage samples.
  140. func getCpuRate(latest, previous secondSample) (uint64, error) {
  141. var elapsed int64
  142. elapsed = latest.Timestamp.Sub(previous.Timestamp).Nanoseconds()
  143. if elapsed < 10*milliSecondsToNanoSeconds {
  144. return 0, fmt.Errorf("elapsed time too small: %d ns: time now %s last %s", elapsed, latest.Timestamp.String(), previous.Timestamp.String())
  145. }
  146. if latest.Cpu < previous.Cpu {
  147. return 0, fmt.Errorf("bad sample: cumulative cpu usage dropped from %d to %d", latest.Cpu, previous.Cpu)
  148. }
  149. // Cpurate is calculated in cpu-milliseconds per second.
  150. cpuRate := (latest.Cpu - previous.Cpu) * secondsToMilliSeconds / uint64(elapsed)
  151. return cpuRate, nil
  152. }
  153. // Returns a percentile sample for a minute by aggregating seconds samples.
  154. func GetMinutePercentiles(stats []*secondSample) info.Usage {
  155. lastSample := secondSample{}
  156. cpu := NewResource(len(stats))
  157. memory := NewResource(len(stats))
  158. for _, stat := range stats {
  159. if !lastSample.Timestamp.IsZero() {
  160. cpuRate, err := getCpuRate(*stat, lastSample)
  161. if err != nil {
  162. continue
  163. }
  164. cpu.AddSample(cpuRate)
  165. memory.AddSample(stat.Memory)
  166. } else {
  167. memory.AddSample(stat.Memory)
  168. }
  169. lastSample = *stat
  170. }
  171. percent := getPercentComplete(stats)
  172. return info.Usage{
  173. PercentComplete: percent,
  174. Cpu: cpu.GetAllPercentiles(),
  175. Memory: memory.GetAllPercentiles(),
  176. }
  177. }