FileDocCategorySizeDatePackage
DeviceMonitor.javaAPI DocAndroid 1.5 API6034Wed May 06 22:42:00 BST 2009com.android.server.am

DeviceMonitor

public class DeviceMonitor extends Object
Monitors device resources periodically for some period of time. Useful for tracking down performance problems.

Fields Summary
private static final String
LOG_TAG
private static final int
SAMPLE_COUNT
Number of samples to take.
private static final int
INTERVAL
Time to wait in ms between samples.
private static final int
MAX_FILES
Time to wait in ms between samples.
private final byte[]
buffer
private boolean
running
Is the monitor currently running?
private static final File
PROC
private static final File
BASE
private static final File[]
PATHS
private static DeviceMonitor
instance
Constructors Summary
private DeviceMonitor()


      
        new Thread() {
            public void run() {
                monitor();
            }
        }.start();
    
Methods Summary
private static voidcloseQuietly(java.io.Closeable closeable)
Closes the given resource. Logs exceptions.

param
closeable

        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            Log.w(LOG_TAG, e);
        }
    
private voiddump()
Dumps the current device stats to a new file.

        OutputStream out = new FileOutputStream(
                new File(BASE, String.valueOf(System.currentTimeMillis())));
        try {
            // Copy /proc/*/stat
            for (File processDirectory : PROC.listFiles()) {
                if (isProcessDirectory(processDirectory)) {
                    dump(new File(processDirectory, "stat"), out);
                }
            }

            // Copy other files.
            for (File file : PATHS) {
                dump(file, out);
            }
        } finally {
            closeQuietly(out);
        }
    
private voiddump(java.io.File from, java.io.OutputStream out)
Copies from a file to an output stream.

        writeHeader(from, out);
        
        FileInputStream in = null;
        try {
            in = new FileInputStream(from);
            int count;
            while ((count = in.read(buffer)) != -1) {
                out.write(buffer, 0, count);
            }
        } finally {
            closeQuietly(in);
        }
    
private static booleanisProcessDirectory(java.io.File file)
Returns true if the given file represents a process directory.

        try {
            Integer.parseInt(file.getName());
            return file.isDirectory();
        } catch (NumberFormatException e) {
            return false;
        }
    
private voidmonitor()
Loops continuously. Pauses until someone tells us to start monitoring.

        while (true) {
            waitForStart();

            purge();

            for (int i = 0; i < SAMPLE_COUNT; i++) {
                try {
                    dump();
                } catch (IOException e) {
                    Log.w(LOG_TAG, "Dump failed.", e);
                }
                pause();
            }

            stop();
        }
    
private voidpause()
Pauses momentarily before we start the next dump.

        try {
            Thread.sleep(INTERVAL);
        } catch (InterruptedException e) { /* ignore */ }
    
private voidpurge()
Deletes old files.



            
       
        File[] files = BASE.listFiles();
        int count = files.length - MAX_FILES;
        if (count > 0) {
            Arrays.sort(files);
            for (int i = 0; i < count; i++) {
                if (!files[i].delete()) {
                    Log.w(LOG_TAG, "Couldn't delete " + files[i] + ".");
                }
            }
        }
    
static voidstart()
Starts monitoring if it hasn't started already.


                
       
        instance.startMonitoring();
    
private synchronized voidstartMonitoring()
Instructs the monitoring to start if it hasn't already.

        if (!running) {
            running = true;
            notifyAll();
        }
    
private synchronized voidstop()
Stops dumping.

        running = false;        
    
private synchronized voidwaitForStart()
Waits until someone starts us.

        while (!running) {
            try {
                wait();
            } catch (InterruptedException e) { /* ignore */ }
        }
    
private static voidwriteHeader(java.io.File file, java.io.OutputStream out)
Writes a header for the given file.

        String header = "*** " + file.toString() + "\n";
        out.write(header.getBytes());