检索 Mac OS X 上的系统信息

发布于 2024-12-24 16:36:59 字数 384 浏览 2 评论 0 原文

使用C++,有没有办法获取有关计算机的基本信息?

例如,有没有一种方法可以检查正在使用多少内存(整个计算机而不仅仅是我的计算机)、可用总内存、虚拟内存使用情况、CPU 使用情况、网络统计信息等等?

我正在使用 Mac OS X v10.6 (Snow Leopard),但我更喜欢可以为所有 Mac OS 实现的解决方案(例如,Mac OS X v10.7 (Lion))。

Using C++, is there a way I could get basic information about the computer?

For example, is there a way I could check how much memory is being used (by the whole computer not just my computer), the total memory available, virtual memory usage, CPU usage, networks stats and so on?

I am using Mac OS X v10.6 (Snow Leopard), but I would prefer a solution that could be implemented for all Mac OSs (for example, Mac OS X v10.7 (Lion)).

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

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

发布评论

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

评论(2

可是我不能没有你 2024-12-31 16:36:59

要了解 Mac OS X 下的系统范围内存使用情况信息,请打开并读取文件 /usr/bin/vm_stat。像这样的事情:

static double ParseMemValue(const char * b)
{
    while((*b)&&(isdigit(*b) == false))
        b++;
    return isdigit(*b) ? atof(b) : -1.0;
}

// Returns a number between 0.0f and 1.0f, with 0.0f meaning all RAM is available, and 1.0f meaning all RAM is currently in use
float GetSystemMemoryUsagePercentage()
{
    FILE * fpIn = popen("/usr/bin/vm_stat", "r");
    if (fpIn)
    {
        double pagesUsed = 0.0, totalPages = 0.0;
        char buf[512];
        while(fgets(buf, sizeof(buf), fpIn) != NULL)
        {
            if (strncmp(buf, "Pages", 5) == 0)
            {
                double val = ParseMemValue(buf);
                if (val >= 0.0)
                {
                    if ((strncmp(buf, "Pages wired",  11) == 0) ||
                        (strncmp(buf, "Pages active", 12) == 0)
                       )

                        pagesUsed += val;

                    totalPages += val;
                }
            }
            else
              if (strncmp(buf, "Mach Virtual Memory Statistics", 30) != 0)
                  break; // Stop at "Translation Faults". We don't care
                         // about anything at or below that
        }
        pclose(fpIn);

        if (totalPages > 0.0)
            return (float) (pagesUsed/totalPages);
    }
    return -1.0f;  // Indicate failure
}

对于 CPU 使用率指示器,请执行以下操作:

#include <mach/mach_init.h>
#include <mach/mach_error.h>
#include <mach/mach_host.h>
#include <mach/vm_map.h>

static unsigned long long _previousTotalTicks = 0;
static unsigned long long _previousIdleTicks = 0;

// Returns 1.0f for "CPU fully pinned", 0.0f for "CPU idle", or somewhere in between
// You'll need to call this at regular intervals, since it measures the load between
// the previous call and the current one.
float GetCPULoad()
{
    host_cpu_load_info_data_t cpuinfo;
    mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
    if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpuinfo, &count) == KERN_SUCCESS)
    {
        unsigned long long totalTicks = 0;
        for(int i=0; i<CPU_STATE_MAX; i++)
            totalTicks += cpuinfo.cpu_ticks[i];
        return CalculateCPULoad(cpuinfo.cpu_ticks[CPU_STATE_IDLE], totalTicks);
    }
    else
       return -1.0f;
}

float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
{
    unsigned long long totalTicksSinceLastTime = totalTicks-_previousTotalTicks;
    unsigned long long idleTicksSinceLastTime  = idleTicks-_previousIdleTicks;
    float ret = 1.0f-((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime)/totalTicksSinceLastTime : 0);
    _previousTotalTicks = totalTicks;
    _previousIdleTicks  = idleTicks;
    return ret;
}

