Search in sources :

Example 6 with ProcessStats

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

the class ProcessStatsService method getStatsOverTime.

public ParcelFileDescriptor getStatsOverTime(long minTime) {
    mAm.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS, null);
    Parcel current = Parcel.obtain();
    long curTime;
    synchronized (mAm) {
        long now = SystemClock.uptimeMillis();
        mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
        mProcessStats.mTimePeriodEndUptime = now;
        mProcessStats.writeToParcel(current, now, 0);
        curTime = mProcessStats.mTimePeriodEndRealtime - mProcessStats.mTimePeriodStartRealtime;
    }
    mWriteLock.lock();
    try {
        if (curTime < minTime) {
            // Need to add in older stats to reach desired time.
            ArrayList<String> files = getCommittedFiles(0, false, true);
            if (files != null && files.size() > 0) {
                current.setDataPosition(0);
                ProcessStats stats = ProcessStats.CREATOR.createFromParcel(current);
                current.recycle();
                int i = files.size() - 1;
                while (i >= 0 && (stats.mTimePeriodEndRealtime - stats.mTimePeriodStartRealtime) < minTime) {
                    AtomicFile file = new AtomicFile(new File(files.get(i)));
                    i--;
                    ProcessStats moreStats = new ProcessStats(false);
                    readLocked(moreStats, file);
                    if (moreStats.mReadError == null) {
                        stats.add(moreStats);
                        StringBuilder sb = new StringBuilder();
                        sb.append("Added stats: ");
                        sb.append(moreStats.mTimePeriodStartClockStr);
                        sb.append(", over ");
                        TimeUtils.formatDuration(moreStats.mTimePeriodEndRealtime - moreStats.mTimePeriodStartRealtime, sb);
                        Slog.i(TAG, sb.toString());
                    } else {
                        Slog.w(TAG, "Failure reading " + files.get(i + 1) + "; " + moreStats.mReadError);
                        continue;
                    }
                }
                current = Parcel.obtain();
                stats.writeToParcel(current, 0);
            }
        }
        final byte[] outData = current.marshall();
        current.recycle();
        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        Thread thr = new Thread("ProcessStats pipe output") {

            public void run() {
                FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(fds[1]);
                try {
                    fout.write(outData);
                    fout.close();
                } catch (IOException e) {
                    Slog.w(TAG, "Failure writing pipe", e);
                }
            }
        };
        thr.start();
        return fds[0];
    } catch (IOException e) {
        Slog.w(TAG, "Failed building output pipe", e);
    } finally {
        mWriteLock.unlock();
    }
    return null;
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) Parcel(android.os.Parcel) IOException(java.io.IOException) BackgroundThread(com.android.internal.os.BackgroundThread) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 7 with ProcessStats

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

the class ProcessStatsService method readLocked.

boolean readLocked(ProcessStats stats, AtomicFile file) {
    try {
        FileInputStream stream = file.openRead();
        stats.read(stream);
        stream.close();
        if (stats.mReadError != null) {
            Slog.w(TAG, "Ignoring existing stats; " + stats.mReadError);
            if (DEBUG) {
                ArrayMap<String, SparseArray<ProcessState>> procMap = stats.mProcesses.getMap();
                final int NPROC = procMap.size();
                for (int ip = 0; ip < NPROC; ip++) {
                    Slog.w(TAG, "Process: " + procMap.keyAt(ip));
                    SparseArray<ProcessState> uids = procMap.valueAt(ip);
                    final int NUID = uids.size();
                    for (int iu = 0; iu < NUID; iu++) {
                        Slog.w(TAG, "  Uid " + uids.keyAt(iu) + ": " + uids.valueAt(iu));
                    }
                }
                ArrayMap<String, SparseArray<SparseArray<ProcessStats.PackageState>>> pkgMap = stats.mPackages.getMap();
                final int NPKG = pkgMap.size();
                for (int ip = 0; ip < NPKG; ip++) {
                    Slog.w(TAG, "Package: " + pkgMap.keyAt(ip));
                    SparseArray<SparseArray<ProcessStats.PackageState>> uids = pkgMap.valueAt(ip);
                    final int NUID = uids.size();
                    for (int iu = 0; iu < NUID; iu++) {
                        Slog.w(TAG, "  Uid: " + uids.keyAt(iu));
                        SparseArray<ProcessStats.PackageState> vers = uids.valueAt(iu);
                        final int NVERS = vers.size();
                        for (int iv = 0; iv < NVERS; iv++) {
                            Slog.w(TAG, "    Vers: " + vers.keyAt(iv));
                            ProcessStats.PackageState pkgState = vers.valueAt(iv);
                            final int NPROCS = pkgState.mProcesses.size();
                            for (int iproc = 0; iproc < NPROCS; iproc++) {
                                Slog.w(TAG, "      Process " + pkgState.mProcesses.keyAt(iproc) + ": " + pkgState.mProcesses.valueAt(iproc));
                            }
                            final int NSRVS = pkgState.mServices.size();
                            for (int isvc = 0; isvc < NSRVS; isvc++) {
                                Slog.w(TAG, "      Service " + pkgState.mServices.keyAt(isvc) + ": " + pkgState.mServices.valueAt(isvc));
                            }
                        }
                    }
                }
            }
            return false;
        }
    } catch (Throwable e) {
        stats.mReadError = "caught exception: " + e;
        Slog.e(TAG, "Error reading process statistics", e);
        return false;
    }
    return true;
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) FileInputStream(java.io.FileInputStream) ProcessState(com.android.internal.app.procstats.ProcessState) SparseArray(android.util.SparseArray)

