Added SelfMemUse()

BUG=None
TEST=None

SelfMemUse was added in order to monitor the memory consumption of the
script itself

Change-Id: I49c577de732b9886c142ce2b6cacf6ff222c8571
Signed-off-by: Thiago Goncales <thiagog@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/27801
Reviewed-by: Grant Grundler <grundler@chromium.org>
diff --git a/memreport.py b/memreport.py
index edd9d04..cb08f15 100644
--- a/memreport.py
+++ b/memreport.py
@@ -174,7 +174,7 @@
     self.exec_mem["Total"] = {}
     smaps = self.getSmapsDict(pids_list, proc_dir)
     self.pid = smaps[0]
-    self.page_addr = smaps[1]
+    #self.page_addr = smaps[1]
     self.valid_mem_types = self.getValidMemTypes()
 
   def getSmapsDict(self, pids_list, proc_dir):
@@ -223,7 +223,8 @@
                                 mem_size)
               self.addToMemTypeDict(mem_type_dict, mem_type, mem_size)
               if is_exec:
-                self.addToExecDict(pid, mem_type, mem_size)
+                #self.addToExecDict(pid, mem_type, mem_size)
+                pass
 
         # Add PID memory to Total
         for item in mem_type_dict:
@@ -338,8 +339,43 @@
 
 
 ##########################################################
+class SelfMemUse(object):
+  '''
+  Objects stores how much memory the current process is using. Primarily used
+  for monitoring the script's memory consumption, and to verify how the script
+  is affect the system memory.
+  '''
+  def __init__(self):
+   self.pid = os.getpid()
+
+  def getMem(self):
+    smaps = {}
+    smaps_path = "/proc/self/smaps"
+    f = open(smaps_path, 'r')
+    f_lines = f.readlines()
+    f.close()
+    for line in f_lines:
+      line = line.split()
+
+     #Check if line is referent to page address
+      if line[2] == "kB":
+        mem_type = line[0].strip(':')
+        mem_size = int(line[1])
+        if mem_type not in ("KernelPageSize", "MMUPageSize"):
+          smaps[mem_type] = smaps.get(mem_type, 0) + mem_size
+          for item in ("Private", "Shared"):
+            if mem_type.startswith(item):
+              smaps[item] = smaps.get(mem_type, 0) + mem_size
+
+    return smaps
+
+  mem = property(getMem, doc="Memory consumption of script, from smaps")
+
+
+##########################################################
 if __name__ == "__main__":
   snapshot = ProcSnapshot()
+  self_mem = SelfMemUse()
   #print snapshot.chrome_pids_list, '\n'
   #print snapshot.sys_pids_list, '\n'
 
@@ -349,3 +385,5 @@
 
   elif "PSS" in snapshot.chrome_smaps.valid_mem_types:
     snapshot.sortedOutput("PSS")
+    print self_mem.pid
+    print self_mem.mem