All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 "Exception.h"
28 #include "PlatformUtils.h"
29 #include "StringUtils.h"
30 
31 // STL
32 #include <fstream>
33 
34 // O.S. Specific
35 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
36 #include <windows.h>
37 #include <winbase.h>
38 
39 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD
40 #include <cstring>
41 #include <errno.h>
42 #include <dirent.h>
43 #include <malloc.h>
44 #include <sys/resource.h>
45 #include <sys/stat.h>
46 #include <sys/sysctl.h>
47 #include <sys/types.h>
48 #include <unistd.h>
49 
50 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE
51 #include <cstdlib>
52 #include <dirent.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 
56 #else
57  #error "Unsuported plataform for physical memory checking"
58 #endif
59 
60 #include <cstdio>
61 #include <cstdlib>
62 
63 namespace te
64 {
65  namespace common
66  {
67  unsigned long int GetFreePhysicalMemory()
68  {
69  unsigned long int freemem = 0;
70 
71 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD || TE_PLATFORM == TE_PLATFORMCODE_APPLE
72  unsigned int usermem;
73 
74  std::size_t usermem_len = sizeof(usermem);
75 
76  int mib[2] = { CTL_HW, HW_USERMEM };
77 
78  if(sysctl(mib, (2 * sizeof(int)), &usermem, &usermem_len, NULL, 0) == 0)
79  {
80  freemem = static_cast<unsigned long int>(usermem);
81  }
82  else
83  {
84  throw Exception("Could not get free physical memory!");
85  }
86 
87 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
88  freemem = static_cast<unsigned long int>( sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES) );
89 
90 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
91  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
92  GlobalMemoryStatus(status_buffer);
93  freemem = static_cast<unsigned long int>(status_buffer->dwAvailPhys);
94  delete status_buffer;
95 #else
96  #error "Unsuported plataform for physical memory checking"
97 #endif
98 
99  return freemem;
100  }
101 
102  unsigned long int GetTotalPhysicalMemory()
103  {
104  unsigned long int totalmem = 0;
105 
106 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD || TE_PLATFORM == TE_PLATFORMCODE_APPLE
107  unsigned int physmem = 0;
108 
109  std::size_t physmem_len = sizeof(physmem);
110 
111  int mib[2] = { CTL_HW, HW_PHYSMEM };
112 
113  if(sysctl(mib, (2 * sizeof(int)), &physmem, &physmem_len, NULL, 0) == 0)
114  {
115  totalmem = static_cast<unsigned long int>(physmem);
116  }
117  else
118  {
119  throw Exception("Could not get total physical memory!");
120  }
121 
122 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
123  totalmem = static_cast<unsigned long int>( sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES) );
124 
125 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
126  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
127  GlobalMemoryStatus(status_buffer);
128  totalmem = static_cast<unsigned long int>(status_buffer->dwTotalPhys);
129  delete status_buffer;
130 #else
131  #error "Unsuported plataform for physical memory checking"
132 #endif
133 
134  return totalmem;
135  }
136 
137  unsigned long int GetUsedVirtualMemory()
138  {
139  unsigned long int usedmem = 0;
140 
141 #if TE_PLATFORM == TE_PLATFORMCODE_FREEBSD || TE_PLATFORM == TE_PLATFORMCODE_OPENBSD
142  struct rusage rusageinfo;
143  getrusage( RUSAGE_SELF, &rusageinfo );
144  usedmem = static_cast<unsigned long int>(1024 * rusageinfo.ru_maxrss);
145 
146 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
147  std::string pid, comm, state, ppid, pgrp, session, tty_nr,
148  tpgid, flags, minflt, cminflt, majflt, cmajflt,
149  utime, stime, cutime, cstime, priority, nice,
150  stringO, itrealvalue, starttime;
151 
152  std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
153 
154  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
155  >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
156  >> utime >> stime >> cutime >> cstime >> priority >> nice
157  >> stringO >> itrealvalue >> starttime >> usedmem;
158 
159 #elif TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
160  struct rusage rusageinfo;
161  getrusage(RUSAGE_SELF, &rusageinfo);
162  usedmem = static_cast<unsigned long int>(1024 * rusageinfo.ru_maxrss);
163 
164 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
165  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
166  GlobalMemoryStatus( status_buffer );
167  usedmem = static_cast<unsigned long int>(status_buffer->dwTotalVirtual - status_buffer->dwAvailVirtual);
168  delete status_buffer;
169 
170 #else
171  #error "Unsuported plataform for virtual memory checking"
172 #endif
173 
174  return usedmem;
175  }
176 
177 
178  unsigned long int GetTotalVirtualMemory()
179  {
180  unsigned long int totalmem = 0;
181 
182 #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
183  struct rlimit info;
184 
185  if( getrlimit( RLIMIT_AS, &info ) == 0 )
186  {
187  totalmem = (unsigned long int)info.rlim_max;
188  }
189 
190 #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
191  LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
192  GlobalMemoryStatus( status_buffer );
193  totalmem = (unsigned long int) status_buffer->dwTotalVirtual;
194  delete status_buffer;
195 
196 #else
197  #error "Unsuported plataform for virtual memory checking"
198 #endif
199 
200  return totalmem;
201  }
202 
203  unsigned int GetPhysProcNumber()
204  {
205  unsigned int procnmb = 0;
206 
207 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
208  SYSTEM_INFO siSysInfo;
209  GetSystemInfo(&siSysInfo);
210  procnmb = static_cast<unsigned int>(siSysInfo.dwNumberOfProcessors);
211 
212 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_AIX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
213  procnmb = static_cast<unsigned int>(sysconf(_SC_NPROCESSORS_ONLN));
214 
215 #else
216  #error "ERROR: Unsupported platform"
217 #endif
218 
219  return procnmb;
220  }
221 
222  void GetDecompostedPathEnvVar( std::vector< std::string >& paths )
223  {
224  paths.clear();
225 
226  char* varValuePtr = getenv("PATH");
227 
228  std::string separator;
229  #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
230  separator = ":";
231  #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
232  separator = ";";
233  #else
234  #error "Unsuported plataform for virtual memory checking"
235  #endif
236 
237  if( varValuePtr )
238  {
239  Tokenize( std::string( varValuePtr ), paths, separator );
240  }
241  }
242 
243  void GetDecompostedLDPathEnvVar( std::vector< std::string >& paths )
244  {
245  paths.clear();
246 
247  char* varValuePtr = getenv("LD_LIBRARY_PATH");
248 
249  std::string separator;
250  #if (TE_PLATFORM == TE_PLATFORMCODE_FREEBSD) || (TE_PLATFORM == TE_PLATFORMCODE_OPENBSD) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) || (TE_PLATFORM == TE_PLATFORMCODE_LINUX)
251  separator = ":";
252  #elif TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
253  separator = ";";
254  #else
255  #error "Unsuported plataform for virtual memory checking"
256  #endif
257 
258  if( varValuePtr )
259  {
260  Tokenize( std::string( varValuePtr ), paths, separator );
261  }
262  }
263 
264  } // end namespace common
265 } // end namespace te
Utility functions for dealing with strings.
void GetDecompostedPathEnvVar(std::vector< std::string > &paths)
Returns the system PATH enviroment variable, decomposed into directory names, or an empty vector if n...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
This file is a wrapper around platform specific include files.
unsigned int GetPhysProcNumber()
Returns the number of physical processors.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
void GetDecompostedLDPathEnvVar(std::vector< std::string > &paths)
Returns the system LD_LIBRARY_PATH enviroment variable, decomposed into directory names...
unsigned long int GetFreePhysicalMemory()
Returns the amount of free physical memory (bytes).
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
unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...