Back to index

salome-med  6.5.0
MemorySpy.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "MemorySpy.hxx"
00024 
00025 #if defined(PCLINUX)  || defined(PCLINUX64) || defined(PCLINUX64_32)
00026 #include <unistd.h>
00027 #include <string.h>
00028 
00029 #define MAXMEM 7
00030 
00031 MemorySpy::MemorySpy()
00032 {
00033   _currentPid=getpid();
00034   _sizeofPage=getpagesize();
00035   char workStr[38];
00036   sprintf( workStr, "/proc/%d/statm", _currentPid);
00037   _statmFile=fopen ( workStr, "r" );
00038 }
00039 
00040 MemorySpy::~MemorySpy()
00041 {
00042   if(_statmFile)
00043     free(_statmFile);
00044 }
00045 
00046 long MemorySpy::getCurrentMemoryUsage()
00047 {
00048   if (!_statmFile)
00049     return -1;
00050   fseek( _statmFile, 0L, 0 );
00051   char workStr[52];
00052   if(!fread( workStr, 1, 50, _statmFile ))
00053     return -1;
00054   return parseString(workStr);
00055 }
00056 
00057 long MemorySpy::parseString(char* line)
00058 {
00059   char *po, *po2,hstr[0x100];
00060   int i;
00061   long tab[MAXMEM];
00062 
00063   memset( hstr, 0, sizeof( hstr ));
00064   po2 = hstr;
00065   po = line;
00066   i = 0;
00067   while ( *po != 0x0 )
00068   {
00069     if ( ( *po != 0x20 ) )
00070     {
00071       *po2 = *po;
00072       po++;
00073       po2++;
00074     }
00075     else
00076     {
00077       tab[i] = atol( hstr ) * _sizeofPage;
00078       i++;
00079       memset( hstr, 0, sizeof( hstr ));
00080       while ( *po != 0x0 )
00081       {
00082         if ( ( *po != 0x20 )&&( *po != '\n' ) )
00083           break;
00084         po++;
00085       }
00086       po2 = hstr;
00087     }
00088   }
00089   if ( strlen( hstr ) != 0 )
00090   {
00091     tab[i] = atol( hstr ) * _sizeofPage;
00092   }
00093   return tab[0];
00094 }
00095 #endif
00096 
00097 #ifdef HP
00098 #include <sys/param.h>
00099 #include <sys/pstat.h>
00100 #include <sys/unistd.h>
00101 
00102 MemorySpy::MemorySpy()
00103 {
00104 }
00105 
00106 MemorySpy::~MemorySpy()
00107 {
00108 }
00109 
00110 long MemorySpy::getCurrentMemoryUsage()
00111 {
00112   struct pst_dynamic dyn;
00113   if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1)
00114     return -1;
00115   else {
00116     return dyn.psd_vm * getpagesize();
00117 }
00118 #endif