Determine Process Info Programmatically in Darwin/OSX

前端 未结 5 397
无人及你
无人及你 2020-11-28 11:26

I have a class with the following member functions:


/// caller pid
virtual pid_t Pid() const = 0; 

/// physical memory size in KB
virtual uint64_t Size() c         


        
相关标签:
5条回答
  • 2020-11-28 11:49

    Most of this info can be gotten from GetProcessInformation().

    By the way, why virtual methods for functions that return processwide info?

    This is CARBON only, and won't work with cocoa

    0 讨论(0)
  • 2020-11-28 11:54

    Since you say no Objective-C we'll rule out most of the MacOS frameworks.

    You can get CPU time using getrusage(), which gives the total amount of User and System CPU time charged to your process. To get a CPU percentage you'd need to snapshot the getrusage values once per second (or however granular you want to be).

    #include <sys/resource.h>
    
    struct rusage r_usage;
    
    if (getrusage(RUSAGE_SELF, &r_usage)) {
        /* ... error handling ... */
    }
    
    printf("Total User CPU = %ld.%ld\n",
            r_usage.ru_utime.tv_sec,
            r_usage.ru_utime.tv_usec);
    printf("Total System CPU = %ld.%ld\n",
            r_usage.ru_stime.tv_sec,
            r_usage.ru_stime.tv_usec);
    

    There is an RSS field in the getrusage structure, but is appears to always be zero in MacOS X 10.5. Michael Knight wrote a blog post several years ago about how to determine the RSS.

    0 讨论(0)
  • 2020-11-28 11:55

    Process info comes from pidinfo:

    cristi:~ diciu$ grep proc_pidinfo /usr/include/libproc.h
    
    int proc_pidinfo(int pid, int flavor, uint64_t arg,  void *buffer, int buffersize);
    

    cpu load comes from host_statistics:

    cristi:~ diciu$ grep -r host_statistics /usr/include/
    
    /usr/include/mach/host_info.h:/* host_statistics() */
    
    /usr/include/mach/mach_host.defs:routine host_statistics(
    
    /usr/include/mach/mach_host.h:/* Routine host_statistics */
    
    /usr/include/mach/mach_host.h:kern_return_t host_statistics
    

    For more details, check out sources for top and lsof, they are open source (you need to register as an Apple developer but that's free of charge):

    https://opensource.apple.com/source/top/top-111.20.1/libtop.c.auto.html

    Later edit: All these interfaces are version specific, so you need to take that into account when writing production code (libproc.h):

    /*
     * This header file contains private interfaces to obtain process information.
     * These interfaces are subject to change in future releases.
     */
    
    0 讨论(0)
  • 2020-11-28 11:58

    I think most of these values are available in the Mach API, but it's been a while since I've poked around in there. Alternatively, you could just look at the source code for the "ps" or "top" commands, and see how they do it.

    0 讨论(0)
  • 2020-11-28 12:03

    You can use below code for process info in mac OS:

    void IsInBSDProcessList(char *name)    { 
      assert( name != NULL); 
      kinfo_proc *result; 
      size_t count = 0; 
      result = (kinfo_proc *)malloc(sizeof(kinfo_proc)); 
      if(GetBSDProcessList(&result,&count) == 0) { 
        for (int i = 0; i < count; i++) { 
          kinfo_proc *proc = NULL; 
          proc = &result[i]; 
          }
      } 
      free(result);
    }
    

    kinfo_proc struct contains all the information about a process.such as Process identifier (pid), process group, process status and etc.

    0 讨论(0)
提交回复
热议问题