1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*=========================================================================
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkKWProcessStatistics.h"
vtkCxxRevisionMacro(vtkKWProcessStatistics, "$Revision: 1.11 $");
#ifdef __linux
#include <sys/procfs.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h> // int uname(struct utsname *buf);
#include <ctype.h> // int isdigit(int c);
#include <errno.h> // extern int errno;
#elif __hpux
#include <sys/param.h>
#include <sys/pstat.h>
#endif
#include "vtkObjectFactory.h"
#include "vtkWindows.h"
vtkStandardNewMacro(vtkKWProcessStatistics);
#ifndef _WIN32
/* This mess was copied from the GNU getpagesize.h. */
#ifndef HAVE_GETPAGESIZE
# ifdef HAVE_UNISTD_H
# include <unistd.h>
# endif
/* Assume that all systems that can run configure have sys/param.h. */
# ifndef HAVE_SYS_PARAM_H
# define HAVE_SYS_PARAM_H 1
# endif
# ifdef _SC_PAGESIZE
# define getpagesize() sysconf(_SC_PAGESIZE)
# else /* no _SC_PAGESIZE */
# ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
# ifdef EXEC_PAGESIZE
# define getpagesize() EXEC_PAGESIZE
# else /* no EXEC_PAGESIZE */
# ifdef NBPG
# define getpagesize() NBPG * CLSIZE
# ifndef CLSIZE
# define CLSIZE 1
# endif /* no CLSIZE */
# else /* no NBPG */
# ifdef NBPC
# define getpagesize() NBPC
# else /* no NBPC */
# ifdef PAGESIZE
# define getpagesize() PAGESIZE
# endif /* PAGESIZE */
# endif /* no NBPC */
# endif /* no NBPG */
# endif /* no EXEC_PAGESIZE */
# else /* no HAVE_SYS_PARAM_H */
# define getpagesize() 8192 /* punt totally */
# endif /* no HAVE_SYS_PARAM_H */
# endif /* no _SC_PAGESIZE */
#endif /* no HAVE_GETPAGESIZE */
#endif // _WIN32
// Construct the ProcessStatistics with eight points.
vtkKWProcessStatistics::vtkKWProcessStatistics()
{
}
int vtkKWProcessStatistics::GetProcessSizeInBytes()
{
#ifdef _solaris
prpsinfo psinfo;
int fd;
char pname[1024];
int pagesize;
pid_t pid;
// Get out process id
pid = getpid();
// Get the size of a page in bytes
pagesize = getpagesize();
// Open the /proc/<pid> file and query the
// process info
sprintf( pname, "/proc/%d", pid );
fd = open( pname, O_RDONLY );
if (fd != -1)
{
psinfo.pr_size = 0;
ioctl( fd, PIOCPSINFO, &psinfo );
close( fd );
}
else
{
vtkErrorMacro(<< "Cannot get size of " << pname);
return 0;
}
// The size in bytes is the page size of the process times
// the size of a page in bytes
return psinfo.pr_size * pagesize;
#else
return 0;
#endif
}
float vtkKWProcessStatistics::GetProcessCPUTimeInMilliseconds()
{
#ifdef _solaris
prpsinfo psinfo;
int fd;
char pname[1024];
pid_t pid;
// Get out process id
pid = getpid();
// Open the /proc/<pid> file and query the
// process info
sprintf( pname, "/proc/%d", pid );
fd = open( pname, O_RDONLY );
ioctl( fd, PIOCPSINFO, &psinfo );
close( fd );
return
(float) psinfo.pr_time.tv_sec * 1000.0 +
(float) psinfo.pr_time.tv_nsec / 1000000.0;
#else
return 0.0;
#endif
}
int vtkKWProcessStatistics::QueryMemory()
{
this->TotalVirtualMemory = -1;
this->TotalPhysicalMemory = -1;
this->AvailableVirtualMemory = -1;
this->AvailablePhysicalMemory = -1;
#ifdef __CYGWIN__
return 0;
#elif _WIN32
MEMORYSTATUS ms;
GlobalMemoryStatus(&ms);
unsigned long tv = ms.dwTotalVirtual;
unsigned long tp = ms.dwTotalPhys;
unsigned long av = ms.dwAvailVirtual;
unsigned long ap = ms.dwAvailPhys;
this->TotalVirtualMemory = tv>>10;
this->TotalPhysicalMemory = tp>>10;
this->AvailableVirtualMemory = av>>10;
this->AvailablePhysicalMemory = ap>>10;
return 1;
#elif __linux
unsigned long tv=0;
unsigned long tp=0;
unsigned long av=0;
unsigned long ap=0;
char buffer[1024]; // for skipping unused lines
int linuxMajor = 0;
int linuxMinor = 0;
// Find the Linux kernel version first
struct utsname unameInfo;
int errorFlag = uname(&unameInfo);
if( errorFlag!=0 )
{
vtkErrorMacro("Problem calling uname(): " << strerror(errno) );
return 0;
}
if( unameInfo.release!=0 && strlen(unameInfo.release)>=3 )
{
// release looks like "2.6.3-15mdk-i686-up-4GB"
char majorChar=unameInfo.release[0];
char minorChar=unameInfo.release[2];
if( isdigit(majorChar) )
{
linuxMajor=majorChar-'0';
}
if( isdigit(minorChar) )
{
linuxMinor=minorChar-'0';
}
}
FILE *fd = fopen("/proc/meminfo", "r" );
if ( !fd )
{
vtkErrorMacro("Problem opening /proc/meminfo");
return 0;
}
if( linuxMajor>=3 || ( (linuxMajor>=2) && (linuxMinor>=6) ) )
{
// new /proc/meminfo format since kernel 2.6.x
// Rigorously, this test should check from the developping version 2.5.x
// that introduced the new format...
long freeMem;
long buffersMem;
long cachedMem;
fscanf(fd,"MemTotal:%ld kB\n", &this->TotalPhysicalMemory);
fscanf(fd,"MemFree:%ld kB\n", &freeMem);
fscanf(fd,"Buffers:%ld kB\n", &buffersMem);
fscanf(fd,"Cached:%ld kB\n", &cachedMem);
this->AvailablePhysicalMemory=freeMem+cachedMem+buffersMem;
// Skip SwapCached, Active, Inactive, HighTotal, HighFree, LowTotal
// and LowFree.
int i=0;
while(i<7)
{
fgets(buffer, sizeof(buffer), fd); // skip a line
++i;
}
fscanf(fd,"SwapTotal:%ld kB\n", &this->TotalVirtualMemory);
fscanf(fd,"SwapFree:%ld kB\n", &this->AvailableVirtualMemory);
}
else
{
// /proc/meminfo format for kernel older than 2.6.x
unsigned long temp;
unsigned long cachedMem;
unsigned long buffersMem;
fgets(buffer, sizeof(buffer), fd); // Skip "total: used:..."
fscanf(fd, "Mem: %lu %lu %lu %lu %lu %lu\n",
&tp, &temp, &ap, &temp, &buffersMem, &cachedMem);
fscanf(fd, "Swap: %lu %lu %lu\n", &tv, &temp, &av);
this->TotalVirtualMemory = tv>>10;
this->TotalPhysicalMemory = tp>>10;
this->AvailableVirtualMemory = av>>10;
this->AvailablePhysicalMemory = (ap+buffersMem+cachedMem)>>10;
}
fclose( fd );
return 1;
#elif __hpux
unsigned long tv=0;
unsigned long tp=0;
unsigned long av=0;
unsigned long ap=0;
struct pst_static pst;
struct pst_dynamic pdy;
unsigned long ps = 0;
if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) != -1)
{
ps = pst.page_size;
tp = pst.physical_memory *ps;
tv = (pst.physical_memory + pst.pst_maxmem) * ps;
if (pstat_getdynamic(&pdy, sizeof(pdy), (size_t) 1, 0) != -1)
{
ap = tp - pdy.psd_rm * ps;
av = tv - pdy.psd_vm;
this->TotalVirtualMemory = tv>>10;
this->TotalPhysicalMemory = tp>>10;
this->AvailableVirtualMemory = av>>10;
this->AvailablePhysicalMemory = ap>>10;
return 1;
}
}
return 0;
#else
return 0;
#endif
}
long vtkKWProcessStatistics::GetTotalVirtualMemory()
{
this->QueryMemory();
return this->TotalVirtualMemory;
}
long vtkKWProcessStatistics::GetAvailableVirtualMemory()
{
this->QueryMemory();
return this->AvailableVirtualMemory;
}
long vtkKWProcessStatistics::GetTotalPhysicalMemory()
{
this->QueryMemory();
return this->TotalPhysicalMemory;
}
long vtkKWProcessStatistics::GetAvailablePhysicalMemory()
{
this->QueryMemory();
return this->AvailablePhysicalMemory;
}
//----------------------------------------------------------------------------
void vtkKWProcessStatistics::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|