All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
MachineSpecs.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: The Internet */
36 
37 #include "ompl/tools/benchmark/MachineSpecs.h"
38 #include "ompl/util/Console.h"
39 
41 
42 #if defined _WIN32
43 
44 // Windows 2000 or newer
45 #include <winsock2.h>
46 #include <windows.h>
47 #include <stdio.h>
48 #include <psapi.h>
49 
50 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
51 {
52  HANDLE hProcess;
53  PROCESS_MEMORY_COUNTERS pmc;
54 
55  hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
56  PROCESS_VM_READ,
57  false,
58  GetCurrentProcessId() );
59 
60  ompl::machine::MemUsage_t result = 0;
61 
62  if (NULL != hProcess)
63  {
64  if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
65  result = pmc.WorkingSetSize;
66  CloseHandle( hProcess );
67  }
68 
69  return result;
70 }
71 
72 #else
73 #if defined __APPLE__
74 
75 // Mac OS 10.2 or newer
76 #include <mach/mach_init.h>
77 #include <mach/task.h>
78 #include <sys/time.h>
79 #include <sys/resource.h>
80 #include <stdint.h>
81 #include <cstring>
82 #include <unistd.h>
83 
84 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
85 {
86 
87  task_basic_info info;
88  kern_return_t rval = 0;
89  mach_port_t task = mach_task_self();
90  mach_msg_type_number_t tcnt = TASK_BASIC_INFO_COUNT;
91  task_info_t tptr = (task_info_t) &info;
92 
93  memset(&info, 0, sizeof(info));
94 
95  rval = task_info(task, TASK_BASIC_INFO, tptr, &tcnt);
96  if (!(rval == KERN_SUCCESS)) return 0;
97  return info.resident_size;
98 }
99 
100 #else
101 #if defined _POSIX_VERSION || defined _POSIX2_VERSION || defined __linux__
102 // we need a posix compliant os that exposes /proc/self/stat
103 
104 #include <unistd.h>
105 #include <ios>
106 #include <iostream>
107 #include <fstream>
108 
109 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
110 {
111  using std::ios_base;
112  using std::ifstream;
113  using std::string;
114 
115  // 'file' stat seems to give the most reliable results
116  //
117  ifstream stat_stream("/proc/self/stat",ios_base::in);
118 
119  if (stat_stream.good() && !stat_stream.eof())
120  {
121  // dummy vars for leading entries in stat that we don't care about
122  //
123  string pid, comm, state, ppid, pgrp, session, tty_nr;
124  string tpgid, flags, minflt, cminflt, majflt, cmajflt;
125  string utime, stime, cutime, cstime, priority, nice;
126  string O, itrealvalue, starttime;
127 
128 
129  // the two fields we want
130  //
131  unsigned long vsize;
132  long rss;
133 
134  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
135  >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
136  >> utime >> stime >> cutime >> cstime >> priority >> nice
137  >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
138 
139  ompl::machine::MemUsage_t page_size = sysconf(_SC_PAGE_SIZE);
140  return rss * page_size;
141  }
142  return 0;
143 }
144 
145 #else
146 // if we have no idea what to do, we return 0
147 ompl::machine::MemUsage_t getProcessMemoryUsageAux(void)
148 {
149  return 0;
150 }
151 #endif // posix
152 #endif // apple
153 #endif // windows
154 
156 {
157  MemUsage_t result = getProcessMemoryUsageAux();
158  if (result == 0)
159  {
160  OMPL_WARN("Unable to get memory usage");
161  }
162  return result;
163 }
164 
165 std::string ompl::machine::getHostname(void)
166 {
167  static const int BUF_SIZE = 1024;
168  char buffer[BUF_SIZE];
169  int len = gethostname(buffer, sizeof(buffer));
170  if (len != 0)
171  return std::string();
172  else
173  {
174  buffer[BUF_SIZE - 1] = '\0';
175  return std::string(buffer);
176  }
177 }
178 
std::string getHostname(void)
Get the hostname of the machine in use.
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
MemUsage_t getProcessMemoryUsage(void)
Get the amount of memory the current process is using. This should work on major platforms (Windows...
unsigned long long MemUsage_t
Amount of memory used, in bytes.
Definition: MachineSpecs.h:50