00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "ompl/util/Memory.h"
00038 #include "ompl/util/Console.h"
00039
00040
00041 #if defined _WIN32
00042
00043
00044 #include <windows.h>
00045 #include <stdio.h>
00046 #include <psapi.h>
00047
00048 ompl::MemUsage_t getProcessMemoryUsageAux(void)
00049 {
00050 HANDLE hProcess;
00051 PROCESS_MEMORY_COUNTERS pmc;
00052
00053 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
00054 PROCESS_VM_READ,
00055 false,
00056 GetCurrentProcessId() );
00057
00058 ompl::MemUsage_t result = 0;
00059
00060 if (NULL != hProcess)
00061 {
00062 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
00063 result = pmc.WorkingSetSize;
00064 CloseHandle( hProcess );
00065 }
00066
00067 return result;
00068 }
00069
00070 #else
00071 #if defined __APPLE__
00072
00073
00074 #include <mach/mach_init.h>
00075 #include <mach/task.h>
00076 #include <sys/time.h>
00077 #include <sys/resource.h>
00078 #include <stdint.h>
00079 #include <cstring>
00080 #include <unistd.h>
00081
00082 ompl::MemUsage_t getProcessMemoryUsageAux(void)
00083 {
00084
00085 task_basic_info info;
00086 kern_return_t rval = 0;
00087 mach_port_t task = mach_task_self();
00088 mach_msg_type_number_t tcnt = TASK_BASIC_INFO_COUNT;
00089 task_info_t tptr = (task_info_t) &info;
00090
00091 memset(&info, 0, sizeof(info));
00092
00093 rval = task_info(task, TASK_BASIC_INFO, tptr, &tcnt);
00094 if (!(rval == KERN_SUCCESS)) return 0;
00095 return info.resident_size;
00096 }
00097
00098 #else
00099 #if defined _POSIX_VERSION
00100
00101
00102 #include <unistd.h>
00103 #include <ios>
00104 #include <iostream>
00105 #include <fstream>
00106 #include <string>
00107
00108 ompl::MemUsage_t getProcessMemoryUsageAux(void)
00109 {
00110 using std::ios_base;
00111 using std::ifstream;
00112 using std::string;
00113
00114
00115
00116 ifstream stat_stream("/proc/self/stat",ios_base::in);
00117
00118 if (stat_stream.good() && !stat_stream.eof())
00119 {
00120
00121
00122 string pid, comm, state, ppid, pgrp, session, tty_nr;
00123 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
00124 string utime, stime, cutime, cstime, priority, nice;
00125 string O, itrealvalue, starttime;
00126
00127
00128
00129
00130 unsigned long vsize;
00131 long rss;
00132
00133 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
00134 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
00135 >> utime >> stime >> cutime >> cstime >> priority >> nice
00136 >> O >> itrealvalue >> starttime >> vsize >> rss;
00137
00138 unsigned long long page_size = sysconf(_SC_PAGE_SIZE);
00139 return rss * page_size;
00140 }
00141 return 0;
00142 }
00143
00144 #else
00145
00146 ompl::MemUsage_t getProcessMemoryUsageAux(void)
00147 {
00148 return 0;
00149 }
00150 #endif // posix
00151 #endif // apple
00152 #endif // windows
00153
00154 ompl::MemUsage_t ompl::getProcessMemoryUsage(void)
00155 {
00156 MemUsage_t result = getProcessMemoryUsageAux();
00157 if (result == 0)
00158 {
00159 static msg::Interface memMsg;
00160 memMsg.warn("Unable to get memory usage");
00161 }
00162 return result;
00163 }