Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions openpilot/common/linux.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class LinuxSystemStats:
def __init__(self) -> None:
self._last_cpu_times = self._read_cpu_times()

@staticmethod
def _read_cpu_times() -> dict[int, tuple[int, int]]:
cpu_times = {}
with open('/proc/stat') as f:
for line in f:
name, *values = line.split()
if not name.startswith('cpu') or not name[3:].isdigit():
continue

times = [int(value) for value in values]
idle = sum(times[3:5])
total = sum(times[:8])
cpu_times[int(name[3:])] = (idle, total)
return cpu_times

def cpu_usage_percent(self) -> list[float]:
current_cpu_times = self._read_cpu_times()
usage = []
for cpu, (idle, total) in sorted(current_cpu_times.items()):
last_times = self._last_cpu_times.get(cpu)
if last_times is None:
usage.append(0.)
continue

last_idle, last_total = last_times
idle_delta = idle - last_idle
total_delta = total - last_total
if idle_delta < 0 or total_delta <= 0:
usage.append(0.)
else:
usage.append(max(0., min(100., 100. * (total_delta - idle_delta) / total_delta)))

self._last_cpu_times = current_cpu_times
return usage

@staticmethod
def memory_usage_percent() -> float:
memory = {}
with open('/proc/meminfo') as f:
for line in f:
key, value, *_ = line.split()
if key in ('MemTotal:', 'MemAvailable:'):
memory[key] = int(value)

total = memory['MemTotal:']
return max(0., min(100., 100. * (total - memory['MemAvailable:']) / total))
8 changes: 4 additions & 4 deletions openpilot/system/hardware/hardwared.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import time
from collections import OrderedDict, namedtuple

import psutil

import openpilot.cereal.messaging as messaging
from openpilot.cereal import log
from openpilot.cereal.services import SERVICE_LIST
Expand All @@ -19,6 +17,7 @@
from openpilot.selfdrive.selfdrived.alertmanager import set_offroad_alert
from openpilot.common.hardware import HARDWARE, TICI, PC
from openpilot.common.hardware.usb import get_usb_state, set_usb_state
from openpilot.common.linux import LinuxSystemStats
from openpilot.system.loggerd.config import get_available_percent
from openpilot.common.swaglog import cloudlog
from openpilot.system.hardware.power_monitoring import PowerMonitoring
Expand Down Expand Up @@ -142,6 +141,7 @@ def hw_state_thread(end_event, hw_queue):


def hardware_thread(end_event, hw_queue) -> None:
system_stats = LinuxSystemStats()
pm = messaging.PubMaster(['deviceState'])
sm = messaging.SubMaster(["peripheralState", "gpsLocationExternal", "selfdriveState", "pandaStates"], poll="pandaStates")

Expand Down Expand Up @@ -232,9 +232,9 @@ def hardware_thread(end_event, hw_queue) -> None:
pass

msg.deviceState.freeSpacePercent = get_available_percent(default=100.0)
msg.deviceState.memoryUsagePercent = int(round(psutil.virtual_memory().percent))
msg.deviceState.memoryUsagePercent = int(round(system_stats.memory_usage_percent()))
msg.deviceState.gpuUsagePercent = int(round(HARDWARE.get_gpu_usage_percent()))
online_cpu_usage = [int(round(n)) for n in psutil.cpu_percent(percpu=True)]
online_cpu_usage = [int(round(n)) for n in system_stats.cpu_usage_percent()]
offline_cpu_usage = [0., ] * (len(msg.deviceState.cpuTempC) - len(online_cpu_usage))
msg.deviceState.cpuUsagePercent = online_cpu_usage + offline_cpu_usage

Expand Down
6 changes: 0 additions & 6 deletions openpilot/system/updated/updated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
import datetime
import subprocess
import psutil
import shutil
import signal
import fcntl
Expand Down Expand Up @@ -427,11 +426,6 @@ def main() -> None:
except OSError as e:
raise RuntimeError("couldn't get overlay lock; is another instance running?") from e

# Set low io priority
proc = psutil.Process()
if psutil.LINUX:
proc.ionice(psutil.IOPRIO_CLASS_BE, value=7)

# Check if we just performed an update
if Path(os.path.join(STAGING_ROOT, "old_openpilot")).is_dir():
cloudlog.event("update installed")
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ dependencies = [
"inputs",

# these should be removed
"psutil",
"setproctitle",

# logreader
Expand Down
120 changes: 0 additions & 120 deletions tools/scripts/cpu_usage_stat.py

This file was deleted.

18 changes: 0 additions & 18 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading