Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkMemoryUtilities.h"
00019
00020 #include <stdio.h>
00021 #if _MSC_VER || __MINGW32__
00022 #include <windows.h>
00023 #include <psapi.h>
00024 #elif defined(__APPLE__)
00025 #include <mach/task.h>
00026 #include <mach/mach_init.h>
00027 #include <mach/mach_host.h>
00028 #else
00029 #include <sys/sysinfo.h>
00030 #include <unistd.h>
00031 #endif
00032
00033
00041 size_t mitk::MemoryUtilities::GetProcessMemoryUsage()
00042 {
00043 #if _MSC_VER || __MINGW32__
00044 size_t size = 0;
00045 DWORD pid = GetCurrentProcessId();
00046 PROCESS_MEMORY_COUNTERS pmc;
00047 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid );
00048 if ( hProcess == NULL )
00049 return 0;
00050 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
00051 {
00052 size = pmc.WorkingSetSize;
00053 }
00054 CloseHandle( hProcess );
00055 return size;
00056 #elif defined(__APPLE__)
00057 struct task_basic_info t_info;
00058 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
00059 task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
00060 size_t size = t_info.virtual_size;
00061 return size;
00062 #else
00063 int size, res, shared, text, sharedLibs, stack, dirtyPages;
00064 if ( ! ReadStatmFromProcFS( &size, &res, &shared, &text, &sharedLibs, &stack, &dirtyPages ) )
00065 return (size_t) size * getpagesize();
00066 else
00067 return 0;
00068 #endif
00069 return 0;
00070 }
00071
00072
00076 size_t mitk::MemoryUtilities::GetTotalSizeOfPhysicalRam()
00077 {
00078 #if _MSC_VER || __MINGW32__
00079 MEMORYSTATUSEX statex;
00080 statex.dwLength = sizeof (statex);
00081 GlobalMemoryStatusEx (&statex);
00082 return (size_t) statex.ullTotalPhys;
00083 #elif defined(__APPLE__)
00084 kern_return_t kr;
00085 host_basic_info_data_t hostinfo;
00086 int count = HOST_BASIC_INFO_COUNT;
00087 kr = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostinfo, (mach_msg_type_number_t*)&count);
00088 if(kr == KERN_SUCCESS)
00089 return (size_t)hostinfo.memory_size;
00090 else
00091 return 0;
00092 #else
00093 struct sysinfo info;
00094 if ( ! sysinfo( &info ) )
00095 return info.totalram * info.mem_unit;
00096 else
00097 return 0;
00098 #endif
00099 }
00100
00101 #ifndef _MSC_VER
00102 #ifndef __APPLE__
00103 int mitk::MemoryUtilities::ReadStatmFromProcFS( int* size, int* res, int* shared, int* text, int* sharedLibs, int* stack, int* dirtyPages )
00104 {
00105 int ret = 0;
00106 FILE* f;
00107 f = fopen( "/proc/self/statm", "r" );
00108 if( f ) {
00109 size_t ignored = fscanf( f, "%d %d %d %d %d %d %d", size, res, shared, text, sharedLibs, stack, dirtyPages );
00110 ++ignored;
00111 fclose( f );
00112 } else {
00113 ret = -1;
00114 }
00115 return ret;
00116 }
00117 #endif
00118 #endif
00119
00120