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.

84 lines
3.0 KiB

  1. // Copyright (c) 2015-2024 MinIO, Inc.
  2. //
  3. // # This file is part of MinIO Object Storage stack
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. package cmd
  18. import (
  19. "context"
  20. "math"
  21. )
  22. const (
  23. sysCPUAvgIdle = "avg_idle"
  24. sysCPUAvgIOWait = "avg_iowait"
  25. sysCPULoad = "load"
  26. sysCPULoadPerc = "load_perc"
  27. sysCPUNice = "nice"
  28. sysCPUSteal = "steal"
  29. sysCPUSystem = "system"
  30. sysCPUUser = "user"
  31. )
  32. var (
  33. sysCPUAvgIdleMD = NewGaugeMD(sysCPUAvgIdle, "Average CPU idle time")
  34. sysCPUAvgIOWaitMD = NewGaugeMD(sysCPUAvgIOWait, "Average CPU IOWait time")
  35. sysCPULoadMD = NewGaugeMD(sysCPULoad, "CPU load average 1min")
  36. sysCPULoadPercMD = NewGaugeMD(sysCPULoadPerc, "CPU load average 1min (percentage)")
  37. sysCPUNiceMD = NewGaugeMD(sysCPUNice, "CPU nice time")
  38. sysCPUStealMD = NewGaugeMD(sysCPUSteal, "CPU steal time")
  39. sysCPUSystemMD = NewGaugeMD(sysCPUSystem, "CPU system time")
  40. sysCPUUserMD = NewGaugeMD(sysCPUUser, "CPU user time")
  41. )
  42. // loadCPUMetrics - `MetricsLoaderFn` for system CPU metrics.
  43. func loadCPUMetrics(ctx context.Context, m MetricValues, c *metricsCache) error {
  44. cpuMetrics, _ := c.cpuMetrics.Get()
  45. if cpuMetrics.LoadStat != nil {
  46. m.Set(sysCPULoad, cpuMetrics.LoadStat.Load1)
  47. perc := cpuMetrics.LoadStat.Load1 * 100 / float64(cpuMetrics.CPUCount)
  48. m.Set(sysCPULoadPerc, math.Round(perc*100)/100)
  49. }
  50. ts := cpuMetrics.TimesStat
  51. if ts != nil {
  52. tot := ts.User + ts.System + ts.Idle + ts.Iowait + ts.Nice + ts.Steal
  53. cpuUserVal := math.Round(ts.User/tot*100*100) / 100
  54. m.Set(sysCPUUser, cpuUserVal)
  55. cpuSystemVal := math.Round(ts.System/tot*100*100) / 100
  56. m.Set(sysCPUSystem, cpuSystemVal)
  57. cpuNiceVal := math.Round(ts.Nice/tot*100*100) / 100
  58. m.Set(sysCPUNice, cpuNiceVal)
  59. cpuStealVal := math.Round(ts.Steal/tot*100*100) / 100
  60. m.Set(sysCPUSteal, cpuStealVal)
  61. }
  62. // metrics-resource.go runs a job to collect resource metrics including their Avg values and
  63. // stores them in resourceMetricsMap. We can use it to get the Avg values of CPU idle and IOWait.
  64. cpuResourceMetrics, found := resourceMetricsMap[cpuSubsystem]
  65. if found {
  66. if cpuIdleMetric, ok := cpuResourceMetrics[getResourceKey(cpuIdle, nil)]; ok {
  67. avgVal := math.Round(cpuIdleMetric.Avg*100) / 100
  68. m.Set(sysCPUAvgIdle, avgVal)
  69. }
  70. if cpuIOWaitMetric, ok := cpuResourceMetrics[getResourceKey(cpuIOWait, nil)]; ok {
  71. avgVal := math.Round(cpuIOWaitMetric.Avg*100) / 100
  72. m.Set(sysCPUAvgIOWait, avgVal)
  73. }
  74. }
  75. return nil
  76. }