-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
607 lines (500 loc) · 16.7 KB
/
extension.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* SPDX-FileCopyrightText: 2023 Florian Müllner <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import GTop from 'gi://GTop';
import Pango from 'gi://Pango';
import Shell from 'gi://Shell';
import St from 'gi://St';
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import ProccessRun from './proccessRun.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
const THRESHOLD_HIGH = 0.80;
// adapted from load-graph.cpp in gnome-system-monitor
/**
* @param {string} str
* @returns {number}
*/
function strHash(str) {
let hash = 0xcbf29ce484222325n;
for (const c of str)
hash = (hash * 0x00000100000001B3n) ^ BigInt(c.codePointAt(0));
return hash;
}
class StatSection extends St.BoxLayout {
static {
GObject.registerClass(this);
}
constructor(iconName, accessibleName) {
super({
style_class: 'system-monitor-stat-section',
accessibleName,
});
const ext = Extension.lookupByURL(import.meta.url);
const file =
ext.dir.resolve_relative_path(`icons/${iconName}.svg`);
this._icon = new St.Icon({
style_class: 'system-monitor-stat-section-icon',
gicon: new Gio.FileIcon({ file }),
});
this.add_child(this._icon);
this.label = new St.Label({
style_class: 'system-monitor-stat-section-label',
y_align: Clutter.ActorAlign.CENTER,
});
this.label.clutter_text.set({
ellipsize: Pango.EllipsizeMode.NONE,
x_align: Clutter.ActorAlign.CENTER,
});
this.add_child(this.label);
this.connect('destroy', () => this._clearTimeout());
this.connect('notify::visible', () => this._sync());
this._sync();
}
_ensureTimeout() {
if (this._updateId)
return;
this._updateId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1,
() => {
this._update();
return GLib.SOURCE_CONTINUE;
});
}
_clearTimeout() {
if (this._updateId)
GLib.source_remove(this._updateId);
delete this._updateId;
}
_sync() {
if (this.visible)
this._ensureTimeout();
else
this._clearTimeout();
if (this.visible)
this._update();
}
_update() {
}
}
class LoadStatSection extends StatSection {
static {
GObject.registerClass(this);
}
#formatter = new Intl.NumberFormat(undefined, {
style: 'percent',
});
_getLoadValue() {
}
_update() {
const load = this._getLoadValue();
this.label.text = this.#formatter.format(load);
if (load >= THRESHOLD_HIGH)
this.add_style_class_name('high-usage');
else
this.remove_style_class_name('high-usage');
}
}
class CpuSection extends StatSection {
static {
GObject.registerClass(this);
}
loadFormatter = new Intl.NumberFormat(undefined, {
style: 'percent',
});
tempFormatter = new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'celsius',
});
#sensors = new ProccessRun('sensors', ['-A', '-j']);
#prevCpu = new GTop.glibtop_cpu();
constructor() {
super('processor-symbolic', _('CPU stats'));
}
_getLoadValue() {
const cpu = new GTop.glibtop_cpu();
GTop.glibtop_get_cpu(cpu);
const total = cpu.total - this.#prevCpu.total;
const user = cpu.user - this.#prevCpu.user;
const sys = cpu.sys - this.#prevCpu.sys;
const nice = cpu.nice - this.#prevCpu.nice;
this.#prevCpu = cpu;
return (user + sys + nice) / Math.max(total, 1.0);
}
_getTempValue(stdout) {
if (stdout == null)
return 0;
const coreFilter = /^Core \d+/
const sensorFilter = /^temp\d+_input/
let data = JSON.parse(stdout);
let temps = [];
for (var chipset in data) {
if (!data.hasOwnProperty(chipset)) {
continue;
}
let chipsetSensors = data[chipset]
for (var sensor in chipsetSensors) {
if (!chipsetSensors.hasOwnProperty(sensor) || !coreFilter.test(sensor)) {
continue;
}
let fields = chipsetSensors[sensor];
for (var key in fields) {
if (fields.hasOwnProperty(key) && sensorFilter.test(key)) {
temps.push(parseFloat(fields[key]));
break;
}
}
}
}
if (temps.length == 0) {
logError('No temps were parsed!');
return 0;
}
return Math.round(temps.reduce((acc, val) => acc + val, 0) / temps.length);
}
_update() {
this.#sensors
.run()
.then(([stdout, _]) => {
const load = this._getLoadValue();
const temp = this._getTempValue(stdout);
this.label.text = ` ${this.loadFormatter.format(load)} ${this.tempFormatter.format(temp)}`;
})
.catch((reason) => {
logError(reason);
});
}
}
class GpuSection extends StatSection {
static {
GObject.registerClass(this);
}
loadFormatter = new Intl.NumberFormat(undefined, {
style: 'percent',
});
tempFormatter = new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'celsius',
});
#smi = new ProccessRun('nvidia-smi', ['--query-gpu=temperature.gpu,utilization.gpu', '--format=csv,noheader']);
constructor() {
super('gpu-symbolic', _('GPU stats'));
}
_getTempLoadValue(stdout) {
if (stdout == null)
return 0, 0;
let [temp, load] = stdout.split('\n')[0].split(', ');
return [parseInt(temp), parseInt(load) / 100]
}
_update() {
this.#smi
.run()
.then(([stdout, _]) => {
const [temp, load] = this._getTempLoadValue(stdout);
this.label.text = ` ${this.loadFormatter.format(load)} ${this.tempFormatter.format(temp)}`;
})
.catch((reason) => {
logError(reason);
});
}
}
class MemSection extends LoadStatSection {
static {
GObject.registerClass(this);
}
constructor() {
super('memory-symbolic', _('Memory stats'));
}
_getLoadValue() {
const mem = new GTop.glibtop_mem();
GTop.glibtop_get_mem(mem);
const { user, total } = mem;
return user / Math.max(total, 1.0);
}
}
class SwapSection extends LoadStatSection {
static {
GObject.registerClass(this);
}
constructor() {
super('swap-symbolic', _('Swap stats'));
}
_getLoadValue() {
const swap = new GTop.glibtop_swap();
GTop.glibtop_get_swap(swap);
const { used, total } = swap;
return used / Math.max(total, 1.0);
}
}
class NetStatSection extends StatSection {
static {
GObject.registerClass(this);
}
#formats = [{
factor: 1000,
unitFactor: 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'kilobyte',
maximumFractionDigits: 1,
minimumFractionDigits: 1,
}),
}, {
factor: 1000 * 10,
unitFactor: 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'kilobyte',
maximumFractionDigits: 0,
}),
}, {
factor: 1000 * 1000,
unitFactor: 1000 * 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'megabyte',
maximumFractionDigits: 1,
minimumFractionDigits: 1,
}),
}, {
factor: 1000 * 1000 * 10,
unitFactor: 1000 * 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'megabyte',
maximumFractionDigits: 0,
}),
}, {
factor: 1000 * 1000 * 1000,
unitFactor: 1000 * 1000 * 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'gigabyte',
maximumFractionDigits: 1,
minimumFractionDigits: 1,
}),
}, {
factor: 1000 * 1000 * 1000 * 10,
unitFactor: 1000 * 1000 * 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'gigabyte',
maximumFractionDigits: 0,
}),
}, {
factor: 1000 * 1000 * 1000 * 1000,
unitFactor: 1000 * 1000 * 1000 * 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'terabyte',
maximumFractionDigits: 1,
minimumFractionDigits: 1,
}),
}, {
factor: 1000 * 1000 * 1000 * 1000 * 10,
unitFactor: 1000 * 1000 * 1000 * 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'terabyte',
maximumFractionDigits: 0,
}),
}, {
factor: 1000 * 1000 * 1000 * 1000 * 1000,
unitFactor: 1000 * 1000 * 1000 * 1000 * 1000,
formatter: new Intl.NumberFormat(undefined, {
style: 'unit',
unit: 'petabyte',
maximumFractionDigits: 1,
minimumFractionDigits: 1,
}),
}];
#lastBytes = 0;
#lastHash = 0;
#lastTime = 0;
_getBytes(_netload) {
}
_getFormat(bytes) {
for (let i = 1; i < this.#formats.length; i++) {
if (bytes < this.#formats.at(i).factor)
return this.#formats.at(i - 1);
}
return this.#formats.at(-1);
}
_update() {
const FLAG_LOOPBACK = 1 << 4; // GTop sucks
const netlist = new GTop.glibtop_netlist();
const ifnames = GTop.glibtop_get_netlist(netlist);
let bytes = 0;
let hash = 1n;
for (const ifname of ifnames) {
const netload = new GTop.glibtop_netload();
GTop.glibtop_get_netload(netload, ifname);
if (netload.if_flags & FLAG_LOOPBACK)
continue;
bytes += this._getBytes(netload);
hash += strHash(ifname);
}
const time = GLib.get_monotonic_time();
let dbytes = 0;
// Skip calculation if new data is less than old (interface
// removed, counters reset, ...) or if it is the first time
if (bytes >= this.#lastBytes &&
hash === this.#lastHash &&
this.#lastTime !== 0) {
const dtime = (time - this.#lastTime) / GLib.USEC_PER_SEC;
dbytes = (bytes - this.#lastBytes) / dtime;
}
this.#lastBytes = bytes;
this.#lastTime = time;
this.#lastHash = hash;
const { unitFactor, formatter } = this._getFormat(dbytes);
this.label.text = formatter.format(dbytes / unitFactor);
}
}
class UploadSection extends NetStatSection {
static {
GObject.registerClass(this);
}
constructor() {
super('upload-symbolic', _('Upload stats'));
}
_getBytes(netload) {
return netload.bytes_out;
}
}
class DownloadSection extends NetStatSection {
static {
GObject.registerClass(this);
}
constructor() {
super('download-symbolic', _('Download stats'));
}
_getBytes(netload) {
return netload.bytes_in;
}
}
class Indicator extends PanelMenu.Button {
static {
GObject.registerClass(this);
}
constructor(settings) {
super(0.5, _('System stats'));
this._settings = settings;
this.connect('destroy',
() => (this._settings = null));
const box = new St.BoxLayout({
styleClass: 'system-monitor-stat-sections',
});
this.add_child(box);
this._placeholder = new St.Icon({
styleClass: 'system-status-icon system-monitor-placeholder',
});
box.add_child(this._placeholder);
this._cpuSection = new CpuSection();
this._settings.bind('show-cpu',
this._cpuSection, 'visible',
Gio.SettingsBindFlags.GET);
box.add_child(this._cpuSection);
this._gpuSection = new GpuSection();
this._settings.bind('show-gpu',
this._gpuSection, 'visible',
Gio.SettingsBindFlags.GET);
box.add_child(this._gpuSection);
this._memSection = new MemSection();
this._settings.bind('show-memory',
this._memSection, 'visible',
Gio.SettingsBindFlags.GET);
box.add_child(this._memSection);
this._swapSection = new SwapSection();
this._settings.bind('show-swap',
this._swapSection, 'visible',
Gio.SettingsBindFlags.GET);
box.add_child(this._swapSection);
this._ulSection = new UploadSection();
this._settings.bind('show-upload',
this._ulSection, 'visible',
Gio.SettingsBindFlags.GET);
box.add_child(this._ulSection);
this._dlSection = new DownloadSection();
this._settings.bind('show-download',
this._dlSection, 'visible',
Gio.SettingsBindFlags.GET);
box.add_child(this._dlSection);
this.menu.addMenuItem(
new PopupMenu.PopupSeparatorMenuItem(_('Show')));
this._cpuItem = this.menu.addAction(_('CPU'),
() => this._toggleSettings('show-cpu'));
this._gpuItem = this.menu.addAction(_('GPU'),
() => this._toggleSettings('show-gpu'));
this._memItem = this.menu.addAction(_('Memory'),
() => this._toggleSettings('show-memory'));
this._swapItem = this.menu.addAction(_('Swap'),
() => this._toggleSettings('show-swap'));
this._ulItem = this.menu.addAction(_('Upload'),
() => this._toggleSettings('show-upload'));
this._dlItem = this.menu.addAction(_('Download'),
() => this._toggleSettings('show-download'));
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._appMenuItem = this.menu.addAction(_('Open System Monitor'),
() => this._openSystemMonitor());
const appSystem = Shell.AppSystem.get_default();
appSystem.connectObject('installed-changed',
() => this._updateSystemMonitorApp(), this);
this._updateSystemMonitorApp();
this._settings.connectObject('changed',
() => this._sync(), this);
this._sync();
}
_updateSystemMonitorApp() {
const appSystem = Shell.AppSystem.get_default();
this._systemMonitorApp =
appSystem.lookup_app('org.gnome.SystemMonitor.desktop');
this._placeholder.gicon = this._systemMonitorApp?.icon ?? null;
this.visible = this._systemMonitorApp != null;
}
_openSystemMonitor() {
this._systemMonitorApp.activate();
Main.overview.hide();
}
_toggleSettings(key) {
this._settings.set_boolean(key, !this._settings.get_boolean(key));
}
_sync() {
this._cpuItem.setOrnament(this._settings.get_boolean('show-cpu')
? PopupMenu.Ornament.CHECK
: PopupMenu.Ornament.NONE);
this._gpuItem.setOrnament(this._settings.get_boolean('show-gpu')
? PopupMenu.Ornament.CHECK
: PopupMenu.Ornament.NONE);
this._memItem.setOrnament(this._settings.get_boolean('show-memory')
? PopupMenu.Ornament.CHECK
: PopupMenu.Ornament.NONE);
this._swapItem.setOrnament(this._settings.get_boolean('show-swap')
? PopupMenu.Ornament.CHECK
: PopupMenu.Ornament.NONE);
this._ulItem.setOrnament(this._settings.get_boolean('show-upload')
? PopupMenu.Ornament.CHECK
: PopupMenu.Ornament.NONE);
this._dlItem.setOrnament(this._settings.get_boolean('show-download')
? PopupMenu.Ornament.CHECK
: PopupMenu.Ornament.NONE);
this._placeholder.visible =
this._settings.list_keys().every(key => !this._settings.get_boolean(key));
}
}
export default class SystemMonitorExtension extends Extension {
enable() {
this._indicator = new Indicator(this.getSettings());
Main.panel.addToStatusArea(this.uuid, this._indicator);
}
disable() {
this._indicator.destroy();
this._indicator = null;
}
}