main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/shirou/gopsutil/cpu"
  5. "github.com/shirou/gopsutil/disk"
  6. "github.com/shirou/gopsutil/mem"
  7. "github.com/shirou/gopsutil/process"
  8. "sort"
  9. "time"
  10. )
  11. type ProcessInfo struct {
  12. Pid int32 `json:"pid"`
  13. Name string `json:"name"`
  14. CpuUsage float64 `json:"cpuUsage"`
  15. MemUsage float32 `json:"memUsage"`
  16. }
  17. // GetCpuPercent cpu总占用率
  18. func GetCpuPercent() float64 {
  19. percent, _ := cpu.Percent(time.Second, false)
  20. return percent[0]
  21. }
  22. // GetMemoryPercent 内存总占用率
  23. func GetMemoryPercent() float64 {
  24. memory, _ := mem.VirtualMemory()
  25. return memory.UsedPercent
  26. }
  27. // GetDiskPercent 磁盘总占用率
  28. func GetDiskPercent() float64 {
  29. parts, _ := disk.Partitions(true)
  30. diskInfo, _ := disk.Usage(parts[0].Mountpoint)
  31. return diskInfo.UsedPercent
  32. }
  33. func GetTop10CpuAndMem() ([]ProcessInfo, []ProcessInfo) {
  34. // 获取所有进程的CPU占用率
  35. processes, err := process.Processes()
  36. if err != nil {
  37. //fmt.Println("Error:", err)
  38. return nil, nil
  39. }
  40. // 创建一个用于存储进程CPU占用率的映射
  41. cpuPercent := make(map[int32]float64)
  42. memPercent := make(map[int32]float32)
  43. // 获取每个进程的CPU占用率
  44. for _, p := range processes {
  45. pid := p.Pid
  46. cpuPercent[pid], err = p.CPUPercent()
  47. memPercent[pid], err = p.MemoryPercent()
  48. }
  49. // 根据CPU占用率对进程进行排序
  50. sortedPidsCpu := make([]int32, 0, len(cpuPercent))
  51. sortedPidsMem := make([]int32, 0, len(cpuPercent))
  52. for pid := range cpuPercent {
  53. sortedPidsCpu = append(sortedPidsCpu, pid)
  54. sortedPidsMem = append(sortedPidsMem, pid)
  55. }
  56. sort.Slice(sortedPidsCpu, func(i, j int) bool {
  57. return cpuPercent[sortedPidsCpu[i]] > cpuPercent[sortedPidsCpu[j]]
  58. })
  59. sort.Slice(sortedPidsMem, func(i, j int) bool {
  60. return memPercent[sortedPidsCpu[i]] > memPercent[sortedPidsCpu[j]]
  61. })
  62. // 输出前10个CPU占用率最高的进程名称
  63. var top10Cpu []ProcessInfo
  64. var top10Mem []ProcessInfo
  65. for i, pid := range sortedPidsCpu {
  66. if i >= 10 {
  67. break
  68. }
  69. p, err := process.NewProcess(pid)
  70. if err != nil {
  71. continue
  72. }
  73. name, _ := p.Name()
  74. top10Cpu = append(top10Cpu, ProcessInfo{
  75. Pid: pid,
  76. Name: name,
  77. CpuUsage: cpuPercent[pid],
  78. MemUsage: memPercent[pid],
  79. })
  80. }
  81. for i, pid := range sortedPidsMem {
  82. if i >= 10 {
  83. break
  84. }
  85. p, err := process.NewProcess(pid)
  86. if err != nil {
  87. continue
  88. }
  89. name, _ := p.Name()
  90. top10Mem = append(top10Mem, ProcessInfo{
  91. Pid: pid,
  92. Name: name,
  93. CpuUsage: cpuPercent[pid],
  94. MemUsage: memPercent[pid],
  95. })
  96. }
  97. return top10Cpu, top10Mem
  98. }
  99. func main() {
  100. fmt.Println("cpu总占用率为:", GetCpuPercent())
  101. fmt.Println("内存总占用率为:", GetMemoryPercent())
  102. fmt.Println("磁盘总占用率为:", GetDiskPercent())
  103. top10cpu, top10mem := GetTop10CpuAndMem()
  104. fmt.Println("top10cpu为:", top10cpu)
  105. fmt.Println("top10mem为:", top10mem)
  106. }