Skip to content

Commit 6381921

Browse files
romtsnmarkushistefanosiano
authored
Reduce the number of IPC calls (#4058)
* Remove binder call for external storage * Remove binder call for memory in profiler * Cache static values to avoid binder calls * Comment * Changelog * Formatting * Fix tests * Minor fixes * change protected method in final class to private --------- Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io> Co-authored-by: stefanosiano <stefano.siano@sentry.io>
1 parent e509503 commit 6381921

18 files changed

+254
-122
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Do not instrument File I/O operations if tracing is disabled ([#4051](https://github.com/getsentry/sentry-java/pull/4051))
88
- Do not instrument User Interaction multiple times ([#4051](https://github.com/getsentry/sentry-java/pull/4051))
99
- Speed up view traversal to find touched target in `UserInteractionIntegration` ([#4051](https://github.com/getsentry/sentry-java/pull/4051))
10+
- Reduce IPC/Binder calls performed by the SDK ([#4058](https://github.com/getsentry/sentry-java/pull/4058))
1011

1112
### Behavioural Changes
1213

sentry-android-core/api/sentry-android-core.api

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ public final class io/sentry/android/core/DeviceInfoUtil {
193193
public static fun getInstance (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;)Lio/sentry/android/core/DeviceInfoUtil;
194194
public fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem;
195195
public fun getSideLoadedInfo ()Lio/sentry/android/core/ContextUtils$SideLoadedInfo;
196+
public fun getTotalMemory ()Ljava/lang/Long;
196197
public static fun isCharging (Landroid/content/Intent;Lio/sentry/SentryOptions;)Ljava/lang/Boolean;
197198
public static fun resetInstance ()V
198199
}
@@ -501,3 +502,14 @@ public class io/sentry/android/core/performance/WindowContentChangedCallback : i
501502
public fun onContentChanged ()V
502503
}
503504

505+
public final class io/sentry/android/core/util/AndroidLazyEvaluator {
506+
public fun <init> (Lio/sentry/android/core/util/AndroidLazyEvaluator$AndroidEvaluator;)V
507+
public fun getValue (Landroid/content/Context;)Ljava/lang/Object;
508+
public fun resetValue ()V
509+
public fun setValue (Ljava/lang/Object;)V
510+
}
511+
512+
public abstract interface class io/sentry/android/core/util/AndroidLazyEvaluator$AndroidEvaluator {
513+
public abstract fun evaluate (Landroid/content/Context;)Ljava/lang/Object;
514+
}
515+

sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ private static void readDefaultOptionValues(
320320
final @NotNull SentryAndroidOptions options,
321321
final @NotNull Context context,
322322
final @NotNull BuildInfoProvider buildInfoProvider) {
323-
final PackageInfo packageInfo =
324-
ContextUtils.getPackageInfo(context, options.getLogger(), buildInfoProvider);
323+
final @Nullable PackageInfo packageInfo =
324+
ContextUtils.getPackageInfo(context, buildInfoProvider);
325325
if (packageInfo != null) {
326326
// Sets App's release if not set by Manifest
327327
if (options.getRelease() == null) {

sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package io.sentry.android.core;
22

3-
import static android.content.Context.ACTIVITY_SERVICE;
43
import static java.util.concurrent.TimeUnit.SECONDS;
54

65
import android.annotation.SuppressLint;
7-
import android.app.ActivityManager;
86
import android.content.Context;
97
import android.os.Build;
108
import android.os.Process;
@@ -265,9 +263,12 @@ public synchronized void bindTransaction(final @NotNull ITransaction transaction
265263
transactionsCounter = 0;
266264

267265
String totalMem = "0";
268-
ActivityManager.MemoryInfo memInfo = getMemInfo();
269-
if (memInfo != null) {
270-
totalMem = Long.toString(memInfo.totalMem);
266+
final @Nullable Long memory =
267+
(options instanceof SentryAndroidOptions)
268+
? DeviceInfoUtil.getInstance(context, (SentryAndroidOptions) options).getTotalMemory()
269+
: null;
270+
if (memory != null) {
271+
totalMem = Long.toString(memory);
271272
}
272273
String[] abis = Build.SUPPORTED_ABIS;
273274

@@ -333,27 +334,6 @@ public void close() {
333334
}
334335
}
335336

336-
/**
337-
* Get MemoryInfo object representing the memory state of the application.
338-
*
339-
* @return MemoryInfo object representing the memory state of the application
340-
*/
341-
private @Nullable ActivityManager.MemoryInfo getMemInfo() {
342-
try {
343-
ActivityManager actManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
344-
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
345-
if (actManager != null) {
346-
actManager.getMemoryInfo(memInfo);
347-
return memInfo;
348-
}
349-
logger.log(SentryLevel.INFO, "Error getting MemoryInfo.");
350-
return null;
351-
} catch (Throwable e) {
352-
logger.log(SentryLevel.ERROR, "Error getting MemoryInfo.", e);
353-
return null;
354-
}
355-
}
356-
357337
@TestOnly
358338
int getTransactionsCounter() {
359339
return transactionsCounter;

sentry-android-core/src/main/java/io/sentry/android/core/AnrV2EventProcessor.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,13 @@ private void setApp(final @NotNull SentryBaseEvent event, final @NotNull Object
374374
if (app == null) {
375375
app = new App();
376376
}
377-
app.setAppName(ContextUtils.getApplicationName(context, options.getLogger()));
377+
app.setAppName(ContextUtils.getApplicationName(context));
378378
// TODO: not entirely correct, because we define background ANRs as not the ones of
379379
// IMPORTANCE_FOREGROUND, but this doesn't mean the app was in foreground when an ANR happened
380380
// but it's our best effort for now. We could serialize AppState in theory.
381381
app.setInForeground(!isBackgroundAnr(hint));
382382

383-
final PackageInfo packageInfo =
384-
ContextUtils.getPackageInfo(context, options.getLogger(), buildInfoProvider);
383+
final PackageInfo packageInfo = ContextUtils.getPackageInfo(context, buildInfoProvider);
385384
if (packageInfo != null) {
386385
app.setAppIdentifier(packageInfo.packageName);
387386
}
@@ -592,8 +591,7 @@ private void mergeUser(final @NotNull SentryBaseEvent event) {
592591
private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
593592
try {
594593
final ContextUtils.SideLoadedInfo sideLoadedInfo =
595-
ContextUtils.retrieveSideLoadedInfo(context, options.getLogger(), buildInfoProvider);
596-
594+
DeviceInfoUtil.getInstance(context, options).getSideLoadedInfo();
597595
if (sideLoadedInfo != null) {
598596
final @NotNull Map<String, String> tags = sideLoadedInfo.asTags();
599597
for (Map.Entry<String, String> entry : tags.entrySet()) {
@@ -662,7 +660,8 @@ private void setDevice(final @NotNull SentryBaseEvent event) {
662660

663661
private void mergeOS(final @NotNull SentryBaseEvent event) {
664662
final OperatingSystem currentOS = event.getContexts().getOperatingSystem();
665-
final OperatingSystem androidOS = getOperatingSystem();
663+
final OperatingSystem androidOS =
664+
DeviceInfoUtil.getInstance(context, options).getOperatingSystem();
666665

667666
// make Android OS the main OS using the 'os' key
668667
event.getContexts().setOperatingSystem(androidOS);
@@ -678,20 +677,5 @@ private void mergeOS(final @NotNull SentryBaseEvent event) {
678677
event.getContexts().put(osNameKey, currentOS);
679678
}
680679
}
681-
682-
private @NotNull OperatingSystem getOperatingSystem() {
683-
OperatingSystem os = new OperatingSystem();
684-
os.setName("Android");
685-
os.setVersion(Build.VERSION.RELEASE);
686-
os.setBuild(Build.DISPLAY);
687-
688-
try {
689-
os.setKernelVersion(ContextUtils.getKernelVersion(options.getLogger()));
690-
} catch (Throwable e) {
691-
options.getLogger().log(SentryLevel.ERROR, "Error getting OperatingSystem.", e);
692-
}
693-
694-
return os;
695-
}
696680
// endregion
697681
}

0 commit comments

Comments
 (0)