Search in sources :

Example 16 with ProcessStats

use of com.android.internal.app.procstats.ProcessStats in project android_frameworks_base by AOSPA.

the class ProcessStatsService method dumpInner.

private void dumpInner(FileDescriptor fd, PrintWriter pw, String[] args) {
    final long now = SystemClock.uptimeMillis();
    boolean isCheckin = false;
    boolean isCompact = false;
    boolean isCsv = false;
    boolean currentOnly = false;
    boolean dumpDetails = false;
    boolean dumpFullDetails = false;
    boolean dumpAll = false;
    boolean quit = false;
    int aggregateHours = 0;
    int lastIndex = 0;
    int maxNum = 2;
    boolean activeOnly = false;
    String reqPackage = null;
    boolean csvSepScreenStats = false;
    int[] csvScreenStats = new int[] { ProcessStats.ADJ_SCREEN_OFF, ProcessStats.ADJ_SCREEN_ON };
    boolean csvSepMemStats = false;
    int[] csvMemStats = new int[] { ProcessStats.ADJ_MEM_FACTOR_CRITICAL };
    boolean csvSepProcStats = true;
    int[] csvProcStats = ProcessStats.ALL_PROC_STATES;
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if ("--checkin".equals(arg)) {
                isCheckin = true;
            } else if ("-c".equals(arg)) {
                isCompact = true;
            } else if ("--csv".equals(arg)) {
                isCsv = true;
            } else if ("--csv-screen".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --csv-screen");
                    dumpHelp(pw);
                    return;
                }
                boolean[] sep = new boolean[1];
                String[] error = new String[1];
                csvScreenStats = parseStateList(DumpUtils.ADJ_SCREEN_NAMES_CSV, ProcessStats.ADJ_SCREEN_MOD, args[i], sep, error);
                if (csvScreenStats == null) {
                    pw.println("Error in \"" + args[i] + "\": " + error[0]);
                    dumpHelp(pw);
                    return;
                }
                csvSepScreenStats = sep[0];
            } else if ("--csv-mem".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --csv-mem");
                    dumpHelp(pw);
                    return;
                }
                boolean[] sep = new boolean[1];
                String[] error = new String[1];
                csvMemStats = parseStateList(DumpUtils.ADJ_MEM_NAMES_CSV, 1, args[i], sep, error);
                if (csvMemStats == null) {
                    pw.println("Error in \"" + args[i] + "\": " + error[0]);
                    dumpHelp(pw);
                    return;
                }
                csvSepMemStats = sep[0];
            } else if ("--csv-proc".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --csv-proc");
                    dumpHelp(pw);
                    return;
                }
                boolean[] sep = new boolean[1];
                String[] error = new String[1];
                csvProcStats = parseStateList(DumpUtils.STATE_NAMES_CSV, 1, args[i], sep, error);
                if (csvProcStats == null) {
                    pw.println("Error in \"" + args[i] + "\": " + error[0]);
                    dumpHelp(pw);
                    return;
                }
                csvSepProcStats = sep[0];
            } else if ("--details".equals(arg)) {
                dumpDetails = true;
            } else if ("--full-details".equals(arg)) {
                dumpFullDetails = true;
            } else if ("--hours".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --hours");
                    dumpHelp(pw);
                    return;
                }
                try {
                    aggregateHours = Integer.parseInt(args[i]);
                } catch (NumberFormatException e) {
                    pw.println("Error: --hours argument not an int -- " + args[i]);
                    dumpHelp(pw);
                    return;
                }
            } else if ("--last".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --last");
                    dumpHelp(pw);
                    return;
                }
                try {
                    lastIndex = Integer.parseInt(args[i]);
                } catch (NumberFormatException e) {
                    pw.println("Error: --last argument not an int -- " + args[i]);
                    dumpHelp(pw);
                    return;
                }
            } else if ("--max".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --max");
                    dumpHelp(pw);
                    return;
                }
                try {
                    maxNum = Integer.parseInt(args[i]);
                } catch (NumberFormatException e) {
                    pw.println("Error: --max argument not an int -- " + args[i]);
                    dumpHelp(pw);
                    return;
                }
            } else if ("--active".equals(arg)) {
                activeOnly = true;
                currentOnly = true;
            } else if ("--current".equals(arg)) {
                currentOnly = true;
            } else if ("--commit".equals(arg)) {
                synchronized (mAm) {
                    mProcessStats.mFlags |= ProcessStats.FLAG_COMPLETE;
                    writeStateLocked(true, true);
                    pw.println("Process stats committed.");
                    quit = true;
                }
            } else if ("--reset".equals(arg)) {
                synchronized (mAm) {
                    mProcessStats.resetSafely();
                    pw.println("Process stats reset.");
                    quit = true;
                }
            } else if ("--clear".equals(arg)) {
                synchronized (mAm) {
                    mProcessStats.resetSafely();
                    ArrayList<String> files = getCommittedFiles(0, true, true);
                    if (files != null) {
                        for (int fi = 0; fi < files.size(); fi++) {
                            (new File(files.get(fi))).delete();
                        }
                    }
                    pw.println("All process stats cleared.");
                    quit = true;
                }
            } else if ("--write".equals(arg)) {
                synchronized (mAm) {
                    writeStateSyncLocked();
                    pw.println("Process stats written.");
                    quit = true;
                }
            } else if ("--read".equals(arg)) {
                synchronized (mAm) {
                    readLocked(mProcessStats, mFile);
                    pw.println("Process stats read.");
                    quit = true;
                }
            } else if ("--start-testing".equals(arg)) {
                synchronized (mAm) {
                    mAm.setTestPssMode(true);
                    pw.println("Started high frequency sampling.");
                    quit = true;
                }
            } else if ("--stop-testing".equals(arg)) {
                synchronized (mAm) {
                    mAm.setTestPssMode(false);
                    pw.println("Stopped high frequency sampling.");
                    quit = true;
                }
            } else if ("-h".equals(arg)) {
                dumpHelp(pw);
                return;
            } else if ("-a".equals(arg)) {
                dumpDetails = true;
                dumpAll = true;
            } else if (arg.length() > 0 && arg.charAt(0) == '-') {
                pw.println("Unknown option: " + arg);
                dumpHelp(pw);
                return;
            } else {
                // Not an option, last argument must be a package name.
                reqPackage = arg;
                // Include all details, since we know we are only going to
                // be dumping a smaller set of data.  In fact only the details
                // contain per-package data, so this is needed to be able
                // to dump anything at all when filtering by package.
                dumpDetails = true;
            }
        }
    }
    if (quit) {
        return;
    }
    if (isCsv) {
        pw.print("Processes running summed over");
        if (!csvSepScreenStats) {
            for (int i = 0; i < csvScreenStats.length; i++) {
                pw.print(" ");
                DumpUtils.printScreenLabelCsv(pw, csvScreenStats[i]);
            }
        }
        if (!csvSepMemStats) {
            for (int i = 0; i < csvMemStats.length; i++) {
                pw.print(" ");
                DumpUtils.printMemLabelCsv(pw, csvMemStats[i]);
            }
        }
        if (!csvSepProcStats) {
            for (int i = 0; i < csvProcStats.length; i++) {
                pw.print(" ");
                pw.print(DumpUtils.STATE_NAMES_CSV[csvProcStats[i]]);
            }
        }
        pw.println();
        synchronized (mAm) {
            dumpFilteredProcessesCsvLocked(pw, null, csvSepScreenStats, csvScreenStats, csvSepMemStats, csvMemStats, csvSepProcStats, csvProcStats, now, reqPackage);
        /*
                dumpFilteredProcessesCsvLocked(pw, "Processes running while critical mem:",
                        false, new int[] {ADJ_SCREEN_OFF, ADJ_SCREEN_ON},
                        true, new int[] {ADJ_MEM_FACTOR_CRITICAL},
                        true, new int[] {STATE_PERSISTENT, STATE_TOP, STATE_FOREGROUND, STATE_VISIBLE,
                                STATE_PERCEPTIBLE, STATE_BACKUP, STATE_SERVICE, STATE_HOME,
                                STATE_PREVIOUS, STATE_CACHED},
                        now, reqPackage);
                dumpFilteredProcessesCsvLocked(pw, "Processes running over all mem:",
                        false, new int[] {ADJ_SCREEN_OFF, ADJ_SCREEN_ON},
                        false, new int[] {ADJ_MEM_FACTOR_CRITICAL, ADJ_MEM_FACTOR_LOW,
                                ADJ_MEM_FACTOR_MODERATE, ADJ_MEM_FACTOR_MODERATE},
                        true, new int[] {STATE_PERSISTENT, STATE_TOP, STATE_FOREGROUND, STATE_VISIBLE,
                                STATE_PERCEPTIBLE, STATE_BACKUP, STATE_SERVICE, STATE_HOME,
                                STATE_PREVIOUS, STATE_CACHED},
                        now, reqPackage);
                */
        }
        return;
    } else if (aggregateHours != 0) {
        pw.print("AGGREGATED OVER LAST ");
        pw.print(aggregateHours);
        pw.println(" HOURS:");
        dumpAggregatedStats(pw, aggregateHours, now, reqPackage, isCompact, dumpDetails, dumpFullDetails, dumpAll, activeOnly);
        return;
    } else if (lastIndex > 0) {
        pw.print("LAST STATS AT INDEX ");
        pw.print(lastIndex);
        pw.println(":");
        ArrayList<String> files = getCommittedFiles(0, false, true);
        if (lastIndex >= files.size()) {
            pw.print("Only have ");
            pw.print(files.size());
            pw.println(" data sets");
            return;
        }
        AtomicFile file = new AtomicFile(new File(files.get(lastIndex)));
        ProcessStats processStats = new ProcessStats(false);
        readLocked(processStats, file);
        if (processStats.mReadError != null) {
            if (isCheckin || isCompact)
                pw.print("err,");
            pw.print("Failure reading ");
            pw.print(files.get(lastIndex));
            pw.print("; ");
            pw.println(processStats.mReadError);
            return;
        }
        String fileStr = file.getBaseFile().getPath();
        boolean checkedIn = fileStr.endsWith(STATE_FILE_CHECKIN_SUFFIX);
        if (isCheckin || isCompact) {
            // Don't really need to lock because we uniquely own this object.
            processStats.dumpCheckinLocked(pw, reqPackage);
        } else {
            pw.print("COMMITTED STATS FROM ");
            pw.print(processStats.mTimePeriodStartClockStr);
            if (checkedIn)
                pw.print(" (checked in)");
            pw.println(":");
            if (dumpDetails || dumpFullDetails) {
                processStats.dumpLocked(pw, reqPackage, now, !dumpFullDetails, dumpAll, activeOnly);
                if (dumpAll) {
                    pw.print("  mFile=");
                    pw.println(mFile.getBaseFile());
                }
            } else {
                processStats.dumpSummaryLocked(pw, reqPackage, now, activeOnly);
            }
        }
        return;
    }
    boolean sepNeeded = false;
    if (dumpAll || isCheckin) {
        mWriteLock.lock();
        try {
            ArrayList<String> files = getCommittedFiles(0, false, !isCheckin);
            if (files != null) {
                int start = isCheckin ? 0 : (files.size() - maxNum);
                if (start < 0) {
                    start = 0;
                }
                for (int i = start; i < files.size(); i++) {
                    if (DEBUG)
                        Slog.d(TAG, "Retrieving state: " + files.get(i));
                    try {
                        AtomicFile file = new AtomicFile(new File(files.get(i)));
                        ProcessStats processStats = new ProcessStats(false);
                        readLocked(processStats, file);
                        if (processStats.mReadError != null) {
                            if (isCheckin || isCompact)
                                pw.print("err,");
                            pw.print("Failure reading ");
                            pw.print(files.get(i));
                            pw.print("; ");
                            pw.println(processStats.mReadError);
                            if (DEBUG)
                                Slog.d(TAG, "Deleting state: " + files.get(i));
                            (new File(files.get(i))).delete();
                            continue;
                        }
                        String fileStr = file.getBaseFile().getPath();
                        boolean checkedIn = fileStr.endsWith(STATE_FILE_CHECKIN_SUFFIX);
                        if (isCheckin || isCompact) {
                            // Don't really need to lock because we uniquely own this object.
                            processStats.dumpCheckinLocked(pw, reqPackage);
                        } else {
                            if (sepNeeded) {
                                pw.println();
                            } else {
                                sepNeeded = true;
                            }
                            pw.print("COMMITTED STATS FROM ");
                            pw.print(processStats.mTimePeriodStartClockStr);
                            if (checkedIn)
                                pw.print(" (checked in)");
                            pw.println(":");
                            // much crud.
                            if (dumpFullDetails) {
                                processStats.dumpLocked(pw, reqPackage, now, false, false, activeOnly);
                            } else {
                                processStats.dumpSummaryLocked(pw, reqPackage, now, activeOnly);
                            }
                        }
                        if (isCheckin) {
                            // Rename file suffix to mark that it has checked in.
                            file.getBaseFile().renameTo(new File(fileStr + STATE_FILE_CHECKIN_SUFFIX));
                        }
                    } catch (Throwable e) {
                        pw.print("**** FAILURE DUMPING STATE: ");
                        pw.println(files.get(i));
                        e.printStackTrace(pw);
                    }
                }
            }
        } finally {
            mWriteLock.unlock();
        }
    }
    if (!isCheckin) {
        synchronized (mAm) {
            if (isCompact) {
                mProcessStats.dumpCheckinLocked(pw, reqPackage);
            } else {
                if (sepNeeded) {
                    pw.println();
                }
                pw.println("CURRENT STATS:");
                if (dumpDetails || dumpFullDetails) {
                    mProcessStats.dumpLocked(pw, reqPackage, now, !dumpFullDetails, dumpAll, activeOnly);
                    if (dumpAll) {
                        pw.print("  mFile=");
                        pw.println(mFile.getBaseFile());
                    }
                } else {
                    mProcessStats.dumpSummaryLocked(pw, reqPackage, now, activeOnly);
                }
                sepNeeded = true;
            }
        }
        if (!currentOnly) {
            if (sepNeeded) {
                pw.println();
            }
            pw.println("AGGREGATED OVER LAST 24 HOURS:");
            dumpAggregatedStats(pw, 24, now, reqPackage, isCompact, dumpDetails, dumpFullDetails, dumpAll, activeOnly);
            pw.println();
            pw.println("AGGREGATED OVER LAST 3 HOURS:");
            dumpAggregatedStats(pw, 3, now, reqPackage, isCompact, dumpDetails, dumpFullDetails, dumpAll, activeOnly);
        }
    }
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) ArrayList(java.util.ArrayList) AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 17 with ProcessStats

