MediaMetrics: Change dumpsys format

As the dumpsys no longer needs to be parseable by a program
convert to a more human readable form.

1) Make default time REALTIME consistently.
2) Dump time as a human readable string.
3) Remove dumpsys versioning code.
4) Delimiter changes.

Test: atest mediametrics_tests
Test: adb shell dumpsys media.metrics
Bug: 138583596
Change-Id: I6ee7d81a18e0e220b258c722d232c05805118abb
diff --git a/services/mediametrics/MediaMetricsService.cpp b/services/mediametrics/MediaMetricsService.cpp
index 3b95f7a..4f8589c 100644
--- a/services/mediametrics/MediaMetricsService.cpp
+++ b/services/mediametrics/MediaMetricsService.cpp
@@ -79,8 +79,7 @@
 MediaMetricsService::MediaMetricsService()
         : mMaxRecords(kMaxRecords),
           mMaxRecordAgeNs(kMaxRecordAgeNs),
-          mMaxRecordsExpiredAtOnce(kMaxExpiredAtOnce),
-          mDumpProtoDefault(mediametrics::Item::PROTO_V1)
+          mMaxRecordsExpiredAtOnce(kMaxExpiredAtOnce)
 {
     ALOGD("%s", __func__);
 }
@@ -171,12 +170,12 @@
     }
 
     if (!isTrusted || item->getTimestamp() == 0) {
-        // WestWorld logs two times for events: ElapsedRealTimeNs (BOOTTIME) and
-        // WallClockTimeNs (REALTIME).  The new audio keys use BOOTTIME.
+        // Westworld logs two times for events: ElapsedRealTimeNs (BOOTTIME) and
+        // WallClockTimeNs (REALTIME), but currently logs REALTIME to cloud.
         //
-        // TODO: Reevaluate time base with other teams.
-        const bool useBootTime = startsWith(item->getKey(), "audio.");
-        const int64_t now = systemTime(useBootTime ? SYSTEM_TIME_BOOTTIME : SYSTEM_TIME_REALTIME);
+        // For consistency and correlation with other logging mechanisms
+        // we use REALTIME here.
+        const int64_t now = systemTime(SYSTEM_TIME_REALTIME);
         item->setTimestamp(now);
     }
 
@@ -206,7 +205,6 @@
 
     // crack any parameters
     const String16 protoOption("-proto");
-    int chosenProto = mDumpProtoDefault;
     const String16 clearOption("-clear");
     bool clear = false;
     const String16 sinceOption("-since");
@@ -221,21 +219,7 @@
         } else if (args[i] == protoOption) {
             i++;
             if (i < n) {
-                String8 value(args[i]);
-                int proto = mediametrics::Item::PROTO_V0;
-                char *endp;
-                const char *p = value.string();
-                proto = strtol(p, &endp, 10);
-                if (endp != p || *endp == '\0') {
-                    if (proto < mediametrics::Item::PROTO_FIRST) {
-                        proto = mediametrics::Item::PROTO_FIRST;
-                    } else if (proto > mediametrics::Item::PROTO_LAST) {
-                        proto = mediametrics::Item::PROTO_LAST;
-                    }
-                    chosenProto = proto;
-                } else {
-                    result.append("unable to parse value for -proto\n\n");
-                }
+                // ignore
             } else {
                 result.append("missing value for -proto\n\n");
             }
@@ -281,8 +265,8 @@
         std::lock_guard _l(mLock);
 
         result.appendFormat("Dump of the %s process:\n", kServiceName);
-        dumpHeaders_l(result, chosenProto, ts_since);
-        dumpRecent_l(result, chosenProto, ts_since, only.c_str());
+        dumpHeaders_l(result, ts_since);
+        dumpRecent_l(result, ts_since, only.c_str());
 
         if (clear) {
             mItemsDiscarded += mItems.size();
@@ -303,9 +287,8 @@
 }
 
 // dump headers
-void MediaMetricsService::dumpHeaders_l(String8 &result, int dumpProto, nsecs_t ts_since)
+void MediaMetricsService::dumpHeaders_l(String8 &result, nsecs_t ts_since)
 {
-    result.appendFormat("Protocol Version: %d\n", dumpProto);
     if (mediametrics::Item::isEnabled()) {
         result.append("Metrics gathering: enabled\n");
     } else {
@@ -326,25 +309,25 @@
 }
 
 void MediaMetricsService::dumpRecent_l(
-        String8 &result, int dumpProto, nsecs_t ts_since, const char * only)
+        String8 &result, nsecs_t ts_since, const char * only)
 {
     if (only != nullptr && *only == '\0') {
         only = nullptr;
     }
     result.append("\nFinalized Metrics (oldest first):\n");
-    dumpQueue_l(result, dumpProto, ts_since, only);
+    dumpQueue_l(result, ts_since, only);
 
     // show who is connected and injecting records?
     // talk about # records fed to the 'readers'
     // talk about # records we discarded, perhaps "discarded w/o reading" too
 }
 
-void MediaMetricsService::dumpQueue_l(String8 &result, int dumpProto) {
-    dumpQueue_l(result, dumpProto, (nsecs_t) 0, nullptr /* only */);
+void MediaMetricsService::dumpQueue_l(String8 &result) {
+    dumpQueue_l(result, (nsecs_t) 0, nullptr /* only */);
 }
 
 void MediaMetricsService::dumpQueue_l(
-        String8 &result, int dumpProto, nsecs_t ts_since, const char * only) {
+        String8 &result, nsecs_t ts_since, const char * only) {
     int slot = 0;
 
     if (mItems.empty()) {
@@ -363,7 +346,7 @@
                 continue;
             }
             result.appendFormat("%5d: %s\n",
-                   slot, item->toString(dumpProto).c_str());
+                   slot, item->toString().c_str());
             slot++;
         }
     }
