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

Commit 70f61cf6 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic 'am_start_attach_agent'

* changes:
  Frameworks: Add agent to ProfilerInfo
  Frameworks: Clean up ProfilerInfo
parents c8600632 83085bb3
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) {
+52 −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,18 +44,50 @@ 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;

    /**
     * Denotes an agent (and its parameters) to attach for profiling.
     */
    public final String agent;

    public ProfilerInfo(String filename, ParcelFileDescriptor fd, int interval, boolean autoStop,
                        boolean streaming) {
            boolean streaming, String agent) {
        profileFile = filename;
        profileFd = fd;
        samplingInterval = interval;
        autoStopProfiler = autoStop;
        streamingOutput = streaming;
        this.agent = agent;
    }

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

    /**
     * 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 +96,7 @@ public class ProfilerInfo implements Parcelable {
        }
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(profileFile);
        if (profileFd != null) {
@@ -70,14 +108,17 @@ public class ProfilerInfo implements Parcelable {
        out.writeInt(samplingInterval);
        out.writeInt(autoStopProfiler ? 1 : 0);
        out.writeInt(streamingOutput ? 1 : 0);
        out.writeString(agent);
    }

    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];
                }
@@ -89,5 +130,6 @@ public class ProfilerInfo implements Parcelable {
        samplingInterval = in.readInt();
        autoStopProfiler = in.readInt() != 0;
        streamingOutput = in.readInt() != 0;
        agent = in.readString();
    }
}
+44 −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,19 @@ 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;
            String agent = 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;
                agent = profilerInfo.agent;
            } else if (app.instr != null && app.instr.mProfileFile != null) {
                profilerInfo = new ProfilerInfo(app.instr.mProfileFile, null, 0, false, false,
                        null);
            }
            boolean enableTrackAllocation = false;
            if (mTrackAllocationApp != null && mTrackAllocationApp.equals(processName)) {
                enableTrackAllocation = true;
@@ -6945,12 +6941,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
@@ -6990,6 +6984,12 @@ public class ActivityManagerService extends IActivityManager.Stub
                }
            }
            // If we were asked to attach an agent on startup, do so now, before we're binding
            // application code.
            if (agent != null) {
                thread.attachAgent(agent);
            }
            checkTime(startTime, "attachApplicationLocked: immediately before bindApplication");
            mStackSupervisor.mActivityMetricsLogger.notifyBindApplication(app);
            if (app.instr != null) {
@@ -7125,11 +7125,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 +12734,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 +15873,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 +23207,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 +23259,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) {
+14 −6
Original line number Diff line number Diff line
@@ -114,6 +114,7 @@ final class ActivityManagerShellCommand extends ShellCommand {
    private int mSamplingInterval;
    private boolean mAutoStop;
    private boolean mStreaming;   // Streaming the profiling output to a file.
    private String mAgent;  // Agent to attach on startup.
    private int mDisplayId;
    private int mStackId;
    private int mTaskId;
@@ -292,6 +293,8 @@ final class ActivityManagerShellCommand extends ShellCommand {
                    mSamplingInterval = Integer.parseInt(getNextArgRequired());
                } else if (opt.equals("--streaming")) {
                    mStreaming = true;
                } else if (opt.equals("--attach-agent")) {
                    mAgent = getNextArgRequired();
                } else if (opt.equals("-R")) {
                    mRepeat = Integer.parseInt(getNextArgRequired());
                } else if (opt.equals("-S")) {
@@ -368,13 +371,16 @@ final class ActivityManagerShellCommand extends ShellCommand {

            ProfilerInfo profilerInfo = null;

            if (mProfileFile != null || mAgent != null) {
                ParcelFileDescriptor fd = null;
                if (mProfileFile != null) {
                ParcelFileDescriptor fd = openOutputFileForSystem(mProfileFile);
                    fd = openOutputFileForSystem(mProfileFile);
                    if (fd == null) {
                        return 1;
                    }
                }
                profilerInfo = new ProfilerInfo(mProfileFile, fd, mSamplingInterval, mAutoStop,
                                                mStreaming);
                        mStreaming, mAgent);
            }

            pw.println("Starting: " + intent);
@@ -747,7 +753,8 @@ final class ActivityManagerShellCommand extends ShellCommand {
            if (fd == null) {
                return -1;
            }
            profilerInfo = new ProfilerInfo(profileFile, fd, mSamplingInterval, false, mStreaming);
            profilerInfo = new ProfilerInfo(profileFile, fd, mSamplingInterval, false, mStreaming,
                    null);
        }

        try {
@@ -2490,6 +2497,7 @@ final class ActivityManagerShellCommand extends ShellCommand {
            pw.println("      --streaming: stream the profiling output to the specified file");
            pw.println("          (use with --start-profiler)");
            pw.println("      -P <FILE>: like above, but profiling stops when app goes idle");
            pw.println("      --attach-agent <agent>: attach the given agent before binding");
            pw.println("      -R: repeat the activity launch <COUNT> times.  Prior to each repeat,");
            pw.println("          the top activity will be finished.");
            pw.println("      -S: force stop the target app before starting the activity");
+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);
                        }
                    }
                }