CMemoryFunction::CMemoryFunction(const void* code, size_t size) : m_code(nullptr) { #ifdef WIN32 m_size = size; m_code = malloc(size); memcpy(m_code, code, size); DWORD oldProtect = 0; BOOL result = VirtualProtect(m_code, size, PAGE_EXECUTE_READWRITE, &oldProtect); assert(result == TRUE); #elif defined(__APPLE__) vm_size_t page_size = 0; host_page_size(mach_task_self(), &page_size); unsigned int allocSize = ((size + page_size - 1) / page_size) * page_size; vm_allocate(mach_task_self(), reinterpret_cast<vm_address_t*>(&m_code), allocSize, TRUE); memcpy(m_code, code, size); sys_icache_invalidate(m_code, size); kern_return_t result = vm_protect(mach_task_self(), reinterpret_cast<vm_address_t>(m_code), size, 0, VM_PROT_READ | VM_PROT_EXECUTE); assert(result == 0); m_size = allocSize; #elif defined(__ANDROID__) || defined(__linux__) || defined(__FreeBSD__) m_size = size; m_code = mmap(nullptr, size, PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); assert(m_code != MAP_FAILED); memcpy(m_code, code, size); #if defined(__arm__) || defined(__aarch64__) __clear_cache(m_code, reinterpret_cast<uint8*>(m_code) + size); #endif #endif }
size_t oskar_get_free_physical_memory(void) { #ifdef OSKAR_OS_LINUX size_t freePhysMem = 0; struct sysinfo memInfo; sysinfo(&memInfo); freePhysMem = memInfo.freeram; freePhysMem *= memInfo.mem_unit; return freePhysMem; #elif defined(OSKAR_OS_MAC) vm_size_t page_size; mach_port_t mach_port; mach_msg_type_number_t count; vm_statistics64_data_t vm_stats; mach_port = mach_host_self(); count = sizeof(vm_stats) / sizeof(natural_t); if (KERN_SUCCESS == host_page_size(mach_port, &page_size) && KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO, (host_info64_t)&vm_stats, &count)) { /* Note on OS X since 10.9, compressed memory makes this value somewhat * flexible. */ return (size_t)((int64_t)vm_stats.free_count*(int64_t)page_size); } return 0; #elif defined(OSKAR_OS_WIN) MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memInfo); DWORDLONG freePhysMem = memInfo.ullAvailPhys; return (size_t)freePhysMem; #endif }
bool ksmach_i_VMStats(vm_statistics_data_t* const vmStats, vm_size_t* const pageSize) { kern_return_t kr; const mach_port_t hostPort = mach_host_self(); if((kr = host_page_size(hostPort, pageSize)) != KERN_SUCCESS) { KSLOG_ERROR("host_page_size: %s", mach_error_string(kr)); return false; } mach_msg_type_number_t hostSize = sizeof(*vmStats) / sizeof(natural_t); kr = host_statistics(hostPort, HOST_VM_INFO, (host_info_t)vmStats, &hostSize); if(kr != KERN_SUCCESS) { KSLOG_ERROR("host_statistics: %s", mach_error_string(kr)); return false; } return true; }
int run_get_dynamic_sys_info(RunSysDyn *rsd) { int mib[4]; size_t len; vm_size_t pagesize; vmtotal_t value; struct timeval tp; struct timezone tzp; vm_statistics_data_t vm_stat; mach_msg_type_number_t count; kern_return_t error; count = sizeof(vm_stat) / sizeof(natural_t); error = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stat, &count); assert(error == KERN_SUCCESS && count == sizeof(vm_stat) / sizeof(natural_t)); host_page_size(mach_host_self(), &pagesize); rsd->mem_free = (size_t)vm_stat.free_count * pagesize; rsd->mem_available = (size_t)(vm_stat.free_count + vm_stat.inactive_count) * pagesize; gettimeofday(&tp, &tzp); rsd->wall_clock = tp.tv_sec + tp.tv_usec * 1e-6; mib[0] = CTL_VM; mib[1] = VM_METER; len = sizeof(vmtotal_t); sysctl(mib, 2, &value, &len, 0, 0); assert(len == sizeof(vmtotal_t)); return 0; }
int mach_init() { 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) host_page_size(mach_host_self(), &vm_page_size); return(0); }
mach_error_t freeBranchIsland( BranchIsland *island ) { assert( island ); assert( (*(long*)&island->instructions[0]) == kIslandTemplate[0] ); assert( island->allocatedHigh ); mach_error_t err = err_none; if( island->allocatedHigh ) { vm_size_t pageSize; err = host_page_size( mach_host_self(), &pageSize ); if( !err ) { assert( sizeof( BranchIsland ) <= pageSize ); err = vm_deallocate( mach_task_self(), (vm_address_t) island, pageSize ); } } else { free( island ); } return err; }
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__ */ }
void darwin_init(pmdaInterface *dp) { if (_isDSO) { int sep = __pmPathSeparator(); char helppath[MAXPATHLEN]; sprintf(helppath, "%s%c" "darwin" "%c" "help", pmGetConfig("PCP_PMDAS_DIR"), sep, sep); pmdaDSO(dp, PMDA_INTERFACE_3, "darwin DSO", helppath); } else { __pmSetProcessIdentity(username); } if (dp->status != 0) return; dp->version.two.instance = darwin_instance; dp->version.two.fetch = darwin_fetch; pmdaSetFetchCallBack(dp, darwin_fetchCallBack); pmdaSetFlags(dp, PMDA_EXT_FLAG_DIRECT); pmdaInit(dp, indomtab, sizeof(indomtab)/sizeof(indomtab[0]), metrictab, sizeof(metrictab)/sizeof(metrictab[0])); mach_host = mach_host_self(); host_page_size(mach_host, &mach_page_size); mach_page_shift = ffs(mach_page_size) - 1; if (refresh_hertz(&mach_hertz) != 0) mach_hertz = 100; init_network(); }
mach_error_t allocateBranchIsland( BranchIsland **island, int allocateHigh, void *originalFunctionAddress) { assert( island ); mach_error_t err = err_none; if( allocateHigh ) { vm_size_t pageSize; err = host_page_size( mach_host_self(), &pageSize ); if( !err ) { assert( sizeof( BranchIsland ) <= pageSize ); #if defined(__x86_64__) vm_address_t first = (uint64_t)originalFunctionAddress & ~(uint64_t)(((uint64_t)1 << 31) - 1) | ((uint64_t)1 << 31); // start in the middle of the page? vm_address_t last = 0x0; #else vm_address_t first = 0xfeffffff; vm_address_t last = 0xfe000000 + pageSize; #endif vm_address_t page = first; int allocated = 0; vm_map_t task_self = mach_task_self(); while( !err && !allocated && page != last ) { err = vm_allocate( task_self, &page, pageSize, 0 ); if( err == err_none ) allocated = 1; else if( err == KERN_NO_SPACE ) { #if defined(__x86_64__) page -= pageSize; #else page += pageSize; #endif err = err_none; } } if( allocated ) *island = (void*) page; else if( !allocated && !err ) err = KERN_NO_SPACE; } } else { void *block = malloc( sizeof( BranchIsland ) ); if( block ) *island = block; else err = KERN_NO_SPACE; } if( !err ) (**island).allocatedHigh = allocateHigh; return err; }
/** * lw6sys_megabytes_available * * @sys_context: global system context * * Gives a raw approximation of available memory, in megabytes. * Value is to be taken with distance, but it can give good hints * when system is running short of ressources. * * Return value: number of megabytes (physical memory) available. */ int lw6sys_megabytes_available (lw6sys_context_t * sys_context) { int ret = 0; #ifdef LW6_MS_WINDOWS MEMORYSTATUS status; memset (&status, 0, sizeof (status)); status.dwLength = sizeof (status); GlobalMemoryStatus (&status); ret = status.dwAvailPhys / MEM_DIVIDE; #else // LW6_MS_WINDOWS #ifdef LW6_MAC_OS_X vm_size_t page_size; mach_port_t mach_port; mach_msg_type_number_t count; vm_statistics_data_t vm_stats; int64_t freeram = 0; mach_port = mach_host_self (); count = sizeof (vm_stats) / sizeof (natural_t); if (KERN_SUCCESS == host_page_size (mach_port, &page_size) && KERN_SUCCESS == host_statistics (mach_port, HOST_VM_INFO, (host_info_t) & vm_stats, &count)) { freeram = (int64_t) vm_stats.free_count * (int64_t) page_size; } freeram /= MEM_DIVIDE; ret = freeram; #else // LW6_MAC_OS_X #ifdef HAVE_SYS_SYSINFO_H /* * sys/sysinfo.h is Linux specific */ struct sysinfo meminfo; int64_t freeram = 0; memset (&meminfo, 0, sizeof (struct sysinfo)); sysinfo (&meminfo); freeram = meminfo.freeram; freeram *= meminfo.mem_unit; freeram /= MEM_DIVIDE; ret = freeram; #else // HAVE_SYS_SYSINFO_H #ifdef _SC_AVPHYS_PAGES /* * Fallback, seems to return low values, dunno why */ ret = (sysconf (_SC_PAGESIZE) * sysconf (_SC_AVPHYS_PAGES)) / MEM_DIVIDE; #else // _SC_AVPHYS_PAGES lw6sys_log (sys_context, LW6SYS_LOG_WARNING, _x_ ("unable to guess how much free memory is available")); #endif // _SC_AVPHYS_PAGES #endif // HAVE_SYS_SYSINFO_H #endif // LW6_MAC_OS_X #endif // LW6_MS_WINDOWS return ret; }
static int tsk_pagesize(RIODesc *desc) { int tid = __get_pid (desc); task_t task = pid_to_task (desc, tid); static vm_size_t pagesize = 0; return pagesize ? pagesize : (host_page_size (task, &pagesize) == KERN_SUCCESS) ? pagesize : 4096; }
bool ZGPageSize(ZGMemoryMap processTask, ZGMemorySize *pageSize) { vm_size_t tempPageSize; kern_return_t retValue = host_page_size(processTask, &tempPageSize); if (retValue == KERN_SUCCESS) { *pageSize = tempPageSize; } return (retValue == KERN_SUCCESS); }
int ofxMemoryUsage::getUsedMemory(){ int mb = 0; int gb = 0; #ifdef TARGET_WIN32 MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memInfo); DWORDLONG used_memory = memInfo.ullTotalPhys - memInfo.ullAvailPhys; mb = used_memory/1048576; #endif #ifdef TARGET_LINUX struct sysinfo memInfo; sysinfo (&memInfo); long long used_memory = memInfo.totalram - memInfo.freeram; used_memory *= memInfo.mem_unit; mb = used_memory/1048576; #endif #if defined(TARGET_OSX) || defined(TARGET_OF_IPHONE) vm_size_t page_size; mach_port_t mach_port; mach_msg_type_number_t count; vm_statistics_data_t vm_stats; int64_t used_memory = 0; int64_t myFreeMemory = 0; mach_port = mach_host_self(); count = sizeof(vm_stats) / sizeof(natural_t); if (KERN_SUCCESS == host_page_size(mach_port, &page_size) && KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count)) { myFreeMemory = (int64_t)vm_stats.free_count * (int64_t)page_size; used_memory = ((int64_t)vm_stats.active_count + (int64_t)vm_stats.inactive_count + (int64_t)vm_stats.wire_count) * (int64_t)page_size; mb = used_memory/1048576; } // int mb = used_memory/1048576; // printf("physical_memory : used : mb : %i\n",mb); // mb = myFreeMemory/1048576; // printf("physical_memory : free : mb : %i\n",mb); #endif return mb; }
static int tsk_pagesize(RIOMach *riom) { //cache the pagesize static vm_size_t pagesize = 0; kern_return_t kr; if (pagesize) return pagesize; kr = host_page_size (mach_host_self (), &pagesize); if (kr != KERN_SUCCESS) pagesize = 4096; return pagesize; }
static int32 GetFreeMemoryMB() { // get free memory vm_size_t PageSize; host_page_size(mach_host_self(), &PageSize); // get memory stats vm_statistics Stats; mach_msg_type_number_t StatsSize = sizeof(Stats); host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&Stats, &StatsSize); return (Stats.free_count * PageSize) / 1024 / 1024; }
mach_error_t makeIslandExecutable(void *address) { mach_error_t err = err_none; vm_size_t pageSize; host_page_size( mach_host_self(), &pageSize ); uintptr_t page = (uintptr_t)address & ~(uintptr_t)(pageSize-1); int e = err_none; e |= mprotect((void *)page, pageSize, PROT_EXEC | PROT_READ | PROT_WRITE); e |= msync((void *)page, pageSize, MS_INVALIDATE ); if (e) { err = err_cannot_override; } return err; }
int VM_MEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result) { char *mode; int ret = SYSINFO_RET_FAIL; if (1 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } if (0 == pagesize) { if (KERN_SUCCESS != host_page_size(mach_host_self(), &pagesize)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain host page size.")); return SYSINFO_RET_FAIL; } } mode = get_rparam(request, 0); if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total")) ret = VM_MEMORY_TOTAL(result); else if (0 == strcmp(mode, "active")) ret = VM_MEMORY_ACTIVE(result); else if (0 == strcmp(mode, "inactive")) ret = VM_MEMORY_INACTIVE(result); else if (0 == strcmp(mode, "wired")) ret = VM_MEMORY_WIRED(result); else if (0 == strcmp(mode, "free")) ret = VM_MEMORY_FREE(result); else if (0 == strcmp(mode, "used")) ret = VM_MEMORY_USED(result); else if (0 == strcmp(mode, "pused")) ret = VM_MEMORY_PUSED(result); else if (0 == strcmp(mode, "available")) ret = VM_MEMORY_AVAILABLE(result); else if (0 == strcmp(mode, "pavailable")) ret = VM_MEMORY_PAVAILABLE(result); else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } return ret; }
EXPORT void GetSystemInfo(LPSYSTEM_INFO lpSystemInfo){ host_name_port_t myhost; host_basic_info_data_t hinfo; vm_size_t page_size; mach_msg_type_number_t count; myhost = mach_host_self(); count = HOST_BASIC_INFO_COUNT; host_info(myhost, HOST_BASIC_INFO, (host_info_t) &hinfo, &count); host_page_size(myhost, &page_size); lpSystemInfo->dwPageSize = page_size; lpSystemInfo->dwNumberOfProcessors = hinfo.avail_cpus; return; }
FMalloc* FIOSPlatformMemory::BaseAllocator() { // get free memory vm_size_t PageSize; host_page_size(mach_host_self(), &PageSize); vm_statistics Stats; mach_msg_type_number_t StatsSize = sizeof(Stats); host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&Stats, &StatsSize); // 1 << FMath::CeilLogTwo(MemoryConstants.TotalPhysical) should really be FMath::RoundUpToPowerOfTwo, // but that overflows to 0 when MemoryConstants.TotalPhysical is close to 4GB, since CeilLogTwo returns 32 // this then causes the MemoryLimit to be 0 and crashing the app uint64 MemoryLimit = FMath::Min<uint64>( uint64(1) << FMath::CeilLogTwo(Stats.free_count * PageSize), 0x100000000); //return new FMallocAnsi(); return new FMallocBinned(PageSize, MemoryLimit); }
static int memory_init(void) { #if HAVE_HOST_STATISTICS port_host = mach_host_self(); host_page_size(port_host, &pagesize); /* #endif HAVE_HOST_STATISTICS */ #elif HAVE_SYSCTLBYNAME /* no init stuff */ /* #endif HAVE_SYSCTLBYNAME */ #elif defined(KERNEL_LINUX) /* no init stuff */ /* #endif KERNEL_LINUX */ #elif defined(HAVE_LIBKSTAT) /* getpagesize(3C) tells me this does not fail.. */ pagesize = getpagesize(); if (get_kstat(&ksp, "unix", 0, "system_pages") != 0) { ksp = NULL; return -1; } if (get_kstat(&ksz, "zfs", 0, "arcstats") != 0) { ksz = NULL; return -1; } /* #endif HAVE_LIBKSTAT */ #elif HAVE_SYSCTL pagesize = getpagesize(); if (pagesize <= 0) { ERROR("memory plugin: Invalid pagesize: %i", pagesize); return -1; } /* #endif HAVE_SYSCTL */ #elif HAVE_LIBSTATGRAB /* no init stuff */ /* #endif HAVE_LIBSTATGRAB */ #elif HAVE_PERFSTAT pagesize = getpagesize(); #endif /* HAVE_PERFSTAT */ return 0; } /* int memory_init */
void sys::OSUnix::getMemInfo(size_t &totalPhysMem, size_t &freePhysMem) const { // Unfortunately sysctl is the best way to do this on OSX, // but sysctl is deprecated in favor of sysconf on linux #if defined(__APPLE__) long long physMem = 0; size_t size = sizeof(physMem); int status = sysctlbyname("hw.memsize", &physMem, &size, 0, 0); if(status) { throw sys::SystemException(Ctxt("Call to sysctl() has failed")); } mach_port_t machPort = mach_host_self(); mach_msg_type_number_t count = HOST_VM_INFO_COUNT; vm_size_t pageSize = 0; vm_statistics_data_t vmstat; if(KERN_SUCCESS != host_statistics(machPort, HOST_VM_INFO, (host_info_t) &vmstat, &count)) { throw sys::SystemException(Ctxt("Call to host_statistics() has failed")); } if(KERN_SUCCESS != host_page_size(machPort, &pageSize)) { throw sys::SystemException(Ctxt("Call to host_page_size has failed")); } long long freeBytes = vmstat.free_count * pageSize; totalPhysMem = physMem / 1024 / 1024; freePhysMem = freeBytes / 1024 / 1024; #else long long pageSize = sysconfCaller(_SC_PAGESIZE); long long totalNumPages = sysconfCaller(_SC_PHYS_PAGES); long long availNumPages = sysconfCaller(_SC_AVPHYS_PAGES); totalPhysMem = (pageSize*totalNumPages/1024)/1024; freePhysMem = (pageSize*availNumPages/1024)/1024; #endif }
vm_size_t child_get_pagesize () { kern_return_t status; static vm_size_t g_cached_child_page_size = 0; if (g_cached_child_page_size == -1) { status = host_page_size (mach_host_self (), &g_cached_child_page_size); /* This is probably being over-careful, since if we can't call host_page_size on ourselves, we probably aren't going to get much further. */ if (status != KERN_SUCCESS) g_cached_child_page_size = 0; MACH_CHECK_ERROR (status); } return g_cached_child_page_size; }
/*==========================================================================*/ size_t SystemInformation::FreeMemorySize() { // Windows #if defined ( KVS_PLATFORM_WINDOWS ) #if defined ( KVS_PLATFORM_CPU_64 ) MEMORYSTATUSEX memstat; GlobalMemoryStatusEx( &memstat ); return memstat.ullAvailPhys; #else MEMORYSTATUS memstat; GlobalMemoryStatus( &memstat ); return memstat.dwAvailPhys; #endif // Linux #elif defined ( KVS_PLATFORM_LINUX ) || defined ( KVS_PLATFORM_CYGWIN ) long avphys_page_size = sysconf( _SC_AVPHYS_PAGES ); kvsMessageWarning( avphys_page_size != -1, ::GetWarningMessage( errno, "_SC_AVPHYS_PAGES is not supported." ) ); long page_size = sysconf( _SC_PAGESIZE ); kvsMessageWarning( page_size != -1, ::GetWarningMessage( errno, "_SC_PAGESIZE is not supported." ) ); return avphys_page_size * page_size; // Mac OS X #elif defined ( KVS_PLATFORM_MACOSX ) kern_return_t kr; vm_size_t page_size = 0; kr = host_page_size( mach_host_self(), &page_size ); kvsMessageWarning( kr != KERN_SUCCESS, "Failure to get page size." ); vm_statistics_data_t page_info; mach_msg_type_number_t count = HOST_VM_INFO_COUNT; kr = host_statistics( mach_host_self(), HOST_VM_INFO, (host_info_t)&page_info, &count ); kvsMessageWarning( kr != KERN_SUCCESS, "Failure to get page info." ); return page_info.free_count * page_size; #endif }
//==================================================================== size_t KLHeapDebug_GetRemain() { mach_port_t hostport; mach_msg_type_number_t szHost; vm_size_t szPage; vm_statistics_data_t stat; hostport = mach_host_self(); szHost = sizeof(vm_statistics_data_t) / sizeof(integer_t); host_page_size( hostport, &szPage ); if( host_statistics( hostport, HOST_VM_INFO, (host_info_t)&stat, &szHost) != KERN_SUCCESS ) { KLLog("[KLHeapDebug] failed GetRemain."); return 0; } return stat.free_count * szPage; }
bool get_available_memory(available_memory_info* info) { const mach_port_t host_port = mach_host_self(); mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t); vm_size_t pagesize; host_page_size(host_port, &pagesize); vm_statistics_data_t vm_stat; if(host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) { return false; } if(info != NULL) { info->mem_used_kb = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * (pagesize/1024); info->mem_free_kb = vm_stat.free_count * (pagesize/1024); info->mem_total_kb = info->mem_used_kb + info->mem_free_kb; } return true; }
/** * Get usage of physical memory and save data to in a SQLite db */ void OSXDeviceInfo::physicalMemory() { long long free_memory; long long used_memory; vm_size_t page_size; mach_port_t mach_port; mach_msg_type_number_t count; vm_statistics64_data_t vm_stats; mach_port = mach_host_self(); count = sizeof(vm_stats) / sizeof(natural_t); // get total physical memory in bytes int mib[2]; int64_t physical_memory; mib[0] = CTL_HW; mib[1] = HW_MEMSIZE; std::size_t length = sizeof(int64_t); sysctl(mib, 2, &physical_memory, &length, NULL, 0); if (KERN_SUCCESS == host_page_size(mach_port, &page_size) && KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO, (host_info64_t)&vm_stats, &count)) { // get free and used memory in bytes free_memory = (int64_t)vm_stats.free_count * (int64_t)page_size; used_memory = ((int64_t)vm_stats.active_count + (int64_t)vm_stats.inactive_count + (int64_t)vm_stats.wire_count) * (int64_t)page_size; } // create a query to save data in Mb std::string sql = "INSERT INTO memory(total, used, available, time) " \ "VALUES (" + std::to_string(physical_memory/(1024*1024)) + "," + \ std::to_string(used_memory/(1024*1024)) + "," + \ std::to_string(free_memory/(1024*1024)) + "," \ "strftime('%H:%M','now', 'localtime'));"; db->query(sql.c_str()); }
int SysInfo::GetMemoryInfo(int* wired, int* active, int* inactive, int* free) { int poll = 0; kern_return_t ke = KERN_SUCCESS; mach_port_t host; vm_size_t hps; vm_statistics_data_t hs; mach_msg_type_number_t vmsz; host = mach_host_self(); vmsz = sizeof(hs) / sizeof(integer_t); while (1) { if ((ke = host_page_size(host, &hps)) != KERN_SUCCESS) { return EXIT_FAILURE; } ke = host_statistics(host, HOST_VM_INFO, (host_info_t) &hs, &vmsz); if (ke != KERN_SUCCESS) { return EXIT_FAILURE; } int used = ((hs.active_count + hs.inactive_count + hs.wire_count)* hps); *free = (hs.free_count * hps); *active = (hs.active_count * hps); *inactive = (hs.inactive_count * hps); *wired = (hs.wire_count * hps); if (poll != 0) { sleep(poll); printf("\n"); } else { break; } } return EXIT_SUCCESS; }
int machine_init(void) { /* * get the page size with "getpagesize" and calculate pageshift from * it */ unsigned int pagesize = 0; pageshift = 0; lcdproc_port = mach_host_self(); host_page_size(lcdproc_port, &pagesize); while (pagesize > 1) { pageshift++; pagesize >>= 1; } /* we only need the amount of log(2)1024 for our conversion */ pageshift -= 10; return (TRUE); }
VALUE method_memory(VALUE self) { VALUE memory = Qnil; vm_size_t pagesize; uint host_count = HOST_VM_INFO_COUNT; kern_return_t err; vm_statistics_data_t vm_stat; err = host_page_size(mach_host_self(), &pagesize); if (err == KERN_SUCCESS) { err = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stat, &host_count); if (err == KERN_SUCCESS) { memory = rb_hash_new(); rb_hash_aset(memory, SYM_PAGESIZE, ULL2NUM(pagesize)); rb_hash_aset(memory, SYM_WIRED, ULL2NUM(vm_stat.active_count)); rb_hash_aset(memory, SYM_ACTIVE, ULL2NUM(vm_stat.inactive_count)); rb_hash_aset(memory, SYM_INACTIVE, ULL2NUM(vm_stat.wire_count)); rb_hash_aset(memory, SYM_FREE, ULL2NUM(vm_stat.free_count)); rb_hash_aset(memory, SYM_WIRED_BYTES, ULL2NUM(vm_stat.wire_count * pagesize)); rb_hash_aset(memory, SYM_ACTIVE_BYTES, ULL2NUM(vm_stat.active_count * pagesize)); rb_hash_aset(memory, SYM_INACTIVE_BYTES, ULL2NUM(vm_stat.inactive_count * pagesize)); rb_hash_aset(memory, SYM_FREE_BYTES, ULL2NUM(vm_stat.free_count * pagesize)); rb_hash_aset(memory, SYM_ZERO_FILLED, ULL2NUM(vm_stat.zero_fill_count)); rb_hash_aset(memory, SYM_REACTIVATED, ULL2NUM(vm_stat.reactivations)); rb_hash_aset(memory, SYM_PURGEABLE, ULL2NUM(vm_stat.purgeable_count)); rb_hash_aset(memory, SYM_PURGED, ULL2NUM(vm_stat.purges)); rb_hash_aset(memory, SYM_PAGEINS, ULL2NUM(vm_stat.pageins)); rb_hash_aset(memory, SYM_PAGEOUTS, ULL2NUM(vm_stat.pageouts)); rb_hash_aset(memory, SYM_FAULTS, ULL2NUM(vm_stat.faults)); rb_hash_aset(memory, SYM_COW_FAULTS, ULL2NUM(vm_stat.cow_faults)); rb_hash_aset(memory, SYM_LOOKUPS, ULL2NUM(vm_stat.lookups)); rb_hash_aset(memory, SYM_HITS, ULL2NUM(vm_stat.hits)); } } return memory; }
const FPlatformMemoryConstants& FIOSPlatformMemory::GetConstants() { static FPlatformMemoryConstants MemoryConstants; if( MemoryConstants.TotalPhysical == 0 ) { // Gather platform memory constants. // Get page size. vm_size_t PageSize; host_page_size(mach_host_self(), &PageSize); // Get swap file info xsw_usage SwapUsage; SIZE_T Size = sizeof(SwapUsage); sysctlbyname("vm.swapusage", &SwapUsage, &Size, NULL, 0); // Get memory. vm_statistics Stats; mach_msg_type_number_t StatsSize = sizeof(Stats); host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&Stats, &StatsSize); uint64_t FreeMem = Stats.free_count * PageSize; uint64_t UsedMem = (Stats.active_count + Stats.inactive_count + Stats.wire_count) * PageSize; uint64_t TotalPhys = FreeMem + UsedMem; uint64_t TotalPageFile = SwapUsage.xsu_total; uint64_t TotalVirtual = TotalPhys + TotalPageFile; MemoryConstants.TotalPhysical = TotalPhys; MemoryConstants.TotalVirtual = TotalVirtual; MemoryConstants.PageSize = (uint32)PageSize; MemoryConstants.TotalPhysicalGB = (MemoryConstants.TotalPhysical + 1024 * 1024 * 1024 - 1) / 1024 / 1024 / 1024; } return MemoryConstants; }