use of com.android.internal.app.procstats.ProcessStats in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ProcStatsData method load.

private void load() {
    try {
        ParcelFileDescriptor pfd = mProcessStats.getStatsOverTime(mDuration);
        mStats = new ProcessStats(false);
        InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
        mStats.read(is);
        try {
            is.close();
        } catch (IOException e) {
        }
        if (mStats.mReadError != null) {
            Log.w(TAG, "Failure reading process stats: " + mStats.mReadError);
        }
    } catch (RemoteException e) {
        Log.e(TAG, "RemoteException:", e);
    }
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) InputStream(java.io.InputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) RemoteException(android.os.RemoteException)

Example 18 with ProcessStats

use of com.android.internal.app.procstats.ProcessStats in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ProcStatsEntry method evaluateTargetPackage.

public void evaluateTargetPackage(PackageManager pm, ProcessStats stats, ProcessStats.ProcessDataCollection bgTotals, ProcessStats.ProcessDataCollection runTotals, Comparator<ProcStatsEntry> compare, boolean useUss) {
    mBestTargetPackage = null;
    if (mPackages.size() == 1) {
        if (DEBUG)
            Log.d(TAG, "Eval pkg of " + mName + ": single pkg " + mPackages.get(0));
        mBestTargetPackage = mPackages.get(0);
        return;
    }
    // See if there is one significant package that was running here.
    for (int ipkg = 0; ipkg < mPackages.size(); ipkg++) {
        if ("android".equals(mPackages.get(ipkg))) {
            mBestTargetPackage = mPackages.get(ipkg);
            return;
        }
    }
    // Collect information about each package running in the process.
    ArrayList<ProcStatsEntry> subProcs = new ArrayList<>();
    for (int ipkg = 0; ipkg < mPackages.size(); ipkg++) {
        SparseArray<ProcessStats.PackageState> vpkgs = stats.mPackages.get(mPackages.get(ipkg), mUid);
        for (int ivers = 0; ivers < vpkgs.size(); ivers++) {
            ProcessStats.PackageState pkgState = vpkgs.valueAt(ivers);
            if (DEBUG)
                Log.d(TAG, "Eval pkg of " + mName + ", pkg " + pkgState + ":");
            if (pkgState == null) {
                Log.w(TAG, "No package state found for " + mPackages.get(ipkg) + "/" + mUid + " in process " + mName);
                continue;
            }
            ProcessState pkgProc = pkgState.mProcesses.get(mName);
            if (pkgProc == null) {
                Log.w(TAG, "No process " + mName + " found in package state " + mPackages.get(ipkg) + "/" + mUid);
                continue;
            }
            subProcs.add(new ProcStatsEntry(pkgProc, pkgState.mPackageName, bgTotals, runTotals, useUss));
        }
    }
    if (subProcs.size() > 1) {
        Collections.sort(subProcs, compare);
        if (subProcs.get(0).mRunWeight > (subProcs.get(1).mRunWeight * 3)) {
            if (DEBUG)
                Log.d(TAG, "Eval pkg of " + mName + ": best pkg " + subProcs.get(0).mPackage + " weight " + subProcs.get(0).mRunWeight + " better than " + subProcs.get(1).mPackage + " weight " + subProcs.get(1).mRunWeight);
            mBestTargetPackage = subProcs.get(0).mPackage;
            return;
        }
        // Couldn't find one that is best by weight, let's decide on best another
        // way: the one that has the longest running service, accounts for at least
        // half of the maximum weight, and has specified an explicit app icon.
        double maxWeight = subProcs.get(0).mRunWeight;
        long bestRunTime = -1;
        boolean bestPersistent = false;
        for (int i = 0; i < subProcs.size(); i++) {
            final ProcStatsEntry subProc = subProcs.get(i);
            if (subProc.mRunWeight < (maxWeight / 2)) {
                if (DEBUG)
                    Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " weight " + subProc.mRunWeight + " too small");
                continue;
            }
            try {
                ApplicationInfo ai = pm.getApplicationInfo(subProc.mPackage, 0);
                if (ai.icon == 0) {
                    if (DEBUG)
                        Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " has no icon");
                    continue;
                }
                if ((ai.flags & ApplicationInfo.FLAG_PERSISTENT) != 0) {
                    long thisRunTime = subProc.mRunDuration;
                    if (!bestPersistent || thisRunTime > bestRunTime) {
                        if (DEBUG)
                            Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " new best pers run time " + thisRunTime);
                        bestRunTime = thisRunTime;
                        bestPersistent = true;
                    } else {
                        if (DEBUG)
                            Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " pers run time " + thisRunTime + " not as good as last " + bestRunTime);
                    }
                    continue;
                } else if (bestPersistent) {
                    if (DEBUG)
                        Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " is not persistent");
                    continue;
                }
            } catch (PackageManager.NameNotFoundException e) {
                if (DEBUG)
                    Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " failed finding app info");
                continue;
            }
            ArrayList<Service> subProcServices = null;
            for (int isp = 0, NSP = mServices.size(); isp < NSP; isp++) {
                ArrayList<Service> subServices = mServices.valueAt(isp);
                if (subServices.get(0).mPackage.equals(subProc.mPackage)) {
                    subProcServices = subServices;
                    break;
                }
            }
            long thisRunTime = 0;
            if (subProcServices != null) {
                for (int iss = 0, NSS = subProcServices.size(); iss < NSS; iss++) {
                    Service service = subProcServices.get(iss);
                    if (service.mDuration > thisRunTime) {
                        if (DEBUG)
                            Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " service " + service.mName + " run time is " + service.mDuration);
                        thisRunTime = service.mDuration;
                        break;
                    }
                }
            }
            if (thisRunTime > bestRunTime) {
                if (DEBUG)
                    Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " new best run time " + thisRunTime);
                mBestTargetPackage = subProc.mPackage;
                bestRunTime = thisRunTime;
            } else {
                if (DEBUG)
                    Log.d(TAG, "Eval pkg of " + mName + ": pkg " + subProc.mPackage + " run time " + thisRunTime + " not as good as last " + bestRunTime);
            }
        }
        // Final fallback, just pick the first subProc.
        if (TextUtils.isEmpty(mBestTargetPackage)) {
            mBestTargetPackage = subProcs.get(0).mPackage;
        }
    } else if (subProcs.size() == 1) {
        mBestTargetPackage = subProcs.get(0).mPackage;
    }
}
Also used : ProcessStats(com.android.internal.app.procstats.ProcessStats) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) ProcessState(com.android.internal.app.procstats.ProcessState) PackageManager(android.content.pm.PackageManager)

