GPU support + system information + Timestamp since boot

BUG=None
TEST=Manual

- Graphs now display GPU memory usage
- Graphs now can have display system information (still not decided on
  how to best do so)
- Timestamp now shows the seconds passed since boot time

Change-Id: I8ffca626e09538305469520efdd8cdd66390e4f0
Signed-off-by: Thiago Goncales <thiagog@chromium.org>
diff --git a/graphlog.py b/graphlog.py
index 8d16971..69b43c7 100644
--- a/graphlog.py
+++ b/graphlog.py
@@ -7,6 +7,7 @@
 from optparse import OptionParser
 import memreport
 import grapher
+from subprocess import Popen, PIPE
 
 def logger(iterations, wait, source="/proc", chrome_only=False):
   """ Logs smaps behavior of chrome and non-chrome processes.
@@ -54,18 +55,19 @@
                     help="show only chrome processes")
   parser.add_option("-g", "--graph", default="pie",
                     help="choose graph type (pie, line, bar)")
+  parser.add_option("-m", "--memory", default="Pss",
+                    help="choose memory type to display")
+
   #TODO: add "save file to destination" option + generate it on the fly
   return parser.parse_args()
 
 
-def setLineDataTable(snapshots_list, mem_type):
+def setLineDataTable(snapshots_list, mem_type, show_gpu=False, uptime=0):
   '''
   Given a list of ProcSnapshots objects, return a list formatted properly for
   a line graph
   '''
   chrome, sys_pids_list = getUniquePidsList(snapshots_list)
-  #print chrome_pids_list
-  #print sys_pids_list
 
   # Doing only for chrome right now. Figure out how to use this for
   # non-Chrome as well
@@ -74,20 +76,24 @@
   chrome_keys = sorted(chrome.keys(), key=int)
   for pid in chrome_keys:
     data_table[0].append(pid + ': ' + chrome[pid])
-  #data_table[0].extend(chrome_pids_list)
+  if show_gpu:
+    data_table[0].append("Total Graphics Memory")
+    data_table[0].append("Current Graphics Memory in GTT")
 
   i = 0
   for snapshot in snapshots_list:
-    # TODO: Here I dont think we want the timestamp per say, but rather
-    # a more simple identifier
     i += 1
-    data_table.append([int(snapshot.timestamp - snapshots_list[0].timestamp)])
+    data_table.append([int(snapshot.timestamp - snapshots_list[0].timestamp +
+                           uptime)])
     # Iterate through all the pids in the first table
     for pid in chrome_keys:
       try:
         data_table[i].append(snapshot.chrome_smaps.pid[pid][mem_type]/1024)
       except KeyError:
         data_table[i].append(0)
+    if show_gpu:
+      data_table[i].append(snapshot.gpu_mem.total / 1024)
+      data_table[i].append(snapshot.gpu_mem.current / 1024)
 
   return data_table
 
@@ -164,6 +170,23 @@
 
   return data_table
 
+def getSystemParameters():
+  sys_info = {}
+
+  # first, get uname
+  sys_info["uname"] = Popen("uname -a", shell=True,
+                            stdout=PIPE).stdout.readline()
+
+  # now get lsb info
+  lsb_f = open("/etc/lsb-release", 'r')
+  lsb = lsb_f.readlines()
+  lsb_f.close()
+  for line in lsb:
+    line = line.split('=')
+    sys_info[line[0]] = line[1]
+
+  return sys_info
+
 # Main Function
 #TODO(thiagog): Add command line support to input all required fields for
 # graph creation.
@@ -173,18 +196,32 @@
   if options.graph not in ("bar", "line", "pie", None):
     sys.exit("ERROR: graph argument invalid")
 
+  uptime_file = open("/proc/uptime", 'r')
+  uptime = float(uptime_file.readline().split()[0])
+  uptime_file.close()
+
   timestamp = time.asctime(time.localtime())
   snapshots = logger(options.iterations, options.interval,
                      options.source, options.chrome_only)
 
+  #TODO: Format this info properly for the html graph
+  sys_info = getSystemParameters()
+
+  gpu_mem = ("Pss", "Rss")
+
   # For testing right now
   mem_type = "Pss"
 
+  show_gpu = mem_type in gpu_mem
+
+  # Testing on linux machine (no gpu memory information)
+  show_gpu = False
+
   html_args = {}
 
   if options.graph == "line":
     html_args["title"] = mem_type + " [mBs] per PID over time"
-    html_args["data"] = setLineDataTable(snapshots, mem_type)
+    html_args["data"] = setLineDataTable(snapshots, mem_type, show_gpu, uptime)
     html_args["vaxis"] = mem_type + " [mBs]"
 
   if options.graph == "pie":