Skip to content

Commit e805991

Browse files
authored
Switch to gtop for kernel stats rather than hard-coded kernel areas- closes BuddiesOfBudgie#890 (BuddiesOfBudgie#891)
1 parent df9954f commit e805991

5 files changed

Lines changed: 80 additions & 108 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
'pkgconfig(gtk-layer-shell-0)' \
2626
'pkgconfig(ibus-1.0)' \
2727
'pkgconfig(libcanberra)' \
28+
'pkgconfig(libgtop-2.0)' \
2829
'pkgconfig(libnotify)' \
2930
'pkgconfig(libpeas-2)' \
3031
'pkgconfig(libpulse)' \

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dep_vala = dependency('vapigen', version: '>= 0.52.5')
3737
dep_gst = dependency('gstreamer-1.0')
3838
dep_cairo = dependency('cairo')
3939
dep_gtk_layer_shell = dependency('gtk-layer-shell-0', version: '>= 0.8.0')
40+
dep_libgtop = dependency('libgtop-2.0')
4041

4142
# Needed for Budgie Menu
4243
dep_cairo = dependency('cairo', version: '>= 1.15.10')

src/raven/widgets/usage-monitor/meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ raven_plugin_usage_monitor_deps = [
2626
libravenplugin_vapi,
2727
dep_gtk3,
2828
dep_peas,
29+
dep_libgtop,
2930
link_libravenplugin
3031
]
3132

3233
shared_library(
3334
'org.buddiesofbudgie.budgie-desktop.raven.widget.UsageMonitor',
3435
raven_plugin_usage_monitor_sources,
3536
dependencies: raven_plugin_usage_monitor_deps,
37+
vala_args: [
38+
'--vapidir', top_vapidir,
39+
],
3640
install: true,
3741
install_dir: raven_plugin_usage_monitor_dir,
3842
)

src/raven/widgets/usage-monitor/usage_monitor.vala

Lines changed: 31 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* (at your option) any later version.
1010
*/
1111

