Skip to content

Commit 1335a5c

Browse files
romtsnmarkushistefanosiano
committed
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 <[email protected]> Co-authored-by: stefanosiano <[email protected]>
1 parent 2fcc388 commit 1335a5c

18 files changed

+260
-122
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- Do not instrument File I/O operations if tracing is disabled ([#4051](https://github.com/getsentry/sentry-java/pull/4051))
8+
- Do not instrument User Interaction multiple times ([#4051](https://github.com/getsentry/sentry-java/pull/4051))
9+
- 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))
11+
512
### Behavioural Changes
613

714
- Reduce the number of broadcasts the SDK is subscribed for ([#4052](https://github.com/getsentry/sentry-java/pull/4052))

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public final class io/sentry/android/core/DeviceInfoUtil {
195195
public static fun getInstance (Landroid/content/Context;Lio/sentry/android/core/SentryAndroidOptions;)Lio/sentry/android/core/DeviceInfoUtil;
196196
public fun getOperatingSystem ()Lio/sentry/protocol/OperatingSystem;
197197
public fun getSideLoadedInfo ()Lio/sentry/android/core/ContextUtils$SideLoadedInfo;
198+
public fun getTotalMemory ()Ljava/lang/Long;
198199
public static fun isCharging (Landroid/content/Intent;Lio/sentry/SentryOptions;)Ljava/lang/Boolean;
199200
public static fun resetInstance ()V
200201
}
@@ -522,3 +523,14 @@ public class io/sentry/android/core/performance/WindowContentChangedCallback : i
522523
public fun onContentChanged ()V
523524
}
524525

526+
public final class io/sentry/android/core/util/AndroidLazyEvaluator {
527+
public fun <init> (Lio/sentry/android/core/util/AndroidLazyEvaluator$AndroidEvaluator;)V
528+
public fun getValue (Landroid/content/Context;)Ljava/lang/Object;
529+
public fun resetValue ()V
530+
public fun setValue (Ljava/lang/Object;)V
531+
}
532+
533+
public abstract interface class io/sentry/android/core/util/AndroidLazyEvaluator$AndroidEvaluator {
534+
public abstract fun evaluate (Landroid/content/Context;)Ljava/lang/Object;
535+
}
536+

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
@@ -324,8 +324,8 @@ private static void readDefaultOptionValues(
324324
final @NotNull SentryAndroidOptions options,
325325
final @NotNull Context context,
326326
final @NotNull BuildInfoProvider buildInfoProvider) {
327-
final PackageInfo packageInfo =
328-
ContextUtils.getPackageInfo(context, options.getLogger(), buildInfoProvider);
327+
final @Nullable PackageInfo packageInfo =
328+
ContextUtils.getPackageInfo(context, buildInfoProvider);
329329
if (packageInfo != null) {
330330
// Sets App's release if not set by Manifest
331331
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;
@@ -258,9 +256,12 @@ public void bindTransaction(final @NotNull ITransaction transaction) {
258256
transactionsCounter = 0;
259257

260258
String totalMem = "0";
261-
ActivityManager.MemoryInfo memInfo = getMemInfo();
262-
if (memInfo != null) {
263-
totalMem = Long.toString(memInfo.totalMem);
259+
final @Nullable Long memory =
260+
(options instanceof SentryAndroidOptions)
261+
? DeviceInfoUtil.getInstance(context, (SentryAndroidOptions) options).getTotalMemory()
262+
: null;
263+
if (memory != null) {
264+
totalMem = Long.toString(memory);
264265
}
265266
String[] abis = Build.SUPPORTED_ABIS;
266267

@@ -327,27 +328,6 @@ public void close() {
327328
}
328329
}
329330

330-
/**
331-
* Get MemoryInfo object representing the memory state of the application.
332-
*
333-
* @return MemoryInfo object representing the memory state of the application
334-
*/
335-
private @Nullable ActivityManager.MemoryInfo getMemInfo() {
336-
try {
337-
ActivityManager actManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
338-
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
339-
if (actManager != null) {
340-
actManager.getMemoryInfo(memInfo);
341-
return memInfo;
342-
}
343-
logger.log(SentryLevel.INFO, "Error getting MemoryInfo.");
344-
return null;
345-
} catch (Throwable e) {
346-
logger.log(SentryLevel.ERROR, "Error getting MemoryInfo.", e);
347-
return null;
348-
}
349-
}
350-
351331
@TestOnly
352332
int getTransactionsCounter() {
353333
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
}
@@ -597,8 +596,7 @@ private void mergeUser(final @NotNull SentryBaseEvent event) {
597596
private void setSideLoadedInfo(final @NotNull SentryBaseEvent event) {
598597
try {
599598
final ContextUtils.SideLoadedInfo sideLoadedInfo =
600-
ContextUtils.retrieveSideLoadedInfo(context, options.getLogger(), buildInfoProvider);
601-
599+
DeviceInfoUtil.getInstance(context, options).getSideLoadedInfo();
602600
if (sideLoadedInfo != null) {
603601
final @NotNull Map<String, String> tags = sideLoadedInfo.asTags();
604602
for (Map.Entry<String, String> entry : tags.entrySet()) {
@@ -667,7 +665,8 @@ private void setDevice(final @NotNull SentryBaseEvent event) {
667665

668666
private void mergeOS(final @NotNull SentryBaseEvent event) {
669667
final OperatingSystem currentOS = event.getContexts().getOperatingSystem();
670-
final OperatingSystem androidOS = getOperatingSystem();
668+
final OperatingSystem androidOS =
669+
DeviceInfoUtil.getInstance(context, options).getOperatingSystem();
671670

672671
// make Android OS the main OS using the 'os' key
673672
event.getContexts().setOperatingSystem(androidOS);
@@ -683,20 +682,5 @@ private void mergeOS(final @NotNull SentryBaseEvent event) {
683682
event.getContexts().put(osNameKey, currentOS);
684683
}
685684
}
686-
687-
private @NotNull OperatingSystem getOperatingSystem() {
688-
OperatingSystem os = new OperatingSystem();
689-
os.setName("Android");
690-
os.setVersion(Build.VERSION.RELEASE);
691-
os.setBuild(Build.DISPLAY);
692-
693-
try {
694-
os.setKernelVersion(ContextUtils.getKernelVersion(options.getLogger()));
695-
} catch (Throwable e) {
696-
options.getLogger().log(SentryLevel.ERROR, "Error getting OperatingSystem.", e);
697-
}
698-
699-
return os;
700-
}
701685
// endregion
702686
}

0 commit comments

Comments
 (0)