对于网络统计信息,我不知道解决方案(除了可能运行 netstat 并以某种方式解析结果......这取决于您对什么网络统计数据感兴趣(我想)。

For system-wide memory usage information under Mac OS X, open and read the file /usr/bin/vm_stat. Something like this:

static double ParseMemValue(const char * b)
{
    while((*b)&&(isdigit(*b) == false))
        b++;
    return isdigit(*b) ? atof(b) : -1.0;
}

// Returns a number between 0.0f and 1.0f, with 0.0f meaning all RAM is available, and 1.0f meaning all RAM is currently in use
float GetSystemMemoryUsagePercentage()
{
    FILE * fpIn = popen("/usr/bin/vm_stat", "r");
    if (fpIn)
    {
        double pagesUsed = 0.0, totalPages = 0.0;
        char buf[512];
        while(fgets(buf, sizeof(buf), fpIn) != NULL)
        {
            if (strncmp(buf, "Pages", 5) == 0)
            {
                double val = ParseMemValue(buf);
                if (val >= 0.0)
                {
                    if ((strncmp(buf, "Pages wired",  11) == 0) ||
                        (strncmp(buf, "Pages active", 12) == 0)
                       )

                        pagesUsed += val;

                    totalPages += val;
                }
            }
            else
              if (strncmp(buf, "Mach Virtual Memory Statistics", 30) != 0)
                  break; // Stop at "Translation Faults". We don't care
                         // about anything at or below that
        }
        pclose(fpIn);

        if (totalPages > 0.0)
            return (float) (pagesUsed/totalPages);
    }
    return -1.0f;  // Indicate failure
}

For a CPU usage indicator, do something like this:

#include <mach/mach_init.h>
#include <mach/mach_error.h>
#include <mach/mach_host.h>
#include <mach/vm_map.h>

static unsigned long long _previousTotalTicks = 0;
static unsigned long long _previousIdleTicks = 0;

// Returns 1.0f for "CPU fully pinned", 0.0f for "CPU idle", or somewhere in between
// You'll need to call this at regular intervals, since it measures the load between
// the previous call and the current one.
float GetCPULoad()
{
    host_cpu_load_info_data_t cpuinfo;
    mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
    if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpuinfo, &count) == KERN_SUCCESS)
    {
        unsigned long long totalTicks = 0;
        for(int i=0; i<CPU_STATE_MAX; i++)
            totalTicks += cpuinfo.cpu_ticks[i];
        return CalculateCPULoad(cpuinfo.cpu_ticks[CPU_STATE_IDLE], totalTicks);
    }
    else
       return -1.0f;
}

float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
{
    unsigned long long totalTicksSinceLastTime = totalTicks-_previousTotalTicks;
    unsigned long long idleTicksSinceLastTime  = idleTicks-_previousIdleTicks;
    float ret = 1.0f-((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime)/totalTicksSinceLastTime : 0);
    _previousTotalTicks = totalTicks;
    _previousIdleTicks  = idleTicks;
    return ret;
}

For network statistics, I don't know the solution (other than maybe to run netstat and parse the results somehow... it depends on what network statistics you are interested in I suppose).

夏日浅笑〃 2024-12-31 16:36:59

首先,如果您只关注 Mac OS,那么选择的语言应该是 Objective-C

需要遵循的步骤

  1. 学习 Objective-C 时 。这其实并没有那么难。我自己有 C++ 背景,在使用这种语言在平台上工作了几周后,我变得相当流利。
  2. 检查 Mac OS X 开发人员库:Mac OS X 开发人员库
  3. 查看 这个
  4. 这个

First off, if you are focusing on Mac OS only, then the language of choice should be Objective-C.

Steps you need to follow

  1. Learn Objective-C. It is not really that hard. I come from a C++ background myself and after a few weeks of working on the platform with this language I became rather fluent
  2. Check the Mac OS X developer library: Mac OS X Developer Library
  3. Check out this
  4. And this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文