00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <malloc.h>
00014 #include <unistd.h>
00015 #include "StMem.h"
00016 double StMem::fUsed=0;
00017 double StMem::Used()
00018 {
00019 struct mallinfo info;
00020 info = mallinfo();
00021 return double(info.uordblks + info.usmblks)/1000000;
00022 }
00023
00024 double StMem::ESize()
00025 {
00026 static char *ps = 0;
00027 double res=0;
00028 if (!ps) {
00029 int pid = ::getpid();
00030 ps = (char*)malloc(20);
00031 sprintf(ps,"ps -l -p %d",pid);
00032 }
00033 FILE *pipe = ::popen(ps,"r");
00034 if (!pipe) return 0.;
00035
00036 char psBuf[130];
00037 psBuf[0] = ' ';
00038 while( !feof( pipe ) ) {
00039 psBuf[1]=0;
00040 if(!fgets( psBuf+1, 128, pipe)) continue;
00041
00042 int ifild=0;char *c;
00043
00044 for (c=psBuf; c[0]; c++) {
00045 if (c[0]==' ' && c[1]!=' ') ifild++;
00046 if (ifild == 10) break;
00047 }
00048 res = (double)atoi(c+1);
00049 if (res) break;
00050 }
00051 ::pclose(pipe);
00052 res *=::getpagesize()/(1024.*1024.);
00053
00054 return res;
00055 }
00056
00057 void StMem::Print(const char *tit)
00058 {
00059 double used = Used();
00060 double exec = ESize();
00061
00062 if (tit) printf("\nStMem::%s",tit);
00063 printf("\t total =%10.6f heap =%10.6f (%+10.6f)\n",exec, used,used-fUsed);
00064 fUsed = used;
00065 }