Search in sources :

Example 16 with FileObserver

use of android.os.FileObserver in project Conversations by siacs.

the class ConversationsFileObserver method startWatching.

public synchronized void startWatching() {
    Stack<String> stack = new Stack<>();
    stack.push(path);
    while (!stack.empty()) {
        String parent = stack.pop();
        mObservers.add(new SingleFileObserver(parent, FileObserver.DELETE));
        final File path = new File(parent);
        final File[] files = path.listFiles();
        if (files == null) {
            continue;
        }
        for (File file : files) {
            if (file.isDirectory() && !file.getName().equals(".") && !file.getName().equals("..")) {
                final String currentPath = file.getAbsolutePath();
                if (depth(file) <= 8 && !stack.contains(currentPath) && !observing(currentPath)) {
                    stack.push(currentPath);
                }
            }
        }
    }
    for (FileObserver observer : mObservers) {
        observer.startWatching();
    }
}
Also used : File(java.io.File) Stack(java.util.Stack) FileObserver(android.os.FileObserver)

Example 17 with FileObserver

use of android.os.FileObserver in project android_frameworks_base by AOSPA.

the class SamplingProfilerService method startWorking.

private void startWorking(Context context) {
    if (LOCAL_LOGV)
        Slog.v(TAG, "starting SamplingProfilerService!");
    final DropBoxManager dropbox = (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE);
    // before FileObserver is ready, there could have already been some snapshots
    // in the directory, we don't want to miss them
    File[] snapshotFiles = new File(SNAPSHOT_DIR).listFiles();
    for (int i = 0; snapshotFiles != null && i < snapshotFiles.length; i++) {
        handleSnapshotFile(snapshotFiles[i], dropbox);
    }
    // detect new snapshot and put it in dropbox
    // delete it afterwards no matter what happened before
    // Note: needs listening at event ATTRIB rather than CLOSE_WRITE, because we set the
    // readability of snapshot files after writing them!
    snapshotObserver = new FileObserver(SNAPSHOT_DIR, FileObserver.ATTRIB) {

        @Override
        public void onEvent(int event, String path) {
            handleSnapshotFile(new File(SNAPSHOT_DIR, path), dropbox);
        }
    };
    snapshotObserver.startWatching();
    if (LOCAL_LOGV)
        Slog.v(TAG, "SamplingProfilerService activated");
}
Also used : DropBoxManager(android.os.DropBoxManager) File(java.io.File) FileObserver(android.os.FileObserver)

Example 18 with FileObserver

use of android.os.FileObserver in project android_frameworks_base by DirtyUnicorns.

the class BootReceiver method logBootEvents.

private void logBootEvents(Context ctx) throws IOException {
    final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
    final String headers = getBootHeadersToLogAndUpdate();
    final String bootReason = SystemProperties.get("ro.boot.bootreason", null);
    String recovery = RecoverySystem.handleAftermath(ctx);
    if (recovery != null && db != null) {
        db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
    }
    String lastKmsgFooter = "";
    if (bootReason != null) {
        lastKmsgFooter = new StringBuilder(512).append("\n").append("Boot info:\n").append("Last boot reason: ").append(bootReason).append("\n").toString();
    }
    HashMap<String, Long> timestamps = readTimestamps();
    if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
        if (StorageManager.inCryptKeeperBounce()) {
        // Encrypted, first boot to get PIN/pattern/password so data is tmpfs
        // Don't set ro.runtime.firstboot so that we will do this again
        // when data is properly mounted
        } else {
            String now = Long.toString(System.currentTimeMillis());
            SystemProperties.set("ro.runtime.firstboot", now);
        }
        if (db != null)
            db.addText("SYSTEM_BOOT", headers);
        // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
        addFileWithFootersToDropBox(db, timestamps, headers, lastKmsgFooter, "/proc/last_kmsg", -LOG_SIZE, "SYSTEM_LAST_KMSG");
        addFileWithFootersToDropBox(db, timestamps, headers, lastKmsgFooter, "/sys/fs/pstore/console-ramoops", -LOG_SIZE, "SYSTEM_LAST_KMSG");
        addFileToDropBox(db, timestamps, headers, "/cache/recovery/log", -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
        addFileToDropBox(db, timestamps, headers, "/cache/recovery/last_kmsg", -LOG_SIZE, "SYSTEM_RECOVERY_KMSG");
        addAuditErrorsToDropBox(db, timestamps, headers, -LOG_SIZE, "SYSTEM_AUDIT");
        addFsckErrorsToDropBox(db, timestamps, headers, -LOG_SIZE, "SYSTEM_FSCK");
    } else {
        if (db != null)
            db.addText("SYSTEM_RESTART", headers);
    }
    // Scan existing tombstones (in case any new ones appeared)
    File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
    for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
        if (tombstoneFiles[i].isFile()) {
            addFileToDropBox(db, timestamps, headers, tombstoneFiles[i].getPath(), LOG_SIZE, "SYSTEM_TOMBSTONE");
        }
    }
    writeTimestamps(timestamps);
    // Start watching for new tombstone files; will record them as they occur.
    // This gets registered with the singleton file observer thread.
    sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {

        @Override
        public void onEvent(int event, String path) {
            HashMap<String, Long> timestamps = readTimestamps();
            try {
                File file = new File(TOMBSTONE_DIR, path);
                if (file.isFile()) {
                    addFileToDropBox(db, timestamps, headers, file.getPath(), LOG_SIZE, "SYSTEM_TOMBSTONE");
                }
            } catch (IOException e) {
                Slog.e(TAG, "Can't log tombstone", e);
            }
            writeTimestamps(timestamps);
        }
    };
    sTombstoneObserver.startWatching();
}
Also used : DropBoxManager(android.os.DropBoxManager) HashMap(java.util.HashMap) IOException(java.io.IOException) AtomicFile(android.util.AtomicFile) File(java.io.File) FileObserver(android.os.FileObserver)

