在 Darwin/OSX 中以编程方式确定进程信息

发布于 2024-07-07 18:56:36 字数 667 浏览 6 评论 0 原文

我有一个具有以下成员函数的类:


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

/// physical memory size in KB
virtual uint64_t Size() const = 0;  

/// resident memory for this process
virtual uint64_t Rss() const = 0; 

/// cpu used by this process
virtual double PercentCpu() const = 0; 

/// memory used by this process
virtual double PercentMemory() const = 0; 

/// number of threads in this process
virtual int32_t Lwps() const = 0; 

该类的职责是返回有关调用者的进程信息。 物理内存大小可以很容易地通过 sysctl 调用来确定,而 pid 是微不足道的,但除了在 ps 或 top 上调用 popen 并解析输出之外,其余的调用都让我困惑——这是不可接受的。 任何帮助将不胜感激。

要求:
在 g++ 4.0 上编译
没有 obj-c
OSX 10.5

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() const = 0;  

/// resident memory for this process
virtual uint64_t Rss() const = 0; 

/// cpu used by this process
virtual double PercentCpu() const = 0; 

/// memory used by this process
virtual double PercentMemory() const = 0; 

/// number of threads in this process
virtual int32_t Lwps() const = 0; 

This class' duty is to return process information about caller. Physical memory size can easily determined by a sysctl call, and pid is trivial, but the remaining calls have eluded me, aside from invoking a popen on ps or top and parsing the output - which isn't acceptable. Any help would be greatly appreciated.

Requirements:
Compiles on g++ 4.0
No obj-c
OSX 10.5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请止步禁区 2024-07-14 18:56:36

进程信息来自 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 负载来自 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

有关更多详细信息,请查看 toplsof 的来源>,它们是开源的(您需要注册为 Apple 开发人员,但这是免费的):

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

稍后编辑:< /strong> 所有这些接口都是特定于版本的,因此在编写生产代码(libproc.h)时需要考虑到这一点:

/*
 * This header file contains private interfaces to obtain process information.
 * These interfaces are subject to change in future releases.
 */

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.
 */
我不会写诗 2024-07-14 18:56:36

既然你说不支持 Objective-C,我们就排除大部分 MacOS 框架。

您可以使用 getrusage() 获取 CPU 时间,它给出进程占用的用户和系统 CPU 时间总量。 要获取 CPU 百分比,您需要每秒快照一次 getrusage 值(或者您想要的粒度)。

#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);

getrusage 结构中有一个 RSS 字段,但在 MacOS X 10.5 中似乎始终为零。 Michael Knight 写了一个博客几年前关于如何确定 RSS 的帖子。

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.

手心的温暖 2024-07-14 18:56:36

您可以使用下面的代码来获取 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 结构体包含有关进程的所有信息。例如进程标识符 (pid)、
进程组、进程状态等

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.

风渺 2024-07-14 18:56:36

我认为大多数这些值都可以在 Mach API 中找到,但我已经有一段时间没有在那里研究了。 或者,您可以只查看“ps”或“top”命令的源代码,看看它们是如何做到的。

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.

远昼 2024-07-14 18:56:36

大多数信息可以从 GetProcessInformation()

顺便说一句,为什么函数的虚拟方法返回进程范围的信息?

这仅限于,不适用于可可

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文