static void measureCpuLoad(meter_sysload_t *load) { // Measure CPU load, inspired by // http://stackoverflow.com/questions/6785069/get-cpu-percent-usage natural_t numCPUsU = 0U; processor_info_array_t cpuInfo; mach_msg_type_number_t numCpuInfo; kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUsU, &cpuInfo, &numCpuInfo); assert(err == KERN_SUCCESS); for (int cpuIndex = 0; cpuIndex < load->nCpus; cpuIndex++) { integer_t loadValue = (getCounterByCpu(cpuInfo, cpuIndex, CPU_STATE_USER) + getCounterByCpu(cpuInfo, cpuIndex, CPU_STATE_SYSTEM)); integer_t totalValue = (loadValue + getCounterByCpu(cpuInfo, cpuIndex, CPU_STATE_IDLE) + getCounterByCpu(cpuInfo, cpuIndex, CPU_STATE_NICE)); accumulator_t *accumulator = load->cpuAccumulators[cpuIndex]; accumulator_update(accumulator, loadValue, totalValue); load->cpuLoad[cpuIndex] = accumulator_get_percentage(accumulator); // Johan's MacBook can go up to 15% per core even when Johan thinks the // system is idle. Censor load measurements to get a quiet meter on // idle system. if (load->cpuLoad[cpuIndex] < 15) { load->cpuLoad[cpuIndex] = 0; } } size_t cpuInfoSize = sizeof(integer_t) * numCpuInfo; vm_deallocate(mach_task_self(), (vm_address_t)cpuInfo, cpuInfoSize); }
std::vector<float> GetCPUsLoad() { SingleCPULoadInfo *cpu_info; mach_msg_type_number_t numbers_fetched; natural_t num_cpus = 0; kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &num_cpus, (processor_info_array_t*)&cpu_info, &numbers_fetched); if( err != KERN_SUCCESS ) return {}; assert( numbers_fetched == num_cpus * 4 ); static auto prior = std::vector<SingleCPULoadInfo>(num_cpus); assert( prior.size() == num_cpus ); std::vector<float> cores_load(num_cpus, 0.f); for( unsigned i = 0; i < num_cpus; ++i ) { int64_t load = cpu_info[i].system + cpu_info[i].nice + cpu_info[i].user - prior[i].system - prior[i].nice - prior[i].user; int64_t idle = cpu_info[i].idle - prior[i].idle; cores_load[i] = load + idle != 0 ? (float(load) / float(load + idle)) : 0; } copy( cpu_info, cpu_info + num_cpus, begin(prior) ); vm_deallocate(mach_task_self(), (vm_address_t)cpu_info, sizeof(SingleCPULoadInfo) * num_cpus); return cores_load; }
void darwin_init_cpu_monitor(void) { kern_return_t r; processor_info_t pinfo; mach_msg_type_number_t msg_count; p_sys = prop_create(prop_get_global(), "system"); r = host_processor_info(mach_host_self (), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t *)&pinfo, &msg_count); if(r != KERN_SUCCESS) { TRACE(TRACE_ERROR, "darwin", "host_processor_info(PROCESSOR_CPU_LOAD_INFO) failed %d", r); return; } p_cpu = calloc(cpu_count, sizeof(prop_t *)); p_load = calloc(cpu_count, sizeof(prop_t *)); last_total = calloc(cpu_count, sizeof(unsigned int)); last_idle = calloc(cpu_count, sizeof(unsigned int)); prop_set_int(prop_create(prop_create(p_sys, "cpuinfo"), "available"), 1); p_cpuroot = prop_create(prop_create(p_sys, "cpuinfo"), "cpus"); vm_deallocate(mach_task_self(), (vm_address_t)pinfo, (vm_size_t)sizeof(*pinfo) * msg_count); cpu_monitor_do(); callout_arm(&timer, timercb, NULL, 1); }
unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p) { mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t); unsigned cpu_count; // TODO Improving the accuracy of the load counts woule help a lot. if(0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t *)p, &info_size)) { err(4, "Unable to retrieve CPU info\n"); } return cpu_count; }
result_t os_base::CPUInfo(v8::Local<v8::Array> &retVal) { retVal = v8::Array::New(isolate); v8::Local<v8::Object> cpuinfo; v8::Local<v8::Object> cputimes; unsigned int ticks = (unsigned int) sysconf(_SC_CLK_TCK), multiplier = ((uint64_t) 1000L / ticks); char model[512]; uint64_t cpuspeed; size_t size; size = sizeof(model); if (sysctlbyname("hw.model", &model, &size, NULL, 0) < 0) return CHECK_ERROR(LastError()); size = sizeof(cpuspeed); if (sysctlbyname("hw.cpufrequency", &cpuspeed, &size, NULL, 0) < 0) return CHECK_ERROR(LastError()); natural_t numcpus; mach_msg_type_number_t count; processor_cpu_load_info_data_t *info; if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus, reinterpret_cast<processor_info_array_t *>(&info), &count) != KERN_SUCCESS) return CHECK_ERROR(LastError()); retVal = v8::Array::New(isolate, numcpus); for (unsigned int i = 0; i < numcpus; i++) { cpuinfo = v8::Object::New(isolate); cputimes = v8::Object::New(isolate); cputimes->Set(v8::String::NewFromUtf8(isolate, "user"), v8::Number::New(isolate, (uint64_t)(info[i].cpu_ticks[0]) * multiplier)); cputimes->Set(v8::String::NewFromUtf8(isolate, "nice"), v8::Number::New(isolate, (uint64_t)(info[i].cpu_ticks[3]) * multiplier)); cputimes->Set(v8::String::NewFromUtf8(isolate, "sys"), v8::Number::New(isolate, (uint64_t)(info[i].cpu_ticks[1]) * multiplier)); cputimes->Set(v8::String::NewFromUtf8(isolate, "idle"), v8::Number::New(isolate, (uint64_t)(info[i].cpu_ticks[2]) * multiplier)); cputimes->Set(v8::String::NewFromUtf8(isolate, "irq"), v8::Number::New(isolate, 0)); cpuinfo->Set(v8::String::NewFromUtf8(isolate, "model"), v8::String::NewFromUtf8(isolate, model)); cpuinfo->Set(v8::String::NewFromUtf8(isolate, "speed"), v8::Number::New(isolate, cpuspeed / 1000000)); cpuinfo->Set(v8::String::NewFromUtf8(isolate, "times"), cputimes); retVal->Set(i, cpuinfo); } vm_deallocate(mach_task_self(), (vm_address_t) info, count); return 0; }
static void cpu_monitor_do(void) { kern_return_t r; processor_info_t pinfo; mach_msg_type_number_t msg_count; unsigned int cpu_count_temp; char buf[100]; r = host_processor_info(mach_host_self (), PROCESSOR_CPU_LOAD_INFO, &cpu_count_temp, (processor_info_array_t *)&pinfo, &msg_count); if(r != KERN_SUCCESS) return; if(cpu_count != cpu_count_temp) { vm_deallocate(mach_task_self(), (vm_address_t)pinfo, (vm_size_t)sizeof(*pinfo) * msg_count); return; } int i; for(i = 0; i < cpu_count; i++) { if(p_cpu[i] == NULL) { p_cpu[i] = prop_create(p_cpuroot, NULL); snprintf(buf, sizeof(buf), "CPU%d", i); prop_set_string(prop_create(p_cpu[i], "name"), buf); p_load[i] = prop_create(p_cpu[i], "load"); } processor_info_t pi = pinfo + (CPU_STATE_MAX * i); unsigned int total = (pi[CPU_STATE_USER] + pi[CPU_STATE_SYSTEM] + pi[CPU_STATE_NICE] + pi[CPU_STATE_IDLE]); unsigned int idle = pi[CPU_STATE_IDLE]; unsigned int di = idle - last_idle[i]; unsigned int dt = total - last_total[i]; last_idle[i] = idle; last_total[i] = total; prop_set_float(p_load[i], 1.0 - ((float)di / (float)dt)); } vm_deallocate(mach_task_self(), (vm_address_t)pinfo, (vm_size_t)sizeof(*pinfo) * msg_count); }
void update_cpu_load() { kern_return_t error; natural_t nmpu; processor_info_array_t info; mach_msg_type_number_t cnt; int infosz; int i; int firstrun = 0; error = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &nmpu, &info, &cnt); if (error != KERN_SUCCESS) { mach_error("update_cpu_load1", error); exit(1); } if (cpu_load == NULL) { cpu_count = nmpu; cpu_load = calloc(nmpu, sizeof (cpu_load_t)); firstrun = 1; } infosz = cnt / nmpu; for (i = 0; i < nmpu; i++) { cpu_load_t newload; newload.cl_system = info[CPU_STATE_SYSTEM + i * infosz]; newload.cl_user = info[CPU_STATE_USER + i * infosz]; newload.cl_nice = info[CPU_STATE_NICE + i * infosz]; newload.cl_idle = info[CPU_STATE_IDLE + i * infosz]; if (!firstrun) { int delta_system = newload.cl_system - cpu_load[i].cl_system; int delta_user = newload.cl_user - cpu_load[i].cl_user; int delta_nice = newload.cl_nice - cpu_load[i].cl_nice; int delta_idle = newload.cl_idle - cpu_load[i].cl_idle; int used = delta_system + delta_user + delta_nice; int percent = 100 * used / (used + delta_idle); fprintf(stdout, "%d ", percent); } memcpy(&cpu_load[i], &newload, sizeof (cpu_load_t)); } if (!firstrun) fprintf(stdout, "\n"); vm_deallocate(mach_task_self(), (vm_address_t)info, cnt); }
int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK), multiplier = ((uint64_t)1000L / ticks); char model[512]; uint64_t cpuspeed; size_t size; unsigned int i; natural_t numcpus; mach_msg_type_number_t msg_type; processor_cpu_load_info_data_t *info; uv_cpu_info_t* cpu_info; size = sizeof(model); if (sysctlbyname("machdep.cpu.brand_string", &model, &size, NULL, 0) && sysctlbyname("hw.model", &model, &size, NULL, 0)) { return -errno; } size = sizeof(cpuspeed); if (sysctlbyname("hw.cpufrequency", &cpuspeed, &size, NULL, 0)) return -errno; if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus, (processor_info_array_t*)&info, &msg_type) != KERN_SUCCESS) { return -EINVAL; /* FIXME(bnoordhuis) Translate error. */ } *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos)); if (!(*cpu_infos)) { vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type); return -ENOMEM; } *count = numcpus; for (i = 0; i < numcpus; i++) { cpu_info = &(*cpu_infos)[i]; cpu_info->cpu_times.user = (uint64_t)(info[i].cpu_ticks[0]) * multiplier; cpu_info->cpu_times.nice = (uint64_t)(info[i].cpu_ticks[3]) * multiplier; cpu_info->cpu_times.sys = (uint64_t)(info[i].cpu_ticks[1]) * multiplier; cpu_info->cpu_times.idle = (uint64_t)(info[i].cpu_ticks[2]) * multiplier; cpu_info->cpu_times.irq = 0; cpu_info->model = uv__strdup(model); cpu_info->speed = cpuspeed/1000000; } vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type); return 0; }
uint64_t PowerStats::GetMacCPUTime() { uint64_t cpu_time = 0; processor_cpu_load_info_t cpuLoad; mach_msg_type_number_t processorMsgCount; natural_t processorCount; host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &processorCount, (processor_info_array_t *)&cpuLoad, &processorMsgCount); for (natural_t i = 0; i < processorCount; i++) { cpu_time += cpuLoad[i].cpu_ticks[CPU_STATE_SYSTEM]; cpu_time += cpuLoad[i].cpu_ticks[CPU_STATE_USER]; cpu_time += cpuLoad[i].cpu_ticks[CPU_STATE_NICE]; } return cpu_time; }
void get_cpu_usage (unsigned int num_cpus, cpu_usage info[], cpu_usage *total) { unsigned int num_cpus_temp; processor_cpu_load_info_data_t *proc_info; mach_msg_type_number_t proc_info_size; int i; for (i = 0; i < num_cpus; i++) { bzero(&(info[i]), sizeof(cpu_usage)); } if (total != NULL) { bzero(total, sizeof(cpu_usage)); } if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &num_cpus_temp, (processor_info_array_t *) &proc_info, &proc_info_size)) { return; } if (num_cpus != num_cpus_temp) { return; } for (i = 0; i < num_cpus; i++) { info[i].user = proc_info[i].cpu_ticks[CPU_STATE_USER]; info[i].sys = proc_info[i].cpu_ticks[CPU_STATE_SYSTEM]; info[i].idle = proc_info[i].cpu_ticks[CPU_STATE_IDLE]; info[i].nice = proc_info[i].cpu_ticks[CPU_STATE_NICE]; info[i].total = (info[i].user + info[i].sys + info[i].idle + info[i].nice); info[i].frequency = 100; if (total != NULL) { total->user += info[i].user; total->sys += info[i].sys; total->idle += info[i].idle; total->nice += info[i].nice; total->total += info[i].total; } } vm_deallocate(mach_task_self(), (vm_address_t) proc_info, proc_info_size); if (total != NULL) { total->frequency = 100; } }
RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu) { #if 0 return RTMpIsCpuPossible(idCpu); #else /** @todo proper ring-3 support on darwin, see @bugref{3014}. */ natural_t nCpus; processor_basic_info_t pinfo; mach_msg_type_number_t count; kern_return_t krc = host_processor_info(mach_host_self(), PROCESSOR_BASIC_INFO, &nCpus, (processor_info_array_t*)&pinfo, &count); AssertReturn (krc == KERN_SUCCESS, true); bool isOnline = idCpu < nCpus ? pinfo[idCpu].running : true; vm_deallocate(mach_task_self(), (vm_address_t)pinfo, count * sizeof(*pinfo)); return isOnline; #endif }
void CPUEngine_mac::sample( int count, lh_cpudata *data ) { unsigned int cpu_count; processor_cpu_load_info_t cpu_load; mach_msg_type_number_t cpu_msg_count; if( data == NULL || count<1 ) return; memset( data, 0, sizeof(lh_cpudata)*count ); if( host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t *) & cpu_load, &cpu_msg_count) == KERN_SUCCESS ) { qint64 when = QDateTime::currentDateTime().toMSecsSinceEpoch(); count_ = cpu_count; if (count > count_) { memset(data, 0, sizeof(lh_cpudata) * count); count = count_; } for(int n = 0; n < count_; ++n) { data[n].when = when; data[n].idle = cpu_load[n].cpu_ticks[CPU_STATE_IDLE]; data[n].work = cpu_load[n].cpu_ticks[CPU_STATE_USER] + cpu_load[n].cpu_ticks[CPU_STATE_NICE] + cpu_load[n].cpu_ticks[CPU_STATE_SYSTEM] ; /* data[n].user = cpu_load[n].cpu_ticks[CPU_STATE_USER]; data[n].user += cpu_load[n].cpu_ticks[CPU_STATE_NICE]; data[n].system = cpu_load[n].cpu_ticks[CPU_STATE_SYSTEM]; data[n].total = cpu_load[n].cpu_ticks[CPU_STATE_IDLE] + data[n].system + data[n].user; Q_ASSERT( data[n].total >= (data[n].user+data[n].system) ); */ } vm_deallocate (mach_task_self (), (vm_address_t) cpu_load, (vm_size_t) (cpu_msg_count * sizeof (*cpu_load))); } return; }
unsigned int get_num_cpus (void) { unsigned int num_cpus; processor_cpu_load_info_data_t *proc_info; mach_msg_type_number_t proc_info_size; if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &num_cpus, (processor_info_array_t *) &proc_info, &proc_info_size)) { return 0; } vm_deallocate(mach_task_self(), (vm_address_t) proc_info, proc_info_size); return num_cpus; }
void MainCpu::setCPUsLoad() { natural_t cpuCount; processor_cpu_load_info_t cpuInfo; mach_msg_type_number_t nbInfo; size_t totalSystemTime = 0, totalUserTime = 0, totalIdleTime = 0; if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpuCount, reinterpret_cast<processor_info_array_t *>(&cpuInfo), &nbInfo) != KERN_SUCCESS) throw std::runtime_error("cpu module: host_processor_info failed"); for (natural_t i = 0; i < cpuCount; i++) { if (i >= WORLD_WIDE_MAX_CPU_ON_UNIT) throw std::runtime_error("cpu error: world wide max cpu record beaten"); size_t system = cpuInfo[i].cpu_ticks[CPU_STATE_SYSTEM]; size_t user = cpuInfo[i].cpu_ticks[CPU_STATE_USER] + cpuInfo[i].cpu_ticks[CPU_STATE_NICE]; size_t idle = cpuInfo[i].cpu_ticks[CPU_STATE_IDLE]; std::ostringstream ss; ss << "CPU " << i << " Usage (%)"; dequeUpdate(ss.str(), calculateCPULoad(system + user, _oldCpusWorkTicks[i], system + user + idle, _oldCpusTotalTicks[i])); _oldCpusWorkTicks[i] = system + user; _oldCpusTotalTicks[i] = system + user + idle; totalSystemTime += system; totalUserTime += user; totalIdleTime += idle; } size_t newTotal = totalIdleTime + totalSystemTime + totalUserTime; size_t newWork = totalSystemTime + totalUserTime; dequeUpdate("CPU Total Usage (%)", calculateCPULoad(newWork, _oldWorkTicks, newTotal, _oldTotalTicks)); _oldTotalTicks = newTotal; _oldWorkTicks = newWork; }
int main() { natural_t cpuCount; processor_info_array_t infoArray; mach_msg_type_number_t infoCount; kern_return_t kr; processor_cpu_load_info_data_t* cpuLoadInfo; unsigned long old_ticks,new_ticks,old_totalTicks,new_totalTicks; int cpu,state; FILE *file=fopen("/Users/delphinus/.screen/.cpu.old","r"); if (file == NULL) old_ticks = old_totalTicks = 0; else fscanf(file,"%ld %ld",&old_ticks,&old_totalTicks); fclose(file); /* get information */ kr = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpuCount, &infoArray, &infoCount); if (kr) { mach_error("host_processor_info error:", kr); return kr; } cpuLoadInfo = (processor_cpu_load_info_data_t*) infoArray; new_ticks = new_totalTicks = 0; for (cpu = 0; cpu<cpuCount; cpu++){ /* state 0:user, 1:system, 2:idle, 3:nice */ for (state = 0; state<CPU_STATE_MAX; state++) { if(state != 2) new_ticks += cpuLoadInfo[cpu].cpu_ticks[state]; new_totalTicks += cpuLoadInfo[cpu].cpu_ticks[state]; } } printf("%5.1lf\n",(double)(new_ticks - old_ticks)/(new_totalTicks - old_totalTicks)*100); if((file = fopen("/Users/delphinus/.screen/.cpu.old","w")) != NULL){ fprintf(file,"%lu %lu",new_ticks,new_totalTicks); fclose(file); } vm_deallocate(mach_task_self(), (vm_address_t)infoArray, infoCount); return 0; }
void glibtop_open_p (glibtop *server, const char *program_name, const unsigned long features, const unsigned flags) { processor_cpu_load_info_data_t *pinfo; mach_msg_type_number_t info_count; natural_t processor_count; /* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */ server->name = program_name; server->machine->uid = getuid (); server->machine->euid = geteuid (); server->machine->gid = getgid (); server->machine->egid = getegid (); /* Drop priviledges. */ if (setreuid (server->machine->euid, server->machine->uid)) _exit (1); if (setregid (server->machine->egid, server->machine->gid)) _exit (1); /* !!! END OF SUID ROOT PART !!! */ /* Our effective uid is now those of the user invoking the server, * so we do no longer have any priviledges. */ if (host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO, &processor_count, (processor_info_array_t*)&pinfo, &info_count)) { glibtop_error_io_r (server, "host_processor_info"); } server->ncpu = (processor_count <= GLIBTOP_NCPU) ? processor_count : GLIBTOP_NCPU; vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count); }
int get_cpu_usage(cpu_usage_t **usage_array, int *count) { *count = 0; *usage_array = 0; natural_t cpu_count; processor_cpu_load_info_data_t *load_info; mach_msg_type_number_t info_count; kern_return_t error = host_processor_info( mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t *) &load_info, &info_count); if (error) { return error; } *count = cpu_count; *usage_array = (cpu_usage_t *) malloc(sizeof(cpu_usage_t) * cpu_count); int cpu; for (cpu = 0; cpu < cpu_count; cpu++) { unsigned int *ticks = load_info[cpu].cpu_ticks; (*usage_array)[cpu].user = ticks[CPU_STATE_USER]; (*usage_array)[cpu].system = ticks[CPU_STATE_SYSTEM]; (*usage_array)[cpu].idle = ticks[CPU_STATE_IDLE]; (*usage_array)[cpu].nice = ticks[CPU_STATE_NICE]; } vm_deallocate( mach_task_self(), (vm_address_t) load_info, info_count); return 0; }
/* * Return a Python list of tuple representing per-cpu times */ static PyObject* get_system_per_cpu_times(PyObject* self, PyObject* args) { natural_t cpu_count; processor_info_array_t info_array; mach_msg_type_number_t info_count; kern_return_t error; processor_cpu_load_info_data_t* cpu_load_info; PyObject* py_retlist = PyList_New(0); PyObject* py_cputime; int i, ret; error = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, &info_array, &info_count); if (error != KERN_SUCCESS) { return PyErr_Format(PyExc_RuntimeError, "Error in host_processor_info(): %s", mach_error_string(error)); } cpu_load_info = (processor_cpu_load_info_data_t*) info_array; for (i = 0; i < cpu_count; i++) { py_cputime = Py_BuildValue("(dddd)", (double)cpu_load_info[i].cpu_ticks[CPU_STATE_USER] / CLK_TCK, (double)cpu_load_info[i].cpu_ticks[CPU_STATE_NICE] / CLK_TCK, (double)cpu_load_info[i].cpu_ticks[CPU_STATE_SYSTEM] / CLK_TCK, (double)cpu_load_info[i].cpu_ticks[CPU_STATE_IDLE] / CLK_TCK ); PyList_Append(py_retlist, py_cputime); Py_XDECREF(py_cputime); } ret = vm_deallocate(mach_task_self(), (vm_address_t)info_array, info_count * sizeof(int)); if (ret != KERN_SUCCESS) { printf("vm_deallocate() failed\n"); } return py_retlist; }
QueryData genCpuTime(QueryContext& context) { QueryData results; natural_t processor_count; processor_cpu_load_info_data_t* processor_times; mach_port_t host = mach_host_self(); mach_msg_type_number_t processor_msg_count; kern_return_t ret = host_processor_info(host, PROCESSOR_CPU_LOAD_INFO, &processor_count, reinterpret_cast<processor_info_t*>(&processor_times), &processor_msg_count); if (ret == KERN_SUCCESS) { // Loop through the cores and add rows for each core. for (unsigned int core = 0; core < processor_count; core++) { Row r; r["core"] = INTEGER(core); r["user"] = BIGINT( ticks_to_usecs(processor_times[core].cpu_ticks[CPU_STATE_USER])); r["idle"] = BIGINT( ticks_to_usecs(processor_times[core].cpu_ticks[CPU_STATE_IDLE])); r["system"] = BIGINT( ticks_to_usecs(processor_times[core].cpu_ticks[CPU_STATE_SYSTEM])); r["nice"] = BIGINT( ticks_to_usecs(processor_times[core].cpu_ticks[CPU_STATE_NICE])); results.push_back(r); } vm_deallocate( mach_task_self(), reinterpret_cast<vm_address_t>(processor_times), static_cast<vm_size_t>(processor_count * sizeof(*processor_times))); } return results; }
VALUE method_cpu(VALUE self) { VALUE cpus = rb_ary_new(); processor_info_array_t cpuInfo; mach_msg_type_number_t numCpuInfo; natural_t numCPUsU = 0U; kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUsU, &cpuInfo, &numCpuInfo); if(err == KERN_SUCCESS) { unsigned i; for(i = 0U; i < numCPUsU; ++i) { VALUE cpu = rb_hash_new(); int pos = CPU_STATE_MAX * i; rb_hash_aset(cpu, SYM_USER, ULL2NUM(cpuInfo[pos + CPU_STATE_USER])); rb_hash_aset(cpu, SYM_SYSTEM, ULL2NUM(cpuInfo[pos + CPU_STATE_SYSTEM])); rb_hash_aset(cpu, SYM_NICE, ULL2NUM(cpuInfo[pos + CPU_STATE_NICE])); rb_hash_aset(cpu, SYM_IDLE, ULL2NUM(cpuInfo[pos + CPU_STATE_IDLE])); rb_ary_push(cpus, cpu); } } return cpus; }
int read_cpu_counter(long *user, long *tot){ processor_cpu_load_info_data_t *pinfo; mach_msg_type_number_t info_count; /*long tot1=0; */ /*long idle1=0; */ unsigned int ncpu0; if (host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO, &ncpu0, (int**) &pinfo, &info_count)) { return -1; } *user=0; *tot=0; for (unsigned int i = 0; i < ncpu0; i++) { long tmp=pinfo[i].cpu_ticks[CPU_STATE_USER] +pinfo[i].cpu_ticks[CPU_STATE_NICE] +pinfo[i].cpu_ticks[CPU_STATE_SYSTEM]; *user+=tmp; *tot+=tmp+pinfo[i].cpu_ticks[CPU_STATE_IDLE]; } return 0; }
result_t os_base::CPUs(int32_t &retVal) { static int cpus = 0; if (cpus > 0) { retVal = cpus; return 0; } natural_t numcpus; mach_msg_type_number_t count; processor_cpu_load_info_data_t *info; if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus, reinterpret_cast<processor_info_array_t *>(&info), &count) != KERN_SUCCESS) return CHECK_ERROR(LastError()); vm_deallocate(mach_task_self(), (vm_address_t) info, count); retVal = cpus = numcpus; return 0; }
static inline void HLCPUInfo_GetCPULoads(natural_t& outNumberCPUs, processor_cpu_load_info_t& outCPULoads, mach_msg_type_number_t& outCPULoadsSize) { kern_return_t theError = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &outNumberCPUs, (processor_info_array_t*)&outCPULoads, &outCPULoadsSize); ThrowIfKernelError(theError, CAException(theError), "HLCPUInfo_GetCPULoads: got an error getting the CPU load"); }