Example 19 with ProcessStats

use of com.android.internal.app.procstats.ProcessStats in project android_frameworks_base by ResurrectionRemix.

the class ProcessStatsService method dumpInner.

private void dumpInner(FileDescriptor fd, PrintWriter pw, String[] args) {
    final long now = SystemClock.uptimeMillis();
    boolean isCheckin = false;
    boolean isCompact = false;
    boolean isCsv = false;
    boolean currentOnly = false;
    boolean dumpDetails = false;
    boolean dumpFullDetails = false;
    boolean dumpAll = false;
    boolean quit = false;
    int aggregateHours = 0;
    int lastIndex = 0;
    int maxNum = 2;
    boolean activeOnly = false;
    String reqPackage = null;
    boolean csvSepScreenStats = false;
    int[] csvScreenStats = new int[] { ProcessStats.ADJ_SCREEN_OFF, ProcessStats.ADJ_SCREEN_ON };
    boolean csvSepMemStats = false;
    int[] csvMemStats = new int[] { ProcessStats.ADJ_MEM_FACTOR_CRITICAL };
    boolean csvSepProcStats = true;
    int[] csvProcStats = ProcessStats.ALL_PROC_STATES;
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if ("--checkin".equals(arg)) {
                isCheckin = true;
            } else if ("-c".equals(arg)) {
                isCompact = true;
            } else if ("--csv".equals(arg)) {
                isCsv = true;
            } else if ("--csv-screen".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --csv-screen");
                    dumpHelp(pw);
                    return;
                }
                boolean[] sep = new boolean[1];
                String[] error = new String[1];
                csvScreenStats = parseStateList(DumpUtils.ADJ_SCREEN_NAMES_CSV, ProcessStats.ADJ_SCREEN_MOD, args[i], sep, error);
                if (csvScreenStats == null) {
                    pw.println("Error in \"" + args[i] + "\": " + error[0]);
                    dumpHelp(pw);
                    return;
                }
                csvSepScreenStats = sep[0];
            } else if ("--csv-mem".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --csv-mem");
                    dumpHelp(pw);
                    return;
                }
                boolean[] sep = new boolean[1];
                String[] error = new String[1];
                csvMemStats = parseStateList(DumpUtils.ADJ_MEM_NAMES_CSV, 1, args[i], sep, error);
                if (csvMemStats == null) {
                    pw.println("Error in \"" + args[i] + "\": " + error[0]);
                    dumpHelp(pw);
                    return;
                }
                csvSepMemStats = sep[0];
            } else if ("--csv-proc".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --csv-proc");
                    dumpHelp(pw);
                    return;
                }
                boolean[] sep = new boolean[1];
                String[] error = new String[1];
                csvProcStats = parseStateList(DumpUtils.STATE_NAMES_CSV, 1, args[i], sep, error);
                if (csvProcStats == null) {
                    pw.println("Error in \"" + args[i] + "\": " + error[0]);
                    dumpHelp(pw);
                    return;
                }
                csvSepProcStats = sep[0];
            } else if ("--details".equals(arg)) {
                dumpDetails = true;
            } else if ("--full-details".equals(arg)) {
                dumpFullDetails = true;
            } else if ("--hours".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --hours");
                    dumpHelp(pw);
                    return;
                }
                try {
                    aggregateHours = Integer.parseInt(args[i]);
                } catch (NumberFormatException e) {
                    pw.println("Error: --hours argument not an int -- " + args[i]);
                    dumpHelp(pw);
                    return;
                }
            } else if ("--last".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --last");
                    dumpHelp(pw);
                    return;
                }
                try {
                    lastIndex = Integer.parseInt(args[i]);
                } catch (NumberFormatException e) {
                    pw.println("Error: --last argument not an int -- " + args[i]);
                    dumpHelp(pw);
                    return;
                }
            } else if ("--max".equals(arg)) {
                i++;
                if (i >= args.length) {
                    pw.println("Error: argument required for --max");
                    dumpHelp(pw);
                    return;
                }
                try {
                    maxNum = Integer.parseInt(args[i]);
                } catch (NumberFormatException e) {
                    pw.println("Error: --max argument not an int -- " + args[i]);
                    dumpHelp(pw);
                    return;
                }
            } else if ("--active".equals(arg)) {
                activeOnly = true;
                currentOnly = true;
            } else if ("--current".equals(arg)) {
                currentOnly = true;
            } else if ("--commit".equals(arg)) {
                synchronized (mAm) {
                    mProcessStats.mFlags |= ProcessStats.FLAG_COMPLETE;
                    writeStateLocked(true, true);
                    pw.println("Process stats committed.");
                    quit = true;
                }
            } else if ("--reset".equals(arg)) {
                synchronized (mAm) {
                    mProcessStats.resetSafely();
                    pw.println("Process stats reset.");
                    quit = true;
                }
            } else if ("--clear".equals(arg)) {
                synchronized (mAm) {
                    mProcessStats.resetSafely();
                    ArrayList<String> files = getCommittedFiles(0, true, true);
                    if (files != null) {
                        for (int fi = 0; fi < files.size(); fi++) {
                            (new File(files.get(fi))).delete();
                        }
                    }
                    pw.println("All process stats cleared.");
                    quit = true;
                }
            } else if ("--write".equals(arg)) {
                synchronized (mAm) {
                    writeStateSyncLocked();
                    pw.println("Process stats written.");
                    quit = true;
                }
            } else if ("--read".equals(arg)) {
                synchronized (mAm) {
                    readLocked(mProcessStats, mFile);
                    pw.println("Process stats read.");
                    quit = true;
                }
            } else if ("--start-testing".equals(arg)) {
                synchronized (mAm) {
                    mAm.setTestPssMode(true);
                    pw.println("Started high frequency sampling.");
                    quit = true;
                }
            } else if ("--stop-testing".equals(arg)) {
                synchronized (mAm) {
                    mAm.setTestPssMode(false);
                    pw.println("Stopped high frequency sampling.");
                    quit = true;
                }
            } else if ("-h".equals(arg)) {
                dumpHelp(pw);
                return;
            } else if ("-a".equals(arg)) {
                dumpDetails = true;
                dumpAll = true;
            } else if (arg.length() > 0 && arg.charAt(0) == '-') {
                pw.println("Unknown option: " + arg);
                dumpHelp(pw);
                return;
            } else {
                // Not an option, last argument must be a package name.
                reqPackage = arg;
                // Include all details, since we know we are only going to
                // be dumping a smaller set of data.  In fact only the details
                // contain per-package data, so this is needed to be able
                // to dump anything at all when filtering by package.
                dumpDetails = true;
            }
        }
    }
    if (quit) {
        return;
    }
    if (isCsv) {
        pw.print("Processes running summed over");
        if (!csvSepScreenStats) {
            for (int i = 0; i < csvScreenStats.length; i++) {
                pw.print(" ");
                DumpUtils.printScreenLabelCsv(pw, csvScreenStats[i]);
            }
        }
        if (!csvSepMemStats) {
            for (int i = 0; i < csvMemStats.length; i++) {
                pw.print(" ");
                DumpUtils.printMemLabelCsv(pw, csvMemStats[i]);
            }
        }
        if (!csvSepProcStats) {
            for (int i = 0; i < csvProcStats.length; i++) {
                pw.print(" ");
                pw.print(DumpUtils.STATE_NAMES_CSV[csvProcStats[i]]);
            }
        }
        pw.println();
        synchronized (mAm) {
            dumpFilteredProcessesCsvLocked(pw, null, csvSepScreenStats, csvScreenStats, csvSepMemStats, csvMemStats, csvSepProcStats, csvProcStats, now, reqPackage);
        /*
                dumpFilteredProcessesCsvLocked(pw, "Processes running while critical mem:",
                        false, new int[] {ADJ_SCREEN_OFF, ADJ_SCREEN_ON},
                        true, new int[] {ADJ_MEM_FACTOR_CRITICAL},
                        true, new int[] {STATE_PERSISTENT, STATE_TOP, STATE_FOREGROUND, STATE_VISIBLE,
                                STATE_PERCEPTIBLE, STATE_BACKUP, STATE_SERVICE, STATE_HOME,
                                STATE_PREVIOUS, STATE_CACHED},
                        now, reqPackage);
                dumpFilteredProcessesCsvLocked(pw, "Processes running over all mem:",
                        false, new int[] {ADJ_SCREEN_OFF, ADJ_SCREEN_ON},
                        false, new int[] {ADJ_MEM_FACTOR_CRITICAL, ADJ_MEM_FACTOR_LOW,
                                ADJ_MEM_FACTOR_MODERATE, ADJ_MEM_FACTOR_MODERATE},
                        true, new int[] {STATE_PERSISTENT, STATE_TOP, STATE_FOREGROUND, STATE_VISIBLE,
                                STATE_PERCEPTIBLE, STATE_BACKUP, STATE_SERVICE, STATE_HOME,
                                STATE_PREVIOUS, STATE_CACHED},
                        now, reqPackage);
                */
        }
        return;
    } else if (aggregateHours != 0) {
        pw.print("AGGREGATED OVER LAST ");
        pw.print(aggregateHours);
        pw.println(" HOURS:");
        dumpAggregatedStats(pw, aggregateHours, now, reqPackage, isCompact, dumpDetails, dumpFullDetails, dumpAll, activeOnly);
        return;
    } else if (lastIndex > 0) {
        pw.print("LAST STATS AT INDEX ");
        pw.print(lastIndex);
        pw.println(":");
        ArrayList<String> files = getCommittedFiles(0, false, true);
        if (lastIndex >= files.size()) {
            pw.print("Only have ");
            pw.print(files.size());
            pw.println(" data sets");
            return;
        }
        AtomicFile file = new AtomicFile(new File(files.get(lastIndex)));
        ProcessStats processStats = new ProcessStats(false);
        readLocked(processStats, file);
        if (processStats.mReadError != null) {
            if (isCheckin || isCompact)
                pw.print("err,");
            pw.print("Failure reading ");
            pw.print(files.get(lastIndex));
            pw.print("; ");
            pw.println(processStats.mReadError);
            return;
        }
        String fileStr = file.getBaseFile().getPath();
        boolean checkedIn = fileStr.endsWith(STATE_FILE_CHECKIN_SUFFIX);
        if (isCheckin || isCompact) {
            // Don't really need to lock because we uniquely own this object.
            processStats.dumpCheckinLocked(pw, reqPackage);
        } else {
            pw.print("COMMITTED STATS FROM ");
            pw.print(processStats.mTimePeriodStartClockStr);
            if (checkedIn)
                pw.print(" (checked in)");
            pw.println(":");
            if (dumpDetails || dumpFullDetails) {
                processStats.dumpLocked(pw, reqPackage, now, !dumpFullDetails, dumpAll, activeOnly);
                if (dumpAll) {
                    pw.print("  mFile=");
                    pw.println(mFile.getBaseFile());
                }
            } else {
                processStats.dumpSummaryLocked(pw, reqPackage, now, activeOnly);
            }
        }
        return;
    }
    boolean sepNeeded = false;
    if (dumpAll || isCheckin) {
        mWriteLock.lock();
        try {
            ArrayList<String> files = getCommittedFiles(0, false, !isCheckin);
            if (files != null) {
                int start = isCheckin ? 0 : (files.size() - maxNum);
                if (start < 0) {
                    start = 0;
                }
                for (int i = start; i < files.size(); i++) {
                    if (DEBUG)
                        Slog.d(TAG, "Retrieving state: " + files.get(i));
                    try {
                        AtomicFile file = new AtomicFile(new File(files.get(i)));
                        ProcessStats processStats = new ProcessStats(false);
                        readLocked(processStats, file);
                        if (processStats.mReadError != null) {
                            if (isCheckin || isCompact)
                                pw.print("err,");
                            pw.print("Failure reading ");
                            pw.print(files.get(i));
                            pw.print("; ");
                            pw.println(processStats.mReadError);
                            if (DEBUG)
                                Slog.d(TAG, "Deleting state: " + files.get(i));
                            (new File(files.get(i))).delete();
                            continue;
                        }
                        String fileStr = file.getBaseFile().getPath();
                        boolean checkedIn = fileStr.endsWith(STATE_FILE_CHECKIN_SUFFIX);
                        if (isCheckin || isCompact) {
                            // Don't really need to lock because we uniquely own this object.
                            processStats.dumpCheckinLocked(pw, reqPackage);
                        } else {
                            if (sepNeeded) {
                                pw.println();
                            } else {
                                sepNeeded = true;
                            }
                            pw.print("COMMITTED STATS FROM ");
                            pw.print(processStats.mTimePeriodStartClockStr);
                            if (checkedIn)
                                pw.print(" (checked in)");
                            pw.println(":");
                            // much crud.
                            if (dumpFullDetails) {
                                processStats.dumpLocked(pw, reqPackage, now, false, false, activeOnly);
                            } else {
                                processStats.dumpSummaryLocked(pw, reqPackage, now, activeOnly);
                            }
                        }
                        if (isCheckin) {
                            // Rename file suffix to mark that it has checked in.
                            file.getBaseFile().renameTo(new File(fileStr + STATE_FILE_CHECKIN_SUFFIX));
                        }
                    } catch (Throwable e) {
                        pw.print("**** FAILURE DUMPING STATE: ");
                        pw.println(files.get(i));
                        e.printStackTrace(pw);
                    }
                }
            }
        } finally {
            mWriteLock.unlock();
        }
    }
    if (!isCheckin) {
        synchronized (mAm) {
            if (isCompact) {
                mProcessStats.dumpCheckinLocked(pw, reqPackage);
            } else {
                if (sepNeeded) {
                    pw.println();
                }
                pw.println("CURRENT STATS:");
                if (dumpDetails || dumpFullDetails) {
                    mProcessStats.dumpLocked(pw, reqPackage, now, !dumpFullDetails, dumpAll, activeOnly);
                    if (dumpAll) {
                        pw.print("  mFile=");
                        pw.println(mFile.getBaseFile());
                    }
                } else {
                    mProcessStats.dumpSummaryLocked(pw, reqPackage, now, activeOnly);
                }
                sepNeeded = true;
            }
        }
        if (!currentOnly) {
            if (sepNeeded) {
                pw.println();
            }
            pw.println("AGGREGATED OVER LAST 24 HOURS:");
            dumpAggregatedStats(pw, 24, now, reqPackage, isCompact, dumpDetails, dumpFullDetails, dumpAll, activeOnly);
            pw.println();
            pw.println("AGGREGATED OVER LAST 3 HOURS:");
            dumpAggregatedStats(pw, 3, now, reqPackage, isCompact, dumpDetails, dumpFullDetails, dumpAll, activeOnly);
        }
    }
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) ArrayList(java.util.ArrayList) AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 20 with ProcessStats

