Per-process network statistics?

Discussion in 'Mac Programming' started by Stingray454, Aug 31, 2011.

  1. Stingray454 macrumors 6502a

    Joined:
    Sep 22, 2009
    #1
    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 :)
     
  2. JoshDC, Aug 31, 2011
    Last edited: Aug 31, 2011

    JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #2
    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.
     
  3. Stingray454 thread starter macrumors 6502a

    Joined:
    Sep 22, 2009
    #3
    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?
     
  4. LostSoul80 macrumors 68020

    LostSoul80

    Joined:
    Jan 25, 2009
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    As noted above use DTrace.
     
  6. (marc) macrumors 6502a

    (marc)

    Joined:
    Sep 15, 2010
    Location:
    the woods
    #6
    I always wondered how that piece of software works. Do you have any insights?
     
  7. Stingray454 thread starter macrumors 6502a

    Joined:
    Sep 22, 2009
    #7
    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.
     
  8. LostSoul80 macrumors 68020

    LostSoul80

    Joined:
    Jan 25, 2009
    #8
    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/
     
  9. Stingray454 thread starter macrumors 6502a

    Joined:
    Sep 22, 2009
    #9
    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 :)
     
  10. LostSoul80 macrumors 68020

    LostSoul80

    Joined:
    Jan 25, 2009
    #10
    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.
     
  11. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #11
    nettop

    Under Lion nettop is installed by default. Maybe you can parse that ?
     

Share This Page