TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/common/system/memoryInfo.cpp
Go to the documentation of this file.
00001 #include "system.hpp"
00002 #include "memoryInfo.hpp"
00003 #include <tuttle/common/utils/global.hpp>
00004 
00005 #if defined( __WINDOWS__ )
00006  #include <windows.h>
00007 #elif defined( __LINUX__ )
00008  #include <sys/sysinfo.h>
00009 #elif defined(__APPLE__)
00010  #include <sys/types.h>
00011  #include <sys/sysctl.h>
00012  #include <mach/vm_statistics.h>
00013  #include <mach/mach_types.h> 
00014  #include <mach/mach_init.h>
00015  #include <mach/mach_host.h>
00016 #else
00017  #warning "System unrecognized. Can't found memory infos."
00018  #include <limits>
00019 #endif
00020 
00021 MemoryInfo getMemoryInfo()
00022 {
00023         MemoryInfo infos;
00024 
00025         #if defined( __WINDOWS__ )
00026         MEMORYSTATUS memory;
00027         GlobalMemoryStatus( &memory );
00028 
00029         // memory.dwMemoryLoad;
00030         infos._totalRam = memory.dwTotalPhys;
00031         infos._freeRam  = memory.dwAvailPhys;
00032         //memory.dwTotalPageFile;
00033         //memory.dwAvailPageFile;
00034         infos._totalSwap = memory.dwTotalVirtual;
00035         infos._freeSwap  = memory.dwAvailVirtual;
00036         #elif defined( __LINUX__ )
00037         struct sysinfo sys_info;
00038         sysinfo( &sys_info );
00039 
00040         infos._totalRam = sys_info.totalram * sys_info.mem_unit;
00041         infos._freeRam  = sys_info.freeram * sys_info.mem_unit;
00042         //infos._sharedRam = sys_info.sharedram * sys_info.mem_unit;
00043         //infos._bufferRam = sys_info.bufferram * sys_info.mem_unit;
00044         infos._totalSwap = sys_info.totalswap * sys_info.mem_unit;
00045         infos._freeSwap  = sys_info.freeswap * sys_info.mem_unit;
00046 //      TUTTLE_LOG_VAR( TUTTLE_TRACE, sys_info.sharedram * sys_info.mem_unit );
00047 //      TUTTLE_LOG_VAR( TUTTLE_TRACE, sys_info.bufferram * sys_info.mem_unit );
00048         #elif defined( __APPLE__ )
00049         uint64_t physmem;  
00050         size_t len = sizeof physmem;  
00051         int mib[2] = { CTL_HW, HW_MEMSIZE };  
00052         size_t miblen = sizeof(mib) / sizeof(mib[0]);  
00053 
00054         // Total physical memory.  
00055         if (sysctl(mib, miblen, &physmem, &len, NULL, 0) == 0 && len == sizeof (physmem))  
00056           infos._totalRam = physmem;  
00057 
00058         // Virtual memory.  
00059         mib[0] = CTL_VM; mib[1] = VM_SWAPUSAGE;  
00060         struct xsw_usage swap;  
00061         len = sizeof(struct xsw_usage);  
00062         if (sysctl(mib, miblen, &swap, &len, NULL, 0) == 0)  
00063         {  
00064                 infos._totalSwap = swap.xsu_total;  
00065                 infos._freeSwap  = swap.xsu_avail;
00066         }
00067 
00068         // In use.  
00069         mach_port_t stat_port = mach_host_self();  
00070         vm_size_t page_size;
00071         vm_statistics_data_t vm_stat;  
00072         mach_msg_type_number_t count = sizeof(vm_stat) / sizeof(natural_t);  
00073         if (KERN_SUCCESS == host_page_size(stat_port, &page_size) &&
00074             KERN_SUCCESS == host_statistics(stat_port, HOST_VM_INFO, (host_info_t)&vm_stat, &count))
00075         {  
00076                 //uint64_t used = ((int64_t)vm_stat.active_count + (int64_t)vm_stat.inactive_count + (int64_t)vm_stat.wire_count) * (int64_t)page_size;  
00077                 //infos._freeRam = infos._totalRam - used;  
00078                 infos._freeRam = (int64_t)vm_stat.free_count * (int64_t)page_size;
00079         }
00080         #else
00081         // TODO: could be done on FreeBSD too
00082         // see https://github.com/xbmc/xbmc/blob/master/xbmc/linux/XMemUtils.cpp
00083         infos._totalRam             =
00084             infos._freeRam          =
00085                 infos._totalSwap    =
00086                     infos._freeSwap = std::numeric_limits<std::size_t>::max();
00087         #endif
00088         TUTTLE_LOG_DEBUG( TUTTLE_INFO, "[Memory infos] " << infos );
00089 
00090         return infos;
00091 }
00092 
00093 std::ostream& operator<<( std::ostream& os, const MemoryInfo& infos )
00094 {
00095         os << "total ram:"  << infos._totalRam << std::endl
00096            << "free ram:"   << infos._freeRam << std::endl
00097            << "total swap:" << infos._totalSwap << std::endl
00098            << "free swap:"  << infos._freeSwap << std::endl;
00099         return os;
00100 }