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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkProcessStatistics.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.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 "vtkProcessStatistics.h"
#ifndef _WIN32
#include <sys/procfs.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkProcessStatistics);
/* 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.
vtkProcessStatistics::vtkProcessStatistics()
{
}
int vtkProcessStatistics::GetProcessSizeInBytes()
{
#ifndef _WIN32
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;
#endif
#ifdef _WIN32
return 0;
#endif
}
double vtkProcessStatistics::GetProcessCPUTimeInMilliseconds()
{
#ifndef _WIN32
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
(double) psinfo.pr_time.tv_sec * 1000.0 +
(double) psinfo.pr_time.tv_nsec / 1000000.0;
#endif
#ifdef _WIN32
return 0.0;
#endif
}
|