DEV Community

Md. Mahmudul Hasan Mabud
Md. Mahmudul Hasan Mabud

Posted on

Leveling Up SysLens: Adding Real-Time CPU Logic and Hardware Health to my Linux CLI Monitor

In my previous post, I shared the birth of SysLens, a lightweight system monitor written in C. It was a solid start, but let's be honest: a system monitor without actual CPU usage percentages or battery health is just a static snapshot.

I’ve just pushed a major update to SysLens. This version moves beyond simple text parsing and starts looking at how Linux handles hardware states and process priority.

What’s New in SysLens v1.0.3?

  • Real-time CPU Usage: A dynamic bar showing how hard your processor is actually working.
  • Hardware Inspection: Added GPU vendor detection (Intel/NVIDIA/AMD) and Product Name (Host).
  • Battery Health: Not just capacity, but a "Health" percentage based on design capacity vs. current full charge.
  • Thermal Monitoring: CPU temperature tracking via hwmon.
  • Advanced Process List: A new table that sorts the top 5 processes by memory usage (VmRSS) and highlights "Zombie" processes in red.
  • USB Detection: Lists removable USB devices currently connected.

The Technical Deep Dive: Calculating CPU %

This was the most interesting challenge. Linux doesn't give you a "current CPU %" file. Instead, /proc/stat gives you Jiffies (units of time since boot).

To get the percentage, I had to:

  1. Read the first line of /proc/stat.
  2. Sum up all values (User, Nice, System, Idle, etc.) to get Total_1.
  3. Wait for a short interval (usleep(100000)).
  4. Read the file again to get Total_2.
  5. Calculate the delta: (1.0 - (IdleDelta / TotalDelta)) * 100.
// Snippet of the new CPU logic
int delta_total = total_2nd - total_1st;
int delta_idle = idle_2nd - idle_1st;
float cpu = (1.0 - (float)delta_idle / delta_total) * 100;
Enter fullscreen mode Exit fullscreen mode

Navigating the /sys Filesystem

While /proc is for processes, /sys is for hardware. I added functions to dive into:

  • /sys/class/power_supply/BAT0/: To compare charge_full against charge_full_design. This reveals how much your battery has degraded over time.
  • /sys/bus/usb/devices/: To iterate through connected peripherals and identify "removable" storage.

The New "Top-Style" Process Table

I implemented a struct for processes and a simple bubble sort to identify memory hogs. Now, SysLens doesn't just tell you how many tasks are running; it shows you exactly who is eating your RAM.

[ Resource Usage ]
RAM         : [###.......] 34% (2.7GiB/7.6GiB)
CPU         : [##........] 22%
-------------------------------------------------------------------------
PID        PPID       STATUS          RES        %MEM       COMMAND   
-------------------------------------------------------------------------
3245       1802       S (sleeping)    452120     5.2        firefox   
...
Enter fullscreen mode Exit fullscreen mode

Lessons Learned

  1. Precision Matters: When calculating CPU usage, the sleep interval between reads directly affects accuracy.
  2. Hex Codes: Identifying GPUs required mapping vendor IDs (like 0x8086 for Intel) to human-readable names.
  3. Global Scope: I introduced my first extern variable (totalram) to allow the process module to calculate %MEM relative to the total RAM detected in the memory module.

Try Version 1.0.3

The code is more modular than ever. If you're curious about how Linux exposes hardware info via files, check out the source!

GitHub: https://github.com/mahmudul626/syslens

I'd love to hear from other C devs: How do you handle thermal monitoring across different kernel versions where hwmon paths might change? Let's discuss in the comments!

Top comments (1)

Collapse
 
mahmudul626 profile image
Md. Mahmudul Hasan Mabud