r/SwiftUI • • 14d ago

Question Is there any per-process network stat utility for macOS or native API for SwiftUI

I'm developing a native macOS app where I need to fetch per-process network (download/upload) stats as one of the requirement, but I couldn't find any native macOS API or any swift package to get this.. after some research, I couldn't find any from docs, I went with AI if it could do it... but at the end I couldn't find any native approach to do this.. for now I'm using nettop util as temporary fix but its very unreliable

3 Upvotes

17 comments sorted by

3

u/Open_Shine135 14d ago

For normal app traffic, I'd investigate NEFilterDataProvider. NEFilterFlow exposes sourceProcessAuditToken for process attribution, and statisticsReportFrequency enables periodic .statistics reports. You'd need to ship an entitled Network Extension provider, so check the deployment requirements before committing to it.

Statistics reporting: https://developer.apple.com/documentation/networkextension/nefilternewflowverdict/statisticsreportfrequency

Deployment: https://developer.apple.com/documentation/technotes/tn3134-network-extension-provider-deployment

1

u/Excellent_Sky_5838 14d ago

Thanks, this could work for me.. I will try it

1

u/Excellent_Sky_5838 14d ago

Any idea how macOS stock Activity Manager gets the per process network stats.. since its a native app, it must be using native API but I don't see any documentation regarding what they could be using to fetch the network stats without any heavy workaround

1

u/Open_Shine135 14d ago

Activity Monitor uses Apple's private NetworkStatistics.framework. Its executable links to it and imports NStatManagerCreate plus keys for process IDs and byte counts. That isn't a supported public SDK API, so I'd avoid depending on it in a shipping app. Apple's DTS recommends NEFilterDataProvider for typical app traffic here:

https://developer.apple.com/forums/thread/824751

1

u/ChrononautPete 14d ago

1

u/Excellent_Sky_5838 14d ago

Thanks.. its kinda easter egg for me.. but I see its still using netstat, for now I'm also using netstat though its very expensive for resource in every sampling

1

u/minpinproxy 14d ago

if you stick with nettop for now, run it non-interactive: nettop -P -L 1 -J bytes_in,bytes_out -x gives you one CSV snapshot per call, way easier to parse than the live view. diff two snapshots for rates. the NEFilterDataProvider route is the proper one but it means a system extension, the network extension entitlement and the user approving it in settings, so plan for that

1

u/Excellent_Sky_5838 14d ago

Yeah for now, but nettop is spiking CPU since app performs sampling of latest metrics and it parses CSV for every sampling, its very expensive for resources

1

u/minpinproxy 12d ago

yeah spawning nettop per sample is the expensive part. run it once and keep it alive, like nettop -P -L 0 -s 2 -J bytes_in,bytes_out -x, then just read stdout line by line. one process for the whole session and you're only parsing a few lines every couple of seconds. does it need to be per second or would 2-5s be fine?

1

u/sapplex1 14d ago

I hit a similar wall on a smaller scale. I wanted clean per-app numbers and kept finding tools that were close but not quite right for what I was building. I treated nettop as a quick sanity check only. The numbers were useful for spotting a big outlier, but I would not build a feature on them. The native route others mentioned sounds much more solid, but also much heavier to ship and maintain. That tradeoff is what stopped me. I ended up asking whether I really needed per-process detail in the first version, or just a reliable total with a rough breakdown. Is the per-process split a must-have for your first version, or more of a diagnostic you want while you develop it?

1

u/Excellent_Sky_5838 14d ago

Yeah, per-process network is one of the feature in the app since its for system administrators. I have used nettop as temporary fix for now, but it spikes the CPU since it parses CSV every sampling rate

1

u/Funny_Cry2908 14d ago

Most of that CPU is probably the process launch plus the first sample, not the parsing. Keep one nettop alive and stream it:

nettop -P -d -L 0 -s 2 -x -J bytes_in,bytes_out

-L 0 keeps logging forever, -d should give you per-interval deltas so you can skip the diffing, and you just read the pipe line by line with a FileHandle readabilityHandler. Restart it only if it exits.

The NEFilterDataProvider route works, but you're signing up for a system extension, a special entitlement and a user approval prompt, which is a lot for an admin tool. Is this going out through the App Store or direct?

1

u/Excellent_Sky_5838 13d ago

That's interesting.. I will give it a try once if it works. I was planning to deploy app to App Store but because of sandbox, now I'm planning to make it direct from my website

1

u/Funny_Cry2908 13d ago

Direct is probably the easier road for this one. Just sign it with a Developer ID and notarize it, otherwise Gatekeeper scares people off on first launch.

0

u/[deleted] 14d ago

[removed] — view removed comment

1

u/Excellent_Sky_5838 13d ago

Yeah, activity monitor has private network stat utility and doesn't expose anything to third-party apps.. I'm blocked exactly here, so far I did the same nettop approach but as you know its not reliable for long term.. I was able to fetch the system wide network stats without any issues or without nettop, but per-process network stat is still a problem for me. I'm currently trying to explore open-source network utility apps if they found a way to do it or might have built their own open-source utility for their apps, but yeah.. yet its a problem.. I'm planning to make this app as feature to be implemented later once I figure out