@@ -49,7 +49,6 @@ public class GaugeManager extends AppStateUpdateHandler {
4949 // This is a guesstimate of the max amount of time to wait before any pending metrics' collection
5050 // might take.
5151 private static final long TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS = 20 ;
52- private static final long APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC = 20 ;
5352 private static final long INVALID_GAUGE_COLLECTION_FREQUENCY = -1 ;
5453
5554 private final Lazy <ScheduledExecutorService > gaugeManagerExecutor ;
@@ -59,6 +58,7 @@ public class GaugeManager extends AppStateUpdateHandler {
5958 private final TransportManager transportManager ;
6059
6160 @ Nullable private GaugeMetadataManager gaugeMetadataManager ;
61+ @ Nullable private ScheduledFuture <?> gaugeManagerDataCollectionJob = null ;
6262 @ Nullable private PerfSession session = null ;
6363 private ApplicationProcessState applicationProcessState =
6464 ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ;
@@ -133,7 +133,8 @@ public void startCollectingGauges(PerfSession session) {
133133 }
134134
135135 ApplicationProcessState gaugeCollectionApplicationProcessState = applicationProcessState ;
136- if (gaugeCollectionApplicationProcessState == ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ) {
136+ if (gaugeCollectionApplicationProcessState
137+ == ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ) {
137138 logger .warn ("Start collecting gauges with APPLICATION_PROCESS_STATE_UNKNOWN" );
138139 // Since the application process state is unknown, collect gauges at the foreground frequency.
139140 gaugeCollectionApplicationProcessState = ApplicationProcessState .FOREGROUND ;
@@ -192,39 +193,43 @@ public void stopCollectingGauges() {
192193 memoryGaugeCollector .get ().stopCollecting ();
193194
194195 logGaugeMetrics ();
195-
196- // TODO(b/394127311): There might be a race condition where a final metric is collected, but
197- // isn't uploaded.
198- GaugeCounter .Companion .getInstance ().resetCounter ();
199196 this .session = null ;
200197 }
201198
202199 /**
203200 * Logs the existing GaugeMetrics to Firelog, associates it with the current {@link PerfSession}
204201 * and {@link ApplicationProcessState}.
202+ *
203+ * @return true if a new data collection job is started, false otherwise.
205204 */
206205 protected boolean logGaugeMetrics () {
207206 if (session == null ) {
208- logger .warn ("Attempted to log Gauge Metrics when session was null." );
207+ logger .debug ("Attempted to log Gauge Metrics when session was null." );
208+ return false ;
209+ }
210+
211+ // If there's an existing gaugeManagerDataCollectionJob, this is a no-op.
212+ if (gaugeManagerDataCollectionJob != null && !gaugeManagerDataCollectionJob .isDone ()) {
213+ logger .debug ("Attempted to start an additional gauge logging operation." );
209214 return false ;
210215 }
211216
212217 logExistingGaugeMetrics (session .sessionId (), applicationProcessState );
213218 return true ;
214219 }
215220
216- private void logExistingGaugeMetrics (String sessionId , ApplicationProcessState applicationProcessState ) {
221+ private void logExistingGaugeMetrics (
222+ String sessionId , ApplicationProcessState applicationProcessState ) {
217223 // Flush any data that was collected and attach it to the given session and app state.
218- @ SuppressWarnings ("FutureReturnValueIgnored" )
219- ScheduledFuture <?> unusedFuture =
220- gaugeManagerExecutor
221- .get ()
222- .schedule (
223- () -> {
224- syncFlush (sessionId , applicationProcessState );
225- },
226- TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS ,
227- TimeUnit .MILLISECONDS );
224+ gaugeManagerDataCollectionJob =
225+ gaugeManagerExecutor
226+ .get ()
227+ .schedule (
228+ () -> {
229+ syncFlush (sessionId , applicationProcessState );
230+ },
231+ TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS ,
232+ TimeUnit .MILLISECONDS );
228233 }
229234
230235 /**
@@ -236,16 +241,19 @@ private void logExistingGaugeMetrics(String sessionId, ApplicationProcessState a
236241 */
237242 private void syncFlush (String sessionId , ApplicationProcessState appState ) {
238243 GaugeMetric .Builder gaugeMetricBuilder = GaugeMetric .newBuilder ();
244+ GaugeCounter gaugeCounter = GaugeCounter .Companion .getInstance ();
239245
240246 // Adding CPU metric readings.
241247 while (!cpuGaugeCollector .get ().cpuMetricReadings .isEmpty ()) {
242248 gaugeMetricBuilder .addCpuMetricReadings (cpuGaugeCollector .get ().cpuMetricReadings .poll ());
249+ gaugeCounter .decrementCounter ();
243250 }
244251
245252 // Adding Memory metric readings.
246253 while (!memoryGaugeCollector .get ().memoryMetricReadings .isEmpty ()) {
247254 gaugeMetricBuilder .addAndroidMemoryReadings (
248255 memoryGaugeCollector .get ().memoryMetricReadings .poll ());
256+ gaugeCounter .decrementCounter ();
249257 }
250258
251259 // Adding Session ID info.
0 commit comments