|
@@ -12,39 +12,66 @@ import (
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
-func GetTop10Cpu() []string {
|
|
|
|
|
|
+type ProcessInfo struct {
|
|
|
|
+ Pid int32 `json:"pid"`
|
|
|
|
+ Name string `json:"name"`
|
|
|
|
+ CpuUsage float64 `json:"cpuUsage"`
|
|
|
|
+ MemUsage float32 `json:"memUsage"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetTop10CpuAndMem() ([]ProcessInfo, []ProcessInfo) {
|
|
// 获取所有进程的CPU占用率
|
|
// 获取所有进程的CPU占用率
|
|
processes, err := process.Processes()
|
|
processes, err := process.Processes()
|
|
if err != nil {
|
|
if err != nil {
|
|
//fmt.Println("Error:", err)
|
|
//fmt.Println("Error:", err)
|
|
- return nil
|
|
|
|
|
|
+ return nil, nil
|
|
}
|
|
}
|
|
|
|
|
|
// 创建一个用于存储进程CPU占用率的映射
|
|
// 创建一个用于存储进程CPU占用率的映射
|
|
cpuPercent := make(map[int32]float64)
|
|
cpuPercent := make(map[int32]float64)
|
|
|
|
+ memPercent := make(map[int32]float32)
|
|
|
|
|
|
// 获取每个进程的CPU占用率
|
|
// 获取每个进程的CPU占用率
|
|
for _, p := range processes {
|
|
for _, p := range processes {
|
|
pid := p.Pid
|
|
pid := p.Pid
|
|
cpuPercent[pid], err = p.CPUPercent()
|
|
cpuPercent[pid], err = p.CPUPercent()
|
|
- if err != nil {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
|
|
+ memPercent[pid], err = p.MemoryPercent()
|
|
}
|
|
}
|
|
|
|
|
|
// 根据CPU占用率对进程进行排序
|
|
// 根据CPU占用率对进程进行排序
|
|
- sortedPids := make([]int32, 0, len(cpuPercent))
|
|
|
|
|
|
+ sortedPidsCpu := make([]int32, 0, len(cpuPercent))
|
|
|
|
+ sortedPidsMem := make([]int32, 0, len(cpuPercent))
|
|
for pid := range cpuPercent {
|
|
for pid := range cpuPercent {
|
|
- sortedPids = append(sortedPids, pid)
|
|
|
|
|
|
+ sortedPidsCpu = append(sortedPidsCpu, pid)
|
|
|
|
+ sortedPidsMem = append(sortedPidsMem, pid)
|
|
}
|
|
}
|
|
- sort.Slice(sortedPids, func(i, j int) bool {
|
|
|
|
- return cpuPercent[sortedPids[i]] > cpuPercent[sortedPids[j]]
|
|
|
|
|
|
+ sort.Slice(sortedPidsCpu, func(i, j int) bool {
|
|
|
|
+ return cpuPercent[sortedPidsCpu[i]] > cpuPercent[sortedPidsCpu[j]]
|
|
|
|
+ })
|
|
|
|
+ sort.Slice(sortedPidsMem, func(i, j int) bool {
|
|
|
|
+ return memPercent[sortedPidsCpu[i]] > memPercent[sortedPidsCpu[j]]
|
|
})
|
|
})
|
|
|
|
|
|
// 输出前10个CPU占用率最高的进程名称
|
|
// 输出前10个CPU占用率最高的进程名称
|
|
- //fmt.Println("Top 10 processes by CPU usage:")
|
|
|
|
- var top10 []string
|
|
|
|
- for i, pid := range sortedPids {
|
|
|
|
|
|
+ var top10Cpu []ProcessInfo
|
|
|
|
+ var top10Mem []ProcessInfo
|
|
|
|
+ for i, pid := range sortedPidsCpu {
|
|
|
|
+ if i >= 10 {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ p, err := process.NewProcess(pid)
|
|
|
|
+ if err != nil {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ name, _ := p.Name()
|
|
|
|
+ top10Cpu = append(top10Cpu, ProcessInfo{
|
|
|
|
+ Pid: pid,
|
|
|
|
+ Name: name,
|
|
|
|
+ CpuUsage: cpuPercent[pid],
|
|
|
|
+ MemUsage: memPercent[pid],
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ for i, pid := range sortedPidsMem {
|
|
if i >= 10 {
|
|
if i >= 10 {
|
|
break
|
|
break
|
|
}
|
|
}
|
|
@@ -53,10 +80,14 @@ func GetTop10Cpu() []string {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
name, _ := p.Name()
|
|
name, _ := p.Name()
|
|
- top10 = append(top10, name)
|
|
|
|
- //fmt.Printf("%d. %s (PID: %d, CPU Usage: %.2f%%)\n", i+1, name, pid, cpuPercent[pid])
|
|
|
|
|
|
+ top10Mem = append(top10Mem, ProcessInfo{
|
|
|
|
+ Pid: pid,
|
|
|
|
+ Name: name,
|
|
|
|
+ CpuUsage: cpuPercent[pid],
|
|
|
|
+ MemUsage: memPercent[pid],
|
|
|
|
+ })
|
|
}
|
|
}
|
|
- return top10
|
|
|
|
|
|
+ return top10Cpu, top10Mem
|
|
}
|
|
}
|
|
|
|
|
|
// cpu总占用率
|
|
// cpu总占用率
|