Example 19 with FileObserver

use of android.os.FileObserver in project android_frameworks_base by DirtyUnicorns.

the class ActivityManagerService method dumpStackTraces.

private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, ProcessCpuTracker processCpuTracker, SparseArray<Boolean> lastPids, String[] nativeProcs) {
    // Use a FileObserver to detect when traces finish writing.
    // The order of traces is considered important to maintain for legibility.
    FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {

        @Override
        public synchronized void onEvent(int event, String path) {
            notify();
        }
    };
    try {
        observer.startWatching();
        // First collect all of the stacks of the most important pids.
        if (firstPids != null) {
            try {
                int num = firstPids.size();
                for (int i = 0; i < num; i++) {
                    synchronized (observer) {
                        if (DEBUG_ANR)
                            Slog.d(TAG, "Collecting stacks for pid " + firstPids.get(i));
                        final long sime = SystemClock.elapsedRealtime();
                        Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
                        // Wait for write-close, give up after 1 sec
                        observer.wait(1000);
                        if (DEBUG_ANR)
                            Slog.d(TAG, "Done with pid " + firstPids.get(i) + " in " + (SystemClock.elapsedRealtime() - sime) + "ms");
                    }
                }
            } catch (InterruptedException e) {
                Slog.wtf(TAG, e);
            }
        }
        // Next collect the stacks of the native pids
        if (nativeProcs != null) {
            int[] pids = Process.getPidsForCommands(nativeProcs);
            if (pids != null) {
                for (int pid : pids) {
                    if (DEBUG_ANR)
                        Slog.d(TAG, "Collecting stacks for native pid " + pid);
                    final long sime = SystemClock.elapsedRealtime();
                    Debug.dumpNativeBacktraceToFile(pid, tracesPath);
                    if (DEBUG_ANR)
                        Slog.d(TAG, "Done with native pid " + pid + " in " + (SystemClock.elapsedRealtime() - sime) + "ms");
                }
            }
        }
        // Lastly, measure CPU usage.
        if (processCpuTracker != null) {
            processCpuTracker.init();
            System.gc();
            processCpuTracker.update();
            try {
                synchronized (processCpuTracker) {
                    // measure over 1/2 second.
                    processCpuTracker.wait(500);
                }
            } catch (InterruptedException e) {
            }
            processCpuTracker.update();
            // We'll take the stack crawls of just the top apps using CPU.
            final int N = processCpuTracker.countWorkingStats();
            int numProcs = 0;
            for (int i = 0; i < N && numProcs < 5; i++) {
                ProcessCpuTracker.Stats stats = processCpuTracker.getWorkingStats(i);
                if (lastPids.indexOfKey(stats.pid) >= 0) {
                    numProcs++;
                    try {
                        synchronized (observer) {
                            if (DEBUG_ANR)
                                Slog.d(TAG, "Collecting stacks for extra pid " + stats.pid);
                            final long stime = SystemClock.elapsedRealtime();
                            Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
                            // Wait for write-close, give up after 1 sec
                            observer.wait(1000);
                            if (DEBUG_ANR)
                                Slog.d(TAG, "Done with extra pid " + stats.pid + " in " + (SystemClock.elapsedRealtime() - stime) + "ms");
                        }
                    } catch (InterruptedException e) {
                        Slog.wtf(TAG, e);
                    }
                } else if (DEBUG_ANR) {
                    Slog.d(TAG, "Skipping next CPU consuming process, not a java proc: " + stats.pid);
                }
            }
        }
    } finally {
        observer.stopWatching();
    }
}
Also used : ProcessCpuTracker(com.android.internal.os.ProcessCpuTracker) Point(android.graphics.Point) FileObserver(android.os.FileObserver)

