1    	/***************************************************************************
2    	 *
3    	 * $Id: StMem.cxx,v 1.5 2012/12/12 23:50:40 fisyak Exp $
4    	 *
5    	 ***************************************************************************
6    	 *
7    	 * Description:
8    	 *
9    	 ***************************************************************************
10   	 **************************************************************************/
11   	#include <stdio.h>
12   	#include <stdlib.h>
13   	#ifndef __APPLE__
14   	#include <malloc.h>
15   	#endif
16   	#include <unistd.h>
17   	#include "StMem.h"
18   	double  StMem::fUsed=0;
19   	double StMem::Used()
20   	{
21   	#ifndef __APPLE__
22   	  struct mallinfo info;
23   	  info = mallinfo();
24   	  return double(info.uordblks + info.usmblks)/1000000;
25   	#else
26   	  return 0;
27   	#endif
28   	}
29   	
30   	double StMem::ESize()
31   	{
32   	  static char *ps = 0;
33   	  double res=0;  
34   	  if (!ps) {
35   	    int pid = ::getpid();
36   	    ps = (char*)malloc(20);
37   	    sprintf(ps,"ps -l -p %d",pid);
38   	  }
39   	  FILE *pipe = ::popen(ps,"r");
40   	  if (!pipe) return 0.;
41   	  
42   	  char   psBuf[130];
43   	  psBuf[0] = ' ';
44   	  while( !feof( pipe ) ) {
45   	    psBuf[1]=0;
46   	    if(!fgets( psBuf+1, 128, pipe)) continue;
47   	//    printf("%s\n",psBuf);
48   	    int ifild=0;char *c;
49   	
50   	    for (c=psBuf; c[0]; c++) {
51   	      if (c[0]==' ' && c[1]!=' ') ifild++;
52   	      if (ifild == 10) break;
53   	    }
54   	    res = (double)atoi(c+1);
55   	    if (res) break;
56   	  }
57   	  ::pclose(pipe);
58   	  res *=::getpagesize()/(1024.*1024.);
59   	
60   	  return res;
61   	}
62   	
63   	void StMem::Print(const char *tit)
64   	{
65   	  double used = Used();
66   	  double exec = ESize();
67   	
68   	  if (tit) printf("\nStMem::%s",tit);
69   	  printf("\t total =%10.6f heap =%10.6f (%+10.6f)\n",exec, used,used-fUsed);
70   	  fUsed = used;
71   	}
72