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