r/swift • • 6d ago

Sysbar: a menu bar system monitor built with SwiftPM only (no .xcodeproj), AppKit + SwiftUI, IOKit/Mach APIs. Open source, plus a few lessons learned

Post image

I've been building **Sysbar**, a small macOS menu bar system monitor (CPU with E/P-core split, GPU, memory, disk, network, top processes). It's open source under Apache-2.0, and I thought the implementation might interest people here.

**Repo:** https://github.com/rodrigotrombeta/Sysbar

**Project setup: no Xcode project at all**

- It's a plain SwiftPM `executableTarget` (about 4.3k lines), linking `IOKit` and `ServiceManagement`.

- A ~30-line `build.sh` runs `swift build -c release`, assembles the `.app` bundle by hand (binary, `Info.plist`, icon), ad-hoc signs it with `codesign --sign -` and installs it. You only need the Command Line Tools.

- Release builds are universal: `swift build --arch arm64 --arch x86_64`, then packaged into a DMG.

**Where the data comes from (public APIs only, no sudo, no helper)**

- CPU: `host_processor_info` for per-core ticks, split into E-cores and P-cores on Apple Silicon.

- GPU: `IOServiceMatching("IOAccelerator")`, reading the `PerformanceStatistics` dictionary ("Device Utilization %" and in-use memory).

- Memory: `host_statistics64(HOST_VM_INFO64)`.

- Disk I/O: the `IOBlockStorageDriver` statistics.

- Top processes: `proc_listallpids` + `proc_pid_rusage(RUSAGE_INFO_V4)`.

- Launch at login: `SMAppService.mainApp`.

**UI**

- An `NSStatusItem` with an icon drawn into an `NSImage(size:flipped:drawingHandler:)` on every tick.

- The panels are SwiftUI hosted in an `NSPanel` subclass. A small `NSHostingView` subclass overrides `acceptsFirstMouse` so buttons respond to the first click even when the panel isn't key.

**One lesson that surprised me**

My sparklines started out as SwiftUI `Canvas`. In a panel that stays pinned open and redraws every second, `Canvas` re-rendered through GPU-backed buffers and added **tens of MB** of memory. Switching to plain `Shape` paths (`SparklineShape` filled with a gradient) brought the app back to about **25 MB of RAM**. If you're building something that redraws constantly, measure `Canvas` before committing to it.

**Credits:** the monitoring logic started from the System Stats panel in [oMLX](https://github.com/jundot/omlx), and the menu bar icon is inspired by [Stats](https://github.com/exelban/stats).

I'd appreciate a code review from people who know AppKit better than I do, especially on the `NSPanel`/SwiftUI hosting and hover-tracking parts. Issues and PRs are welcome.

7 Upvotes

10 comments sorted by

-1

u/ThanhDongNguyen 6d ago

Nice that it's all public APIs and no helper. A few things I'd check:

  • proc_pid_rusage on processes owned by other users (root stuff like WindowServer or mds) fails with a permission error when you're not root, I think. So the top processes list may quietly miss the exact processes that eat CPU or power. Worth testing.
  • for the E/P split, are you relying on core order? sysctl hw.perflevel0.logicalcpu and hw.perflevel1.logicalcpu give the P and E counts per machine, safer across base, Pro and Max chips.
  • the DMG is ad-hoc signed, so people downloading it on recent macOS get the "can't be opened" dialog and have to go to Privacy & Security > Open Anyway. I'd at least put that in the README, or go Developer ID + notarytool if you want it smooth.

1

u/Affectionate_Pea3516 5d ago

Thanks for your valuable comments, u/ThanhDongNguyen . I don't have a Developer account, so better if I add to README some details regarding that will help.

1

u/Affectionate_Pea3516 5d ago

Regarding:
proc_pid_rusage: yes, it fails for processes owned by other users, and Sysbar currently just skips them. As far as I know there's no unprivileged workaround (Activity Monitor relies on a privileged service, and ps is setuid root). I'll add a small note in the processes section so it's clear system processes aren't included.

E/P split: the counts already come from hw.nperflevels / hw.perflevel1.logicalcpu, but you're right that mapping which cores are E still assumes the E cluster comes first. That holds on every Apple Silicon chip so far, but it's a convention, not a guarantee. Reading cluster-type from the CPU nodes in the IORegistry would make it explicit, so I'll look into that

1

u/honestly_i 2h ago

Jesus christ it's an ai post with ai comments
This is hell

-2

u/[deleted] 5d ago

[removed] — view removed comment

1

u/Affectionate_Pea3516 5d ago

Thanks! It surprised me too. Before switching to Shapes, a pinned panel would climb by tens of MB, mostly GPU-backed buffers that Canvas re-rendered on every tick.

I measured it with [Activity Monitor's Memory column / Instruments Allocations / footprint -p <pid>], comparing the same pinned panel with Canvas and with Shapes.

When the panels are closed, the per-process scan stops entirely (it only runs while a panel that shows processes is visible), and only the menu bar icon gets redrawn. I haven't done a proper long run with everything closed yet, though, so I can't say for sure that it stays flat. That's next on my list, and I'll post the numbers here.

1

u/Affectionate_Pea3516 5d ago

Not sure why this got downvoted. It's a fair question, and it pointed at something I hadn't verified

-2

u/rhysmorgan iOS 5d ago

AI slop comment.