use of com.android.internal.app.procstats.ProcessStats in project android_frameworks_base by ResurrectionRemix.

the class ProcessStatsService method dumpAggregatedStats.

private void dumpAggregatedStats(PrintWriter pw, long aggregateHours, long now, String reqPackage, boolean isCompact, boolean dumpDetails, boolean dumpFullDetails, boolean dumpAll, boolean activeOnly) {
    ParcelFileDescriptor pfd = getStatsOverTime(aggregateHours * 60 * 60 * 1000 - (ProcessStats.COMMIT_PERIOD / 2));
    if (pfd == null) {
        pw.println("Unable to build stats!");
        return;
    }
    ProcessStats stats = new ProcessStats(false);
    InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
    stats.read(stream);
    if (stats.mReadError != null) {
        pw.print("Failure reading: ");
        pw.println(stats.mReadError);
        return;
    }
    if (isCompact) {
        stats.dumpCheckinLocked(pw, reqPackage);
    } else {
        if (dumpDetails || dumpFullDetails) {
            stats.dumpLocked(pw, reqPackage, now, !dumpFullDetails, dumpAll, activeOnly);
        } else {
            stats.dumpSummaryLocked(pw, reqPackage, now, activeOnly);
        }
    }
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Aggregations

ProcessStats (com.android.internal.app.procstats.ProcessStats)22 IProcessStats (com.android.internal.app.procstats.IProcessStats)21 ParcelFileDescriptor (android.os.ParcelFileDescriptor)11 AtomicFile (android.util.AtomicFile)10 File (java.io.File)10 FileInputStream (java.io.FileInputStream)10 ProcessState (com.android.internal.app.procstats.ProcessState)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 Parcel (android.os.Parcel)5 SparseArray (android.util.SparseArray)5 BackgroundThread (com.android.internal.os.BackgroundThread)5 FileOutputStream (java.io.FileOutputStream)5 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1 RemoteException (android.os.RemoteException)1