Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2b073a0e authored by Andreas Gampe's avatar Andreas Gampe
Browse files

Frameworks: Clean up ProfilerInfo

Use a ProfilerInfo object in ActivityManagerService to reduce the
number of exposed fields. In preparation for adding an agent during
startup.

Bug: 62445317
Test: m
Test: Device boots
Test: Manual test for profiling
Test: cts/hostsidetests/services/activityandwindowmanager/util/run-test CtsServicesHostTestCases android.server.cts.ActivityManagerAmProfileTests
Change-Id: I5fa347ded760c263a8ce3754bb2631205ea7b4b8
parent 2fa4a346
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
@@ -5138,11 +5138,7 @@ public final class ActivityThread {
                Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
                        + " -- can the process access this path?");
            } finally {
                try {
                    profilerInfo.profileFd.close();
                } catch (IOException e) {
                    Slog.w(TAG, "Failure closing profile fd", e);
                }
                profilerInfo.closeFd();
            }
        } else {
            switch (profileType) {
+43 −10
Original line number Diff line number Diff line
@@ -17,8 +17,11 @@
package android.app;

import android.os.Parcel;
import android.os.Parcelable;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.util.Slog;

import java.io.IOException;

/**
 * System private API for passing profiler settings.
@@ -27,6 +30,8 @@ import android.os.ParcelFileDescriptor;
 */
public class ProfilerInfo implements Parcelable {

    private static final String TAG = "ProfilerInfo";

    /* Name of profile output file. */
    public final String profileFile;

@@ -39,7 +44,9 @@ public class ProfilerInfo implements Parcelable {
    /* Automatically stop the profiler when the app goes idle. */
    public final boolean autoStopProfiler;

    /* Indicates whether to stream the profiling info to the out file continuously. */
    /*
     * Indicates whether to stream the profiling info to the out file continuously.
     */
    public final boolean streamingOutput;

    public ProfilerInfo(String filename, ParcelFileDescriptor fd, int interval, boolean autoStop,
@@ -51,6 +58,29 @@ public class ProfilerInfo implements Parcelable {
        streamingOutput = streaming;
    }

    public ProfilerInfo(ProfilerInfo in) {
        profileFile = in.profileFile;
        profileFd = in.profileFd;
        samplingInterval = in.samplingInterval;
        autoStopProfiler = in.autoStopProfiler;
        streamingOutput = in.streamingOutput;
    }

    /**
     * Close profileFd, if it is open. The field will be null after a call to this function.
     */
    public void closeFd() {
        if (profileFd != null) {
            try {
                profileFd.close();
            } catch (IOException e) {
                Slog.w(TAG, "Failure closing profile fd", e);
            }
            profileFd = null;
        }
    }

    @Override
    public int describeContents() {
        if (profileFd != null) {
            return profileFd.describeContents();
@@ -59,6 +89,7 @@ public class ProfilerInfo implements Parcelable {
        }
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(profileFile);
        if (profileFd != null) {
@@ -74,10 +105,12 @@ public class ProfilerInfo implements Parcelable {

    public static final Parcelable.Creator<ProfilerInfo> CREATOR =
            new Parcelable.Creator<ProfilerInfo>() {
                @Override
                public ProfilerInfo createFromParcel(Parcel in) {
                    return new ProfilerInfo(in);
                }

                @Override
                public ProfilerInfo[] newArray(int size) {
                    return new ProfilerInfo[size];
                }
+35 −50
Original line number Diff line number Diff line
@@ -1499,11 +1499,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    boolean mControllerIsAMonkey = false;
    String mProfileApp = null;
    ProcessRecord mProfileProc = null;
    String mProfileFile;
    ParcelFileDescriptor mProfileFd;
    int mSamplingInterval = 0;
    boolean mAutoStopProfiler = false;
    boolean mStreamingOutput = false;
    ProfilerInfo mProfilerInfo = null;
    int mProfileType = 0;
    final ProcessMap<Pair<Long, String>> mMemWatchProcesses = new ProcessMap<>();
    String mMemWatchDumpProcName;
@@ -6909,19 +6905,16 @@ public class ActivityManagerService extends IActivityManager.Stub
                    mWaitForDebugger = mOrigWaitForDebugger;
                }
            }
            String profileFile = app.instr != null ? app.instr.mProfileFile : null;
            ParcelFileDescriptor profileFd = null;
            int samplingInterval = 0;
            boolean profileAutoStop = false;
            boolean profileStreamingOutput = false;
            ProfilerInfo profilerInfo = null;
            if (mProfileApp != null && mProfileApp.equals(processName)) {
                mProfileProc = app;
                profileFile = mProfileFile;
                profileFd = mProfileFd;
                samplingInterval = mSamplingInterval;
                profileAutoStop = mAutoStopProfiler;
                profileStreamingOutput = mStreamingOutput;
                profilerInfo = (mProfilerInfo != null && mProfilerInfo.profileFile != null) ?
                        new ProfilerInfo(mProfilerInfo) : null;
            } else if (app.instr != null && app.instr.mProfileFile != null) {
                profilerInfo = new ProfilerInfo(app.instr.mProfileFile, null, 0, false, false);
            }
            boolean enableTrackAllocation = false;
            if (mTrackAllocationApp != null && mTrackAllocationApp.equals(processName)) {
                enableTrackAllocation = true;
@@ -6945,12 +6938,10 @@ public class ActivityManagerService extends IActivityManager.Stub
                    + processName + " with config " + getGlobalConfiguration());
            ApplicationInfo appInfo = app.instr != null ? app.instr.mTargetInfo : app.info;
            app.compat = compatibilityInfoForPackageLocked(appInfo);
            if (profileFd != null) {
                profileFd = profileFd.dup();
            if (profilerInfo != null && profilerInfo.profileFd != null) {
                profilerInfo.profileFd = profilerInfo.profileFd.dup();
            }
            ProfilerInfo profilerInfo = profileFile == null ? null
                    : new ProfilerInfo(profileFile, profileFd, samplingInterval, profileAutoStop,
                                       profileStreamingOutput);
            // We deprecated Build.SERIAL and it is not accessible to
            // apps that target the v2 security sandbox. Since access to
@@ -7125,11 +7116,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                        mStackSupervisor.activityIdleInternalLocked(token, false /* fromTimeout */,
                                false /* processPausingActivities */, config);
                if (stopProfiling) {
                    if ((mProfileProc == r.app) && (mProfileFd != null)) {
                        try {
                            mProfileFd.close();
                        } catch (IOException e) {
                        }
                    if ((mProfileProc == r.app) && mProfilerInfo != null) {
                        clearProfilerLocked();
                    }
                }
@@ -12738,18 +12725,16 @@ public class ActivityManagerService extends IActivityManager.Stub
                }
            }
            mProfileApp = processName;
            mProfileFile = profilerInfo.profileFile;
            if (mProfileFd != null) {
            if (mProfilerInfo != null) {
                if (mProfilerInfo.profileFd != null) {
                    try {
                    mProfileFd.close();
                        mProfilerInfo.profileFd.close();
                    } catch (IOException e) {
                    }
                mProfileFd = null;
                }
            mProfileFd = profilerInfo.profileFd;
            mSamplingInterval = profilerInfo.samplingInterval;
            mAutoStopProfiler = profilerInfo.autoStopProfiler;
            mStreamingOutput = profilerInfo.streamingOutput;
            }
            mProfilerInfo = new ProfilerInfo(profilerInfo);
            mProfileType = 0;
        }
    }
@@ -15879,20 +15864,24 @@ public class ActivityManagerService extends IActivityManager.Stub
                pw.println("  mTrackAllocationApp=" + mTrackAllocationApp);
            }
        }
        if (mProfileApp != null || mProfileProc != null || mProfileFile != null
                || mProfileFd != null) {
        if (mProfileApp != null || mProfileProc != null || (mProfilerInfo != null &&
                (mProfilerInfo.profileFile != null || mProfilerInfo.profileFd != null))) {
            if (dumpPackage == null || dumpPackage.equals(mProfileApp)) {
                if (needSep) {
                    pw.println();
                    needSep = false;
                }
                pw.println("  mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc);
                pw.println("  mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd);
                pw.println("  mSamplingInterval=" + mSamplingInterval + " mAutoStopProfiler="
                        + mAutoStopProfiler + " mStreamingOutput=" + mStreamingOutput);
                if (mProfilerInfo != null) {
                    pw.println("  mProfileFile=" + mProfilerInfo.profileFile + " mProfileFd=" +
                            mProfilerInfo.profileFd);
                    pw.println("  mSamplingInterval=" + mProfilerInfo.samplingInterval +
                            " mAutoStopProfiler=" + mProfilerInfo.autoStopProfiler +
                            " mStreamingOutput=" + mProfilerInfo.streamingOutput);
                    pw.println("  mProfileType=" + mProfileType);
                }
            }
        }
        if (mNativeDebuggingApp != null) {
            if (dumpPackage == null || dumpPackage.equals(mNativeDebuggingApp)) {
                if (needSep) {
@@ -23209,19 +23198,15 @@ public class ActivityManagerService extends IActivityManager.Stub
    }
    private void clearProfilerLocked() {
        if (mProfileFd != null) {
        if (mProfilerInfo !=null && mProfilerInfo.profileFd != null) {
            try {
                mProfileFd.close();
                mProfilerInfo.profileFd.close();
            } catch (IOException e) {
            }
        }
        mProfileApp = null;
        mProfileProc = null;
        mProfileFile = null;
        mProfileType = 0;
        mAutoStopProfiler = false;
        mStreamingOutput = false;
        mSamplingInterval = 0;
        mProfilerInfo = null;
    }
    public boolean profileControl(String process, int userId, boolean start,
@@ -23265,10 +23250,10 @@ public class ActivityManagerService extends IActivityManager.Stub
                    proc.thread.profilerControl(start, profilerInfo, profileType);
                    fd = null;
                    try {
                        mProfileFd.close();
                        mProfilerInfo.profileFd.close();
                    } catch (IOException e) {
                    }
                    mProfileFd = null;
                    mProfilerInfo.profileFd = null;
                } else {
                    stopProfilerLocked(proc, profileType);
                    if (profilerInfo != null && profilerInfo.profileFd != null) {
+6 −15
Original line number Diff line number Diff line
@@ -1442,26 +1442,17 @@ public class ActivityStackSupervisor extends ConfigurationContainer implements D
                if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
                    if (mService.mProfileProc == null || mService.mProfileProc == app) {
                        mService.mProfileProc = app;
                        final String profileFile = mService.mProfileFile;
                        if (profileFile != null) {
                            ParcelFileDescriptor profileFd = mService.mProfileFd;
                            if (profileFd != null) {
                        ProfilerInfo profilerInfoSvc = mService.mProfilerInfo;
                        if (profilerInfoSvc != null && profilerInfoSvc.profileFile != null) {
                            if (profilerInfoSvc.profileFd != null) {
                                try {
                                    profileFd = profileFd.dup();
                                    profilerInfoSvc.profileFd = profilerInfoSvc.profileFd.dup();
                                } catch (IOException e) {
                                    if (profileFd != null) {
                                        try {
                                            profileFd.close();
                                        } catch (IOException o) {
                                        }
                                        profileFd = null;
                                    }
                                    profilerInfoSvc.closeFd();
                                }
                            }

                            profilerInfo = new ProfilerInfo(profileFile, profileFd,
                                    mService.mSamplingInterval, mService.mAutoStopProfiler,
                                    mService.mStreamingOutput);
                            profilerInfo = new ProfilerInfo(profilerInfoSvc);
                        }
                    }
                }