You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

329 lines
9.0 KiB

  1. // Copyright 2014 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. // Package measurement export utility functions to manipulate/format performance profile sample values.
  15. package measurement
  16. import (
  17. "fmt"
  18. "math"
  19. "strings"
  20. "time"
  21. "github.com/google/pprof/profile"
  22. )
  23. // ScaleProfiles updates the units in a set of profiles to make them
  24. // compatible. It scales the profiles to the smallest unit to preserve
  25. // data.
  26. func ScaleProfiles(profiles []*profile.Profile) error {
  27. if len(profiles) == 0 {
  28. return nil
  29. }
  30. periodTypes := make([]*profile.ValueType, 0, len(profiles))
  31. for _, p := range profiles {
  32. if p.PeriodType != nil {
  33. periodTypes = append(periodTypes, p.PeriodType)
  34. }
  35. }
  36. periodType, err := CommonValueType(periodTypes)
  37. if err != nil {
  38. return fmt.Errorf("period type: %v", err)
  39. }
  40. // Identify common sample types
  41. numSampleTypes := len(profiles[0].SampleType)
  42. for _, p := range profiles[1:] {
  43. if numSampleTypes != len(p.SampleType) {
  44. return fmt.Errorf("inconsistent samples type count: %d != %d", numSampleTypes, len(p.SampleType))
  45. }
  46. }
  47. sampleType := make([]*profile.ValueType, numSampleTypes)
  48. for i := 0; i < numSampleTypes; i++ {
  49. sampleTypes := make([]*profile.ValueType, len(profiles))
  50. for j, p := range profiles {
  51. sampleTypes[j] = p.SampleType[i]
  52. }
  53. sampleType[i], err = CommonValueType(sampleTypes)
  54. if err != nil {
  55. return fmt.Errorf("sample types: %v", err)
  56. }
  57. }
  58. for _, p := range profiles {
  59. if p.PeriodType != nil && periodType != nil {
  60. period, _ := Scale(p.Period, p.PeriodType.Unit, periodType.Unit)
  61. p.Period, p.PeriodType.Unit = int64(period), periodType.Unit
  62. }
  63. ratios := make([]float64, len(p.SampleType))
  64. for i, st := range p.SampleType {
  65. if sampleType[i] == nil {
  66. ratios[i] = 1
  67. continue
  68. }
  69. ratios[i], _ = Scale(1, st.Unit, sampleType[i].Unit)
  70. p.SampleType[i].Unit = sampleType[i].Unit
  71. }
  72. if err := p.ScaleN(ratios); err != nil {
  73. return fmt.Errorf("scale: %v", err)
  74. }
  75. }
  76. return nil
  77. }
  78. // CommonValueType returns the finest type from a set of compatible
  79. // types.
  80. func CommonValueType(ts []*profile.ValueType) (*profile.ValueType, error) {
  81. if len(ts) <= 1 {
  82. return nil, nil
  83. }
  84. minType := ts[0]
  85. for _, t := range ts[1:] {
  86. if !compatibleValueTypes(minType, t) {
  87. return nil, fmt.Errorf("incompatible types: %v %v", *minType, *t)
  88. }
  89. if ratio, _ := Scale(1, t.Unit, minType.Unit); ratio < 1 {
  90. minType = t
  91. }
  92. }
  93. rcopy := *minType
  94. return &rcopy, nil
  95. }
  96. func compatibleValueTypes(v1, v2 *profile.ValueType) bool {
  97. if v1 == nil || v2 == nil {
  98. return true // No grounds to disqualify.
  99. }
  100. // Remove trailing 's' to permit minor mismatches.
  101. if t1, t2 := strings.TrimSuffix(v1.Type, "s"), strings.TrimSuffix(v2.Type, "s"); t1 != t2 {
  102. return false
  103. }
  104. return v1.Unit == v2.Unit ||
  105. (isTimeUnit(v1.Unit) && isTimeUnit(v2.Unit)) ||
  106. (isMemoryUnit(v1.Unit) && isMemoryUnit(v2.Unit))
  107. }
  108. // Scale a measurement from an unit to a different unit and returns
  109. // the scaled value and the target unit. The returned target unit
  110. // will be empty if uninteresting (could be skipped).
  111. func Scale(value int64, fromUnit, toUnit string) (float64, string) {
  112. // Avoid infinite recursion on overflow.
  113. if value < 0 && -value > 0 {
  114. v, u := Scale(-value, fromUnit, toUnit)
  115. return -v, u
  116. }
  117. if m, u, ok := memoryLabel(value, fromUnit, toUnit); ok {
  118. return m, u
  119. }
  120. if t, u, ok := timeLabel(value, fromUnit, toUnit); ok {
  121. return t, u
  122. }
  123. // Skip non-interesting units.
  124. switch toUnit {
  125. case "count", "sample", "unit", "minimum", "auto":
  126. return float64(value), ""
  127. default:
  128. return float64(value), toUnit
  129. }
  130. }
  131. // Label returns the label used to describe a certain measurement.
  132. func Label(value int64, unit string) string {
  133. return ScaledLabel(value, unit, "auto")
  134. }
  135. // ScaledLabel scales the passed-in measurement (if necessary) and
  136. // returns the label used to describe a float measurement.
  137. func ScaledLabel(value int64, fromUnit, toUnit string) string {
  138. v, u := Scale(value, fromUnit, toUnit)
  139. sv := strings.TrimSuffix(fmt.Sprintf("%.2f", v), ".00")
  140. if sv == "0" || sv == "-0" {
  141. return "0"
  142. }
  143. return sv + u
  144. }
  145. // Percentage computes the percentage of total of a value, and encodes
  146. // it as a string. At least two digits of precision are printed.
  147. func Percentage(value, total int64) string {
  148. var ratio float64
  149. if total != 0 {
  150. ratio = math.Abs(float64(value)/float64(total)) * 100
  151. }
  152. switch {
  153. case math.Abs(ratio) >= 99.95 && math.Abs(ratio) <= 100.05:
  154. return " 100%"
  155. case math.Abs(ratio) >= 1.0:
  156. return fmt.Sprintf("%5.2f%%", ratio)
  157. default:
  158. return fmt.Sprintf("%5.2g%%", ratio)
  159. }
  160. }
  161. // isMemoryUnit returns whether a name is recognized as a memory size
  162. // unit.
  163. func isMemoryUnit(unit string) bool {
  164. switch strings.TrimSuffix(strings.ToLower(unit), "s") {
  165. case "byte", "b", "kilobyte", "kb", "megabyte", "mb", "gigabyte", "gb":
  166. return true
  167. }
  168. return false
  169. }
  170. func memoryLabel(value int64, fromUnit, toUnit string) (v float64, u string, ok bool) {
  171. fromUnit = strings.TrimSuffix(strings.ToLower(fromUnit), "s")
  172. toUnit = strings.TrimSuffix(strings.ToLower(toUnit), "s")
  173. switch fromUnit {
  174. case "byte", "b":
  175. case "kb", "kbyte", "kilobyte":
  176. value *= 1024
  177. case "mb", "mbyte", "megabyte":
  178. value *= 1024 * 1024
  179. case "gb", "gbyte", "gigabyte":
  180. value *= 1024 * 1024 * 1024
  181. case "tb", "tbyte", "terabyte":
  182. value *= 1024 * 1024 * 1024 * 1024
  183. case "pb", "pbyte", "petabyte":
  184. value *= 1024 * 1024 * 1024 * 1024 * 1024
  185. default:
  186. return 0, "", false
  187. }
  188. if toUnit == "minimum" || toUnit == "auto" {
  189. switch {
  190. case value < 1024:
  191. toUnit = "b"
  192. case value < 1024*1024:
  193. toUnit = "kb"
  194. case value < 1024*1024*1024:
  195. toUnit = "mb"
  196. case value < 1024*1024*1024*1024:
  197. toUnit = "gb"
  198. case value < 1024*1024*1024*1024*1024:
  199. toUnit = "tb"
  200. default:
  201. toUnit = "pb"
  202. }
  203. }
  204. var output float64
  205. switch toUnit {
  206. default:
  207. output, toUnit = float64(value), "B"
  208. case "kb", "kbyte", "kilobyte":
  209. output, toUnit = float64(value)/1024, "kB"
  210. case "mb", "mbyte", "megabyte":
  211. output, toUnit = float64(value)/(1024*1024), "MB"
  212. case "gb", "gbyte", "gigabyte":
  213. output, toUnit = float64(value)/(1024*1024*1024), "GB"
  214. case "tb", "tbyte", "terabyte":
  215. output, toUnit = float64(value)/(1024*1024*1024*1024), "TB"
  216. case "pb", "pbyte", "petabyte":
  217. output, toUnit = float64(value)/(1024*1024*1024*1024*1024), "PB"
  218. }
  219. return output, toUnit, true
  220. }
  221. // isTimeUnit returns whether a name is recognized as a time unit.
  222. func isTimeUnit(unit string) bool {
  223. unit = strings.ToLower(unit)
  224. if len(unit) > 2 {
  225. unit = strings.TrimSuffix(unit, "s")
  226. }
  227. switch unit {
  228. case "nanosecond", "ns", "microsecond", "millisecond", "ms", "s", "second", "sec", "hr", "day", "week", "year":
  229. return true
  230. }
  231. return false
  232. }
  233. func timeLabel(value int64, fromUnit, toUnit string) (v float64, u string, ok bool) {
  234. fromUnit = strings.ToLower(fromUnit)
  235. if len(fromUnit) > 2 {
  236. fromUnit = strings.TrimSuffix(fromUnit, "s")
  237. }
  238. toUnit = strings.ToLower(toUnit)
  239. if len(toUnit) > 2 {
  240. toUnit = strings.TrimSuffix(toUnit, "s")
  241. }
  242. var d time.Duration
  243. switch fromUnit {
  244. case "nanosecond", "ns":
  245. d = time.Duration(value) * time.Nanosecond
  246. case "microsecond":
  247. d = time.Duration(value) * time.Microsecond
  248. case "millisecond", "ms":
  249. d = time.Duration(value) * time.Millisecond
  250. case "second", "sec", "s":
  251. d = time.Duration(value) * time.Second
  252. case "cycle":
  253. return float64(value), "", true
  254. default:
  255. return 0, "", false
  256. }
  257. if toUnit == "minimum" || toUnit == "auto" {
  258. switch {
  259. case d < 1*time.Microsecond:
  260. toUnit = "ns"
  261. case d < 1*time.Millisecond:
  262. toUnit = "us"
  263. case d < 1*time.Second:
  264. toUnit = "ms"
  265. case d < 1*time.Minute:
  266. toUnit = "sec"
  267. case d < 1*time.Hour:
  268. toUnit = "min"
  269. case d < 24*time.Hour:
  270. toUnit = "hour"
  271. case d < 15*24*time.Hour:
  272. toUnit = "day"
  273. case d < 120*24*time.Hour:
  274. toUnit = "week"
  275. default:
  276. toUnit = "year"
  277. }
  278. }
  279. var output float64
  280. dd := float64(d)
  281. switch toUnit {
  282. case "ns", "nanosecond":
  283. output, toUnit = dd/float64(time.Nanosecond), "ns"
  284. case "us", "microsecond":
  285. output, toUnit = dd/float64(time.Microsecond), "us"
  286. case "ms", "millisecond":
  287. output, toUnit = dd/float64(time.Millisecond), "ms"
  288. case "min", "minute":
  289. output, toUnit = dd/float64(time.Minute), "mins"
  290. case "hour", "hr":
  291. output, toUnit = dd/float64(time.Hour), "hrs"
  292. case "day":
  293. output, toUnit = dd/float64(24*time.Hour), "days"
  294. case "week", "wk":
  295. output, toUnit = dd/float64(7*24*time.Hour), "wks"
  296. case "year", "yr":
  297. output, toUnit = dd/float64(365*24*time.Hour), "yrs"
  298. default:
  299. // "sec", "second", "s" handled by default case.
  300. output, toUnit = dd/float64(time.Second), "s"
  301. }
  302. return output, toUnit, true
  303. }