Skip to content

Commit

Permalink
Merge pull request #372 from nokyan/swap-columns
Browse files Browse the repository at this point in the history
Swap Columns
  • Loading branch information
nokyan authored Oct 29, 2024
2 parents ed581f2 + be7d6f5 commit fd8c58c
Show file tree
Hide file tree
Showing 21 changed files with 579 additions and 319 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![GNOME Circle](https://circle.gnome.org/assets/button/badge.svg
)](https://apps.gnome.org/app/net.nokyan.Resources/) [![Please do not theme this app](https://stopthemingmy.app/badge.svg)](https://stopthemingmy.app)

Resources is a simple yet powerful monitor for your system resources and processes, written in Rust and using GTK 4 and libadwaita for its GUI. It’s capable of displaying usage and details of your CPU, memory, GPUs, network interfaces and block devices. It’s also capable of listing and terminating running graphical applications as well as processes.
Resources is a simple yet powerful monitor for your system resources and processes, written in Rust and using GTK 4 and libadwaita for its GUI. It’s capable of displaying usage and details of your CPU, memory, GPUs, NPUs, network interfaces and block devices. It’s also capable of listing and terminating running graphical applications as well as processes.

<details>
<summary><b>Click me for screenshots!</b></summary>
Expand Down
8 changes: 8 additions & 0 deletions data/net.nokyan.Resources.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<default>false</default>
<summary>Display GPU memory usage in Applications view</summary>
</key>
<key name="apps-show-swap" type="b">
<default>false</default>
<summary>Display swap usage in Applications view</summary>
</key>
<key name="processes-show-id" type="b">
<default>true</default>
<summary>Display process ID in Processes view</summary>
Expand Down Expand Up @@ -157,6 +161,10 @@
<default>false</default>
<summary>Display priority in Processes view</summary>
</key>
<key name="processes-show-swap" type="b">
<default>false</default>
<summary>Display swap usage in Processes view</summary>
</key>
<key name="show-logical-cpus" type="b">
<default>false</default>
<summary>Display logical CPU graphs in Processor view</summary>
Expand Down
9 changes: 9 additions & 0 deletions data/resources/ui/dialogs/app_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@
<property name="subtitle-selectable">true</property>
</object>
</child>
<child>
<object class="AdwActionRow" id="swap_usage">
<property name="title" translatable="yes">Swap</property>
<style>
<class name="property"/>
</style>
<property name="subtitle-selectable">true</property>
</object>
</child>
<child>
<object class="AdwActionRow" id="drive_read_speed">
<property name="title" translatable="yes">Drive Read</property>
Expand Down
9 changes: 9 additions & 0 deletions data/resources/ui/dialogs/process_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
<property name="title" translatable="yes">Memory</property>
</object>
</child>
<child>
<object class="AdwActionRow" id="swap_usage">
<style>
<class name="property"/>
</style>
<property name="subtitle-selectable">true</property>
<property name="title" translatable="yes">Swap</property>
</object>
</child>
<child>
<object class="AdwActionRow" id="drive_read_speed">
<property name="title" translatable="yes">Drive Read</property>
Expand Down
10 changes: 10 additions & 0 deletions data/resources/ui/dialogs/settings_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@
<property name="title" translatable="yes">Video Decoder</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="apps_show_swap_row">
<property name="title" translatable="yes">Swap</property>
</object>
</child>
</object>
</child>
</object>
Expand Down Expand Up @@ -280,6 +285,11 @@
<property name="title" translatable="yes">Priority</property>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="processes_show_swap_row">
<property name="title" translatable="yes">Swap</property>
</object>
</child>
</object>
</child>
</object>
Expand Down
69 changes: 59 additions & 10 deletions lib/process_data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ use std::str::FromStr;
use std::sync::RwLock;
use std::{path::PathBuf, time::SystemTime};

static USERS_CACHE: Lazy<HashMap<u32, String>> = Lazy::new(|| unsafe {
const STAT_OFFSET: usize = 2; // we split the stat contents where the executable name ends, which is the second element
const STAT_PARENT_PID: usize = 3 - STAT_OFFSET;
const STAT_USER_CPU_TIME: usize = 13 - STAT_OFFSET;
const STAT_SYSTEM_CPU_TIME: usize = 14 - STAT_OFFSET;
const STAT_NICE: usize = 18 - STAT_OFFSET;
const STAT_STARTTIME: usize = 21 - STAT_OFFSET;

static USERS_CACHE: Lazy<HashMap<libc::uid_t, String>> = Lazy::new(|| unsafe {
uzers::all_users()
.map(|user| (user.uid(), user.name().to_string_lossy().to_string()))
.collect()
Expand All @@ -34,6 +41,8 @@ static RE_UID: Lazy<Regex> = lazy_regex!(r"Uid:\s*(\d+)");

static RE_AFFINITY: Lazy<Regex> = lazy_regex!(r"Cpus_allowed:\s*([0-9A-Fa-f]+)");

static RE_SWAP_USAGGE: Lazy<Regex> = lazy_regex!(r"VmSwap:\s*([0-9]+)\s*kB");

static RE_IO_READ: Lazy<Regex> = lazy_regex!(r"read_bytes:\s*(\d+)");

static RE_IO_WRITE: Lazy<Regex> = lazy_regex!(r"write_bytes:\s*(\d+)");
Expand Down Expand Up @@ -133,7 +142,7 @@ pub struct GpuUsageStats {
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProcessData {
pub pid: libc::pid_t,
pub parent_pid: i32,
pub parent_pid: libc::pid_t,
pub user: String,
pub comm: String,
pub commandline: String,
Expand All @@ -142,6 +151,7 @@ pub struct ProcessData {
pub niceness: Niceness,
pub affinity: Vec<bool>,
pub memory_usage: usize,
pub swap_usage: usize,
pub starttime: u64, // in clock ticks, see man proc(5)!
pub cgroup: Option<String>,
pub containerization: Containerization,
Expand Down Expand Up @@ -260,10 +270,26 @@ impl ProcessData {
let comm = comm.replace('\n', "");

// -2 to accommodate for only collecting after the second item (which is the executable name as mentioned above)
let parent_pid = stat[3 - 2].parse()?;
let user_cpu_time = stat[13 - 2].parse()?;
let system_cpu_time = stat[14 - 2].parse()?;
let nice = stat[18 - 2].parse()?;
let parent_pid = stat
.get(STAT_PARENT_PID)
.context("wrong stat file format")
.and_then(|x| x.parse().context("couldn't parse stat file content"))?;
let user_cpu_time = stat
.get(STAT_USER_CPU_TIME)
.context("wrong stat file format")
.and_then(|x| x.parse().context("couldn't parse stat file content"))?;
let system_cpu_time = stat
.get(STAT_SYSTEM_CPU_TIME)
.context("wrong stat file format")
.and_then(|x| x.parse().context("couldn't parse stat file content"))?;
let nice = stat
.get(STAT_NICE)
.context("wrong stat file format")
.and_then(|x| x.parse().context("couldn't parse stat file content"))?;
let starttime = stat
.get(STAT_STARTTIME)
.context("wrong stat file format")
.and_then(|x| x.parse().context("couldn't parse stat file content"))?;

let mut affinity = Vec::with_capacity(*NUM_CPUS);
RE_AFFINITY
Expand All @@ -284,10 +310,32 @@ impl ProcessData {
});
});

let memory_usage =
(statm[1].parse::<usize>()? - statm[2].parse::<usize>()?).saturating_mul(*PAGESIZE);

let starttime = stat[21 - 2].parse()?;
let swap_usage = RE_SWAP_USAGGE
.captures(&status)
.and_then(|captures| captures.get(1))
.map(|capture| capture.as_str())
.unwrap_or_default()
.parse::<usize>()
.unwrap_or_default() // kworkers don't have swap usage
.saturating_mul(1000);

let memory_usage = statm
.get(1)
.context("wrong statm file format")
.and_then(|x| {
x.parse::<usize>()
.context("couldn't parse statm file content")
})?
.saturating_sub(
statm
.get(2)
.context("wrong statm file format")
.and_then(|x| {
x.parse::<usize>()
.context("couldn't parse statm file content")
})?,
)
.saturating_mul(*PAGESIZE);

let cgroup = std::fs::read_to_string(proc_path.join("cgroup"))
.ok()
Expand Down Expand Up @@ -330,6 +378,7 @@ impl ProcessData {
niceness: nice,
affinity,
memory_usage,
swap_usage,
starttime,
cgroup,
containerization,
Expand Down
Loading

0 comments on commit fd8c58c

Please sign in to comment.