Skip to content

Flattened PerformanceCollectionData #4505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- No longer send out empty log envelopes ([#4497](https://github.com/getsentry/sentry-java/pull/4497))

### Internal

- Flattened PerformanceCollectionData ([#4505](https://github.com/getsentry/sentry-java/pull/4505))

### Dependencies

- Bump Gradle from v8.14.1 to v8.14.2 ([#4473](https://github.com/getsentry/sentry-java/pull/4473))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import android.os.SystemClock;
import android.system.Os;
import android.system.OsConstants;
import io.sentry.CpuCollectionData;
import io.sentry.ILogger;
import io.sentry.IPerformanceSnapshotCollector;
import io.sentry.PerformanceCollectionData;
import io.sentry.SentryLevel;
import io.sentry.SentryNanotimeDate;
import io.sentry.util.FileUtils;
import io.sentry.util.Objects;
import java.io.File;
Expand Down Expand Up @@ -72,11 +70,8 @@ public void collect(final @NotNull PerformanceCollectionData performanceCollecti
// number from 0 to 100, so we are going to multiply it by 100
final double cpuUsagePercentage = cpuNanosDiff / (double) realTimeNanosDiff;

CpuCollectionData cpuData =
new CpuCollectionData(
(cpuUsagePercentage / (double) numCores) * 100.0, new SentryNanotimeDate());

performanceCollectionData.addCpuData(cpuData);
performanceCollectionData.setCpuUsagePercentage(
(cpuUsagePercentage / (double) numCores) * 100.0);
}

/** Read the /proc/self/stat file and parses the result. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import android.os.Debug;
import io.sentry.IPerformanceSnapshotCollector;
import io.sentry.MemoryCollectionData;
import io.sentry.PerformanceCollectionData;
import io.sentry.SentryNanotimeDate;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -18,8 +16,7 @@ public void setup() {}
public void collect(final @NotNull PerformanceCollectionData performanceCollectionData) {
long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize();
MemoryCollectionData memoryData =
new MemoryCollectionData(usedMemory, usedNativeMemory, new SentryNanotimeDate());
performanceCollectionData.addMemoryData(memoryData);
performanceCollectionData.setUsedHeapMemory(usedMemory);
performanceCollectionData.setUsedNativeMemory(usedNativeMemory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import android.os.Debug;
import android.os.Process;
import android.os.SystemClock;
import io.sentry.CpuCollectionData;
import io.sentry.DateUtils;
import io.sentry.ILogger;
import io.sentry.ISentryExecutorService;
import io.sentry.ISentryLifecycleToken;
import io.sentry.MemoryCollectionData;
import io.sentry.PerformanceCollectionData;
import io.sentry.SentryDate;
import io.sentry.SentryLevel;
import io.sentry.SentryNanotimeDate;
import io.sentry.SentryUUID;
Expand Down Expand Up @@ -156,7 +153,7 @@ public void onFrameMetricCollected(
// profileStartNanos is calculated through SystemClock.elapsedRealtimeNanos(),
// but frameEndNanos uses System.nanotime(), so we convert it to get the timestamp
// relative to profileStartNanos
final SentryDate timestamp = new SentryNanotimeDate();
final long timestampNanos = new SentryNanotimeDate().nanoTimestamp();
final long frameTimestampRelativeNanos =
frameEndNanos
- System.nanoTime()
Expand All @@ -171,17 +168,17 @@ public void onFrameMetricCollected(
if (isFrozen) {
frozenFrameRenderMeasurements.addLast(
new ProfileMeasurementValue(
frameTimestampRelativeNanos, durationNanos, timestamp));
frameTimestampRelativeNanos, durationNanos, timestampNanos));
} else if (isSlow) {
slowFrameRenderMeasurements.addLast(
new ProfileMeasurementValue(
frameTimestampRelativeNanos, durationNanos, timestamp));
frameTimestampRelativeNanos, durationNanos, timestampNanos));
}
if (refreshRate != lastRefreshRate) {
lastRefreshRate = refreshRate;
screenFrameRateMeasurements.addLast(
new ProfileMeasurementValue(
frameTimestampRelativeNanos, refreshRate, timestamp));
frameTimestampRelativeNanos, refreshRate, timestampNanos));
}
}
});
Expand Down Expand Up @@ -318,32 +315,28 @@ private void putPerformanceCollectionDataInMeasurements(
new ArrayDeque<>(performanceCollectionData.size());

synchronized (performanceCollectionData) {
for (PerformanceCollectionData performanceData : performanceCollectionData) {
CpuCollectionData cpuData = performanceData.getCpuData();
MemoryCollectionData memoryData = performanceData.getMemoryData();
if (cpuData != null) {
for (final @NotNull PerformanceCollectionData data : performanceCollectionData) {
final long nanoTimestamp = data.getNanoTimestamp();
final long relativeStartNs = nanoTimestamp + timestampDiff;
final @Nullable Double cpuUsagePercentage = data.getCpuUsagePercentage();
final @Nullable Long usedHeapMemory = data.getUsedHeapMemory();
final @Nullable Long usedNativeMemory = data.getUsedNativeMemory();

if (cpuUsagePercentage != null) {
cpuUsageMeasurements.add(
new ProfileMeasurementValue(
cpuData.getTimestamp().nanoTimestamp() + timestampDiff,
cpuData.getCpuUsagePercentage(),
cpuData.getTimestamp()));
new ProfileMeasurementValue(relativeStartNs, cpuUsagePercentage, nanoTimestamp));
}
if (memoryData != null && memoryData.getUsedHeapMemory() > -1) {
if (usedHeapMemory != null) {
memoryUsageMeasurements.add(
new ProfileMeasurementValue(
memoryData.getTimestamp().nanoTimestamp() + timestampDiff,
memoryData.getUsedHeapMemory(),
memoryData.getTimestamp()));
new ProfileMeasurementValue(relativeStartNs, usedHeapMemory, nanoTimestamp));
}
if (memoryData != null && memoryData.getUsedNativeMemory() > -1) {
if (usedNativeMemory != null) {
nativeMemoryUsageMeasurements.add(
new ProfileMeasurementValue(
memoryData.getTimestamp().nanoTimestamp() + timestampDiff,
memoryData.getUsedNativeMemory(),
memoryData.getTimestamp()));
new ProfileMeasurementValue(relativeStartNs, usedNativeMemory, nanoTimestamp));
}
}
}

if (!cpuUsageMeasurements.isEmpty()) {
measurementsMap.put(
ProfileMeasurement.ID_CPU_USAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import android.os.Build
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.CompositePerformanceCollector
import io.sentry.CpuCollectionData
import io.sentry.DataCategory
import io.sentry.IConnectionStatusProvider
import io.sentry.ILogger
import io.sentry.IScopes
import io.sentry.MemoryCollectionData
import io.sentry.PerformanceCollectionData
import io.sentry.ProfileLifecycle
import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.SentryNanotimeDate
import io.sentry.SentryTracer
import io.sentry.TracesSampler
import io.sentry.TransactionContext
Expand Down Expand Up @@ -433,10 +430,11 @@ class AndroidContinuousProfilerTest {
@Test
fun `profiler sends chunk with measurements`() {
val performanceCollector = mock<CompositePerformanceCollector>()
val collectionData = PerformanceCollectionData()
val collectionData = PerformanceCollectionData(10)

collectionData.addMemoryData(MemoryCollectionData(2, 3, SentryNanotimeDate()))
collectionData.addCpuData(CpuCollectionData(3.0, SentryNanotimeDate()))
collectionData.usedHeapMemory = 2
collectionData.usedNativeMemory = 3
collectionData.cpuUsagePercentage = 3.0
whenever(performanceCollector.stop(any<String>())).thenReturn(listOf(collectionData))

fixture.options.compositePerformanceCollector = performanceCollector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ class AndroidCpuCollectorTest {

@Test
fun `collect works only after setup`() {
val data = PerformanceCollectionData()
val data = PerformanceCollectionData(10)
fixture.getSut().collect(data)
assertNull(data.cpuData)
assertNull(data.cpuUsagePercentage)
}

@Test
fun `when collect cpu is collected`() {
val data = PerformanceCollectionData()
val data = PerformanceCollectionData(10)
val collector = fixture.getSut()
collector.setup()
collector.collect(data)
val cpuData = data.cpuData
val cpuData = data.cpuUsagePercentage
assertNotNull(cpuData)
assertNotEquals(0.0, cpuData.cpuUsagePercentage)
assertNotEquals(0, cpuData.timestamp.nanoTimestamp())
assertNotEquals(0.0, cpuData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ class AndroidMemoryCollectorTest {

@Test
fun `when collect, both native and heap memory are collected`() {
val data = PerformanceCollectionData()
val data = PerformanceCollectionData(10)
val usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize()
val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory()
fixture.collector.collect(data)
val memoryData = data.memoryData
assertNotNull(memoryData)
assertNotEquals(-1, memoryData.usedNativeMemory)
assertEquals(usedNativeMemory, memoryData.usedNativeMemory)
assertEquals(usedMemory, memoryData.usedHeapMemory)
assertNotEquals(0, memoryData.timestamp.nanoTimestamp())
assertNotNull(data.usedHeapMemory)
assertNotNull(data.usedNativeMemory)
assertNotEquals(-1, data.usedNativeMemory)
assertEquals(usedNativeMemory, data.usedNativeMemory)
assertEquals(usedMemory, data.usedHeapMemory)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package io.sentry.android.core
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.CpuCollectionData
import io.sentry.ILogger
import io.sentry.ISentryExecutorService
import io.sentry.MemoryCollectionData
import io.sentry.PerformanceCollectionData
import io.sentry.SentryDate
import io.sentry.SentryExecutorService
import io.sentry.SentryLevel
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector
Expand Down Expand Up @@ -258,15 +255,15 @@ class AndroidProfilerTest {
fun `profiler includes performance measurements when passed on end`() {
val profiler = fixture.getSut()
val performanceCollectionData = ArrayList<PerformanceCollectionData>()
var singleData = PerformanceCollectionData()
val t1 = mock<SentryDate>()
val t2 = mock<SentryDate>()
singleData.addMemoryData(MemoryCollectionData(2, 3, t1))
singleData.addCpuData(CpuCollectionData(1.4, t1))
var singleData = PerformanceCollectionData(10)
singleData.usedHeapMemory = 2
singleData.usedNativeMemory = 3
singleData.cpuUsagePercentage = 1.4
performanceCollectionData.add(singleData)

singleData = PerformanceCollectionData()
singleData.addMemoryData(MemoryCollectionData(3, 4, t2))
singleData = PerformanceCollectionData(20)
singleData.usedHeapMemory = 3
singleData.usedNativeMemory = 4
performanceCollectionData.add(singleData)

profiler.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import android.content.Context
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.CpuCollectionData
import io.sentry.ILogger
import io.sentry.IScopes
import io.sentry.ISentryExecutorService
import io.sentry.MemoryCollectionData
import io.sentry.PerformanceCollectionData
import io.sentry.ProfilingTraceData
import io.sentry.SentryLevel
Expand Down Expand Up @@ -459,13 +457,15 @@ class AndroidTransactionProfilerTest {
fun `profiler includes performance measurements when passed on transaction finish`() {
val profiler = fixture.getSut(context)
val performanceCollectionData = ArrayList<PerformanceCollectionData>()
var singleData = PerformanceCollectionData()
singleData.addMemoryData(MemoryCollectionData(2, 3, mock()))
singleData.addCpuData(CpuCollectionData(1.4, mock()))
var singleData = PerformanceCollectionData(10)
singleData.usedHeapMemory = 2
singleData.usedNativeMemory = 3
singleData.cpuUsagePercentage = 1.4
performanceCollectionData.add(singleData)

singleData = PerformanceCollectionData()
singleData.addMemoryData(MemoryCollectionData(3, 4, mock()))
singleData = PerformanceCollectionData(20)
singleData.usedHeapMemory = 3
singleData.usedNativeMemory = 4
performanceCollectionData.add(singleData)

profiler.start()
Expand Down
35 changes: 10 additions & 25 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,6 @@ public abstract interface class io/sentry/CompositePerformanceCollector {
public abstract fun stop (Ljava/lang/String;)Ljava/util/List;
}

public final class io/sentry/CpuCollectionData {
public fun <init> (DLio/sentry/SentryDate;)V
public fun getCpuUsagePercentage ()D
public fun getTimestamp ()Lio/sentry/SentryDate;
}

public final class io/sentry/CustomSamplingContext {
public fun <init> ()V
public fun get (Ljava/lang/String;)Ljava/lang/Object;
Expand Down Expand Up @@ -791,10 +785,6 @@ public abstract interface class io/sentry/ILogger {
public abstract fun log (Lio/sentry/SentryLevel;Ljava/lang/Throwable;Ljava/lang/String;[Ljava/lang/Object;)V
}

public abstract interface class io/sentry/IMemoryCollector {
public abstract fun collect ()Lio/sentry/MemoryCollectionData;
}

public abstract interface class io/sentry/IOptionsObserver {
public abstract fun setDist (Ljava/lang/String;)V
public abstract fun setEnvironment (Ljava/lang/String;)V
Expand Down Expand Up @@ -1349,14 +1339,6 @@ public final class io/sentry/MeasurementUnit$Information : java/lang/Enum, io/se
public static fun values ()[Lio/sentry/MeasurementUnit$Information;
}

public final class io/sentry/MemoryCollectionData {
public fun <init> (JJLio/sentry/SentryDate;)V
public fun <init> (JLio/sentry/SentryDate;)V
public fun getTimestamp ()Lio/sentry/SentryDate;
public fun getUsedHeapMemory ()J
public fun getUsedNativeMemory ()J
}

public final class io/sentry/MonitorConfig : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public fun <init> (Lio/sentry/MonitorSchedule;)V
public fun getCheckinMargin ()Ljava/lang/Long;
Expand Down Expand Up @@ -1931,11 +1913,14 @@ public final class io/sentry/OutboxSender : io/sentry/IEnvelopeSender {
}

public final class io/sentry/PerformanceCollectionData {
public fun <init> ()V
public fun addCpuData (Lio/sentry/CpuCollectionData;)V
public fun addMemoryData (Lio/sentry/MemoryCollectionData;)V
public fun getCpuData ()Lio/sentry/CpuCollectionData;
public fun getMemoryData ()Lio/sentry/MemoryCollectionData;
public fun <init> (J)V
public fun getCpuUsagePercentage ()Ljava/lang/Double;
public fun getNanoTimestamp ()J
public fun getUsedHeapMemory ()Ljava/lang/Long;
public fun getUsedNativeMemory ()Ljava/lang/Long;
public fun setCpuUsagePercentage (Ljava/lang/Double;)V
public fun setUsedHeapMemory (Ljava/lang/Long;)V
public fun setUsedNativeMemory (Ljava/lang/Long;)V
}

public final class io/sentry/ProfileChunk : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
Expand Down Expand Up @@ -4915,10 +4900,10 @@ public final class io/sentry/profilemeasurements/ProfileMeasurement$JsonKeys {

public final class io/sentry/profilemeasurements/ProfileMeasurementValue : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public fun <init> ()V
public fun <init> (Ljava/lang/Long;Ljava/lang/Number;Lio/sentry/SentryDate;)V
public fun <init> (Ljava/lang/Long;Ljava/lang/Number;J)V
public fun equals (Ljava/lang/Object;)Z
public fun getRelativeStartNs ()Ljava/lang/String;
public fun getTimestamp ()Ljava/lang/Double;
public fun getTimestamp ()D
public fun getUnknown ()Ljava/util/Map;
public fun getValue ()D
public fun hashCode ()I
Expand Down
23 changes: 0 additions & 23 deletions sentry/src/main/java/io/sentry/CpuCollectionData.java

This file was deleted.

Loading
Loading