[ART] Prepare streaming output of method tracing for developer use.

Changes include:

* Flush the buffer to file when tracing ends, so every record is
  preserved.

* At the end of the streaming output, add what's the header when the
  output mode is not strreaming (called 'trace summary' in code).
  * A new special token (kOpTraceSummary) is added for this purpose.
  * The summary used to be written to a .sec file, which has
    permission issues with non-rooted devices.

* Update the streaming output converter (stream-trace-converter.py)
  to handle the updated format. When the trace summary is present,
  it is used to populate the output header, which provides more
  complete info than existing implementation where some info is
  inferred from the records.

The changes to trace.h/cc impact streaming output mode only.

The updated stream-trace-converter.py works with or without the
trace summary at the file end, so pulling the file in the middle
is still supported.

Bug: b/33300765

Test: m -j48 ART_TEST_TRACE=true ART_TEST_TRACE_STREAM=true test-art-host
I also tested manually: flashed a Nexus 6P, collected traces, and
parsed them in Studio.

Change-Id: I697d2ec09bed56033cfce9a8f27dc4710d8b0798
diff --git a/tools/stream-trace-converter.py b/tools/stream-trace-converter.py
index 951b05b..7e341f2 100755
--- a/tools/stream-trace-converter.py
+++ b/tools/stream-trace-converter.py
@@ -124,12 +124,20 @@
     self._threads.append('%d\t%s\n' % (tid, str))
     print 'New thread: %d/%s' % (tid, str)
 
+  def ProcessTraceSummary(self, input):
+    summaryLength = ReadIntLE(input)
+    str = input.read(summaryLength)
+    self._summary = str
+    print 'Summary: \"%s\"' % str
+
   def ProcessSpecial(self, input):
     code = ord(input.read(1))
     if code == 1:
       self.ProcessMethod(input)
     elif code == 2:
       self.ProcessThread(input)
+    elif code == 3:
+      self.ProcessTraceSummary(input)
     else:
       raise MyException("Unknown special!")
 
@@ -147,9 +155,24 @@
       print 'Buffer underrun, file was probably truncated. Results should still be usable.'
 
   def Finalize(self, header):
-    header.write('*threads\n')
-    for t in self._threads:
-      header.write(t)
+    # If the summary is present in the input file, use it as the header except
+    # for the methods section which is emtpy in the input file. If not present,
+    # apppend header with the threads that are recorded in the input stream.
+    if (self._summary):
+      # Erase the contents that's already written earlier by PrintHeader.
+      header.seek(0)
+      header.truncate()
+      # Copy the lines from the input summary to the output header until
+      # the methods section is seen.
+      for line in self._summary.splitlines(True):
+        if line == "*methods\n":
+          break
+        else:
+          header.write(line)
+    else:
+      header.write('*threads\n')
+      for t in self._threads:
+        header.write(t)
     header.write('*methods\n')
     for m in self._methods:
       header.write(m)
@@ -166,6 +189,7 @@
 
     self._methods = []
     self._threads = []
+    self._summary = None
     self.Process(input, body)
 
     self.Finalize(header)