Example 20 with FileObserver

use of android.os.FileObserver in project android_frameworks_base by ResurrectionRemix.

the class BootReceiver method logBootEvents.

private void logBootEvents(Context ctx) throws IOException {
    final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
    final String headers = getBootHeadersToLogAndUpdate();
    final String bootReason = SystemProperties.get("ro.boot.bootreason", null);
    String recovery = RecoverySystem.handleAftermath(ctx);
    if (recovery != null && db != null) {
        db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
    }
    String lastKmsgFooter = "";
    if (bootReason != null) {
        lastKmsgFooter = new StringBuilder(512).append("\n").append("Boot info:\n").append("Last boot reason: ").append(bootReason).append("\n").toString();
    }
    HashMap<String, Long> timestamps = readTimestamps();
    if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
        if (StorageManager.inCryptKeeperBounce()) {
        // Encrypted, first boot to get PIN/pattern/password so data is tmpfs
        // Don't set ro.runtime.firstboot so that we will do this again
        // when data is properly mounted
        } else {
            String now = Long.toString(System.currentTimeMillis());
            SystemProperties.set("ro.runtime.firstboot", now);
        }
        if (db != null)
            db.addText("SYSTEM_BOOT", headers);
        // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
        addFileWithFootersToDropBox(db, timestamps, headers, lastKmsgFooter, "/proc/last_kmsg", -LOG_SIZE, "SYSTEM_LAST_KMSG");
        addFileWithFootersToDropBox(db, timestamps, headers, lastKmsgFooter, "/sys/fs/pstore/console-ramoops", -LOG_SIZE, "SYSTEM_LAST_KMSG");
        addFileToDropBox(db, timestamps, headers, "/cache/recovery/log", -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
        addFileToDropBox(db, timestamps, headers, "/cache/recovery/last_kmsg", -LOG_SIZE, "SYSTEM_RECOVERY_KMSG");
        addAuditErrorsToDropBox(db, timestamps, headers, -LOG_SIZE, "SYSTEM_AUDIT");
        addFsckErrorsToDropBox(db, timestamps, headers, -LOG_SIZE, "SYSTEM_FSCK");
    } else {
        if (db != null)
            db.addText("SYSTEM_RESTART", headers);
    }
    // Scan existing tombstones (in case any new ones appeared)
    File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
    for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
        if (tombstoneFiles[i].isFile()) {
            addFileToDropBox(db, timestamps, headers, tombstoneFiles[i].getPath(), LOG_SIZE, "SYSTEM_TOMBSTONE");
        }
    }
    writeTimestamps(timestamps);
    // Start watching for new tombstone files; will record them as they occur.
    // This gets registered with the singleton file observer thread.
    sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {

        @Override
        public void onEvent(int event, String path) {
            HashMap<String, Long> timestamps = readTimestamps();
            try {
                File file = new File(TOMBSTONE_DIR, path);
                if (file.isFile()) {
                    addFileToDropBox(db, timestamps, headers, file.getPath(), LOG_SIZE, "SYSTEM_TOMBSTONE");
                }
            } catch (IOException e) {
                Slog.e(TAG, "Can't log tombstone", e);
            }
            writeTimestamps(timestamps);
        }
    };
    sTombstoneObserver.startWatching();
}
Also used : DropBoxManager(android.os.DropBoxManager) HashMap(java.util.HashMap) IOException(java.io.IOException) AtomicFile(android.util.AtomicFile) File(java.io.File) FileObserver(android.os.FileObserver)

Aggregations

FileObserver (android.os.FileObserver)22 File (java.io.File)15 DropBoxManager (android.os.DropBoxManager)12 IOException (java.io.IOException)7 AtomicFile (android.util.AtomicFile)5 HashMap (java.util.HashMap)5 ProcessStats (com.android.internal.os.ProcessStats)3 Point (android.graphics.Point)2 ProcessCpuTracker (com.android.internal.os.ProcessCpuTracker)2 SharedPreferences (android.content.SharedPreferences)1 Stack (java.util.Stack)1