github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/node_status_executor_windows.go (about) 1 // +build windows 2 3 package nodes 4 5 import ( 6 "context" 7 "github.com/shirou/gopsutil/v3/cpu" 8 "github.com/shirou/gopsutil/v3/mem" 9 "math" 10 "sync" 11 "time" 12 ) 13 14 type WindowsLoadValue struct { 15 Timestamp int64 16 Value int 17 } 18 19 var windowsLoadValues = []*WindowsLoadValue{} 20 var windowsLoadLocker = &sync.Mutex{} 21 22 // 更新内存 23 func (this *NodeStatusExecutor) updateMem(status *NodeStatus) { 24 stat, err := mem.VirtualMemory() 25 if err != nil { 26 status.Error = err.Error() 27 return 28 } 29 status.MemoryUsage = stat.UsedPercent 30 status.MemoryTotal = stat.Total 31 } 32 33 // 更新负载 34 func (this *NodeStatusExecutor) updateLoad(status *NodeStatus) { 35 timestamp := time.Now().Unix() 36 37 currentLoad := 0 38 info, err := cpu.ProcInfo() 39 if err == nil && len(info) > 0 && info[0].ProcessorQueueLength < 1000 { 40 currentLoad = int(info[0].ProcessorQueueLength) 41 } 42 43 // 删除15分钟之前的数据 44 windowsLoadLocker.Lock() 45 result := []*WindowsLoadValue{} 46 for _, v := range windowsLoadValues { 47 if timestamp-v.Timestamp > 15*60 { 48 continue 49 } 50 result = append(result, v) 51 } 52 result = append(result, &WindowsLoadValue{ 53 Timestamp: timestamp, 54 Value: currentLoad, 55 }) 56 windowsLoadValues = result 57 58 total1 := 0 59 count1 := 0 60 total5 := 0 61 count5 := 0 62 total15 := 0 63 count15 := 0 64 for _, v := range result { 65 if timestamp-v.Timestamp <= 60 { 66 total1 += v.Value 67 count1++ 68 } 69 70 if timestamp-v.Timestamp <= 300 { 71 total5 += v.Value 72 count5++ 73 } 74 75 total15 += v.Value 76 count15++ 77 } 78 79 load1 := float64(0) 80 load5 := float64(0) 81 load15 := float64(0) 82 if count1 > 0 { 83 load1 = math.Round(float64(total1*100)/float64(count1)) / 100 84 } 85 if count5 > 0 { 86 load5 = math.Round(float64(total5*100)/float64(count5)) / 100 87 } 88 if count15 > 0 { 89 load15 = math.Round(float64(total15*100)/float64(count15)) / 100 90 } 91 92 windowsLoadLocker.Unlock() 93 94 // 在老Windows上不显示错误 95 if err == context.DeadlineExceeded { 96 err = nil 97 } 98 status.Load1m = load1 99 status.Load5m = load5 100 status.Load15m = load15 101 }