service: Fix trace write period drifting

Align trace file write periods to fixed intervals to avoid drifting over
time.

Bug: 73452932
Change-Id: Icf7290a3d89219a1ab51aa1515417de322b4f794
diff --git a/src/tracing/core/service_impl.h b/src/tracing/core/service_impl.h
index ba74bb5..baf9f73 100644
--- a/src/tracing/core/service_impl.h
+++ b/src/tracing/core/service_impl.h
@@ -190,10 +190,10 @@
 
     size_t num_buffers() const { return buffers_index.size(); }
 
-    int next_write_period_ms() const {
-      PERFETTO_DCHECK(write_period_ms);
-      // TODO(primiano): this will drift. Synchronize % period so it aligns.
-      return write_period_ms;
+    int delay_to_next_write_period_ms() const {
+      PERFETTO_DCHECK(write_period_ms > 0);
+      return write_period_ms -
+             (base::GetWallTimeMs().count() % write_period_ms);
     }
 
     // The consumer that started the session.