diff --git a/services/mediametrics/MediaMetricsService.h b/services/mediametrics/MediaMetricsService.h
index 74a114a..935bee2 100644
--- a/services/mediametrics/MediaMetricsService.h
+++ b/services/mediametrics/MediaMetricsService.h
@@ -85,11 +85,11 @@
     bool expirations_l(const std::shared_ptr<const mediametrics::Item>& item);
 
     // support for generating output
-    void dumpQueue_l(String8 &result, int dumpProto);
-    void dumpQueue_l(String8 &result, int dumpProto, nsecs_t, const char *only);
-    void dumpHeaders_l(String8 &result, int dumpProto, nsecs_t ts_since);
-    void dumpSummaries_l(String8 &result, int dumpProto, nsecs_t ts_since, const char * only);
-    void dumpRecent_l(String8 &result, int dumpProto, nsecs_t ts_since, const char * only);
+    void dumpQueue_l(String8 &result);
+    void dumpQueue_l(String8 &result, nsecs_t, const char *only);
+    void dumpHeaders_l(String8 &result, nsecs_t ts_since);
+    void dumpSummaries_l(String8 &result, nsecs_t ts_since, const char * only);
+    void dumpRecent_l(String8 &result, nsecs_t ts_since, const char * only);
 
     // The following variables accessed without mLock
 
@@ -100,7 +100,6 @@
     const nsecs_t mMaxRecordAgeNs;
     // max to expire per expirations_l() invocation
     const size_t mMaxRecordsExpiredAtOnce;
-    const int mDumpProtoDefault;
 
     std::atomic<int64_t> mItemsSubmitted{}; // accessed outside of lock.
 
diff --git a/services/mediametrics/TimeMachine.h b/services/mediametrics/TimeMachine.h
index 4d24ce4..87de1c4 100644
--- a/services/mediametrics/TimeMachine.h
+++ b/services/mediametrics/TimeMachine.h
@@ -157,9 +157,22 @@
             }
             std::stringstream ss;
             ss << key << "." << tsPair.first << "={";
-            do {
-                ss << eptr->first << ":" << eptr->second << ",";
-            } while (++eptr != timeSequence.end());
+
+            time_string_t last_timestring{}; // last timestring used.
+            while (true) {
+                const time_string_t timestring = mediametrics::timeStringFromNs(eptr->first);
+                // find common prefix offset.
+                const size_t offset = commonTimePrefixPosition(timestring.time,
+                        last_timestring.time);
+                last_timestring = timestring;
+                ss << "(" << (offset == 0 ? "" : "~") << &timestring.time[offset]
+                    << ") " << eptr->second;
+                if (++eptr == timeSequence.end()) {
+                    ss << "}";
+                    break;
+                }
+                ss << ", ";
+            }
             ss << "};\n";
             return ss.str();
         }
diff --git a/services/mediametrics/TransactionLog.h b/services/mediametrics/TransactionLog.h
index 0c5d12b..27e2c14 100644
--- a/services/mediametrics/TransactionLog.h
+++ b/services/mediametrics/TransactionLog.h
@@ -155,7 +155,7 @@
             --ll;
             for (const auto &item : itemMap.second) {
                 if (ll <= 0) break;
-                ss << "  { " << item.first << ", " << item.second->toString() << " }\n";
+                ss << "  " << item.second->toString() << "\n";
                 --ll;
             }
         }