关于一些unix代码的问题?

时间:2012-01-07 04:59:52

标签: c++ c macos unix

#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];
        sysLoadPercentage = 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;
}

我有一些关于代码的问题,我希望你可以帮助我:

  • 什么是“host_cpu_load_info_data_t”结构?它用于什么?
  • 什么是“mach_msg_type_number_t”结构?它用于什么?
  • 预处理器定义“HOST_CPU_LOAD_INFO_COUNT”及其用途是什么?
  • 什么是host_statistics函数?
  • 上面列出的host_statistics函数的每个参数是什么意思? (从未见过他们)
  • 什么是预处理器定义CPU_STATE_MAX和CPU_STATE_IDLE?
  • 预处理器定义KERN_SUCCESS是什么?

如果无法回答,请将我引荐至包含所有这些答案的网站。我已经尝试使用Google搜索但找不到任何答案,也找不到任何文档。此外,如果问题太具体,我会删除这个问题,请建议一个来源,这样的问题是有效的。

由于

1 个答案:

答案 0 :(得分:0)

包含“所有这些答案”的网站是http://www.opensource.apple.com/source/xnu/xnu-1699.24.8/。您可能还会发现 Mac OS X Internals (Amit Singh)这本书很有用。