Kaynağa Gözat

优化函数写法,避免某些情况下阻塞卡死

niujiuru 3 saat önce
ebeveyn
işleme
b9649e2d63
1 değiştirilmiş dosya ile 9 ekleme ve 14 silme
  1. 9 14
      web-ui.service/page1_handler.go

+ 9 - 14
web-ui.service/page1_handler.go

@@ -11,6 +11,7 @@ import (
 	"strconv"
 	"strings"
 	"sync"
+	"syscall"
 	"time"
 )
 
@@ -218,27 +219,21 @@ func getMemInfo() (int, string, string) {
 }
 
 func getDiskInfo() (int, string, string) {
-	out, err := exec.Command("df", "-hP", "/").Output()
-	if err != nil {
+	var fsStat syscall.Statfs_t
+	if err := syscall.Statfs("/", &fsStat); err != nil {
 		return 0, noValue, noValue
 	}
 
-	lines := strings.Split(string(out), "\n")
-	if len(lines) < 2 {
-		return 0, noValue, noValue
-	}
+	bsize := uint64(fsStat.Bsize)
+	used := (fsStat.Blocks - fsStat.Bfree) * bsize
+	avail := fsStat.Bavail * bsize
+	total := fsStat.Blocks * bsize
 
-	f := strings.Fields(lines[1])
-	if len(f) < 5 {
+	if used+avail == 0 {
 		return 0, noValue, noValue
 	}
 
-	percent, err := strconv.Atoi(strings.TrimSuffix(f[4], "%"))
-	if err != nil {
-		percent = 0
-	}
-
-	return percent, f[2], f[1]
+	return int(used * 100 / (used + avail)), formatSize(used), formatSize(total)
 }
 
 func formatSize(size uint64) string {