瀏覽代碼

Updated usageinfo.go to support darwin env

AY 4 年之前
父節點
當前提交
5366c849ed
共有 1 個文件被更改,包括 59 次插入27 次删除
  1. 59 27
      mod/info/usageinfo/usageinfo.go

+ 59 - 27
mod/info/usageinfo/usageinfo.go

@@ -19,7 +19,7 @@ import (
 
 const query_cpuproc_command = "ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10"
 const query_freemem_command = "top -d1 | sed '4q;d' | awk '{print $(NF-1)}'"
-const query_freemem_command_darwin = "top -d | sed '4q;d' | awk '{print $(NF-1)}'"
+const query_freemem_command_darwin = "ps -A -o %mem | awk '{mem += $1} END {print mem}'"
 const query_phymem_command = "sysctl hw.physmem | awk '{print $NF}'"
 const query_phymem_command_darwin = "sysctl hw.memsize | awk '{print $NF}'"
 
@@ -166,15 +166,10 @@ func GetNumericRAMUsage() (int64, int64) {
 			return usedRam, totalRam
 		}
 
-	} else if runtime.GOOS == "freebsd" || runtime.GOOS == "darwin" {
+	} else if runtime.GOOS == "freebsd" {
 
 		// Get usused memory size (free)
-		var cmd *exec.Cmd
-		if runtime.GOOS == "freebsd" {
-			cmd = exec.Command("bash", "-c", query_freemem_command)
-		} else if runtime.GOOS == "darwin" {
-			cmd = exec.Command("bash", "-c", query_freemem_command_darwin)
-		}
+		cmd := exec.Command("bash", "-c", query_freemem_command)
 
 		freeMemByteArr, err := cmd.CombinedOutput()
 		if err != nil {
@@ -185,11 +180,7 @@ func GetNumericRAMUsage() (int64, int64) {
 		freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
 
 		// Get phy memory size
-		if runtime.GOOS == "freebsd" {
-			cmd = exec.Command("bash", "-c", query_phymem_command)
-		} else if runtime.GOOS == "darwin" {
-			cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
-		}
+		cmd = exec.Command("bash", "-c", query_phymem_command)
 		phyMemByteArr, err := cmd.CombinedOutput()
 		if err != nil {
 			return usedRam, totalRam
@@ -212,6 +203,29 @@ func GetNumericRAMUsage() (int64, int64) {
 		usedRam = int64(used)
 		log.Println(totalRam, usedRam)
 		return usedRam, totalRam
+	} else if runtime.GOOS == "darwin" {
+		cmd := exec.Command("bash", "-c", query_freemem_command_darwin)
+		freeMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam
+		}
+		cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
+		phyMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam
+		}
+
+		freeMem, err := strconv.ParseFloat(strings.TrimSpace(string(freeMemStr)), 10)
+		if err != nil {
+			return usedRam, totalRam
+		}
+		phyMem, err := strconv.ParseInt(strings.TrimSpace(string(phyMemStr)), 10, 64)
+		if err != nil {
+			return usedRam, totalRam
+		}
+		totalRam = phyMem
+		usedRam = int64(float64(phyMem) - float64(phyMem)*freeMem)
+		return usedRam, totalRam
 	}
 	return -1, -1
 }
@@ -279,15 +293,10 @@ func GetRAMUsage() (string, string, float64) {
 			return usedRam, totalRam, usedPercentage
 		}
 
-	} else if runtime.GOOS == "freebsd" || runtime.GOOS == "darwin" {
+	} else if runtime.GOOS == "freebsd" {
 
 		// Get usused memory size (free)
-		var cmd *exec.Cmd
-		if runtime.GOOS == "freebsd" {
-			cmd = exec.Command("bash", "-c", query_freemem_command)
-		} else if runtime.GOOS == "darwin" {
-			cmd = exec.Command("bash", "-c", query_freemem_command_darwin)
-		}
+		cmd := exec.Command("bash", "-c", query_freemem_command)
 		freeMemByteArr, err := cmd.CombinedOutput()
 		if err != nil {
 			return usedRam, totalRam, usedPercentage
@@ -297,11 +306,7 @@ func GetRAMUsage() (string, string, float64) {
 		freeMemSize, err := strconv.ParseFloat(strings.ReplaceAll(string(freeMemStr), "M", ""), 10)
 		log.Println("freeMem", freeMemStr)
 		// Get phy memory size
-		if runtime.GOOS == "freebsd" {
-			cmd = exec.Command("bash", "-c", query_phymem_command)
-		} else if runtime.GOOS == "darwin" {
-			cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
-		}
+		cmd = exec.Command("bash", "-c", query_phymem_command)
 		phyMemByteArr, err := cmd.CombinedOutput()
 		if err != nil {
 			return usedRam, totalRam, usedPercentage
@@ -309,7 +314,6 @@ func GetRAMUsage() (string, string, float64) {
 
 		phyMemStr := string(phyMemByteArr)
 		phyMemStr = strings.ReplaceAll(phyMemStr, "\n", "")
-		log.Println("phyMemStr", phyMemStr)
 
 		// phyMemSize in MB
 		phyMemSizeFloat, err := strconv.ParseFloat(phyMemStr, 10)
@@ -321,10 +325,38 @@ func GetRAMUsage() (string, string, float64) {
 		usedRAMSizeFloat := phyMemSizeFloat - freeMemSize
 		usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
 		usedRam = strconv.FormatFloat(usedRAMSizeFloat, 'f', -1, 64) + "MB"
-		log.Println("usedRam", usedRam)
 
 		usedPercentage = usedRAMSizeFloat / phyMemSizeFloat * 100
 
+		return usedRam, totalRam, usedPercentage
+	} else if runtime.GOOS == "darwin" {
+		cmd := exec.Command("bash", "-c", query_freemem_command_darwin)
+		freeMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		cmd = exec.Command("bash", "-c", query_phymem_command_darwin)
+		phyMemStr, err := cmd.CombinedOutput()
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		freeMemSizeFloat, err := strconv.ParseFloat(strings.TrimSpace(string(freeMemStr)), 10)
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		phyMemSizeFloat, err := strconv.ParseFloat(strings.TrimSpace(string(phyMemStr)), 10)
+		if err != nil {
+			return usedRam, totalRam, usedPercentage
+		}
+		phyMemSizeFloat = phyMemSizeFloat / 1048576
+		phyMemSizeFloat = math.Floor(phyMemSizeFloat)
+		totalRam = strconv.FormatFloat(phyMemSizeFloat, 'f', -1, 64) + "MB"
+
+		usedRAMSizeFloat := float64(phyMemSizeFloat) - float64(phyMemSizeFloat)*(1-(freeMemSizeFloat/100))
+		usedRAMSizeFloat = math.Floor(usedRAMSizeFloat)
+		usedRam = strconv.FormatFloat(usedRAMSizeFloat, 'f', -1, 64) + "MB"
+
+		usedPercentage = freeMemSizeFloat
 		return usedRam, totalRam, usedPercentage
 	}