forked from m1k1o/neko
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpu_linux.go
More file actions
147 lines (125 loc) · 3.59 KB
/
cpu_linux.go
File metadata and controls
147 lines (125 loc) · 3.59 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//go:build linux
package benchmarks
import (
"bufio"
"fmt"
"os"
"runtime"
"strconv"
"strings"
)
// CPUStats represents CPU usage statistics
type CPUStats struct {
User uint64
System uint64
Idle uint64
Total uint64
}
// GetProcessCPUStats retrieves CPU stats for the current process
func GetProcessCPUStats() (*CPUStats, error) {
// Read /proc/self/stat
data, err := os.ReadFile("/proc/self/stat")
if err != nil {
return nil, fmt.Errorf("failed to read /proc/self/stat: %w", err)
}
// Parse the stat file
// Fields: pid comm state ... utime stime ...
// utime is field 14 (index 13), stime is field 15 (index 14)
fields := strings.Fields(string(data))
if len(fields) < 15 {
return nil, fmt.Errorf("unexpected /proc/self/stat format")
}
utime, err := strconv.ParseUint(fields[13], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse utime: %w", err)
}
stime, err := strconv.ParseUint(fields[14], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse stime: %w", err)
}
return &CPUStats{
User: utime,
System: stime,
Idle: 0,
Total: utime + stime,
}, nil
}
// GetSystemCPUStats retrieves system-wide CPU stats
func GetSystemCPUStats() (*CPUStats, error) {
file, err := os.Open("/proc/stat")
if err != nil {
return nil, fmt.Errorf("failed to open /proc/stat: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
if !scanner.Scan() {
return nil, fmt.Errorf("failed to read /proc/stat")
}
line := scanner.Text()
if !strings.HasPrefix(line, "cpu ") {
return nil, fmt.Errorf("unexpected /proc/stat format")
}
// cpu user nice system idle iowait irq softirq ...
fields := strings.Fields(line)
if len(fields) < 5 {
return nil, fmt.Errorf("not enough fields in /proc/stat")
}
user, _ := strconv.ParseUint(fields[1], 10, 64)
nice, _ := strconv.ParseUint(fields[2], 10, 64)
system, _ := strconv.ParseUint(fields[3], 10, 64)
idle, _ := strconv.ParseUint(fields[4], 10, 64)
total := user + nice + system + idle
if len(fields) >= 8 {
iowait, _ := strconv.ParseUint(fields[5], 10, 64)
irq, _ := strconv.ParseUint(fields[6], 10, 64)
softirq, _ := strconv.ParseUint(fields[7], 10, 64)
total += iowait + irq + softirq
}
return &CPUStats{
User: user + nice,
System: system,
Idle: idle,
Total: total,
}, nil
}
// CalculateCPUPercent calculates CPU usage percentage from two snapshots
func CalculateCPUPercent(before, after *CPUStats) float64 {
if before == nil || after == nil {
return 0.0
}
deltaTotal := after.Total - before.Total
if deltaTotal == 0 {
return 0.0
}
deltaUsed := (after.User + after.System) - (before.User + before.System)
return (float64(deltaUsed) / float64(deltaTotal)) * 100.0
}
// GetProcessMemoryMB returns current process memory usage in MB
func GetProcessMemoryMB() float64 {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
return float64(memStats.Alloc) / 1024 / 1024
}
// GetProcessRSSMemoryMB returns RSS memory from /proc/self/status
func GetProcessRSSMemoryMB() (float64, error) {
file, err := os.Open("/proc/self/status")
if err != nil {
return 0, fmt.Errorf("failed to open /proc/self/status: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "VmRSS:") {
fields := strings.Fields(line)
if len(fields) >= 2 {
rssKB, err := strconv.ParseFloat(fields[1], 64)
if err != nil {
return 0, fmt.Errorf("failed to parse RSS: %w", err)
}
return rssKB / 1024, nil // Convert KB to MB
}
}
}
return 0, fmt.Errorf("VmRSS not found in /proc/self/status")
}