int sysinfo(struct sysinfo *info){ kvm_t *kvmh; double load_avg[NLOADS]; int page_s = getpagesize(); if (info == NULL) R_ERROR(EFAULT); memset(info, 0, sizeof(struct sysinfo)); info -> mem_unit = UNIT_S; /*kvm init*/ if ((kvmh = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) R_ERROR(0); /*load averages*/ if (kvm_getloadavg(kvmh, load_avg, NLOADS) == -1) R_ERROR(0); info -> loads[0] = (u_long)((float)load_avg[0] * USHRT_MAX); info -> loads[1] = (u_long)((float)load_avg[1] * USHRT_MAX); info -> loads[2] = (u_long)((float)load_avg[2] * USHRT_MAX); /*swap space*/ struct kvm_swap k_swap; if (kvm_getswapinfo(kvmh, &k_swap, 1, 0) == -1) R_ERROR(0); info -> totalswap = (u_long)PAGE_2_UNIT(k_swap.ksw_total); info -> freeswap = info -> totalswap - (u_long)PAGE_2_UNIT(k_swap.ksw_used); /*processes*/ int n_procs; if (kvm_getprocs(kvmh, KERN_PROC_ALL, 0, &n_procs) == NULL) R_ERROR(0); info -> procs = (u_short)n_procs; /*end of kvm session*/ if (kvm_close(kvmh) == -1) R_ERROR(0); /*uptime*/ struct timespec ts; if (clock_gettime(CLOCK_UPTIME, &ts) == -1) R_ERROR(R_IGNORE); info -> uptime = (long)ts.tv_sec; /*ram*/ int total_pages, free_pages, active_pages, inactive_pages; u_long shmmax; if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("vm.stats.vm.v_active_count", active_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("kern.ipc.shmmax", shmmax) == -1) R_ERROR(R_IGNORE); info -> totalram = (u_long)PAGE_2_UNIT(total_pages); info -> freeram = (u_long)PAGE_2_UNIT(free_pages); info -> bufferram = (u_long)PAGE_2_UNIT(active_pages); info -> sharedram = shmmax / UNIT_S; /*high mem (todo)*/ info -> totalhigh = 0; /*Does this supose to refer to HMA or reserved ram?*/ info -> freehigh = 0; return 0; }
void get_system_info(struct system_info *si) { register long total; register int i; unsigned int count = HOST_CPU_LOAD_INFO_COUNT; if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpuload, &count) == KERN_SUCCESS) { for (i = 0; i < CPU_STATE_MAX; i++) { cp_time[i] = cpuload.cpu_ticks[i]; } } #ifdef MAX_VERBOSE /* * print out the entries */ for(i = 0; i < CPU_STATE_MAX; i++) printf("cp_time[%d] = %d\n", i, cp_time[i]); fflush(stdout); #endif /* MAX_VERBOSE */ /* * get the load averages */ if(kvm_getloadavg(kd, si->load_avg, NUM_AVERAGES) == -1) { puke("error: kvm_getloadavg() failed (%s)", strerror(errno)); return; } #ifdef MAX_VERBOSE printf("%-30s%03.2f, %03.2f, %03.2f\n", "load averages:", si->load_avg[0], si->load_avg[1], si->load_avg[2]); #endif /* MAX_VERBOSE */ total = percentages(CPU_STATE_MAX, cpu_states, cp_time, cp_old, cp_diff); /* * get the memory statistics */ { kern_return_t status; count = HOST_VM_INFO_COUNT; status = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count); if(status != KERN_SUCCESS) { puke("error: vm_statistics() failed (%s)", strerror(errno)); return; } /* * we already have the total memory, we just need * to get it in the right format. */ memory_stats[0] = pagetok(maxmem / pagesize); memory_stats[1] = pagetok(vm_stats.free_count); memory_stats[2] = pagetok(vm_stats.active_count); memory_stats[3] = pagetok(vm_stats.inactive_count); memory_stats[4] = pagetok(vm_stats.wire_count); if(swappgsin < 0) { memory_stats[5] = 1; memory_stats[6] = 1; } else { memory_stats[5] = pagetok(((vm_stats.pageins - swappgsin))); memory_stats[6] = pagetok(((vm_stats.pageouts - swappgsout))); } swappgsin = vm_stats.pageins; swappgsout = vm_stats.pageouts; } si->cpustates = cpu_states; si->memory = memory_stats; si->last_pid = -1; return; }