LingxinMeng 1 năm trước cách đây
mục cha
commit
dadbc329f4
2 tập tin đã thay đổi với 53 bổ sung16 xóa
  1. 7 1
      aarch64/pjisuv/common/config/c_resource.go
  2. 46 15
      common/util/u_resource.go

+ 7 - 1
aarch64/pjisuv/common/config/c_resource.go

@@ -3,6 +3,7 @@ package config
 import (
 	"cicv-data-closedloop/common/config/c_log"
 	"cicv-data-closedloop/common/util"
+	"encoding/json"
 	"time"
 )
 
@@ -10,13 +11,18 @@ import (
 func SendResourceUsage() {
 	for {
 		time.Sleep(time.Duration(2) * time.Second)
+		top10Cpu, top10Mem := util.GetTop10CpuAndMem()
+		top10CpuJson, _ := json.MarshalIndent(top10Cpu, "", "    ")
+		top10MemJson, _ := json.MarshalIndent(top10Mem, "", "    ")
 		responseString, err := util.HttpPostJsonWithHeaders(
 			CloudConfig.Monitor.Url,
 			map[string]string{"Authorization": "U9yKpD6kZZDDe4LFKK6myAxBUT1XRrDM"},
 			map[string]string{
 				"totalCpuUsage":    util.ToString(util.GetCpuPercent()),
 				"totalMemoryUsage": util.ToString(util.GetMemoryPercent()),
-				"top10Process":     util.ToString(util.GetTop10Cpu()),
+				"top10Process":     string(top10CpuJson),
+				"top10Cpu":         string(top10CpuJson),
+				"top10Mem":         string(top10MemJson),
 				"deviceNumber":     LocalConfig.EquipmentNo,
 				"socIp":            LocalConfig.Node.Ip,
 			},

+ 46 - 15
common/util/u_resource.go

@@ -12,39 +12,66 @@ import (
 	"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占用率
 	processes, err := process.Processes()
 	if err != nil {
 		//fmt.Println("Error:", err)
-		return nil
+		return nil, nil
 	}
 
 	// 创建一个用于存储进程CPU占用率的映射
 	cpuPercent := make(map[int32]float64)
+	memPercent := make(map[int32]float32)
 
 	// 获取每个进程的CPU占用率
 	for _, p := range processes {
 		pid := p.Pid
 		cpuPercent[pid], err = p.CPUPercent()
-		if err != nil {
-			continue
-		}
+		memPercent[pid], err = p.MemoryPercent()
 	}
 
 	// 根据CPU占用率对进程进行排序
-	sortedPids := make([]int32, 0, len(cpuPercent))
+	sortedPidsCpu := make([]int32, 0, len(cpuPercent))
+	sortedPidsMem := make([]int32, 0, len(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占用率最高的进程名称
-	//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 {
 			break
 		}
@@ -53,10 +80,14 @@ func GetTop10Cpu() []string {
 			continue
 		}
 		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总占用率