All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PlatformUtils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/PlatformUtils.cpp
22 
23  \brief This file contains several utility functions when dealing with Linux specific API.
24 */
25 
26 // TerraLib
27 #include "../Defines.h"
28 #include "Exception.h"
29 #include "PlatformUtils.h"
30 #include "StringUtils.h"
31 #include "Translator.h"
32 
33 // STL
34 #include <fstream>
35 
36 // O.S. Specific
37 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
38 #include <windows.h>
39 #include <winbase.h>
40 
41 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD
42 #include <cstring>
43 #include <errno.h>
44 #include <dirent.h>
45 #include <malloc.h>
46 #include <sys/resource.h>
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <sys/types.h>
50 #include <unistd.h>
51 
52 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE
53 #include <cstdlib>
54 #include <dirent.h>
55 #include <sys/stat.h>
56 #include <sys/sysctl.h>
57 
58 #else
59  #error "Unsuported plataform for physical memory checking"
60 #endif
61 
62 #include <cstdio>
63 #include <cstdlib>
64 
65 // Boost
66 #include <boost/filesystem.hpp>
67 
69 {
70  unsigned long int freemem = 0;
71 
72 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD || TE_PLATFORM == TE_PLATFORMCODE_APPLE
73  unsigned int usermem;
74 
75  std::size_t usermem_len = sizeof(usermem);
76 
77  int mib[2] = { CTL_HW, HW_USERMEM };
78 
79  if(sysctl(mib, (2 * sizeof(int)), &usermem, &usermem_len, NULL, 0) == 0)
80  {
81  freemem = static_cast<unsigned long int>(usermem);
82  }
83  else
84  {
85  throw Exception("Could not get free physical memory!");
86  }
87 
88 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
89  freemem = static_cast<unsigned long int>( sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES) );
90 
91 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
92  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
93  GlobalMemoryStatus(status_buffer);
94  freemem = static_cast<unsigned long int>(status_buffer->dwAvailPhys);
95  delete status_buffer;
96 #else
97  #error "Unsuported plataform for physical memory checking"
98 #endif
99 
100  return freemem;
101 }
102 
104 {
105  unsigned long int totalmem = 0;
106 
107 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD || TE_PLATFORM == TE_PLATFORMCODE_APPLE
108  unsigned int physmem = 0;
109 
110  std::size_t physmem_len = sizeof(physmem);
111 
112  int mib[2] = { CTL_HW, HW_PHYSMEM };
113 
114  if(sysctl(mib, (2 * sizeof(int)), &physmem, &physmem_len, NULL, 0) == 0)
115  {
116  totalmem = static_cast<unsigned long int>(physmem);
117  }
118  else
119  {
120  throw Exception("Could not get total physical memory!");
121  }
122 
123 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
124  totalmem = static_cast<unsigned long int>( sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES) );
125 
126 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
127  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
128  GlobalMemoryStatus(status_buffer);
129  totalmem = static_cast<unsigned long int>(status_buffer->dwTotalPhys);
130  delete status_buffer;
131 #else
132  #error "Unsuported plataform for physical memory checking"
133 #endif
134 
135  return totalmem;
136 }
137 
139 {
140  unsigned long int usedmem = 0;
141 
142 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD
143  struct rusage rusageinfo;
144  getrusage( RUSAGE_SELF, &rusageinfo );
145  usedmem = static_cast<unsigned long int>(1024 * rusageinfo.ru_maxrss);
146 
147 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
148  std::string pid, comm, state, ppid, pgrp, session, tty_nr,
149  tpgid, flags, minflt, cminflt, majflt, cmajflt,
150  utime, stime, cutime, cstime, priority, nice,
151  stringO, itrealvalue, starttime;
152 
153  std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
154 
155  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
156  >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
157  >> utime >> stime >> cutime >> cstime >> priority >> nice
158  >> stringO >> itrealvalue >> starttime >> usedmem;
159 
160 #elif TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
161  struct rusage rusageinfo;
162  getrusage(RUSAGE_SELF, &rusageinfo);
163  usedmem = static_cast<unsigned long int>(1024 * rusageinfo.ru_maxrss);
164 
165 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
166  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
167  GlobalMemoryStatus( status_buffer );
168  usedmem = static_cast<unsigned long int>(status_buffer->dwTotalVirtual - status_buffer->dwAvailVirtual);
169  delete status_buffer;
170 
171 #else
172  #error "Unsuported plataform for virtual memory checking"
173 #endif
174 
175  return usedmem;
176 }
177 
178 
180 {
181  unsigned long int totalmem = 0;
182 
183 #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
184  struct rlimit info;
185 
186  if( getrlimit( RLIMIT_AS, &info ) == 0 )
187  {
188  totalmem = (unsigned long int)info.rlim_max;
189  }
190 
191 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
192  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
193  GlobalMemoryStatus( status_buffer );
194  totalmem = (unsigned long int) status_buffer->dwTotalVirtual;
195  delete status_buffer;
196 
197 #else
198  #error "Unsuported plataform for virtual memory checking"
199 #endif
200 
201  return totalmem;
202 }
203 
205 {
206  unsigned int procnmb = 0;
207 
208 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
209  SYSTEM_INFO siSysInfo;
210  GetSystemInfo(&siSysInfo);
211  procnmb = static_cast<unsigned int>(siSysInfo.dwNumberOfProcessors);
212 
213 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
214  procnmb = static_cast<unsigned int>(sysconf(_SC_NPROCESSORS_ONLN));
215 
216 #else
217  #error "ERROR: Unsupported platform"
218 #endif
219 
220  return procnmb;
221 }
222 
223 void te::common::GetDecompostedPathEnvVar( std::vector< std::string >& paths )
224 {
225  paths.clear();
226 
227  char* varValuePtr = getenv("PATH");
228 
229  std::string separator;
230  #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
231  separator = ":";
232  #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
233  separator = ";";
234  #else
235  #error "Unsuported plataform for virtual memory checking"
236  #endif
237 
238  if( varValuePtr )
239  {
240  Tokenize( std::string( varValuePtr ), paths, separator );
241  }
242 }
243 
244 void te::common::GetDecompostedLDPathEnvVar( std::vector< std::string >& paths )
245 {
246  paths.clear();
247 
248  char* varValuePtr = getenv("LD_LIBRARY_PATH");
249 
250  std::string separator;
251  #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
252  separator = ":";
253  #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
254  separator = ";";
255  #else
256  #error "Unsuported plataform for virtual memory checking"
257  #endif
258 
259  if( varValuePtr )
260  {
261  Tokenize( std::string( varValuePtr ), paths, separator );
262  }
263 }
264 
265 std::string te::common::FindInTerraLibPath(const std::string& p)
266 {
267 // 1st: look in the neighborhood of the executable
268  boost::filesystem::path tl_path = boost::filesystem::current_path();
269 
270  boost::filesystem::path eval_path = tl_path / p;
271 
272  if(boost::filesystem::exists(eval_path))
273  return eval_path.string();
274 
275  tl_path /= "..";
276 
277  eval_path = tl_path / p;
278 
279  if(boost::filesystem::exists(eval_path))
280  return eval_path.string();
281 
282 // 2nd: look into the codebase path
283  tl_path = TERRALIB_CODEBASE_PATH;
284 
285  eval_path = tl_path / p;
286 
287  if(boost::filesystem::exists(eval_path))
288  return eval_path.string();
289 
290 // 3rd: look for an environment variable defined by macro TERRALIB_DIR_VAR_NAME
291  const char* te_env = getenv(TERRALIB_DIR_VAR_NAME);
292 
293  if(te_env != 0)
294  {
295  tl_path = te_env;
296 
297  eval_path = tl_path / p;
298 
299  if(boost::filesystem::exists(eval_path))
300  return eval_path.string();
301  }
302 
303 // 4th: look into install prefix-path
304  tl_path = TERRALIB_INSTALL_PREFIX_PATH;
305 
306  eval_path = tl_path / p;
307 
308  if(boost::filesystem::exists(eval_path))
309  return eval_path.string();
310 
311  return "";
312 }
TECOMMONEXPORT void GetDecompostedLDPathEnvVar(std::vector< std::string > &paths)
Returns the system LD_LIBRARY_PATH enviroment variable, decomposed into directory names...
This file is a wrapper around platform specific include files.
TECOMMONEXPORT std::string FindInTerraLibPath(const std::string &p)
Returns the path relative to a directory or file in the context of TerraLib.
TECOMMONEXPORT void GetDecompostedPathEnvVar(std::vector< std::string > &paths)
Returns the system PATH enviroment variable, decomposed into directory names, or an empty vector if n...
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
TECOMMONEXPORT unsigned int GetPhysProcNumber()
Returns the number of physical processors.
TECOMMONEXPORT unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
This class is designed for dealing with multi-language text translation in TerraLib.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Utility functions for dealing with strings.
TECOMMONEXPORT unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
TECOMMONEXPORT unsigned long int GetFreePhysicalMemory()
Returns the amount of free physical memory (bytes).
TECOMMONEXPORT unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...