size_t Page_Size(void) { #if defined(__MACH30__) kern_return_t result; vm_size_t page_size = 0; result = host_page_size(mach_host_self(), &page_size); MACH_CHECK_ERROR(result); return (size_t)page_size; #else /* ! __MACH30__ */ static struct vm_statistics stats_data; static struct vm_statistics *stats = NULL; kern_return_t kret; if (stats == NULL) { kret = vm_statistics(mach_task_self(), &stats_data); MACH_CHECK_ERROR(kret); stats = &stats_data; } return stats->pagesize; #endif /* ! __MACH30__ */ }
int mach_init() { vm_statistics_data_t vm_stat; kern_return_t kr; #undef mach_task_self /* * Get the important ports into the cached values, * as required by "mach_init.h". */ mach_task_self_ = mach_task_self(); /* * Initialize the single mig reply port */ mig_init(0); /* * Cache some other valuable system constants */ (void) vm_statistics(mach_task_self_, &vm_stat); vm_page_size = vm_stat.pagesize; mach_init_ports(); return(0); }
static error_t rootdir_gc_vmstat (void *hook, char **contents, ssize_t *contents_len) { host_basic_info_data_t hbi; mach_msg_type_number_t cnt; struct vm_statistics vmstats; error_t err; err = vm_statistics (mach_task_self (), &vmstats); if (err) return EIO; cnt = HOST_BASIC_INFO_COUNT; err = host_info (mach_host_self (), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt); if (err) return err; assert (cnt == HOST_BASIC_INFO_COUNT); *contents_len = asprintf (contents, "nr_free_pages %lu\n" "nr_inactive_anon %lu\n" "nr_active_anon %lu\n" "nr_inactive_file %lu\n" "nr_active_file %lu\n" "nr_unevictable %lu\n" "nr_mlock %lu\n" "pgpgin %lu\n" "pgpgout %lu\n" "pgfault %lu\n", (long unsigned) vmstats.free_count, /* FIXME: how can we distinguish the anon/file pages? Maybe we can ask the default pager how many it manages? */ (long unsigned) vmstats.inactive_count, (long unsigned) vmstats.active_count, (long unsigned) 0, (long unsigned) 0, (long unsigned) vmstats.wire_count, (long unsigned) vmstats.wire_count, (long unsigned) vmstats.pageins, (long unsigned) vmstats.pageouts, (long unsigned) vmstats.faults); return 0; }
static error_t rootdir_gc_meminfo (void *hook, char **contents, ssize_t *contents_len) { host_basic_info_data_t hbi; mach_msg_type_number_t cnt; struct vm_statistics vmstats; default_pager_info_t swap; error_t err; err = vm_statistics (mach_task_self (), &vmstats); if (err) return EIO; cnt = HOST_BASIC_INFO_COUNT; err = host_info (mach_host_self (), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt); if (err) return err; err = get_swapinfo (&swap); if (err) return err; assert (cnt == HOST_BASIC_INFO_COUNT); *contents_len = asprintf (contents, "MemTotal: %14lu kB\n" "MemFree: %14lu kB\n" "Active: %14lu kB\n" "Inactive: %14lu kB\n" "Mlocked: %14lu kB\n" "SwapTotal:%14lu kB\n" "SwapFree: %14lu kB\n" , /* TODO: check that these are really 1024-bytes kBs. */ (long unsigned) hbi.memory_size / 1024, (long unsigned) vmstats.free_count * PAGE_SIZE / 1024, (long unsigned) vmstats.active_count * PAGE_SIZE / 1024, (long unsigned) vmstats.inactive_count * PAGE_SIZE / 1024, (long unsigned) vmstats.wire_count * PAGE_SIZE / 1024, (long unsigned) swap.dpi_total_space / 1024, (long unsigned) swap.dpi_free_space / 1024); return 0; }
static error_t rootdir_gc_stat (void *hook, char **contents, ssize_t *contents_len) { struct timeval boottime, time, idletime; struct vm_statistics vmstats; unsigned long up_ticks, idle_ticks; error_t err; err = gettimeofday (&time, NULL); if (err < 0) return errno; err = get_boottime (hook, &boottime); if (err) return err; err = get_idletime (hook, &idletime); if (err) return err; err = vm_statistics (mach_task_self (), &vmstats); if (err) return EIO; timersub (&time, &boottime, &time); up_ticks = opt_clk_tck * (time.tv_sec * 1000000. + time.tv_usec) / 1000000.; idle_ticks = opt_clk_tck * (idletime.tv_sec * 1000000. + idletime.tv_usec) / 1000000.; *contents_len = asprintf (contents, "cpu %lu 0 0 %lu 0 0 0 0 0\n" "cpu0 %lu 0 0 %lu 0 0 0 0 0\n" "intr 0\n" "page %d %d\n" "btime %lu\n", up_ticks - idle_ticks, idle_ticks, up_ticks - idle_ticks, idle_ticks, vmstats.pageins, vmstats.pageouts, boottime.tv_sec); return 0; }
void glibtop_get_mem_s (glibtop *server, glibtop_mem *buf) { vm_statistics_data_t vmstats; glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0); memset (buf, 0, sizeof (glibtop_mem)); buf->flags = _glibtop_sysdeps_mem; (void) vm_statistics(task_self(), &vmstats); buf->free = vmstats.free_count * vmstats.pagesize; buf->used = vmstats.active_count * vmstats.pagesize; /* [FIXME]: Is this correct? */ buf->total = (vmstats.active_count + vmstats.inactive_count + vmstats.free_count + vmstats.wire_count) * vmstats.pagesize; }
get_system_info(struct system_info *si) { long avenrun[3]; long total; /* get the cp_time array */ (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time), "_cp_time"); /* get load average array */ (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun), "_avenrun"); /* get mpid -- process id of last process */ (void) getkval(mpid_offset, &(si->last_pid), sizeof(si->last_pid), "_mpid"); /* convert load averages to doubles */ { register int i; for(i=0; i<3; i++) si->load_avg[i] = ((double)avenrun[i])/LSCALE; } /* convert cp_time counts to percentages */ total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); /* sum memory statistics */ { /* get total -- systemwide main memory usage structure */ /* Does not work on NeXT system. Use vm_statistics() for paging info. */ /* struct vmtotal total; * (void) getkval(total_offset, (int *)(&total), sizeof(total), * "_total"); */ /* convert memory stats to Kbytes */ /* memory_stats[0] = -1; * memory_stats[1] = pagetok(total.t_arm); * memory_stats[2] = pagetok(total.t_rm); * memory_stats[3] = -1; * memory_stats[4] = pagetok(total.t_avm); * memory_stats[5] = pagetok(total.t_vm); * memory_stats[6] = -1; * memory_stats[7] = pagetok(total.t_free); */ kern_return_t status; unsigned int count=HOST_BASIC_INFO_COUNT; status = vm_statistics(task_self(), &vm_stats); #ifdef DEBUG if(status != KERN_SUCCESS) mach_error("An error calling vm_statistics()!", status); #endif status = host_info(host_self(), HOST_BASIC_INFO, (host_info_t)&host_stats, &count); #ifdef DEBUG if(status != KERN_SUCCESS) mach_error("An error calling host_info()!", status); #endif /* convert memory stats to Kbytes */ memory_stats[0] = pagetok(host_stats.memory_size / vm_stats.pagesize); memory_stats[1] = pagetok(vm_stats.active_count); memory_stats[2] = pagetok(vm_stats.inactive_count); memory_stats[3] = pagetok(vm_stats.wire_count); memory_stats[4] = pagetok(vm_stats.free_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; } /* set arrays and strings */ si->cpustates = cpu_states; si->memory = memory_stats; }
GetHwInfo() { struct tbl_sysinfo sibuf; vm_statistics_data_t vmstats; int r_tot; long old_ticks[4],new_ticks[4],diff_ticks[4]; long delta_ticks=0; int i; /* CPU의 사용량을 계산한다. */ loc_sadb->cpuCount = 1; /* TRU64는 모든 CPU의 값을 합해서 사용한다 */ if (table(TBL_SYSINFO,0,&sibuf,1,sizeof(struct tbl_sysinfo))<0) { Error("TBL_SYSINFO"); } old_ticks[0] = sibuf.si_user ; old_ticks[1] = sibuf.si_nice; old_ticks[2] = sibuf.si_sys ; old_ticks[3] = sibuf.si_idle; commlib_microSleep(100000); if (table(TBL_SYSINFO,0,&sibuf,1,sizeof(struct tbl_sysinfo))<0) { Error("TBL_SYSINFO"); } new_ticks[0] = sibuf.si_user ; new_ticks[1] = sibuf.si_nice; new_ticks[2] = sibuf.si_sys ; new_ticks[3] = sibuf.si_idle; for(i=0;i<4;i++) { diff_ticks[i] = new_ticks[i] - old_ticks[i]; delta_ticks += diff_ticks[i]; } if(delta_ticks) { loc_sadb->cpu_usage[0] = 1000 - (long)(diff_ticks[3]*1000/delta_ticks); } /* MEM의 사용량을 계산한다. */ #if 1 (void) vm_statistics(task_self(),&vmstats); r_tot = pagetok((vmstats.free_count + vmstats.active_count + vmstats.inactive_count + vmstats.wire_count ))/1024; loc_sadb->mem_usage = (long) (1000*pagetok(vmstats.active_count + vmstats.wire_count)/1024) /r_tot; #endif #if 0 /* shell command : "vmstat -r 1" */ loc_sadb->mem_usage = getMemUsage_sh (); #endif /* 통계 데이타를 위해 */ statistic_cnt++; #ifdef DEBUG fprintf(stderr,"jean2222 statistic_cnt %d\n", statistic_cnt); #endif for ( i=0 ; i < loc_sadb->cpuCount ; i++) { system_statistic.average_cpu[i] += loc_sadb->cpu_usage[i]; if (system_statistic.max_cpu[i] < loc_sadb->cpu_usage[i] ) { system_statistic.max_cpu[i] = loc_sadb->cpu_usage[i]; } } system_statistic.average_mem += loc_sadb->mem_usage; if (system_statistic.max_mem < loc_sadb->mem_usage) system_statistic.max_mem = loc_sadb->mem_usage; }