Browse Source

HPCC-14424 Memory stats not all correct

Signed-off-by: Mark Kelly <mark.kelly@lexisnexis.com>
Mark Kelly 9 years ago
parent
commit
66dab1949b
2 changed files with 44 additions and 17 deletions
  1. 43 17
      system/jlib/jdebug.cpp
  2. 1 0
      system/jlib/jdebug.hpp

+ 43 - 17
system/jlib/jdebug.cpp

@@ -876,6 +876,11 @@ memsize_t getMapInfo(const char *type)
     return 0; // TODO/UNKNOWN
 }
 
+memsize_t getVMInfo(const char *type)
+{
+    return 0; // TODO/UNKNOWN
+}
+
 void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed)
 {
     // MORE: Might be a better way to get CPU speed (actual) than the one stored in Registry
@@ -1001,6 +1006,38 @@ memsize_t getMapInfo(const char *type)
     return ret;
 }
 
+memsize_t getVMInfo(const char *type)
+{
+    memsize_t ret = 0;
+    VStringBuffer name("%s:", type);
+    VStringBuffer procMaps("/proc/self/status");
+    FILE *diskfp = fopen(procMaps.str(), "r");
+    if (!diskfp)
+        return 0;
+    char ln[256];
+    memsize_t value = 0;
+    char unitStr[256];
+    while (fgets(ln, sizeof(ln), diskfp))
+    {
+        if (!strncmp(ln, name.str(), name.length()))
+        {
+            if (2 == sscanf(&ln[name.length()], "%lu%s", &value, unitStr))
+            {
+                if (!strcasecmp(unitStr, "kB"))
+                    value *= 1024ULL;
+                if (!strcasecmp(unitStr, "mB"))
+                    value *= 1024ULL * 1024ULL;
+                if (!strcasecmp(unitStr, "gB"))
+                    value *= 1024ULL * 1024ULL * 1024ULL;
+                ret = value;
+                break;
+            }
+        }
+    }
+    fclose(diskfp);
+    return ret;
+}
+
 void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed)
 {
     int cpufd = open("/proc/cpuinfo",O_RDONLY);
@@ -1137,20 +1174,10 @@ public:
 void getMemStats(StringBuffer &out, unsigned &memused, unsigned &memtot)
 {
 #ifdef __linux__
-    struct mallinfo mi = mallinfo();
-    static CInt64fix fixuordblks;
-    fixuordblks.set(mi.uordblks);
-    static CInt64fix fixusmblks;
-    fixusmblks.set(mi.usmblks);
-    static CInt64fix fixhblkhd;
-    fixhblkhd.set(mi.hblkhd);
-    static CInt64fix fixarena;
-    fixarena.set(mi.arena);
-
-    __int64 sbrkmem = fixuordblks.get()+fixusmblks.get();
-    __int64 mmapmem = fixhblkhd.get();
-    __int64 arena =  fixarena.get();
-    __int64 total = mmapmem+sbrkmem;
+    __int64 total = getMapInfo("heap");
+    __int64 sbrkmem = getMapInfo("sbrk");
+    __int64 mmapmem = total - sbrkmem;
+    __int64 virttot = getVMInfo("VmData");
     unsigned mu;
     unsigned ma;
     unsigned mt;
@@ -1158,9 +1185,8 @@ void getMemStats(StringBuffer &out, unsigned &memused, unsigned &memtot)
     unsigned su;
     getMemUsage(mu,ma,mt,st,su);
     unsigned muval = (unsigned)(((__int64)mu+(__int64)su)*100/((__int64)mt+(__int64)st));
-    __int64 proctot = arena+mmapmem;
     if (sizeof(memsize_t)==4) {
-        unsigned muval2 = (proctot*100)/(3*(__int64)0x40000000);
+        unsigned muval2 = (virttot*100)/(3*(__int64)0x40000000);
         if (muval2>muval)
             muval = muval2;
     }
@@ -1169,7 +1195,7 @@ void getMemStats(StringBuffer &out, unsigned &memused, unsigned &memtot)
 
 
     out.appendf("MU=%3u%% MAL=%" I64F "d MMP=%" I64F "d SBK=%" I64F "d TOT=%uK RAM=%uK SWP=%uK", 
-        muval, total, mmapmem, sbrkmem, (unsigned)(proctot/1024), mu, su);
+        muval, total, mmapmem, sbrkmem, (unsigned)(virttot/1024), mu, su);
 #ifdef _USE_MALLOC_HOOK
     if (totalMem) 
         out.appendf(" TM=%" I64F "d",totalMem);

+ 1 - 0
system/jlib/jdebug.hpp

@@ -343,6 +343,7 @@ unsigned jlib_decl setAllocHook(bool on);  // bwd compat returns unsigned
 extern jlib_decl void getHardwareInfo(HardwareInfo &hdwInfo, const char *primDiskPath = NULL, const char *secDiskPath = NULL);
 extern jlib_decl void getProcessTime(UserSystemTime_t & time);
 extern jlib_decl memsize_t getMapInfo(const char *type);
+extern jlib_decl memsize_t getVMInfo(const char *type);
 extern jlib_decl void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed);
 extern jlib_decl void getPeakMemUsage(memsize_t &peakVm,memsize_t &peakResident);
 extern jlib_decl unsigned getAffinityCpus();