Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ public boolean logGaugeMetadata(String sessionId, ApplicationProcessState appSta

private GaugeMetadata getGaugeMetadata() {
return GaugeMetadata.newBuilder()
.setProcessName(gaugeMetadataManager.getProcessName())
.setDeviceRamSizeKb(gaugeMetadataManager.getDeviceRamSizeKb())
.setMaxAppJavaHeapMemoryKb(gaugeMetadataManager.getMaxAppJavaHeapMemoryKb())
.setMaxEncouragedAppJavaHeapMemoryKb(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.firebase.perf.logging.AndroidLogger;
import com.google.firebase.perf.util.StorageUnit;
Expand All @@ -28,7 +27,6 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -43,7 +41,6 @@ class GaugeMetadataManager {
private final Runtime runtime;
private final ActivityManager activityManager;
private final MemoryInfo memoryInfo;
private final String currentProcessName;
private final Context appContext;

GaugeMetadataManager(Context appContext) {
Expand All @@ -57,15 +54,6 @@ class GaugeMetadataManager {
this.activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);

// Assign the current process name here to avoid iterating through all the running processes
// each time getCurrentProcessName() is called.
currentProcessName = getCurrentProcessName();
}

/** Returns the name of the process FirebaseApp is associated with. */
public String getProcessName() {
return currentProcessName;
}

/**
Expand Down Expand Up @@ -112,23 +100,4 @@ int readTotalRAM(String procFileName) {

return 0;
}

/** Returns the name of the process the Application is associated with. */
private String getCurrentProcessName() {
int myProcessPid = android.os.Process.myPid();

@Nullable
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos =
activityManager.getRunningAppProcesses();

if (runningAppProcessInfos != null) {
for (ActivityManager.RunningAppProcessInfo processInfo : runningAppProcessInfos) {
if (processInfo.pid == myProcessPid) {
return processInfo.processName;
}
}
}

return appContext.getPackageName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,8 @@ message AndroidMemoryReading {
//
// Next tag: 7
message GaugeMetadata {
// The process in which Firebase instance is initialized and collecting data.
// Fireperf sdk collects information in the context of a process (instead of
// the whole app). The process name helps developer identifies which process
// are the gauge data coming from.
optional string process_name = 1;
// Deprecated on 09/2022.
optional string process_name = 1 [deprecated = true];

// Clock rate of the cpu of the device, in kHz.
optional int32 cpu_clock_rate_khz = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public void testGaugeMetricIsValid() {
// Construct GaugeMetadata
GaugeMetadata gaugeMetadata =
createValidGaugeMetadata(
"processName",
/* deviceRamSizeKb= */ 2000,
/* maxAppJavaHeapMemoryKb= */ 1000,
/* maxEncouragedAppJavaHeapMemoryKb= */ 800);
Expand Down Expand Up @@ -115,7 +114,6 @@ public void testGaugeMetricWithOnlyMemoryMetricIsValid() {
public void testGaugeMetricWithOnlyGaugeMetadataIsValid() {
GaugeMetadata gaugeMetadata =
createValidGaugeMetadata(
"processName",
/* deviceRamSizeKb= */ 2000,
/* maxAppJavaHeapMemoryKb= */ 1000,
/* maxEncouragedAppJavaHeapMemoryKb= */ 800);
Expand All @@ -134,7 +132,6 @@ public void testGaugeMetricWithOnlyGaugeMetadataIsValid() {
public void testGaugeMetadataWithoutMaxJavaHeapIsNotValid() {
GaugeMetadata gaugeMetadata =
GaugeMetadata.newBuilder()
.setProcessName("processName")
.setDeviceRamSizeKb(2000)
.setMaxEncouragedAppJavaHeapMemoryKb(800)
.build();
Expand Down Expand Up @@ -202,12 +199,8 @@ private AndroidMemoryReading createValidAndroidMetricReading(int currentUsedAppJ
}

private GaugeMetadata createValidGaugeMetadata(
String processName,
int deviceRamSizeKb,
int maxAppJavaHeapMemoryKb,
int maxEncouragedAppJavaHeapMemoryKb) {
int deviceRamSizeKb, int maxAppJavaHeapMemoryKb, int maxEncouragedAppJavaHeapMemoryKb) {
GaugeMetadata.Builder fakeGaugeMetadataBuilder = GaugeMetadata.newBuilder();
fakeGaugeMetadataBuilder.setProcessName(processName);
fakeGaugeMetadataBuilder.setDeviceRamSizeKb(deviceRamSizeKb);
fakeGaugeMetadataBuilder.setMaxAppJavaHeapMemoryKb(maxAppJavaHeapMemoryKb);
fakeGaugeMetadataBuilder.setMaxEncouragedAppJavaHeapMemoryKb(maxEncouragedAppJavaHeapMemoryKb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@ public void testStartGaugeManagerWithNewSessionIdAndNewAppState() {

@Test
public void testLogGaugeMetadataSendDataToTransport() {
when(fakeGaugeMetadataManager.getProcessName()).thenReturn("processName");
when(fakeGaugeMetadataManager.getDeviceRamSizeKb()).thenReturn(2000);
when(fakeGaugeMetadataManager.getMaxAppJavaHeapMemoryKb()).thenReturn(1000);
when(fakeGaugeMetadataManager.getMaxEncouragedAppJavaHeapMemoryKb()).thenReturn(800);
Expand All @@ -649,7 +648,6 @@ public void testLogGaugeMetadataSendDataToTransport() {
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric.getGaugeMetadata();

assertThat(recordedGaugeMetric.getSessionId()).isEqualTo("sessionId");
assertThat(recordedGaugeMetadata.getProcessName()).isEqualTo("processName");

assertThat(recordedGaugeMetadata.getDeviceRamSizeKb())
.isEqualTo(fakeGaugeMetadataManager.getDeviceRamSizeKb());
Expand Down Expand Up @@ -699,7 +697,6 @@ public void testLogGaugeMetadataLogsAfterApplicationContextIsSet() {
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric.getGaugeMetadata();

assertThat(recordedGaugeMetric.getSessionId()).isEqualTo("sessionId");
assertThat(recordedGaugeMetadata.hasProcessName()).isTrue();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.robolectric.Shadows.shadowOf;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Environment;
import androidx.test.core.app.ApplicationProvider;
Expand All @@ -32,8 +29,6 @@
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -78,38 +73,6 @@ private void mockMemory() {
shadowOf(activityManager).setMemoryClass(RUNTIME_MAX_ENCOURAGED_MEMORY_MB);
}

@Test
public void testInitialization_getProcessNameReturnsNull_doesNotCrash() {
ActivityManager activityManagerPartialMock = spy(activityManager);
when(activityManagerPartialMock.getRunningAppProcesses()).thenReturn(null);

assertThat(new GaugeMetadataManager(runtime, appContext)).isNotNull();
}

@Test
public void testGetProcessName_noProcessInfoList_returnsPackageName() {
shadowOf(activityManager).setProcesses(new ArrayList<>());
assertThat(new GaugeMetadataManager(runtime, appContext).getProcessName())
.isEqualTo(appContext.getPackageName());
}

@Test
public void testGetProcessName_processListWithoutCurrentPid_returnsPackageName() {
shadowOf(activityManager)
.setProcesses(
generateFakeAppProcessInfoListThatContainsPid(android.os.Process.myPid() + 100));
assertThat(new GaugeMetadataManager(runtime, appContext).getProcessName())
.isEqualTo(appContext.getPackageName());
}

@Test
public void testGetProcessName_processListWithCurrentPid_returnsProcessName() {
shadowOf(activityManager)
.setProcesses(generateFakeAppProcessInfoListThatContainsPid(android.os.Process.myPid()));
assertThat(new GaugeMetadataManager(runtime, appContext).getProcessName())
.isEqualTo("fakeProcessName");
}

@Test
public void testGetMaxAppJavaHeapMemory_returnsExpectedValue() {
assertThat(testGaugeMetadataManager.getMaxAppJavaHeapMemoryKb()).isGreaterThan(0);
Expand Down Expand Up @@ -146,17 +109,6 @@ private String createFakeMemInfoFile() throws IOException {
return file.getAbsolutePath();
}

private List<RunningAppProcessInfo> generateFakeAppProcessInfoListThatContainsPid(int pid) {
ActivityManager.RunningAppProcessInfo fakeProcessInfoList = new RunningAppProcessInfo();
fakeProcessInfoList.pid = pid;
fakeProcessInfoList.processName = "fakeProcessName";

List<RunningAppProcessInfo> processInfoList = new ArrayList<>();
processInfoList.add(fakeProcessInfoList);

return processInfoList;
}

private static final String MEM_INFO_CONTENTS =
"MemTotal: "
+ DEVICE_RAM_SIZE_KB
Expand Down