12+
using GTop;
13+
1214
public class UsageMonitorRavenPlugin : Budgie.RavenPlugin, Peas.ExtensionBase {
1315
public Budgie.RavenWidget new_widget_instance(string uuid, GLib.Settings? settings) {
1416
return new UsageMonitorRavenWidget(uuid, settings);
@@ -24,13 +26,16 @@ public class UsageMonitorRavenWidget : Budgie.RavenWidget {
2426
private UsageMonitorRow? cpu = null;
2527
private UsageMonitorRow? ram = null;
2628
private UsageMonitorRow? swap = null;
27-
private ProcStatContents? prev = null;
29+
30+
private GTop.Cpu? prev_cpu = null;
2831

2932
private uint timeout_id = 0;
3033

3134
public UsageMonitorRavenWidget(string uuid, GLib.Settings? settings) {
3235
initialize(uuid, settings);
3336

37+
GTop.init();
38+
3439
var main_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
3540
add(main_box);
3641

@@ -127,111 +132,42 @@ public class UsageMonitorRavenWidget : Budgie.RavenWidget {
127132
}
128133

129134
private void update_cpu() {
130-
ProcStatContents? stat = read_proc_stat();
131-
132-
if (prev != null && stat != null) {
133-
float total_cpu_usage = (float) (stat.busy - prev.busy) / (float) (stat.total - prev.total);
134-
cpu.update(total_cpu_usage);
135+
GTop.Cpu current_cpu;
136+
GTop.get_cpu(out current_cpu);
137+
138+
if (prev_cpu != null) {
139+
uint64 total_delta = current_cpu.total - prev_cpu.total;
140+
if (total_delta > 0) {
141+
// idle + iowait = time the CPU was not doing useful work
142+
uint64 idle_delta = (current_cpu.idle + current_cpu.iowait) -
143+
(prev_cpu.idle + prev_cpu.iowait);
144+
float usage = (float)(total_delta - idle_delta) / (float)total_delta;
145+
cpu.update(usage.clamp(0.0f, 1.0f));
146+
}
135147
}
136148

137-
prev = stat;
149+
prev_cpu = current_cpu;
138150
}
139151

140152
private void update_ram_and_swap() {
141-
ProcMeminfoContents? meminfo = read_proc_meminfo();
142-
143-
if (meminfo == null) {
144-
ram.hide();
145-
swap.hide();
146-
return;
147-
}
148-
149-
if (meminfo.swap_total > 0 && !swap.stay_hidden) {
150-
var swap_used = meminfo.swap_total - meminfo.swap_free - meminfo.swap_cached;
151-
swap.update((float) swap_used / (float) meminfo.swap_total);
152-
} else {
153-
swap.hide();
154-
}
153+
GTop.Mem mem;
154+
GTop.get_mem(out mem);
155155

156-
if (meminfo.mem_total > 0) {
157-
var mem_used = meminfo.mem_total - meminfo.mem_available;
158-
ram.update((float) mem_used / (float) meminfo.mem_total);
156+
if (mem.total > 0) {
157+
float mem_fraction = (float) mem.used / (float) mem.total;
158+
ram.update(mem_fraction.clamp(0.0f, 1.0f));
159159
} else {
160160
ram.hide();
161161
}
162-
}
163162

164-
private ProcStatContents? read_proc_stat() {
165-
var stat_file = File.new_for_path("/proc/stat");
166-
if (!stat_file.query_exists()) return null;
163+
GTop.Swap swap_info;
164+
GTop.get_swap(out swap_info);
167165

168-
try {
169-
var input_stream = new DataInputStream(stat_file.read());
170-
171-
string line;
172-
while ((line = input_stream.read_line()) != null) {
173-
if (!line.has_prefix("cpu ")) {
174-
continue;
175-
}
176-
177-
ulong user = 0;
178-
ulong nice = 0;
179-
ulong system = 0;
180-
ulong idle = 0;
181-
ulong iowait = 0;
182-
ulong irq = 0;
183-
ulong softirq = 0;
184-
185-
int read = line.scanf(
186-
"%*s %lu %lu %lu %lu %lu %lu %lu",
187-
&user, &nice, &system, &idle, &iowait, &irq, &softirq
188-
);
189-
190-
if (read == 7) {
191-
ProcStatContents? contents = ProcStatContents();
192-
contents.total = user + nice + system + idle + iowait + irq + softirq;
193-
contents.busy = contents.total - idle - iowait;
194-
return contents;
195-
}
196-
}
197-
} catch (Error e) {}
198-
199-
return null;
200-
}
201-
202-
private ProcMeminfoContents? read_proc_meminfo() {
203-
var meminfo_file = File.new_for_path("/proc/meminfo");
204-
if (!meminfo_file.query_exists()) {
205-
return null;
206-
}
207-
208-
try {
209-
var input_stream = new DataInputStream(meminfo_file.read());
210-
211-
var contents = ProcMeminfoContents();
212-
string line;
213-
while ((line = input_stream.read_line()) != null) {
214-
string label = "";
215-
ulong value = -1;
216-
217-
line.scanf("%s %lu", label, &value);
218-
219-
if (label == "MemTotal:") {
220-
contents.mem_total = value;
221-
} else if (label == "MemAvailable:") {
222-
contents.mem_available = value;
223-
} else if (label == "SwapTotal:") {
224-
contents.swap_total = value;
225-
} else if (label == "SwapFree:") {
226-
contents.swap_free = value;
227-
} else if (label == "SwapCached:") {
228-
contents.swap_cached = value;
229-
}
230-
}
231-
232-
return contents;
233-
} catch (Error e) {
234-
return null;
166+
if (swap_info.total > 0 && !swap.stay_hidden) {
167+
float swap_fraction = (float) swap_info.used / (float) swap_info.total;
168+
swap.update(swap_fraction.clamp(0.0f, 1.0f));
169+
} else {
170+
swap.hide();
235171
}
236172
}
237173

@@ -250,19 +186,6 @@ public class UsageMonitorRavenWidgetSettings : Gtk.Grid {
250186
}
251187
}
252188

253-
private struct ProcMeminfoContents {
254-
ulong mem_total;
255-
ulong mem_available;
256-
ulong swap_total;
257-
ulong swap_free;
258-
ulong swap_cached;
259-
}
260-
261-
private struct ProcStatContents {
262-
ulong total;
263-
ulong busy;
264-
}
265-
266189
private class UsageMonitorRow {
267190
public Gtk.Label label;
268191
public Gtk.LevelBar bar;

vapi/libgtop-2.0.vapi

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[CCode (cprefix = "GTop", lower_case_cprefix = "glibtop_", cheader_filename = "glibtop.h,glibtop/cpu.h,glibtop/mem.h,glibtop/swap.h")]
2+
namespace GTop {
3+
[CCode (cname = "glibtop_init")]
4+
public static void init();
5+
6+
[CCode (cname = "glibtop_cpu", destroy_function = "", has_copy_function = false)]
7+
public struct Cpu {
8+
public uint64 total;
9+
public uint64 user;
10+
public uint64 nice;
11+
public uint64 sys;
12+
public uint64 idle;
13+
public uint64 iowait;
14+
public uint64 irq;
15+
public uint64 softirq;
16+
}
17+
18+
[CCode (cname = "glibtop_get_cpu")]
19+
public static void get_cpu(out Cpu buf);
20+
21+
[CCode (cname = "glibtop_mem", destroy_function = "", has_copy_function = false)]
22+
public struct Mem {
23+
public uint64 total;
24+
public uint64 used;
25+
public uint64 free;
26+
public uint64 shared;
27+
public uint64 buffer;
28+
public uint64 cached;
29+
}
30+
31+
[CCode (cname = "glibtop_get_mem")]
32+
public static void get_mem(out Mem buf);
33+
34+
[CCode (cname = "glibtop_swap", destroy_function = "", has_copy_function = false)]
35+
public struct Swap {
36+
public uint64 total;
37+
public uint64 used;
38+
public uint64 free;
39+
}
40+
41+
[CCode (cname = "glibtop_get_swap")]
42+
public static void get_swap(out Swap buf);
43+
}

0 commit comments

Comments
 (0)