Example 8 with ProcessStats

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

the class ProcessStatsService method getStatsOverTime.

public ParcelFileDescriptor getStatsOverTime(long minTime) {
    mAm.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS, null);
    Parcel current = Parcel.obtain();
    long curTime;
    synchronized (mAm) {
        long now = SystemClock.uptimeMillis();
        mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
        mProcessStats.mTimePeriodEndUptime = now;
        mProcessStats.writeToParcel(current, now, 0);
        curTime = mProcessStats.mTimePeriodEndRealtime - mProcessStats.mTimePeriodStartRealtime;
    }
    mWriteLock.lock();
    try {
        if (curTime < minTime) {
            // Need to add in older stats to reach desired time.
            ArrayList<String> files = getCommittedFiles(0, false, true);
            if (files != null && files.size() > 0) {
                current.setDataPosition(0);
                ProcessStats stats = ProcessStats.CREATOR.createFromParcel(current);
                current.recycle();
                int i = files.size() - 1;
                while (i >= 0 && (stats.mTimePeriodEndRealtime - stats.mTimePeriodStartRealtime) < minTime) {
                    AtomicFile file = new AtomicFile(new File(files.get(i)));
                    i--;
                    ProcessStats moreStats = new ProcessStats(false);
                    readLocked(moreStats, file);
                    if (moreStats.mReadError == null) {
                        stats.add(moreStats);
                        StringBuilder sb = new StringBuilder();
                        sb.append("Added stats: ");
                        sb.append(moreStats.mTimePeriodStartClockStr);
                        sb.append(", over ");
                        TimeUtils.formatDuration(moreStats.mTimePeriodEndRealtime - moreStats.mTimePeriodStartRealtime, sb);
                        Slog.i(TAG, sb.toString());
                    } else {
                        Slog.w(TAG, "Failure reading " + files.get(i + 1) + "; " + moreStats.mReadError);
                        continue;
                    }
                }
                current = Parcel.obtain();
                stats.writeToParcel(current, 0);
            }
        }
        final byte[] outData = current.marshall();
        current.recycle();
        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        Thread thr = new Thread("ProcessStats pipe output") {

            public void run() {
                FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(fds[1]);
                try {
                    fout.write(outData);
                    fout.close();
                } catch (IOException e) {
                    Slog.w(TAG, "Failure writing pipe", e);
                }
            }
        };
        thr.start();
        return fds[0];
    } catch (IOException e) {
        Slog.w(TAG, "Failed building output pipe", e);
    } finally {
        mWriteLock.unlock();
    }
    return null;
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) Parcel(android.os.Parcel) IOException(java.io.IOException) BackgroundThread(com.android.internal.os.BackgroundThread) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 9 with ProcessStats

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

the class ProcessStatsService method readLocked.

