99 * (at your option) any later version.
1010 */
1111
12+ using GTop ;
13+
1214public 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-
266189private class UsageMonitorRow {
267190 public Gtk . Label label;
268191 public Gtk . LevelBar bar;
0 commit comments