Skip to content

Commit 4767a34

Browse files
committed
Implement an atomic counter to log gauges
1 parent 1b108a2 commit 4767a34

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/session/gauges/CpuGaugeCollector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class CpuGaugeCollector {
7676
private final String procFileName;
7777
private final long clockTicksPerSecond;
7878

79-
@Nullable private ScheduledFuture cpuMetricCollectorJob = null;
79+
@Nullable private ScheduledFuture<?> cpuMetricCollectorJob = null;
8080
private long cpuMetricCollectionRateMs = UNSET_CPU_METRIC_COLLECTION_RATE;
8181

8282
// TODO(b/258263016): Migrate to go/firebase-android-executors
@@ -166,6 +166,7 @@ private synchronized void scheduleCpuMetricCollectionWithRate(
166166
CpuMetricReading currCpuReading = syncCollectCpuMetric(referenceTime);
167167
if (currCpuReading != null) {
168168
cpuMetricReadings.add(currCpuReading);
169+
GaugeCounter.Companion.getInstance().incrementCounter();
169170
}
170171
},
171172
/* initialDelay */ 0,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.google.firebase.perf.session.gauges
2+
3+
import java.util.concurrent.atomic.AtomicInteger
4+
5+
6+
class GaugeCounter private constructor() {
7+
private val counter = AtomicInteger(0)
8+
private val gaugeManager: GaugeManager = GaugeManager.getInstance()
9+
10+
fun incrementCounter() {
11+
val metricsCount = counter.incrementAndGet()
12+
13+
if (metricsCount >= MAX_METRIC_COUNT && gaugeManager.logGaugeMetrics()) {
14+
counter.set(0)
15+
}
16+
}
17+
18+
fun resetCounter() {
19+
counter.set(0)
20+
}
21+
22+
companion object {
23+
val instance: GaugeCounter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { GaugeCounter() }
24+
const val MAX_METRIC_COUNT = 25
25+
}
26+
}

firebase-perf/src/main/java/com/google/firebase/perf/session/gauges/GaugeManager.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public class GaugeManager extends AppStateUpdateHandler {
5959
private final TransportManager transportManager;
6060

6161
@Nullable private GaugeMetadataManager gaugeMetadataManager;
62-
@Nullable private ScheduledFuture<?> gaugeManagerDataCollectionJob = null;
6362
@Nullable private PerfSession session = null;
6463
private ApplicationProcessState applicationProcessState =
6564
ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN;
@@ -183,30 +182,36 @@ public void stopCollectingGauges() {
183182
return;
184183
}
185184

186-
// This is needed, otherwise the Runnable might use a stale value.
187-
final String sessionIdForScheduledTask = session.sessionId();
188-
final ApplicationProcessState applicationProcessStateForScheduledTask = applicationProcessState;
189-
190185
cpuGaugeCollector.get().stopCollecting();
191186
memoryGaugeCollector.get().stopCollecting();
192187

193-
if (gaugeManagerDataCollectionJob != null) {
194-
gaugeManagerDataCollectionJob.cancel(false);
188+
logGaugeMetrics();
189+
GaugeCounter.Companion.getInstance().resetCounter();
190+
this.session = null;
191+
}
192+
193+
protected boolean logGaugeMetrics() {
194+
if (session == null) {
195+
logger.warn("Attempted to log Gauge Metrics when session was null.");
196+
return false;
195197
}
196198

197-
// Flush any data that was collected for this session one last time.
199+
logExistingGaugeMetrics(session.sessionId(), applicationProcessState);
200+
return true;
201+
}
202+
203+
private void logExistingGaugeMetrics(String sessionId, ApplicationProcessState applicationProcessState) {
204+
// Flush any data that was collected and attach it to the given session and app state.
198205
@SuppressWarnings("FutureReturnValueIgnored")
199206
ScheduledFuture<?> unusedFuture =
200-
gaugeManagerExecutor
201-
.get()
202-
.schedule(
203-
() -> {
204-
syncFlush(sessionIdForScheduledTask, applicationProcessStateForScheduledTask);
205-
},
206-
TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS,
207-
TimeUnit.MILLISECONDS);
208-
209-
this.session = null;
207+
gaugeManagerExecutor
208+
.get()
209+
.schedule(
210+
() -> {
211+
syncFlush(sessionId, applicationProcessState);
212+
},
213+
TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS,
214+
TimeUnit.MILLISECONDS);
210215
}
211216

212217
/**

firebase-perf/src/main/java/com/google/firebase/perf/session/gauges/MemoryGaugeCollector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ private synchronized void scheduleMemoryMetricCollectionWithRate(
129129
AndroidMemoryReading memoryReading = syncCollectMemoryMetric(referenceTime);
130130
if (memoryReading != null) {
131131
memoryMetricReadings.add(memoryReading);
132+
GaugeCounter.Companion.getInstance().incrementCounter();
132133
}
133134
},
134135
/* initialDelay */ 0,

0 commit comments

Comments
 (0)