boolean readLocked(ProcessStats stats, AtomicFile file) {
    try {
        FileInputStream stream = file.openRead();
        stats.read(stream);
        stream.close();
        if (stats.mReadError != null) {
            Slog.w(TAG, "Ignoring existing stats; " + stats.mReadError);
            if (DEBUG) {
                ArrayMap<String, SparseArray<ProcessState>> procMap = stats.mProcesses.getMap();
                final int NPROC = procMap.size();
                for (int ip = 0; ip < NPROC; ip++) {
                    Slog.w(TAG, "Process: " + procMap.keyAt(ip));
                    SparseArray<ProcessState> uids = procMap.valueAt(ip);
                    final int NUID = uids.size();
                    for (int iu = 0; iu < NUID; iu++) {
                        Slog.w(TAG, "  Uid " + uids.keyAt(iu) + ": " + uids.valueAt(iu));
                    }
                }
                ArrayMap<String, SparseArray<SparseArray<ProcessStats.PackageState>>> pkgMap = stats.mPackages.getMap();
                final int NPKG = pkgMap.size();
                for (int ip = 0; ip < NPKG; ip++) {
                    Slog.w(TAG, "Package: " + pkgMap.keyAt(ip));
                    SparseArray<SparseArray<ProcessStats.PackageState>> uids = pkgMap.valueAt(ip);
                    final int NUID = uids.size();
                    for (int iu = 0; iu < NUID; iu++) {
                        Slog.w(TAG, "  Uid: " + uids.keyAt(iu));
                        SparseArray<ProcessStats.PackageState> vers = uids.valueAt(iu);
                        final int NVERS = vers.size();
                        for (int iv = 0; iv < NVERS; iv++) {
                            Slog.w(TAG, "    Vers: " + vers.keyAt(iv));
                            ProcessStats.PackageState pkgState = vers.valueAt(iv);
                            final int NPROCS = pkgState.mProcesses.size();
                            for (int iproc = 0; iproc < NPROCS; iproc++) {
                                Slog.w(TAG, "      Process " + pkgState.mProcesses.keyAt(iproc) + ": " + pkgState.mProcesses.valueAt(iproc));
                            }
                            final int NSRVS = pkgState.mServices.size();
                            for (int isvc = 0; isvc < NSRVS; isvc++) {
                                Slog.w(TAG, "      Service " + pkgState.mServices.keyAt(isvc) + ": " + pkgState.mServices.valueAt(isvc));
                            }
                        }
                    }
                }
            }
            return false;
        }
    } catch (Throwable e) {
        stats.mReadError = "caught exception: " + e;
        Slog.e(TAG, "Error reading process statistics", e);
        return false;
    }
    return true;
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) FileInputStream(java.io.FileInputStream) ProcessState(com.android.internal.app.procstats.ProcessState) SparseArray(android.util.SparseArray)

Example 10 with ProcessStats

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

the class ProcessStatsService method getStatsOverTime.

public ParcelFileDescriptor getStatsOverTime(long minTime) {
    mAm.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS, null);
    Parcel current = Parcel.obtain();
    long curTime;
    synchronized (mAm) {
        long now = SystemClock.uptimeMillis();
        mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
        mProcessStats.mTimePeriodEndUptime = now;
        mProcessStats.writeToParcel(current, now, 0);
        curTime = mProcessStats.mTimePeriodEndRealtime - mProcessStats.mTimePeriodStartRealtime;
    }
    mWriteLock.lock();
    try {
        if (curTime < minTime) {
            // Need to add in older stats to reach desired time.
            ArrayList<String> files = getCommittedFiles(0, false, true);
            if (files != null && files.size() > 0) {
                current.setDataPosition(0);
                ProcessStats stats = ProcessStats.CREATOR.createFromParcel(current);
                current.recycle();
                int i = files.size() - 1;
                while (i >= 0 && (stats.mTimePeriodEndRealtime - stats.mTimePeriodStartRealtime) < minTime) {
                    AtomicFile file = new AtomicFile(new File(files.get(i)));
                    i--;
                    ProcessStats moreStats = new ProcessStats(false);
                    readLocked(moreStats, file);
                    if (moreStats.mReadError == null) {
                        stats.add(moreStats);
                        StringBuilder sb = new StringBuilder();
                        sb.append("Added stats: ");
                        sb.append(moreStats.mTimePeriodStartClockStr);
                        sb.append(", over ");
                        TimeUtils.formatDuration(moreStats.mTimePeriodEndRealtime - moreStats.mTimePeriodStartRealtime, sb);
                        Slog.i(TAG, sb.toString());
                    } else {
                        Slog.w(TAG, "Failure reading " + files.get(i + 1) + "; " + moreStats.mReadError);
                        continue;
                    }
                }
                current = Parcel.obtain();
                stats.writeToParcel(current, 0);
            }
        }
        final byte[] outData = current.marshall();
        current.recycle();
        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        Thread thr = new Thread("ProcessStats pipe output") {

            public void run() {
                FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(fds[1]);
                try {
                    fout.write(outData);
                    fout.close();
                } catch (IOException e) {
                    Slog.w(TAG, "Failure writing pipe", e);
                }
            }
        };
        thr.start();
        return fds[0];
    } catch (IOException e) {
        Slog.w(TAG, "Failed building output pipe", e);
    } finally {
        mWriteLock.unlock();
    }
    return null;
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) Parcel(android.os.Parcel) IOException(java.io.IOException) BackgroundThread(com.android.internal.os.BackgroundThread) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) AtomicFile(android.util.AtomicFile) File(java.io.File)

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