Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MedClient / src / MemorySpy.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "MemorySpy.hxx"
24
25 #if defined(PCLINUX)  || defined(PCLINUX64) || defined(PCLINUX64_32)
26 #include <unistd.h>
27 #include <string.h>
28
29 #define MAXMEM 7
30
31 MemorySpy::MemorySpy()
32 {
33   _currentPid=getpid();
34   _sizeofPage=getpagesize();
35   char workStr[38];
36   sprintf( workStr, "/proc/%d/statm", _currentPid);
37   _statmFile=fopen ( workStr, "r" );
38 }
39
40 MemorySpy::~MemorySpy()
41 {
42   if(_statmFile)
43     free(_statmFile);
44 }
45
46 long MemorySpy::getCurrentMemoryUsage()
47 {
48   if (!_statmFile)
49     return -1;
50   fseek( _statmFile, 0L, 0 );
51   char workStr[52];
52   if(!fread( workStr, 1, 50, _statmFile ))
53     return -1;
54   return parseString(workStr);
55 }
56
57 long MemorySpy::parseString(char* line)
58 {
59   char *po, *po2,hstr[0x100];
60   int i;
61   long tab[MAXMEM];
62
63   memset( hstr, 0, sizeof( hstr ));
64   po2 = hstr;
65   po = line;
66   i = 0;
67   while ( *po != 0x0 )
68   {
69     if ( ( *po != 0x20 ) )
70     {
71       *po2 = *po;
72       po++;
73       po2++;
74     }
75     else
76     {
77       tab[i] = atol( hstr ) * _sizeofPage;
78       i++;
79       memset( hstr, 0, sizeof( hstr ));
80       while ( *po != 0x0 )
81       {
82         if ( ( *po != 0x20 )&&( *po != '\n' ) )
83           break;
84         po++;
85       }
86       po2 = hstr;
87     }
88   }
89   if ( strlen( hstr ) != 0 )
90   {
91     tab[i] = atol( hstr ) * _sizeofPage;
92   }
93   return tab[0];
94 }
95 #endif
96
97 #ifdef HP
98 #include <sys/param.h>
99 #include <sys/pstat.h>
100 #include <sys/unistd.h>
101
102 MemorySpy::MemorySpy()
103 {
104 }
105
106 MemorySpy::~MemorySpy()
107 {
108 }
109
110 long MemorySpy::getCurrentMemoryUsage()
111 {
112   struct pst_dynamic dyn;
113   if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1)
114     return -1;
115   else {
116     return dyn.psd_vm * getpagesize();
117 }
118 #endif