File: resource_linux.go

package info (click to toggle)
witr 0.2.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 736 kB
  • sloc: sh: 79; makefile: 10
file content (180 lines) | stat: -rw-r--r-- 4,094 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//go:build linux

package proc

import (
	"context"
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"

	"github.com/pranshuparmar/witr/pkg/model"
)

// GetResourceContext returns resource usage context for a process
func GetResourceContext(pid int) *model.ResourceContext {
	ctx := &model.ResourceContext{}
	ctx.PreventsSleep = checkPreventsSleep(pid)
	ctx.ThermalState = getThermalState()
	ctx.AppNapped = getAppNapped(pid)
	ctx.EnergyImpact = GetEnergyImpact(pid)
	return ctx
}

// thermal zone info from /sys/class/thermal
func getThermalState() string {

	path := "/sys/class/thermal/thermal_zone0/temp"
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return ""
	}
	readText, err := os.ReadFile(path)
	if err != nil {
		return ""
	}
	tempstr := strings.TrimSpace(string(readText))
	temp, err := strconv.Atoi(tempstr)
	if err != nil {
		return ""
	}
	tempC := temp / 1000
	switch {
	case tempC > 90:
		return fmt.Sprintf("Critical thermal pressure %d", tempC)
	case tempC > 70:
		return fmt.Sprintf("High thermal pressure %d", tempC)
	case tempC > 60:
		return fmt.Sprintf("Warm thermal state %d", tempC)
	default:
		return fmt.Sprintf("Normal thermal state %d", tempC)
	}
}

// checkPreventsSleep checks if a process has sleep prevention assertions
func checkPreventsSleep(pid int) bool {
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	out, err := exec.CommandContext(ctx, "systemd-inhibit", "--list").Output()
	if err != nil {
		return false
	}
	pidStr := strconv.Itoa(pid)
	lines := strings.Split(string(out), "\n")
	for _, line := range lines {
		// Check if this line references our PID and is a sleep prevention assertion
		if !strings.Contains(line, pidStr) {
			continue
		}
		if strings.Contains(line, pidStr) {
			lower := strings.ToLower(line)
			if strings.Contains(lower, "sleep") ||
				strings.Contains(lower, "idle") ||
				strings.Contains(lower, "shutdown") {
				return true
			}
		}
	}
	return false
}

// detect if process is in a stopped/suspended state
func getAppNapped(pid int) bool {
	statFile := fmt.Sprintf("/proc/%d/stat", pid)
	data, err := os.ReadFile(statFile)
	if err != nil {
		return false
	}

	dataStr := string(data)
	lastParenIndex := strings.LastIndex(dataStr, ")")
	if lastParenIndex == -1 || lastParenIndex+2 >= len(dataStr) {
		return false
	}

	rest := dataStr[lastParenIndex+2:]
	fields := strings.Fields(rest)
	if len(fields) < 1 {
		return false
	}

	state := fields[0]
	return state == "T" || state == "t"
}

// GetEnergyImpact attempts to get energy impact for a process
func GetEnergyImpact(pid int, usePs ...bool) string {
	var cpu float64

	shouldUsePs := len(usePs) > 0 && usePs[0]

	if shouldUsePs {
		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
		defer cancel()

		out, err := exec.CommandContext(ctx, "ps", "-p", strconv.Itoa(pid), "-o", "pcpu=").Output()
		if err != nil {
			return ""
		}

		cpuStr := strings.TrimSpace(string(out))
		if cpuStr == "" {
			return ""
		}

		cpu, err = strconv.ParseFloat(cpuStr, 64)
		if err != nil {
			return ""
		}
	} else {
		// Use top (default)
		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
		defer cancel()

		out, err := exec.CommandContext(ctx, "top", "-b", "-n", "1", "-p", strconv.Itoa(pid)).Output()
		if err != nil {
			return ""
		}

		lines := strings.Split(string(out), "\n")
		found := false

		for _, line := range lines {
			if strings.Contains(line, strconv.Itoa(pid)) {
				fields := strings.Fields(line)
				// CPU% is generally the 9th field in top output
				// Output pattern: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
				if len(fields) >= 9 {
					cpuStr := strings.TrimSuffix(fields[8], "%")
					cpu, err = strconv.ParseFloat(cpuStr, 64)
					if err == nil {
						found = true
						break
					}
				}
			}
		}

		if !found {
			return ""
		}
	}

	switch {
	case cpu > 50:
		return "Very High"
	case cpu > 25:
		return "High"
	case cpu > 10:
		return "Medium"
	case cpu > 2:
		return "Low"
	case cpu > 0:
		return "Very Low"
	default:
		return ""
	}
}