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) 2008 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 "terralib_config.h"
28 #include "../Defines.h"
29 #include "Exception.h"
30 #include "PlatformUtils.h"
31 #include "StringUtils.h"
32 #include "Translator.h"
33 
34 // STL
35 #include <fstream>
36 
37 // O.S. Specific
38 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
39 #include <windows.h>
40 #include <winbase.h>
41 
42 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD
43 #include <cstring>
44 #include <errno.h>
45 #include <dirent.h>
46 #include <malloc.h>
47 #include <sys/resource.h>
48 #include <sys/stat.h>
49 #include <sys/sysctl.h>
50 #include <sys/types.h>
51 #include <unistd.h>
52 
53 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE
54 #include <cstdlib>
55 #include <dirent.h>
56 #include <sys/stat.h>
57 #include <sys/sysctl.h>
58 
59 #else
60  #error "Unsuported plataform for physical memory checking"
61 #endif
62 
63 #include <cstdio>
64 #include <cstdlib>
65 
66 // Boost
67 #include <boost/filesystem.hpp>
68 
70 {
71  unsigned long int freemem = 0;
72 
73 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD || TE_PLATFORM == TE_PLATFORMCODE_APPLE
74  unsigned int usermem;
75 
76  std::size_t usermem_len = sizeof(usermem);
77 
78  int mib[2] = { CTL_HW, HW_USERMEM };
79 
80  if(sysctl(mib, (2 * sizeof(int)), &usermem, &usermem_len, NULL, 0) == 0)
81  {
82  freemem = static_cast<unsigned long int>(usermem);
83  }
84  else
85  {
86  throw Exception("Could not get free physical memory!");
87  }
88 
89 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
90  freemem = static_cast<unsigned long int>( sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES) );
91 
92 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
93  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
94  GlobalMemoryStatus(status_buffer);
95  freemem = static_cast<unsigned long int>(status_buffer->dwAvailPhys);
96  delete status_buffer;
97 #else
98  #error "Unsuported plataform for physical memory checking"
99 #endif
100 
101  return freemem;
102 }
103 
105 {
106  unsigned long int totalmem = 0;
107 
108  #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD || TE_PLATFORM == TE_PLATFORMCODE_APPLE
109  #ifdef HW_MEMSIZE /* OSX. --------------------- */
110  int64_t physmem = 0;
111  int mib[2] = { CTL_HW, HW_MEMSIZE };
112  #elif defined(HW_PHYSMEM) /* FreeBSD. ----------------- */
113  unsigned int physmem = 0;
114  int mib[2] = { CTL_HW, HW_PHYSMEM };
115  #elif defined(HW_PHYSMEM64) /* DragonFly BSD. ----------- */
116  int64_t physmem = 0;
117  int mib[2] = { CTL_HW, HW_PHYSMEM64 };
118  #elif defined(HW_REALMEM) /* FreeBSD. ----------------- */
119  unsigned int physmem = 0;
120  int mib[2] = { CTL_HW, HW_REALMEM };
121  #endif
122 
123  std::size_t physmem_len = sizeof(physmem);
124 
125  if(sysctl(mib, 2, &physmem, &physmem_len, NULL, 0) == 0)
126  {
127  totalmem = static_cast<unsigned long int>(physmem);
128  }
129  else
130  {
131  throw Exception("Could not get total physical memory!");
132  }
133  #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
134  totalmem = static_cast<unsigned long int>( sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES) );
135 
136  #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
137  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
138  GlobalMemoryStatus(status_buffer);
139  totalmem = static_cast<unsigned long int>(status_buffer->dwTotalPhys);
140  delete status_buffer;
141  #else
142  #error "Unsuported plataform for physical memory checking"
143  #endif
144 
145  return totalmem;
146 }
147 
149 {
150  unsigned long int usedmem = 0;
151 
152 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD
153  struct rusage rusageinfo;
154  getrusage( RUSAGE_SELF, &rusageinfo );
155  usedmem = static_cast<unsigned long int>(1024 * rusageinfo.ru_maxrss);
156 
157 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
158  std::string pid, comm, state, ppid, pgrp, session, tty_nr,
159  tpgid, flags, minflt, cminflt, majflt, cmajflt,
160  utime, stime, cutime, cstime, priority, nice,
161  stringO, itrealvalue, starttime;
162 
163  std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
164 
165  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
166  >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
167  >> utime >> stime >> cutime >> cstime >> priority >> nice
168  >> stringO >> itrealvalue >> starttime >> usedmem;
169 
170 #elif TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
171  struct rusage rusageinfo;
172  getrusage(RUSAGE_SELF, &rusageinfo);
173  usedmem = static_cast<unsigned long int>(1024 * rusageinfo.ru_maxrss);
174 
175 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
176  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
177  GlobalMemoryStatus( status_buffer );
178  usedmem = static_cast<unsigned long int>(status_buffer->dwTotalVirtual - status_buffer->dwAvailVirtual);
179  delete status_buffer;
180 
181 #else
182  #error "Unsuported plataform for virtual memory checking"
183 #endif
184 
185  return usedmem;
186 }
187 
188 
190 {
191  unsigned long int totalmem = 0;
192 
193 #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
194  struct rlimit info;
195 
196  if( getrlimit( RLIMIT_AS, &info ) == 0 )
197  {
198  totalmem = (unsigned long int)info.rlim_max;
199  }
200 
201 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
202  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
203  GlobalMemoryStatus( status_buffer );
204  totalmem = (unsigned long int) status_buffer->dwTotalVirtual;
205  delete status_buffer;
206 
207 #else
208  #error "Unsuported plataform for virtual memory checking"
209 #endif
210 
211  return totalmem;
212 }
213 
215 {
216  unsigned int procnmb = 0;
217 
218 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
219  SYSTEM_INFO siSysInfo;
220  GetSystemInfo(&siSysInfo);
221  procnmb = static_cast<unsigned int>(siSysInfo.dwNumberOfProcessors);
222 
223 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
224  procnmb = static_cast<unsigned int>(sysconf(_SC_NPROCESSORS_ONLN));
225 
226 #else
227  #error "ERROR: Unsupported platform"
228 #endif
229 
230  return procnmb;
231 }
232 
233 void te::common::GetDecompostedPathEnvVar( std::vector< std::string >& paths )
234 {
235  paths.clear();
236 
237  char* varValuePtr = getenv("PATH");
238 
239  std::string separator;
240  #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
241  separator = ":";
242  #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
243  separator = ";";
244  #else
245  #error "Unsuported plataform for virtual memory checking"
246  #endif
247 
248  if( varValuePtr )
249  {
250  Tokenize( std::string( varValuePtr ), paths, separator );
251  }
252 }
253 
254 void te::common::GetDecompostedLDPathEnvVar( std::vector< std::string >& paths )
255 {
256  paths.clear();
257 
258  char* varValuePtr = getenv("LD_LIBRARY_PATH");
259 
260  std::string separator;
261  #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
262  separator = ":";
263  #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
264  separator = ";";
265  #else
266  #error "Unsuported plataform for virtual memory checking"
267  #endif
268 
269  if( varValuePtr )
270  {
271  Tokenize( std::string( varValuePtr ), paths, separator );
272  }
273 }
274 
275 std::string te::common::FindInTerraLibPath(const std::string& p)
276 {
277 // 1st: look in the neighborhood of the executable
278  boost::filesystem::path tl_path = boost::filesystem::current_path();
279 
280  boost::filesystem::path eval_path = tl_path / p;
281 
282  if(boost::filesystem::exists(eval_path))
283  return eval_path.string();
284 
285  tl_path /= "..";
286 
287  eval_path = tl_path / p;
288 
289  if(boost::filesystem::exists(eval_path))
290  return eval_path.string();
291 
292 // 2rd: look for an environment variable defined by macro TERRALIB_DIR_VAR_NAME
293  const char* te_env = getenv(TERRALIB_DIR_VAR_NAME);
294 
295  if(te_env != 0)
296  {
297  tl_path = te_env;
298 
299  eval_path = tl_path / p;
300 
301  if(boost::filesystem::exists(eval_path))
302  return eval_path.string();
303  }
304 
305 // 3th: look into install prefix-path
306  tl_path = TERRALIB_INSTALL_PREFIX_PATH;
307 
308  eval_path = tl_path / p;
309 
310  if(boost::filesystem::exists(eval_path))
311  return eval_path.string();
312 
313 // 4nd: look into the codebase path
314  tl_path = TERRALIB_CODEBASE_PATH;
315 
316  eval_path = tl_path / p;
317 
318  if(boost::filesystem::exists(eval_path))
319  return eval_path.string();
320 
321 
322  return "";
323 }
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...