检索MacOS X上的系统信息? [英] Retrieve system information on MacOS X?

查看:133
本文介绍了检索MacOS X上的系统信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,使用C ++可以得到有关计算机的基本信息吗? 例如,有没有一种方法可以检查正在使用的内存量(整个计算机而不仅仅是我的计算机),可用的总内存,虚拟内存使用率,CPU使用率,网络统计信息等等?

Using C++ is there anyway 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?

我正在使用Mac OS X Snow Leopard,但我希望可以为所有Mac OS(例如Lion)实现的解决方案

I am using Mac OS X Snow Leopard but I would prefer a solution that could be implemented for all Mac OSs (i.e. Lion)

推荐答案

有关MacOS/X下系统范围内存使用情况的信息,请打开并读取文件/usr/bin/vm_stat,如下所示:

For system-wide memory usage information under MacOS/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
}

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

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;
}

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

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).

这篇关于检索MacOS X上的系统信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