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/tools/benchmark/MachineSpecs.h"
00038 #include "ompl/util/Console.h"
00039
00040 #ifndef _WIN32
00041 #include <unistd.h>
00042 #endif
00043
00045
00046 #if defined _WIN32
00047
00048
00049 #include <windows.h>
00050 #include <stdio.h>
00051 #include <psapi.h>
00052 #include <winsock2.h>
00053
00054 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
00055 {
00056 HANDLE hProcess;
00057 PROCESS_MEMORY_COUNTERS pmc;
00058
00059 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
00060 PROCESS_VM_READ,
00061 false,
00062 GetCurrentProcessId() );
00063
00064 ompl::machine::MemUsage_t result = 0;
00065
00066 if (NULL != hProcess)
00067 {
00068 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
00069 result = pmc.WorkingSetSize;
00070 CloseHandle( hProcess );
00071 }
00072
00073 return result;
00074 }
00075
00076 #else
00077 #if defined __APPLE__
00078
00079
00080 #include <mach/mach_init.h>
00081 #include <mach/task.h>
00082 #include <sys/time.h>
00083 #include <sys/resource.h>
00084 #include <stdint.h>
00085 #include <cstring>
00086 #include <unistd.h>
00087
00088 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
00089 {
00090
00091 task_basic_info info;
00092 kern_return_t rval = 0;
00093 mach_port_t task = mach_task_self();
00094 mach_msg_type_number_t tcnt = TASK_BASIC_INFO_COUNT;
00095 task_info_t tptr = (task_info_t) &info;
00096
00097 memset(&info, 0, sizeof(info));
00098
00099 rval = task_info(task, TASK_BASIC_INFO, tptr, &tcnt);
00100 if (!(rval == KERN_SUCCESS)) return 0;
00101 return info.resident_size;
00102 }
00103
00104 #else
00105 #if defined _POSIX_VERSION
00106
00107
00108 #include <unistd.h>
00109 #include <ios>
00110 #include <iostream>
00111 #include <fstream>
00112
00113 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
00114 {
00115 using std::ios_base;
00116 using std::ifstream;
00117 using std::string;
00118
00119
00120
00121 ifstream stat_stream("/proc/self/stat",ios_base::in);
00122
00123 if (stat_stream.good() && !stat_stream.eof())
00124 {
00125
00126
00127 string pid, comm, state, ppid, pgrp, session, tty_nr;
00128 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
00129 string utime, stime, cutime, cstime, priority, nice;
00130 string O, itrealvalue, starttime;
00131
00132
00133
00134
00135 unsigned long vsize;
00136 long rss;
00137
00138 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
00139 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
00140 >> utime >> stime >> cutime >> cstime >> priority >> nice
00141 >> O >> itrealvalue >> starttime >> vsize >> rss;
00142
00143 ompl::machine::MemUsage_t page_size = sysconf(_SC_PAGE_SIZE);
00144 return rss * page_size;
00145 }
00146 return 0;
00147 }
00148
00149 #else
00150
00151 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
00152 {
00153 return 0;
00154 }
00155 #endif // posix
00156 #endif // apple
00157 #endif // windows
00158
00159 ompl::machine::MemUsage_t ompl::machine::getProcessMemoryUsage(void)
00160 {
00161 MemUsage_t result = getProcessMemoryUsageAux();
00162 if (result == 0)
00163 {
00164 static msg::Interface memMsg;
00165 memMsg.warn("Unable to get memory usage");
00166 }
00167 return result;
00168 }
00169
00170 std::string ompl::machine::getHostname(void)
00171 {
00172 static const int BUF_SIZE = 1024;
00173 char buffer[BUF_SIZE];
00174 int len = gethostname(buffer, sizeof(buffer));
00175 if (len != 0)
00176 return std::string();
00177 else
00178 {
00179 buffer[BUF_SIZE - 1] = '\0';
00180 return std::string(buffer);
00181 }
00182 }
00183