Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Stingray454

macrumors 6502a
Original poster
Sep 22, 2009
593
115
I'm trying to find a way to get the networks statistics (bytes/second sent and recieved) for a specific process under OS X (Objective C app).

This seems quite difficult - I have not found any per-process statistics so far. The is no /proc directory on OS X, and sysctl seems to only give system-wide statistics. My best bet so far is to sniff all incoming/outgoing packets and try to map them by port to specific processes, but it seems like quite a complex solution (and also, I haven't yet figured out how to map open ports to specific processes).

Is there any way to get this information in ObjectiveC under OS X? It would seem that it's possible, I've heard of third party apps that can display this information, but I have no idea how they did it :)
 
Instruments has an activity monitor instrument that you can configure for Net Bytes In/Out Per Second and attach to a process.

If you need more control it might be worth looking at DTrace.

Edit: It seems the activity monitor instrument still works system wide even if you attach it to a specific process. Searching on Google seems to show a number of DTrace scripts for per process network utilisation.
 
Last edited:
Thanx, but I think you misunderstand me. I don't want to use a third-party app to check the network statistics of my computer - I want to develop my own app, where I programatically can watch running processes for network data. My question is - is there any API available in OS X to achieve this?
 
Thanx, but I think you misunderstand me. I don't want to use a third-party app to check the network statistics of my computer - I want to develop my own app, where I programatically can watch running processes for network data. My question is - is there any API available in OS X to achieve this?

As noted above use DTrace.
 
Ah, I didn't hear about DTrace before, and googling it quickly I got the impression that only worked as a command-line tool, but I see now you can make quite complicated DTrance scripts to incorporate in your own apps. I'm totally new to DTrace, it seems to be quite a bit of information to take in, but it looks like it might be able to do what I want. Thanks for the tip, I will have a look at it.
 
I always wondered how that piece of software works. Do you have any insights?

Actually no, but I have never tried understanding how it works. A quick google search reveals Little Snitch uses a kernel extension to block connection on a per process basis. While blocking may not be a immediate task to achieve, finding out the active connections seems easier - for example, take a look at here http://www.tcpdump.org/
 
Actually no, but I have never tried understanding how it works. A quick google search reveals Little Snitch uses a kernel extension to block connection on a per process basis. While blocking may not be a immediate task to achieve, finding out the active connections seems easier - for example, take a look at here http://www.tcpdump.org/

I looked briefly at libpcap when I started looking into this, it seems to only catch device-wide packets - if you want to group them for each pid, that seems to have to be done manually (like, count per open connection and then try to figure out what process has which ports open).

DTrace looks interesting, though I'm having problem finding documentation and/or example code to make calls and retrieve data through an Objective C app. Making your own KEXT also seems like a possible solution. Guessing I'm in a bit over my head here, given I'm quite new to Mac programming.. But I'll keep digging :)
 
Making your own KEXT also seems like a possible solution. Guessing I'm in a bit over my head here, given I'm quite new to Mac programming.. But I'll keep digging :)
Keep in mind that writing a kernel extension is not as simple as it sounds, security wise. Read the documentation about it, as it's not something you should attempt unless you really know what you're doing. Also, Apple provides a sample project about NKEs.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.