| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2023 Florian Müllner <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
import Clutter from 'gi://Clutter'; |
| 8 |
import Gio from 'gi://Gio'; |
| 9 |
import GLib from 'gi://GLib'; |
| 10 |
import GObject from 'gi://GObject'; |
| 11 |
import GTop from 'gi://GTop'; |
| 12 |
import Pango from 'gi://Pango'; |
| 13 |
import Shell from 'gi://Shell'; |
| 14 |
import St from 'gi://St'; |
| 15 |
|
| 16 |
import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js'; |
| 17 |
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js'; |
| 18 |
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js'; |
| 19 |
|
| 20 |
import * as Main from 'resource:///org/gnome/shell/ui/main.js'; |
| 21 |
|
| 22 |
const THRESHOLD_HIGH = 0.80; |
| 23 |
|
| 24 |
// adapted from load-graph.cpp in gnome-system-monitor |
| 25 |
/** |
| 26 |
* @param {string} str |
| 27 |
* @returns {number} |
| 28 |
*/ |
| 29 |
function strHash(str) { |
| 30 |
let hash = 0xcbf29ce484222325n; |
| 31 |
|
| 32 |
for (const c of str) |
| 33 |
hash = (hash * 0x00000100000001B3n) ^ BigInt(c.codePointAt(0)); |
| 34 |
return hash; |
| 35 |
} |
| 36 |
|
| 37 |
class StatSection extends St.BoxLayout { |
| 38 |
static { |
| 39 |
GObject.registerClass(this); |
| 40 |
} |
| 41 |
|
| 42 |
constructor(iconDir, iconName, accessibleName) { |
| 43 |
super({ |
| 44 |
style_class: 'system-monitor-stat-section', |
| 45 |
accessibleName, |
| 46 |
}); |
| 47 |
|
| 48 |
const file = iconDir.get_child(`${iconName}.svg`); |
| 49 |
|
| 50 |
this._icon = new St.Icon({ |
| 51 |
style_class: 'system-monitor-stat-section-icon', |
| 52 |
gicon: new Gio.FileIcon({file}), |
| 53 |
}); |
| 54 |
this.add_child(this._icon); |
| 55 |
|
| 56 |
this.label = new St.Label({ |
| 57 |
style_class: 'system-monitor-stat-section-label', |
| 58 |
y_align: Clutter.ActorAlign.CENTER, |
| 59 |
}); |
| 60 |
this.label.clutter_text.set({ |
| 61 |
ellipsize: Pango.EllipsizeMode.NONE, |
| 62 |
x_align: Clutter.ActorAlign.CENTER, |
| 63 |
}); |
| 64 |
this.add_child(this.label); |
| 65 |
|
| 66 |
this.connect('destroy', () => this._clearTimeout()); |
| 67 |
this.connect('notify::visible', () => this._sync()); |
| 68 |
this._sync(); |
| 69 |
} |
| 70 |
|
| 71 |
_ensureTimeout() { |
| 72 |
if (this._updateId) |
| 73 |
return; |
| 74 |
|
| 75 |
this._updateId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, |
| 76 |
() => { |
| 77 |
this._update(); |
| 78 |
return GLib.SOURCE_CONTINUE; |
| 79 |
}); |
| 80 |
} |
| 81 |
|
| 82 |
_clearTimeout() { |
| 83 |
if (this._updateId) |
| 84 |
GLib.source_remove(this._updateId); |
| 85 |
delete this._updateId; |
| 86 |
} |
| 87 |
|
| 88 |
_sync() { |
| 89 |
if (this.visible) |
| 90 |
this._ensureTimeout(); |
| 91 |
else |
| 92 |
this._clearTimeout(); |
| 93 |
|
| 94 |
if (this.visible) |
| 95 |
this._update(); |
| 96 |
} |
| 97 |
|
| 98 |
_update() { |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
class LoadStatSection extends StatSection { |
| 103 |
static { |
| 104 |
GObject.registerClass(this); |
| 105 |
} |
| 106 |
|
| 107 |
#formatter = new Intl.NumberFormat(undefined, { |
| 108 |
style: 'percent', |
| 109 |
}); |
| 110 |
|
| 111 |
_getLoadValue() { |
| 112 |
} |
| 113 |
|
| 114 |
_update() { |
| 115 |
const load = this._getLoadValue(); |
| 116 |
this.label.text = this.#formatter.format(load); |
| 117 |
|
| 118 |
if (load >= THRESHOLD_HIGH) |
| 119 |
this.add_style_class_name('high-usage'); |
| 120 |
else |
| 121 |
this.remove_style_class_name('high-usage'); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
class CpuSection extends LoadStatSection { |
| 126 |
static { |
| 127 |
GObject.registerClass(this); |
| 128 |
} |
| 129 |
|
| 130 |
#prevCpu = new GTop.glibtop_cpu(); |
| 131 |
|
| 132 |
constructor(iconDir) { |
| 133 |
super(iconDir, 'processor-symbolic', _('CPU stats')); |
| 134 |
} |
| 135 |
|
| 136 |
_getLoadValue() { |
| 137 |
const cpu = new GTop.glibtop_cpu(); |
| 138 |
GTop.glibtop_get_cpu(cpu); |
| 139 |
|
| 140 |
const total = cpu.total - this.#prevCpu.total; |
| 141 |
const user = cpu.user - this.#prevCpu.user; |
| 142 |
const sys = cpu.sys - this.#prevCpu.sys; |
| 143 |
const nice = cpu.nice - this.#prevCpu.nice; |
| 144 |
|
| 145 |
this.#prevCpu = cpu; |
| 146 |
|
| 147 |
return (user + sys + nice) / Math.max(total, 1.0); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
class MemSection extends LoadStatSection { |
| 152 |
static { |
| 153 |
GObject.registerClass(this); |
| 154 |
} |
| 155 |
|
| 156 |
constructor(iconDir) { |
| 157 |
super(iconDir, 'memory-symbolic', _('Memory stats')); |
| 158 |
} |
| 159 |
|
| 160 |
_getLoadValue() { |
| 161 |
const mem = new GTop.glibtop_mem(); |
| 162 |
GTop.glibtop_get_mem(mem); |
| 163 |
|
| 164 |
const {user, total} = mem; |
| 165 |
return user / Math.max(total, 1.0); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
class SwapSection extends LoadStatSection { |
| 170 |
static { |
| 171 |
GObject.registerClass(this); |
| 172 |
} |
| 173 |
|
| 174 |
constructor(iconDir) { |
| 175 |
super(iconDir, 'swap-symbolic', _('Swap stats')); |
| 176 |
} |
| 177 |
|
| 178 |
_getLoadValue() { |
| 179 |
const swap = new GTop.glibtop_swap(); |
| 180 |
GTop.glibtop_get_swap(swap); |
| 181 |
|
| 182 |
const {used, total} = swap; |
| 183 |
return used / Math.max(total, 1.0); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
class NetStatSection extends StatSection { |
| 188 |
static { |
| 189 |
GObject.registerClass(this); |
| 190 |
} |
| 191 |
|
| 192 |
#formats = [{ |
| 193 |
factor: 1000, |
| 194 |
unitFactor: 1000, |
| 195 |
formatter: new Intl.NumberFormat(undefined, { |
| 196 |
style: 'unit', |
| 197 |
unit: 'kilobyte', |
| 198 |
maximumFractionDigits: 1, |
| 199 |
minimumFractionDigits: 1, |
| 200 |
}), |
| 201 |
}, { |
| 202 |
factor: 1000 * 10, |
| 203 |
unitFactor: 1000, |
| 204 |
formatter: new Intl.NumberFormat(undefined, { |
| 205 |
style: 'unit', |
| 206 |
unit: 'kilobyte', |
| 207 |
maximumFractionDigits: 0, |
| 208 |
}), |
| 209 |
}, { |
| 210 |
factor: 1000 * 1000, |
| 211 |
unitFactor: 1000 * 1000, |
| 212 |
formatter: new Intl.NumberFormat(undefined, { |
| 213 |
style: 'unit', |
| 214 |
unit: 'megabyte', |
| 215 |
maximumFractionDigits: 1, |
| 216 |
minimumFractionDigits: 1, |
| 217 |
}), |
| 218 |
}, { |
| 219 |
factor: 1000 * 1000 * 10, |
| 220 |
unitFactor: 1000 * 1000, |
| 221 |
formatter: new Intl.NumberFormat(undefined, { |
| 222 |
style: 'unit', |
| 223 |
unit: 'megabyte', |
| 224 |
maximumFractionDigits: 0, |
| 225 |
}), |
| 226 |
}, { |
| 227 |
factor: 1000 * 1000 * 1000, |
| 228 |
unitFactor: 1000 * 1000 * 1000, |
| 229 |
formatter: new Intl.NumberFormat(undefined, { |
| 230 |
style: 'unit', |
| 231 |
unit: 'gigabyte', |
| 232 |
maximumFractionDigits: 1, |
| 233 |
minimumFractionDigits: 1, |
| 234 |
}), |
| 235 |
}, { |
| 236 |
factor: 1000 * 1000 * 1000 * 10, |
| 237 |
unitFactor: 1000 * 1000 * 1000, |
| 238 |
formatter: new Intl.NumberFormat(undefined, { |
| 239 |
style: 'unit', |
| 240 |
unit: 'gigabyte', |
| 241 |
maximumFractionDigits: 0, |
| 242 |
}), |
| 243 |
}, { |
| 244 |
factor: 1000 * 1000 * 1000 * 1000, |
| 245 |
unitFactor: 1000 * 1000 * 1000 * 1000, |
| 246 |
formatter: new Intl.NumberFormat(undefined, { |
| 247 |
style: 'unit', |
| 248 |
unit: 'terabyte', |
| 249 |
maximumFractionDigits: 1, |
| 250 |
minimumFractionDigits: 1, |
| 251 |
}), |
| 252 |
}, { |
| 253 |
factor: 1000 * 1000 * 1000 * 1000 * 10, |
| 254 |
unitFactor: 1000 * 1000 * 1000 * 1000, |
| 255 |
formatter: new Intl.NumberFormat(undefined, { |
| 256 |
style: 'unit', |
| 257 |
unit: 'terabyte', |
| 258 |
maximumFractionDigits: 0, |
| 259 |
}), |
| 260 |
}, { |
| 261 |
factor: 1000 * 1000 * 1000 * 1000 * 1000, |
| 262 |
unitFactor: 1000 * 1000 * 1000 * 1000 * 1000, |
| 263 |
formatter: new Intl.NumberFormat(undefined, { |
| 264 |
style: 'unit', |
| 265 |
unit: 'petabyte', |
| 266 |
maximumFractionDigits: 1, |
| 267 |
minimumFractionDigits: 1, |
| 268 |
}), |
| 269 |
}]; |
| 270 |
|
| 271 |
#lastBytes = 0; |
| 272 |
#lastHash = 0; |
| 273 |
#lastTime = 0; |
| 274 |
|
| 275 |
_getBytes(_netload) { |
| 276 |
} |
| 277 |
|
| 278 |
_getFormat(bytes) { |
| 279 |
for (let i = 1; i < this.#formats.length; i++) { |
| 280 |
if (bytes < this.#formats.at(i).factor) |
| 281 |
return this.#formats.at(i - 1); |
| 282 |
} |
| 283 |
return this.#formats.at(-1); |
| 284 |
} |
| 285 |
|
| 286 |
_update() { |
| 287 |
const FLAG_LOOPBACK = 1 << 4; // GTop sucks |
| 288 |
|
| 289 |
const netlist = new GTop.glibtop_netlist(); |
| 290 |
const ifnames = GTop.glibtop_get_netlist(netlist); |
| 291 |
|
| 292 |
let bytes = 0; |
| 293 |
let hash = 1n; |
| 294 |
|
| 295 |
for (const ifname of ifnames) { |
| 296 |
const netload = new GTop.glibtop_netload(); |
| 297 |
GTop.glibtop_get_netload(netload, ifname); |
| 298 |
|
| 299 |
if (netload.if_flags & FLAG_LOOPBACK) |
| 300 |
continue; |
| 301 |
|
| 302 |
bytes += this._getBytes(netload); |
| 303 |
hash += strHash(ifname); |
| 304 |
} |
| 305 |
|
| 306 |
const time = GLib.get_monotonic_time(); |
| 307 |
|
| 308 |
let dbytes = 0; |
| 309 |
|
| 310 |
// Skip calculation if new data is less than old (interface |
| 311 |
// removed, counters reset, ...) or if it is the first time |
| 312 |
if (bytes >= this.#lastBytes && |
| 313 |
hash === this.#lastHash && |
| 314 |
this.#lastTime !== 0) { |
| 315 |
const dtime = (time - this.#lastTime) / GLib.USEC_PER_SEC; |
| 316 |
dbytes = (bytes - this.#lastBytes) / dtime; |
| 317 |
} |
| 318 |
|
| 319 |
this.#lastBytes = bytes; |
| 320 |
this.#lastTime = time; |
| 321 |
this.#lastHash = hash; |
| 322 |
|
| 323 |
const {unitFactor, formatter} = this._getFormat(dbytes); |
| 324 |
this.label.text = formatter.format(dbytes / unitFactor); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
class UploadSection extends NetStatSection { |
| 329 |
static { |
| 330 |
GObject.registerClass(this); |
| 331 |
} |
| 332 |
|
| 333 |
constructor(iconDir) { |
| 334 |
super(iconDir, 'upload-symbolic', _('Upload stats')); |
| 335 |
} |
| 336 |
|
| 337 |
_getBytes(netload) { |
| 338 |
return netload.bytes_out; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
class DownloadSection extends NetStatSection { |
| 343 |
static { |
| 344 |
GObject.registerClass(this); |
| 345 |
} |
| 346 |
|
| 347 |
constructor(iconDir) { |
| 348 |
super(iconDir, 'download-symbolic', _('Download stats')); |
| 349 |
} |
| 350 |
|
| 351 |
_getBytes(netload) { |
| 352 |
return netload.bytes_in; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
class Indicator extends PanelMenu.Button { |
| 357 |
static { |
| 358 |
GObject.registerClass(this); |
| 359 |
} |
| 360 |
|
| 361 |
constructor(extension) { |
| 362 |
GTop.glibtop_init(); |
| 363 |
super(0.5, _('System stats')); |
| 364 |
|
| 365 |
this._settings = extension.getSettings(); |
| 366 |
this.connect('destroy', |
| 367 |
() => (this._settings = null)); |
| 368 |
|
| 369 |
const box = new St.BoxLayout({ |
| 370 |
styleClass: 'system-monitor-stat-sections', |
| 371 |
}); |
| 372 |
|
| 373 |
this.add_child(box); |
| 374 |
|
| 375 |
this._placeholder = new St.Icon({ |
| 376 |
styleClass: 'system-status-icon system-monitor-placeholder', |
| 377 |
}); |
| 378 |
box.add_child(this._placeholder); |
| 379 |
|
| 380 |
const iconDir = extension.dir.resolve_relative_path('icons'); |
| 381 |
|
| 382 |
this._cpuSection = new CpuSection(iconDir); |
| 383 |
this._settings.bind('show-cpu', |
| 384 |
this._cpuSection, 'visible', |
| 385 |
Gio.SettingsBindFlags.GET); |
| 386 |
box.add_child(this._cpuSection); |
| 387 |
|
| 388 |
this._memSection = new MemSection(iconDir); |
| 389 |
this._settings.bind('show-memory', |
| 390 |
this._memSection, 'visible', |
| 391 |
Gio.SettingsBindFlags.GET); |
| 392 |
box.add_child(this._memSection); |
| 393 |
|
| 394 |
this._swapSection = new SwapSection(iconDir); |
| 395 |
this._settings.bind('show-swap', |
| 396 |
this._swapSection, 'visible', |
| 397 |
Gio.SettingsBindFlags.GET); |
| 398 |
box.add_child(this._swapSection); |
| 399 |
|
| 400 |
this._ulSection = new UploadSection(iconDir); |
| 401 |
this._settings.bind('show-upload', |
| 402 |
this._ulSection, 'visible', |
| 403 |
Gio.SettingsBindFlags.GET); |
| 404 |
box.add_child(this._ulSection); |
| 405 |
|
| 406 |
this._dlSection = new DownloadSection(iconDir); |
| 407 |
this._settings.bind('show-download', |
| 408 |
this._dlSection, 'visible', |
| 409 |
Gio.SettingsBindFlags.GET); |
| 410 |
box.add_child(this._dlSection); |
| 411 |
|
| 412 |
this.menu.addMenuItem( |
| 413 |
new PopupMenu.PopupSeparatorMenuItem(_('Show'))); |
| 414 |
|
| 415 |
this._cpuItem = this.menu.addAction(_('CPU'), |
| 416 |
() => this._toggleSettings('show-cpu')); |
| 417 |
this._memItem = this.menu.addAction(_('Memory'), |
| 418 |
() => this._toggleSettings('show-memory')); |
| 419 |
this._swapItem = this.menu.addAction(_('Swap'), |
| 420 |
() => this._toggleSettings('show-swap')); |
| 421 |
this._ulItem = this.menu.addAction(_('Upload'), |
| 422 |
() => this._toggleSettings('show-upload')); |
| 423 |
this._dlItem = this.menu.addAction(_('Download'), |
| 424 |
() => this._toggleSettings('show-download')); |
| 425 |
|
| 426 |
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); |
| 427 |
|
| 428 |
this._appMenuItem = this.menu.addAction(_('Open System Monitor'), |
| 429 |
() => this._openSystemMonitor()); |
| 430 |
|
| 431 |
const appSystem = Shell.AppSystem.get_default(); |
| 432 |
appSystem.connectObject('installed-changed', |
| 433 |
() => this._updateSystemMonitorApp(), this); |
| 434 |
this._updateSystemMonitorApp(); |
| 435 |
|
| 436 |
this._settings.connectObject('changed', |
| 437 |
() => this._sync(), this); |
| 438 |
this._sync(); |
| 439 |
} |
| 440 |
|
| 441 |
_updateSystemMonitorApp() { |
| 442 |
const appSystem = Shell.AppSystem.get_default(); |
| 443 |
this._systemMonitorApp = |
| 444 |
appSystem.lookup_app('org.gnome.SystemMonitor.desktop'); |
| 445 |
this._placeholder.gicon = this._systemMonitorApp?.icon ?? null; |
| 446 |
this.visible = this._systemMonitorApp != null; |
| 447 |
} |
| 448 |
|
| 449 |
_openSystemMonitor() { |
| 450 |
this._systemMonitorApp.activate(); |
| 451 |
Main.overview.hide(); |
| 452 |
} |
| 453 |
|
| 454 |
_toggleSettings(key) { |
| 455 |
this._settings.set_boolean(key, !this._settings.get_boolean(key)); |
| 456 |
} |
| 457 |
|
| 458 |
_sync() { |
| 459 |
this._cpuItem.setOrnament(this._settings.get_boolean('show-cpu') |
| 460 |
? PopupMenu.Ornament.CHECK |
| 461 |
: PopupMenu.Ornament.NONE); |
| 462 |
this._memItem.setOrnament(this._settings.get_boolean('show-memory') |
| 463 |
? PopupMenu.Ornament.CHECK |
| 464 |
: PopupMenu.Ornament.NONE); |
| 465 |
this._swapItem.setOrnament(this._settings.get_boolean('show-swap') |
| 466 |
? PopupMenu.Ornament.CHECK |
| 467 |
: PopupMenu.Ornament.NONE); |
| 468 |
this._ulItem.setOrnament(this._settings.get_boolean('show-upload') |
| 469 |
? PopupMenu.Ornament.CHECK |
| 470 |
: PopupMenu.Ornament.NONE); |
| 471 |
this._dlItem.setOrnament(this._settings.get_boolean('show-download') |
| 472 |
? PopupMenu.Ornament.CHECK |
| 473 |
: PopupMenu.Ornament.NONE); |
| 474 |
|
| 475 |
this._placeholder.visible = |
| 476 |
this._settings.list_keys().every(key => !this._settings.get_boolean(key)); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
export default class SystemMonitorExtension extends Extension { |
| 481 |
enable() { |
| 482 |
this._indicator = new Indicator(this); |
| 483 |
Main.panel.addToStatusArea(this.uuid, this._indicator); |
| 484 |
} |
| 485 |
|
| 486 |
disable() { |
| 487 |
this._indicator.destroy(); |
| 488 |
this._indicator = null; |
| 489 |
} |
| 490 |
} |