Merge "tp: Implement error offset in messaging"
diff --git a/docs/data-sources/battery-counters.md b/docs/data-sources/battery-counters.md
index 5cc837c..0628263 100644
--- a/docs/data-sources/battery-counters.md
+++ b/docs/data-sources/battery-counters.md
@@ -111,14 +111,15 @@
 }
 ```
 
-## Power rails
+## {#odpm} On-Device Power Rails Monitor (ODPM)
 
 _This data source has been introduced in Android 10 (Q) and requires the
 dedicated hardware on the device. This hardware is not yet available on
 most production phones._
 
 Recent version of Android introduced the support for more advanced power
-monitoring at the hardware subsystem level, known as "Power rail counters".
+monitoring at the hardware subsystem level, known as
+"On-Device Power Rail Monitors" (ODPMs).
 These counters measure the energy drained by (groups of) hardware units.
 
 Unlike the battery counters, they are not affected by the charging/discharging
@@ -128,11 +129,14 @@
 manufacturer. At the platform level this data is obtained polling the
 Android [IPowerStats HAL][power-hal].
 
+Googlers: See [go/power-rails-internal-doc](http://go/power-rails-internal-doc)
+for instructions on how to change the refault rail selection on Pixel devices.
+
 [power-hal]: https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/power/stats/1.0/IPowerStats.hal
 
 Simplified block diagram:
 
-![](/docs/images/power-rails.png "Block diagram of power rail counters")
+![](/docs/images/power-rails.png "Block diagram of ODPMs")
 
 ### TraceConfig
 
diff --git a/include/perfetto/tracing/traced_value.h b/include/perfetto/tracing/traced_value.h
index 5a43e51..366db4b 100644
--- a/include/perfetto/tracing/traced_value.h
+++ b/include/perfetto/tracing/traced_value.h
@@ -177,9 +177,9 @@
   static TracedValue CreateFromProto(protos::pbzero::DebugAnnotation* proto,
                                      EventContext* event_context = nullptr);
 
-  inline explicit TracedValue(protos::pbzero::DebugAnnotation* annotation,
-                              EventContext* event_context,
-                              internal::CheckedScope* parent_scope)
+  inline TracedValue(protos::pbzero::DebugAnnotation* annotation,
+                     EventContext* event_context,
+                     internal::CheckedScope* parent_scope)
       : annotation_(annotation),
         event_context_(event_context),
         checked_scope_(parent_scope) {}
@@ -228,9 +228,9 @@
  private:
   friend class TracedValue;
 
-  inline explicit TracedArray(protos::pbzero::DebugAnnotation* annotation,
-                              EventContext* event_context,
-                              internal::CheckedScope* parent_scope)
+  inline TracedArray(protos::pbzero::DebugAnnotation* annotation,
+                     EventContext* event_context,
+                     internal::CheckedScope* parent_scope)
       : annotation_(annotation),
         event_context_(event_context),
         checked_scope_(parent_scope) {}
@@ -286,7 +286,7 @@
   // Create a |TracedDictionary| which will populate the given field of the
   // given |message|.
   template <typename MessageType, typename FieldMetadata>
-  inline explicit TracedDictionary(
+  inline TracedDictionary(
       MessageType* message,
       protozero::proto_utils::internal::FieldMetadataHelper<FieldMetadata>,
       EventContext* event_context,
diff --git a/src/trace_processor/importers/proto/active_chrome_processes_tracker.cc b/src/trace_processor/importers/proto/active_chrome_processes_tracker.cc
index 4a54095..1c20836 100644
--- a/src/trace_processor/importers/proto/active_chrome_processes_tracker.cc
+++ b/src/trace_processor/importers/proto/active_chrome_processes_tracker.cc
@@ -19,6 +19,12 @@
 namespace perfetto {
 namespace trace_processor {
 
+void ActiveChromeProcessesTracker::AddActiveProcessMetadata(int64_t timestamp,
+                                                            UniquePid upid) {
+  process_data_[upid].metadata_timestamps.insert(timestamp);
+  global_metadata_timestamps_.insert(timestamp);
+}
+
 std::vector<ProcessWithDataLoss>
 ActiveChromeProcessesTracker::GetProcessesWithDataLoss() const {
   std::vector<ProcessWithDataLoss> processes_with_data_loss;
@@ -46,8 +52,16 @@
         // There's no matching descriptor, and there're no descriptors in the
         // future.
         last_loss_moment = metadata_ts;
-        next_no_loss_moment = base::nullopt;
-        break;
+        auto global_metadata_it =
+            global_metadata_timestamps_.upper_bound(metadata_ts);
+        if (global_metadata_it != global_metadata_timestamps_.end()) {
+          // The process terminated before the next incremental state reset.
+          // So it has no data loss from the next reset until the end of the
+          // trace.
+          next_no_loss_moment = *global_metadata_it;
+        } else {
+          next_no_loss_moment = base::nullopt;
+        }
       }
     }
     if (last_loss_moment) {
@@ -60,7 +74,7 @@
 void ActiveChromeProcessesTracker::NotifyEndOfFile() {
   const auto processes = GetProcessesWithDataLoss();
   for (const auto& p : processes) {
-    tables::ExperimentalMissingChromeProcessesTable::Row row;
+    tables::ExpMissingChromeProcTable::Row row;
     row.upid = p.upid;
     row.reliable_from = p.reliable_from;
     context_->storage->mutable_experimental_missing_chrome_processes_table()
diff --git a/src/trace_processor/importers/proto/active_chrome_processes_tracker.h b/src/trace_processor/importers/proto/active_chrome_processes_tracker.h
index ed5b83d..3dd1499 100644
--- a/src/trace_processor/importers/proto/active_chrome_processes_tracker.h
+++ b/src/trace_processor/importers/proto/active_chrome_processes_tracker.h
@@ -43,9 +43,7 @@
   explicit ActiveChromeProcessesTracker(TraceProcessorContext* context)
       : context_(context) {}
 
-  void AddActiveProcessMetadata(int64_t timestamp, UniquePid upid) {
-    process_data_[upid].metadata_timestamps.insert(timestamp);
-  }
+  void AddActiveProcessMetadata(int64_t timestamp, UniquePid upid);
   void AddProcessDescriptor(int64_t timestamp, UniquePid upid) {
     process_data_[upid].descriptor_timestamps.insert(timestamp);
   }
@@ -60,6 +58,8 @@
 
   TraceProcessorContext* context_;
   base::FlatHashMap<UniquePid, ProcessData> process_data_;
+  // Metadata timestamps across all processes.
+  std::set<int64_t> global_metadata_timestamps_;
 };
 
 }  // namespace trace_processor
diff --git a/src/trace_processor/importers/proto/active_chrome_processes_tracker_unittest.cc b/src/trace_processor/importers/proto/active_chrome_processes_tracker_unittest.cc
index 6fdc1e8..8917eb5 100644
--- a/src/trace_processor/importers/proto/active_chrome_processes_tracker_unittest.cc
+++ b/src/trace_processor/importers/proto/active_chrome_processes_tracker_unittest.cc
@@ -91,6 +91,22 @@
   EXPECT_THAT(tracker.GetProcessesWithDataLoss(), IsEmpty());
 }
 
+TEST(ActiveChromeProcessesTracker, TemrinatedProcess) {
+  ActiveChromeProcessesTracker tracker(nullptr);
+  // First metadata packet - two processes.
+  tracker.AddActiveProcessMetadata(/*timestamp=*/10, /*upid=*/1);
+  tracker.AddActiveProcessMetadata(/*timestamp=*/10, /*upid=*/2);
+  // Second metadata packet - only one process, the first process terminated.
+  tracker.AddActiveProcessMetadata(/*timestamp=*/15, /*upid=*/2);
+
+  // The first process is reliable since the second snapshot - it terminated,
+  // so it has no data loss.
+  // The second process has data loss till the end of the trace.
+  EXPECT_THAT(tracker.GetProcessesWithDataLoss(),
+              UnorderedElementsAre(ProcessWithDataLoss{1, 15},
+                                   ProcessWithDataLoss{2, base::nullopt}));
+}
+
 }  // namespace
 }  // namespace trace_processor
 }  // namespace perfetto
diff --git a/src/trace_processor/storage/trace_storage.h b/src/trace_processor/storage/trace_storage.h
index 6b85fed..bea6e5f 100644
--- a/src/trace_processor/storage/trace_storage.h
+++ b/src/trace_processor/storage/trace_storage.h
@@ -694,11 +694,11 @@
     return &experimental_proto_content_table_;
   }
 
-  const tables::ExperimentalMissingChromeProcessesTable&
+  const tables::ExpMissingChromeProcTable&
   experimental_missing_chrome_processes_table() const {
     return experimental_missing_chrome_processes_table_;
   }
-  tables::ExperimentalMissingChromeProcessesTable*
+  tables::ExpMissingChromeProcTable*
   mutable_experimental_missing_chrome_processes_table() {
     return &experimental_missing_chrome_processes_table_;
   }
@@ -936,7 +936,7 @@
   tables::ExperimentalProtoContentTable experimental_proto_content_table_{
       &string_pool_, nullptr};
 
-  tables::ExperimentalMissingChromeProcessesTable
+  tables::ExpMissingChromeProcTable
       experimental_missing_chrome_processes_table_{&string_pool_, nullptr};
 
   views::ThreadSliceView thread_slice_view_{&slice_table_, &thread_track_table_,
diff --git a/src/trace_processor/tables/metadata_tables.h b/src/trace_processor/tables/metadata_tables.h
index 562dd84..0e4b564 100644
--- a/src/trace_processor/tables/metadata_tables.h
+++ b/src/trace_processor/tables/metadata_tables.h
@@ -141,15 +141,13 @@
 PERFETTO_TP_TABLE(PERFETTO_TP_PROCESS_TABLE_DEF);
 
 // Experimental table, subject to arbitrary breaking changes.
-#define PERFETTO_TP_EXPERIMENTAL_MISSING_CHROME_PROCESSES_TABLE_DEF(NAME,      \
-                                                                    PARENT, C) \
-  NAME(ExperimentalMissingChromeProcessesTable,                                \
-       "experimental_missing_chrome_processes")                                \
-  PERFETTO_TP_ROOT_TABLE(PARENT, C)                                            \
-  C(uint32_t, upid)                                                            \
+#define PERFETTO_TP_EXP_MISSING_CHROME_PROC_TABLE_DEF(NAME, PARENT, C)     \
+  NAME(ExpMissingChromeProcTable, "experimental_missing_chrome_processes") \
+  PERFETTO_TP_ROOT_TABLE(PARENT, C)                                        \
+  C(uint32_t, upid)                                                        \
   C(base::Optional<int64_t>, reliable_from)
 
-PERFETTO_TP_TABLE(PERFETTO_TP_EXPERIMENTAL_MISSING_CHROME_PROCESSES_TABLE_DEF);
+PERFETTO_TP_TABLE(PERFETTO_TP_EXP_MISSING_CHROME_PROC_TABLE_DEF);
 
 // Contains information of processes seen during the trace
 //
diff --git a/src/trace_processor/tables/table_destructors.cc b/src/trace_processor/tables/table_destructors.cc
index 115a89e..a16b65e 100644
--- a/src/trace_processor/tables/table_destructors.cc
+++ b/src/trace_processor/tables/table_destructors.cc
@@ -43,8 +43,7 @@
 // metadata_tables.h
 RawTable::~RawTable() = default;
 ArgTable::~ArgTable() = default;
-ExperimentalMissingChromeProcessesTable::
-    ~ExperimentalMissingChromeProcessesTable() = default;
+ExpMissingChromeProcTable::~ExpMissingChromeProcTable() = default;
 MetadataTable::~MetadataTable() = default;
 CpuTable::~CpuTable() = default;
 CpuFreqTable::~CpuFreqTable() = default;