Add wall clock timestamp for ConfigMetricsReport and gauge atoms.

Fix the bug when serializing multiple atoms in gauge metric

BUG: b/74159560

Test: new test for ALL_CONDITION_CHANGES sampling method.
Change-Id: I6d33c1efbac92b6e13be2d64c323e090cb1f84aa
diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp
index 02d4dc9..8f72a8f 100644
--- a/cmds/statsd/src/StatsLogProcessor.cpp
+++ b/cmds/statsd/src/StatsLogProcessor.cpp
@@ -62,6 +62,9 @@
 const int FIELD_ID_UID_MAP = 2;
 const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3;
 const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4;
+const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5;
+const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6;
+
 
 #define STATS_DATA_DIR "/data/misc/stats-data"
 
@@ -260,6 +263,8 @@
             proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS);
 
     int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
+    int64_t lastReportWallClockNs = it->second->getLastReportWallClockNs();
+
     // First, fill in ConfigMetricsReport using current data on memory, which
     // starts from filling in StatsLogReport's.
     it->second->onDumpReport(dumpTimeStampNs, &proto);
@@ -276,6 +281,10 @@
                 (long long)lastReportTimeNs);
     proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
                 (long long)dumpTimeStampNs);
+    proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS,
+                (long long)lastReportWallClockNs);
+    proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
+                (long long)getWallClockNs());
 
     // End of ConfigMetricsReport (reports).
     proto.end(reportsToken);
diff --git a/cmds/statsd/src/metrics/GaugeMetricProducer.cpp b/cmds/statsd/src/metrics/GaugeMetricProducer.cpp
index e479e5c..9ac1bca 100644
--- a/cmds/statsd/src/metrics/GaugeMetricProducer.cpp
+++ b/cmds/statsd/src/metrics/GaugeMetricProducer.cpp
@@ -56,6 +56,7 @@
 const int FIELD_ID_END_BUCKET_ELAPSED_NANOS = 2;
 const int FIELD_ID_ATOM = 3;
 const int FIELD_ID_ELAPSED_ATOM_TIMESTAMP = 4;
+const int FIELD_ID_WALL_CLOCK_ATOM_TIMESTAMP = 5;
 
 GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric,
                                          const int conditionIndex,
@@ -168,21 +169,28 @@
                                (long long)bucket.mBucketEndNs);
 
             if (!bucket.mGaugeAtoms.empty()) {
-                uint64_t atomsToken =
-                    protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_ATOM);
                 for (const auto& atom : bucket.mGaugeAtoms) {
+                    uint64_t atomsToken =
+                        protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
+                                           FIELD_ID_ATOM);
                     writeFieldValueTreeToStream(mTagId, *(atom.mFields), protoOutput);
+                    protoOutput->end(atomsToken);
                 }
-                protoOutput->end(atomsToken);
+                const bool truncateTimestamp =
+                    android::util::kNotTruncatingTimestampAtomWhiteList.find(mTagId) ==
+                    android::util::kNotTruncatingTimestampAtomWhiteList.end();
+                const int64_t wall_clock_ns = truncateTimestamp ?
+                    truncateTimestampNsToFiveMinutes(getWallClockNs()) : getWallClockNs();
                 for (const auto& atom : bucket.mGaugeAtoms) {
-                    const bool truncateTimestamp =
-                        android::util::kNotTruncatingTimestampAtomWhiteList.find(mTagId) ==
-                        android::util::kNotTruncatingTimestampAtomWhiteList.end();
                     int64_t timestampNs =  truncateTimestamp ?
                         truncateTimestampNsToFiveMinutes(atom.mTimestamps) : atom.mTimestamps;
                     protoOutput->write(
                         FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_ELAPSED_ATOM_TIMESTAMP,
                         (long long)timestampNs);
+                    protoOutput->write(
+                        FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED |
+                            FIELD_ID_WALL_CLOCK_ATOM_TIMESTAMP,
+                        (long long)wall_clock_ns);
                 }
             }
             protoOutput->end(bucketInfoToken);
diff --git a/cmds/statsd/src/metrics/MetricsManager.cpp b/cmds/statsd/src/metrics/MetricsManager.cpp
index 1ca59a3..6209bbe 100644
--- a/cmds/statsd/src/metrics/MetricsManager.cpp
+++ b/cmds/statsd/src/metrics/MetricsManager.cpp
@@ -53,7 +53,9 @@
                                const sp<UidMap> &uidMap,
                                const sp<AlarmMonitor>& anomalyAlarmMonitor,
                                const sp<AlarmMonitor>& periodicAlarmMonitor)
-    : mConfigKey(key), mUidMap(uidMap), mLastReportTimeNs(timeBaseSec * NS_PER_SEC) {
+    : mConfigKey(key), mUidMap(uidMap),
+      mLastReportTimeNs(timeBaseSec * NS_PER_SEC),
+      mLastReportWallClockNs(getWallClockNs()) {
     mConfigValid =
             initStatsdConfig(key, config, *uidMap, anomalyAlarmMonitor, periodicAlarmMonitor,
                              timeBaseSec, mTagIds, mAllAtomMatchers,
@@ -193,6 +195,7 @@
         }
     }
     mLastReportTimeNs = dumpTimeStampNs;
+    mLastReportWallClockNs = getWallClockNs();
     VLOG("=========================Metric Reports End==========================");
 }
 
diff --git a/cmds/statsd/src/metrics/MetricsManager.h b/cmds/statsd/src/metrics/MetricsManager.h
index dbab814..bebd53c 100644
--- a/cmds/statsd/src/metrics/MetricsManager.h
+++ b/cmds/statsd/src/metrics/MetricsManager.h
@@ -69,10 +69,14 @@
     void dumpStates(FILE* out, bool verbose);
 
     // Returns the elapsed realtime when this metric manager last reported metrics.
-    uint64_t getLastReportTimeNs() {
+    inline int64_t getLastReportTimeNs() const {
         return mLastReportTimeNs;
     };
 
+    inline int64_t getLastReportWallClockNs() const {
+        return mLastReportWallClockNs;
+    };
+
     virtual void dropData(const uint64_t dropTimeNs);
 
     // Config source owner can call onDumpReport() to get all the metrics collected.
@@ -89,7 +93,8 @@
 
     bool mConfigValid = false;
 
-    uint64_t mLastReportTimeNs;
+    int64_t mLastReportTimeNs;
+    int64_t mLastReportWallClockNs;
 
     // The uid log sources from StatsdConfig.
     std::vector<int32_t> mAllowedUid;
diff --git a/cmds/statsd/src/stats_log.proto b/cmds/statsd/src/stats_log.proto
index 3c5f5a2..0412538 100644
--- a/cmds/statsd/src/stats_log.proto
+++ b/cmds/statsd/src/stats_log.proto
@@ -46,7 +46,7 @@
 
   optional Atom atom = 2;
 
-  optional int64 wall_clock_timestamp_sec = 3;
+  optional int64 wall_clock_timestamp_nanos = 3;
 }
 
 message CountBucketInfo {
@@ -105,6 +105,8 @@
   repeated Atom atom = 3;
 
   repeated int64 elapsed_timestamp_nanos = 4;
+
+  repeated int64 wall_clock_timestamp_nanos = 5;
 }
 
 message GaugeMetricData {
@@ -154,6 +156,10 @@
   optional int64 last_report_elapsed_nanos = 3;
 
   optional int64 current_report_elapsed_nanos = 4;
+
+  optional int64 last_report_wall_clock_nanos = 5;
+
+  optional int64 current_report_wall_clock_nanos = 6;
 }
 
 message ConfigMetricsReportList {