/
sysstats_windows.go
112 lines (96 loc) · 2.74 KB
/
sysstats_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2020 Albert "Drauthius" Diserholt. All rights reserved.
// Licensed under the MIT License.
// +build windows
// Get system status from TypePerf (Windows edition)
package main
import (
"bufio"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
// Start TypePerf for the specified fields (counters), and feed the results to the specified channel.
func typeperf(interval uint, result chan []string, quit chan bool, fields []string) {
defer close(result)
fields = append(append(fields, "-si"), strconv.Itoa(int(interval)))
cmd := exec.Command("TypePerf", fields...)
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Println("Failed to connect stdout for TypePerf: ", err)
return
} else if err := cmd.Start(); err != nil {
log.Println("Failed to start TypePerf:", err)
return
}
defer cmd.Process.Kill()
reader := bufio.NewReader(stdout)
// Read the first empty line and header.
reader.ReadString('\n')
reader.ReadString('\n')
for {
select {
case <-quit:
return
default:
line, err := reader.ReadString('\n')
if err != nil {
log.Println("Read error from TypePerf:", err)
return
}
result <- strings.Split(strings.TrimSuffix(line, "\r\n"), ",")
}
}
}
// Get system statistics at the specified interval, rounded to whole seconds.
// This will get the current CPU, memory, swap (page file), and disk usage in fractions (0.0-1.0)
func SystemStats(interval time.Duration, results chan []float64, quit chan bool) {
defer close(results)
tp := make(chan []string, 5)
go typeperf(uint(interval.Seconds()), tp, quit, []string{
`\Processor(_Total)\% Processor Time`,
`\Memory\% Committed Bytes In Use`,
`\Paging file(_Total)\% Usage`,
`\PhysicalDisk(_Total)\% Disk Time`,
})
// System status will take a second to fill up. To avoid it feeling like lag, send an empty result directly.
results <- make([]float64, 4)
for {
select {
case fields, more := <-tp:
if !more {
return
}
values := make([]float64, 4)
for i, field := range fields {
if i != 0 { // First field is a timestamp
value, err := strconv.ParseFloat(strings.TrimRight(strings.TrimLeft(field, `"`), `"`), 64)
if err != nil {
log.Printf("Failed to parse field %d in TypePerf data: '%s'\n", i, field)
value = 0
}
values[i-1] = value / 100
if *gArgs.debug {
switch i {
case 1:
log.Println("CPU%: ", value)
case 2:
log.Println("Mem%: ", value)
case 3:
log.Println("Swap: ", value)
case 4:
log.Println("Disk: ", value)
}
}
}
}
results <- values
case <-time.After(10 * time.Second):
log.Println("TypePerf read timed out")
quit <- true // FIXME: This is not enough if the process has hung
}
}
}