Screens/About.py: Calculate correct value for "Free" memory, avoid crash
authorMike Looijmans <milo@openpli.org>
Wed, 30 Dec 2015 14:03:22 +0000 (15:03 +0100)
committerMike Looijmans <milo@openpli.org>
Wed, 30 Dec 2015 14:26:33 +0000 (15:26 +0100)
Lines in /proc/meminfo may not always have 3 entries (e.g. huge pages) handle
this gracefully instead of crashing.

Use "enumerate" instead of handcrafting a counter.

Use simpler string match (startswith instead of "in").

Consider "Cached" and "Buffers" as free memory too.

lib/python/Screens/About.py

index c99408f..ca8ad6f 100644 (file)
@@ -257,29 +257,33 @@ class MemoryInfo(Screen):
                        lvalue = rvalue = ""
                        mem = 1
                        free = 0
-                       i = 0
-                       for line in open('/proc/meminfo','r'):
-                               ( name, size, units ) = line.strip().split()
-                               if "MemTotal" in name:
+                       rows_in_column = self["params"].rows_in_column
+                       for i, line in enumerate(open('/proc/meminfo','r')):
+                               s = line.strip().split(None, 2)
+                               if len(s) == 3:
+                                       name, size, units = s
+                               elif len(s) == 2:
+                                       name, size = s
+                                       units = ""
+                               else:
+                                       continue
+                               if name.startswith("MemTotal"):
                                        mem = int(size)
-                               if "MemFree" in name:
-                                       free = int(size)
-                               if i < self["params"].rows_in_column:
+                               if name.startswith("MemFree") or name.startswith("Buffers") or name.startswith("Cached"):
+                                       free += int(size)
+                               if i < rows_in_column:
                                        ltext += "".join((name,"\n"))
                                        lvalue += "".join((size," ",units,"\n"))
                                else:
                                        rtext += "".join((name,"\n"))
                                        rvalue += "".join((size," ",units,"\n"))
-                               i += 1
                        self['lmemtext'].setText(ltext)
                        self['lmemvalue'].setText(lvalue)
                        self['rmemtext'].setText(rtext)
                        self['rmemvalue'].setText(rvalue)
-
                        self["slide"].setValue(int(100.0*(mem-free)/mem+0.25))
                        self['pfree'].setText("%.1f %s" % (100.*free/mem,'%'))
                        self['pused'].setText("%.1f %s" % (100.*(mem-free)/mem,'%'))
-
                except Exception, e:
                        print "[About] getMemoryInfo FAIL:", e