Repository navigation
Expand file tree
/
Copy pathprobalance.py
More file actions
224 lines (195 loc) · 8.68 KB
/
Copy pathprobalance.py
File metadata and controls
224 lines (195 loc) · 8.68 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""ProBalance state machine: throttle CPU hogs, restore when calm."""
from __future__ import annotations
import logging
import os
import psutil
from dataclasses import dataclass, field
from typing import Optional
import utils
from process_info import (
ProcessIdentity,
ProcessInfo,
ProcessPolicyView,
ProcessSnapshot,
)
log = logging.getLogger(__name__)
@dataclass
class _ProcState:
state: str = "NORMAL" # "NORMAL" | "THROTTLED"
consecutive_high: float = 0.0 # seconds spent above threshold
consecutive_low: float = 0.0 # seconds spent below restore threshold
original_nice: Optional[int] = None
throttle_nice: Optional[int] = None
process_name: str = ""
class ProBalance:
"""Apply temporary nice throttling and retry restoration until it succeeds."""
def __init__(self, config: dict, log_callback=None):
self._cfg = config
self._log_callback = log_callback
self._states: dict[ProcessIdentity, _ProcState] = {}
def update_config(self, config: dict):
was_enabled = self._cfg.get("enabled", True)
self._cfg = config
if was_enabled and not config.get("enabled", True):
self._restore_all_throttled()
elif config.get("enabled", True):
self._restore_exempt_throttled()
@staticmethod
def _identity_is_current(identity: ProcessIdentity) -> bool:
"""Return whether PID still names the process that owns this state."""
# Zero is retained only for legacy/test mappings that lack create_time.
if identity.create_time == 0.0:
return True
try:
return psutil.Process(identity.pid).create_time() == identity.create_time
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
return False
def _restore_all_throttled(self):
"""Restore every process currently throttled by ProBalance."""
for identity, state in list(self._states.items()):
if state.state != "THROTTLED":
continue
if not self._identity_is_current(identity):
self._states.pop(identity, None)
continue
original_nice = state.original_nice
if original_nice is None:
original_nice = 0
if utils.set_nice(identity.pid, original_nice):
self._log(
f"[ProBalance] RESTORE pid={identity.pid} nice→{original_nice} "
"(ProBalance disabled)"
)
state.state = "NORMAL"
state.consecutive_high = 0.0
state.consecutive_low = 0.0
state.original_nice = original_nice
state.throttle_nice = None
def _restore_exempt_throttled(self):
"""Immediately restore processes newly covered by the exempt list."""
for identity, state in list(self._states.items()):
if state.state != "THROTTLED" or not self._is_exempt(state.process_name):
continue
if not self._identity_is_current(identity):
self._states.pop(identity, None)
continue
original_nice = state.original_nice if state.original_nice is not None else 0
if utils.set_nice(identity.pid, original_nice):
self._log(
f"[ProBalance] RESTORE {state.process_name}({identity.pid}) "
f"nice→{original_nice} (process exempted)"
)
self._states.pop(identity, None)
def set_log_callback(self, cb):
self._log_callback = cb
def forget_pid(self, pid: int):
"""Discard runtime state when a process exits or its PID is reused."""
self._states = {
identity: state
for identity, state in self._states.items()
if identity.pid != pid
}
def _log(self, msg: str):
log.info(msg)
if self._log_callback:
self._log_callback(msg)
def _is_exempt(self, name: str) -> bool:
patterns = self._cfg.get("exempt_patterns", [])
name_lower = name.lower()
return any(p.lower() in name_lower for p in patterns)
def tick(
self,
snapshot: list[ProcessInfo | ProcessSnapshot | ProcessPolicyView],
tick_seconds: float,
):
"""
Called every ProBalance update interval.
snapshot: list of dicts with keys: pid, name, cpu_percent, nice
tick_seconds: elapsed time since last tick
"""
if not self._cfg.get("enabled", True):
# update_config() restores immediately; retry here in case a process
# temporarily rejected that first attempt.
alive_identities = {ProcessIdentity.from_record(p) for p in snapshot}
self._states = {
identity: state
for identity, state in self._states.items()
if identity in alive_identities
}
self._restore_all_throttled()
return
threshold = self._cfg.get("cpu_threshold_percent", 85.0)
consec_threshold = self._cfg.get("consecutive_seconds", 3)
adjustment = self._cfg.get("nice_adjustment", 10)
nice_floor = self._cfg.get("nice_floor", 15)
restore_threshold = self._cfg.get("restore_threshold_percent", 40.0)
restore_hysteresis = self._cfg.get("restore_hysteresis_seconds", 5)
alive_identities = {ProcessIdentity.from_record(p) for p in snapshot}
# Clean up dead processes
dead = [identity for identity in self._states if identity not in alive_identities]
for identity in dead:
del self._states[identity]
for proc in snapshot:
pid = proc["pid"]
identity = ProcessIdentity.from_record(proc)
name = proc["name"]
cpu = proc.get("cpu_percent", 0.0)
current_nice = proc.get("nice", 0)
if pid == os.getpid():
continue
if self._is_exempt(name):
state = self._states.get(identity)
if state is not None:
state.process_name = name
if state.state == "THROTTLED":
self._restore_exempt_throttled()
continue
if identity not in self._states:
self._states[identity] = _ProcState(
original_nice=current_nice,
process_name=name,
)
state = self._states[identity]
state.process_name = name
if state.state == "NORMAL":
if cpu > threshold:
state.consecutive_high += tick_seconds
if state.consecutive_high >= consec_threshold:
# Throttle
new_nice = min(current_nice + adjustment, nice_floor)
state.original_nice = current_nice
if utils.set_nice(pid, new_nice):
state.state = "THROTTLED"
state.throttle_nice = new_nice
state.consecutive_high = 0.0
state.consecutive_low = 0.0
self._log(
f"[ProBalance] THROTTLE {name}({pid}) "
f"cpu={cpu:.1f}% nice {current_nice}→{new_nice}"
)
else:
state.consecutive_high = max(0.0, state.consecutive_high - tick_seconds)
elif state.state == "THROTTLED":
if cpu < restore_threshold:
state.consecutive_low += tick_seconds
if state.consecutive_low >= restore_hysteresis:
# Restore
orig = state.original_nice if state.original_nice is not None else 0
if utils.set_nice(pid, orig):
self._log(
f"[ProBalance] RESTORE {name}({pid}) "
f"cpu={cpu:.1f}% nice {current_nice}→{orig}"
)
state.state = "NORMAL"
state.consecutive_high = 0.0
state.consecutive_low = 0.0
state.original_nice = orig
state.throttle_nice = None
else:
state.consecutive_low = 0.0
def get_throttled_pids(self) -> set[int]:
return {
identity.pid
for identity, state in self._states.items()
if state.state == "THROTTLED"
}