From 56147febf15bf28e114f52c176cf8e0413a34a4a Mon Sep 17 00:00:00 2001 From: stefanosiano Date: Mon, 8 Jan 2024 11:46:37 +0100 Subject: [PATCH 1/2] put list for loop in synchronized block in AndroidProfiler --- .../sentry/android/core/AndroidProfiler.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java index ee4dd84374..a66c110c3f 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java @@ -313,26 +313,29 @@ private void putPerformanceCollectionDataInMeasurements( new ArrayDeque<>(performanceCollectionData.size()); final @NotNull ArrayDeque cpuUsageMeasurements = new ArrayDeque<>(performanceCollectionData.size()); - for (PerformanceCollectionData performanceData : performanceCollectionData) { - CpuCollectionData cpuData = performanceData.getCpuData(); - MemoryCollectionData memoryData = performanceData.getMemoryData(); - if (cpuData != null) { - cpuUsageMeasurements.add( + + synchronized (performanceCollectionData) { + for (PerformanceCollectionData performanceData : performanceCollectionData) { + CpuCollectionData cpuData = performanceData.getCpuData(); + MemoryCollectionData memoryData = performanceData.getMemoryData(); + if (cpuData != null) { + cpuUsageMeasurements.add( new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis()) + timestampDiff, - cpuData.getCpuUsagePercentage())); - } - if (memoryData != null && memoryData.getUsedHeapMemory() > -1) { - memoryUsageMeasurements.add( + TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis()) + timestampDiff, + cpuData.getCpuUsagePercentage())); + } + if (memoryData != null && memoryData.getUsedHeapMemory() > -1) { + memoryUsageMeasurements.add( new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, - memoryData.getUsedHeapMemory())); - } - if (memoryData != null && memoryData.getUsedNativeMemory() > -1) { - nativeMemoryUsageMeasurements.add( + TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, + memoryData.getUsedHeapMemory())); + } + if (memoryData != null && memoryData.getUsedNativeMemory() > -1) { + nativeMemoryUsageMeasurements.add( new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, - memoryData.getUsedNativeMemory())); + TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, + memoryData.getUsedNativeMemory())); + } } } if (!cpuUsageMeasurements.isEmpty()) { From d9e64bcae0bae2a1b1c8887ce6d8fab4d9a13e6b Mon Sep 17 00:00:00 2001 From: stefanosiano Date: Mon, 8 Jan 2024 11:49:14 +0100 Subject: [PATCH 2/2] updated changelog --- CHANGELOG.md | 1 + .../sentry/android/core/AndroidProfiler.java | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9773f3a15d..502b4a63b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- Avoid concurrency in AndroidProfiler performance data collection ([#3130](https://github.com/getsentry/sentry-java/pull/3130)) - Improve thresholds for network changes breadcrumbs ([#3083](https://github.com/getsentry/sentry-java/pull/3083)) - SchedulerFactoryBeanCustomizer now runs first so user customization is not overridden ([#3095](https://github.com/getsentry/sentry-java/pull/3095)) - If you are setting global job listeners please also add `SentryJobListener` diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java index a66c110c3f..ff595b9191 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java @@ -320,21 +320,21 @@ private void putPerformanceCollectionDataInMeasurements( MemoryCollectionData memoryData = performanceData.getMemoryData(); if (cpuData != null) { cpuUsageMeasurements.add( - new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis()) + timestampDiff, - cpuData.getCpuUsagePercentage())); + new ProfileMeasurementValue( + TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis()) + timestampDiff, + cpuData.getCpuUsagePercentage())); } if (memoryData != null && memoryData.getUsedHeapMemory() > -1) { memoryUsageMeasurements.add( - new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, - memoryData.getUsedHeapMemory())); + new ProfileMeasurementValue( + TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, + memoryData.getUsedHeapMemory())); } if (memoryData != null && memoryData.getUsedNativeMemory() > -1) { nativeMemoryUsageMeasurements.add( - new ProfileMeasurementValue( - TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, - memoryData.getUsedNativeMemory())); + new ProfileMeasurementValue( + TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis()) + timestampDiff, + memoryData.getUsedNativeMemory())); } } }