percentiles.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 (s Uint64Slice) Len() int { return len(s) }
  27. func (s Uint64Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  28. func (s Uint64Slice) Less(i, j int) bool { return s[i] < s[j] }
  29. // Get percentile of the provided samples. Round to integer.
  30. func (s Uint64Slice) GetPercentile(d float64) uint64 {
  31. if d < 0.0 || d > 1.0 {
  32. return 0
  33. }
  34. count := s.Len()
  35. if count == 0 {
  36. return 0
  37. }
  38. sort.Sort(s)
  39. n := float64(d * (float64(count) + 1))
  40. idx, frac := math.Modf(n)
  41. index := int(idx)
  42. percentile := float64(s[index-1])
  43. if index > 1 && index < count {
  44. percentile += frac * float64(s[index]-s[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 (m *mean) Add(value uint64) {
  55. m.count++
  56. if m.count == 1 {
  57. m.Mean = float64(value)
  58. return
  59. }
  60. c := float64(m.count)
  61. v := float64(value)
  62. m.Mean = (m.Mean*(c-1) + v) / c
  63. }
  64. type Percentile interface {
  65. Add(info.Percentiles)
  66. AddSample(uint64)
  67. GetAllPercentiles() info.Percentiles
  68. }
  69. type resource struct {
  70. // list of samples being tracked.
  71. samples Uint64Slice
  72. // average from existing samples.
  73. mean mean
  74. // maximum value seen so far in the added samples.
  75. max uint64
  76. }
  77. // Adds a new percentile sample.
  78. func (r *resource) Add(p info.Percentiles) {
  79. if !p.Present {
  80. return
  81. }
  82. if p.Max > r.max {
  83. r.max = p.Max
  84. }
  85. r.mean.Add(p.Mean)
  86. // Selecting 90p of 90p :(
  87. r.samples = append(r.samples, p.Ninety)
  88. }
  89. // Add a single sample. Internally, we convert it to a fake percentile sample.
  90. func (r *resource) AddSample(val uint64) {
  91. sample := info.Percentiles{
  92. Present: true,
  93. Mean: val,
  94. Max: val,
  95. Fifty: val,
  96. Ninety: val,
  97. NinetyFive: val,
  98. }
  99. r.Add(sample)
  100. }
  101. // Get max, average, and 90p from existing samples.
  102. func (r *resource) GetAllPercentiles() info.Percentiles {
  103. p := info.Percentiles{}
  104. p.Mean = uint64(r.mean.Mean)
  105. p.Max = r.max
  106. p.Fifty = r.samples.GetPercentile(0.5)
  107. p.Ninety = r.samples.GetPercentile(0.9)
  108. p.NinetyFive = r.samples.GetPercentile(0.95)
  109. p.Present = true
  110. return p
  111. }
  112. func NewResource(size int) Percentile {
  113. return &resource{
  114. samples: make(Uint64Slice, 0, size),
  115. mean: mean{count: 0, Mean: 0},
  116. }
  117. }
  118. // Return aggregated percentiles from the provided percentile samples.
  119. func GetDerivedPercentiles(stats []*info.Usage) info.Usage {
  120. cpu := NewResource(len(stats))
  121. memory := NewResource(len(stats))
  122. for _, stat := range stats {
  123. cpu.Add(stat.Cpu)
  124. memory.Add(stat.Memory)
  125. }
  126. usage := info.Usage{}
  127. usage.Cpu = cpu.GetAllPercentiles()
  128. usage.Memory = memory.GetAllPercentiles()
  129. return usage
  130. }
  131. // Calculate part of a minute this sample set represent.
  132. func getPercentComplete(stats []*secondSample) (percent int32) {
  133. numSamples := len(stats)
  134. if numSamples > 1 {
  135. percent = 100
  136. timeRange := stats[numSamples-1].Timestamp.Sub(stats[0].Timestamp).Nanoseconds()
  137. // allow some slack
  138. if timeRange < 58*secondsToNanoSeconds {
  139. percent = int32((timeRange * 100) / 60 * secondsToNanoSeconds)
  140. }
  141. }
  142. return
  143. }
  144. // Calculate cpurate from two consecutive total cpu usage samples.
  145. func getCPURate(latest, previous secondSample) (uint64, error) {
  146. elapsed := latest.Timestamp.Sub(previous.Timestamp).Nanoseconds()
  147. if elapsed < 10*milliSecondsToNanoSeconds {
  148. return 0, fmt.Errorf("elapsed time too small: %d ns: time now %s last %s", elapsed, latest.Timestamp.String(), previous.Timestamp.String())
  149. }
  150. if latest.Cpu < previous.Cpu {
  151. return 0, fmt.Errorf("bad sample: cumulative cpu usage dropped from %d to %d", latest.Cpu, previous.Cpu)
  152. }
  153. // Cpurate is calculated in cpu-milliseconds per second.
  154. cpuRate := (latest.Cpu - previous.Cpu) * secondsToMilliSeconds / uint64(elapsed)
  155. return cpuRate, nil
  156. }
  157. // Returns a percentile sample for a minute by aggregating seconds samples.
  158. func GetMinutePercentiles(stats []*secondSample) info.Usage {
  159. lastSample := secondSample{}
  160. cpu := NewResource(len(stats))
  161. memory := NewResource(len(stats))
  162. for _, stat := range stats {
  163. if !lastSample.Timestamp.IsZero() {
  164. cpuRate, err := getCPURate(*stat, lastSample)
  165. if err != nil {
  166. continue
  167. }
  168. cpu.AddSample(cpuRate)
  169. memory.AddSample(stat.Memory)
  170. } else {
  171. memory.AddSample(stat.Memory)
  172. }
  173. lastSample = *stat
  174. }
  175. percent := getPercentComplete(stats)
  176. return info.Usage{
  177. PercentComplete: percent,
  178. Cpu: cpu.GetAllPercentiles(),
  179. Memory: memory.GetAllPercentiles(),
  180. }
  181. }