@@ -60,10 +60,11 @@ public class GaugeManager {
6060
6161  @ Nullable  private  GaugeMetadataManager  gaugeMetadataManager ;
6262  @ Nullable  private  ScheduledFuture <?> gaugeManagerDataCollectionJob  = null ;
63-   @ Nullable  private  String  sessionId  = null ;
64-   @ Nullable  private  String  aqsSessionId  = null ;
65-   private  ApplicationProcessState  applicationProcessState  =
66-       ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ;
63+   @ Nullable  private  PerfSession  session  = null ;
64+ 
65+   // The default value for application process state is Foreground. This will be updated based on 
66+   // app state changes. 
67+   private  ApplicationProcessState  applicationProcessState  = ApplicationProcessState .FOREGROUND ;
6768
6869  // TODO(b/258263016): Migrate to go/firebase-android-executors 
6970  @ SuppressLint ("ThreadPoolCreation" )
@@ -104,48 +105,34 @@ public static synchronized GaugeManager getInstance() {
104105    return  instance ;
105106  }
106107
107-   /** 
108-    * Starts the collection of available gauges for the given {@code sessionId} and {@code 
109-    * applicationProcessState}. The collected Gauge Metrics will be flushed at regular intervals. 
110-    * 
111-    * <p>GaugeManager can only collect gauges for one session at a time, and if this method is called 
112-    * again with the same or new sessionId while it's already collecting gauges, all future gauges 
113-    * will then be associated with the same or new sessionId and applicationProcessState. 
114-    * 
115-    * @param session The {@link PerfSession} to which the collected gauges will be associated with. 
116-    * @param applicationProcessState The {@link ApplicationProcessState} the collected GaugeMetrics 
117-    *     will be associated with. 
118-    * @note: This method is NOT thread safe - {@link this.startCollectingGauges()} and {@link 
119-    *     this.stopCollectingGauges()} should always be called from the same thread. 
120-    */ 
121-   public  void  startCollectingGauges (
122-       PerfSession  session , ApplicationProcessState  applicationProcessState ) {
123-     if  (this .sessionId  != null ) {
124-       stopCollectingGauges ();
108+   public  void  updateGaugeCollection (ApplicationProcessState  applicationProcessState ) {
109+     if  (gaugeManagerDataCollectionJob  != null ) {
110+       gaugeManagerDataCollectionJob .cancel (false );
125111    }
126112
127-     long   collectionFrequency  =  startCollectingGauges ( applicationProcessState ,  session .getTimer ()); 
128-     if  ( collectionFrequency  ==  INVALID_GAUGE_COLLECTION_FREQUENCY ) { 
129-       logger . warn ( "Invalid gauge collection frequency. Unable to start collecting Gauges." );
113+     if  ( session  ==  null  || ! session .isGaugeAndEventCollectionEnabled ()) { 
114+        logger . warn ( "Not starting gauge collection." ); 
115+       stopCollectingGauges ( );
130116      return ;
131117    }
132118
133-     this . sessionId  =  session .sessionId (); 
134-     this . aqsSessionId  =  session . aqsSessionId ( );
135-     this . applicationProcessState  =  applicationProcessState ; 
119+     if  ( session .aqsSessionId () ==  null ) { 
120+        logger . warn ( "Not starting gauge collection." );
121+     } 
136122
137-     // This is needed, otherwise the Runnable might use a stale value. 
138-     final  String  sessionIdForScheduledTask  = aqsSessionId ;
139-     final  ApplicationProcessState  applicationProcessStateForScheduledTask  = applicationProcessState ;
123+     long  collectionFrequency  =
124+         Math .min (
125+             getCpuGaugeCollectionFrequencyMs (applicationProcessState ),
126+             getMemoryGaugeCollectionFrequencyMs (applicationProcessState ));
127+     String  sessionIdForScheduledTask  = session .aqsSessionId ();
140128
141-     // TODO(b/394127311): Switch to using AQS. 
142129    try  {
143130      gaugeManagerDataCollectionJob  =
144131          gaugeManagerExecutor 
145132              .get ()
146133              .scheduleWithFixedDelay (
147134                  () -> {
148-                     syncFlush (sessionIdForScheduledTask , applicationProcessStateForScheduledTask );
135+                     syncFlush (sessionIdForScheduledTask , applicationProcessState );
149136                  },
150137                  /* initialDelay= */  collectionFrequency 
151138                      * APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC ,
@@ -155,6 +142,36 @@ public void startCollectingGauges(
155142    } catch  (RejectedExecutionException  e ) {
156143      logger .warn ("Unable to start collecting Gauges: "  + e .getMessage ());
157144    }
145+ 
146+     if  (this .applicationProcessState  != applicationProcessState ) {
147+       this .applicationProcessState  = applicationProcessState ;
148+ 
149+       // If there's a change in App State - update the gauge collection frequency as well. 
150+       stopCollectingGauges ();
151+       startCollectingGauges (applicationProcessState , this .session .getTimer ());
152+     }
153+   }
154+ 
155+   /** 
156+    * Starts the collection of available gauges for the given {@code session}. 
157+    * 
158+    * <p>GaugeManager can only collect gauges for one session at a time, and if this method is called 
159+    * again with the same or new sessionId while it's already collecting gauges, all future gauges 
160+    * will then be associated with the same or new sessionId and applicationProcessState. 
161+    * 
162+    * @param session The {@link PerfSession} to which the collected gauges will be associated with. 
163+    * @note: This method is NOT thread safe - {@link this.startCollectingGauges()} and {@link 
164+    *     this.stopCollectingGauges()} should always be called from the same thread. 
165+    */ 
166+   public  void  startCollectingGauges (PerfSession  session ) {
167+     if  (this .session  != null ) {
168+       stopCollectingGauges ();
169+     }
170+ 
171+     long  collectionFrequency  = startCollectingGauges (applicationProcessState , session .getTimer ());
172+     if  (collectionFrequency  == INVALID_GAUGE_COLLECTION_FREQUENCY ) {
173+       logger .warn ("Invalid gauge collection frequency. Unable to start collecting Gauges." );
174+     }
158175  }
159176
160177  /** 
@@ -192,12 +209,14 @@ private long startCollectingGauges(ApplicationProcessState appState, Timer refer
192209   *     this.stopCollectingGauges()} should always be called from the same thread. 
193210   */ 
194211  public  void  stopCollectingGauges () {
195-     if  (this .sessionId  == null ) {
212+     if  (this .session  == null ) {
196213      return ;
197214    }
198215
199216    // This is needed, otherwise the Runnable might use a stale value. 
200-     final  String  sessionIdForScheduledTask  = aqsSessionId ;
217+ 
218+     // AQS is guaranteed to be available when stopping gauge collection. 
219+     final  String  sessionIdForScheduledTask  = session .aqsSessionId ();
201220    final  ApplicationProcessState  applicationProcessStateForScheduledTask  = applicationProcessState ;
202221
203222    cpuGaugeCollector .get ().stopCollecting ();
@@ -207,7 +226,6 @@ public void stopCollectingGauges() {
207226      gaugeManagerDataCollectionJob .cancel (false );
208227    }
209228
210-     // TODO(b/394127311): Switch to using AQS. 
211229    // Flush any data that was collected for this session one last time. 
212230    @ SuppressWarnings ("FutureReturnValueIgnored" )
213231    ScheduledFuture <?> unusedFuture  =
@@ -220,8 +238,7 @@ public void stopCollectingGauges() {
220238                TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS ,
221239                TimeUnit .MILLISECONDS );
222240
223-     this .sessionId  = null ;
224-     this .applicationProcessState  = ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ;
241+     this .session  = null ;
225242  }
226243
227244  /** 
@@ -407,4 +424,9 @@ private long getMemoryGaugeCollectionFrequencyMs(
407424      return  memoryGaugeCollectionFrequency ;
408425    }
409426  }
427+ 
428+   @ VisibleForTesting 
429+   void  setApplicationProcessState (ApplicationProcessState  applicationProcessState ) {
430+     this .applicationProcessState  = applicationProcessState ;
431+   }
410432}
0 commit comments