extends RefCounted class_name CpuCollector var _previous_total: int = 0 var _previous_idle: int = 0 var _has_previous: bool = false func collect() -> float: var stats: PackedStringArray = _read_stats() if stats.is_empty(): return 0.0 var total: int = 0 var idle: int = 0 for i in range(1, stats.size()): var val: int = stats[i].to_int() total += val if i == 4: # idle is the 4th token (index 4) idle = val if not _has_previous: _previous_total = total _previous_idle = idle _has_previous = true return 0.0 var total_delta: int = total - _previous_total var idle_delta: int = idle - _previous_idle _previous_total = total _previous_idle = idle if total_delta == 0: return 0.0 return 100.0 * (1.0 - float(idle_delta) / float(total_delta)) func _read_stats() -> PackedStringArray: var file := FileAccess.open("/proc/stat", FileAccess.READ) if file == null: return PackedStringArray() var line: String = file.get_line() if not line.begins_with("cpu "): return PackedStringArray() return line.split(" ", false)