struct proc and sysctl(3)

topic posted Mon, December 10, 2007 - 3:05 PM by  hoco
I'm trying to use the sysctl function call to get a process table. It kind of works, but p_pctcpu is always 0, even for processes which are consuming a lot of cpu time, such as backupd.

First, does sysctl work? It must since that is how ps is implemented. Can I see the source code to ps somewhere?

Second, there are lots of references to something called 'struct proc' but it isn't defined anywhere. There is an extern_proc structure, but no proc structure. mystifying. How does it work?
posted by:
hoco
SF Bay Area
  • Re: struct proc and sysctl(3)

    Tue, December 11, 2007 - 7:24 PM
    I don't know if it'll help much, but the FreeBSD source is all online, and in particular, the source for that version of "ps" is here:

    www.freebsd.org/cgi/cvsweb...src/bin/ps/

    HTH - Good luck!



    Dana
    • Re: struct proc and sysctl(3)

      Tue, December 11, 2007 - 8:08 PM

      Dana, Hoco ...

      You can download the source to the Darwin kernel and the command line utils -- pretty much the whole *nix part of OSX -- yourself from Apple, free of charge.

      Regards,

      John

      Falling You - exploring the beauty of voice and sound
      www.fallingyou.com

      • Re: struct proc and sysctl(3)

        Wed, December 12, 2007 - 9:03 AM
        I actually independently discovered this, but thanks. I finally found the ps program in the adv_cmds archive. I dove in and found that I still don't know what a struct proc is, but it doesn't seem to matter. There is a kinfo_proc that is what is really being used.

        The source to ps was enlightening. It seems that there is a member called p_pctcpu which is supposed to be the percentage used. It must be vestigal because it is always 0. I instrumented the ps program and recompiled it and found that even there it is 0. In OS X (and apparently in BSD) the cpu usage is calculated by adding up the cpu usage of the threads in a process. A chunk of code that makes use of some low-level mach functions for which I can find no docs; however, I was able to learn what I needed and use the same approach to get the cpu usage I wanted.

        Having access to the darwin source was absolutely essential -- I never would have figured it out without it.

Recent topics in "OS X Development"