Fields Summary |
---|
private static final String | LOG_TAG |
private static final int | SAMPLE_COUNTNumber of samples to take. |
private static final int | INTERVALTime to wait in ms between samples. |
private static final int | MAX_FILESTime to wait in ms between samples. |
private final byte[] | buffer |
private boolean | runningIs the monitor currently running? |
private static final File | PROC |
private static final File | BASE |
private static final File[] | PATHS |
private static DeviceMonitor | instance |
Methods Summary |
---|
private static void | closeQuietly(java.io.Closeable closeable)Closes the given resource. Logs exceptions.
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
Log.w(LOG_TAG, e);
}
|
private void | dump()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 void | dump(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 boolean | isProcessDirectory(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 void | monitor()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 void | pause()Pauses momentarily before we start the next dump.
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) { /* ignore */ }
|
private void | purge()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 void | start()Starts monitoring if it hasn't started already.
instance.startMonitoring();
|
private synchronized void | startMonitoring()Instructs the monitoring to start if it hasn't already.
if (!running) {
running = true;
notifyAll();
}
|
private synchronized void | stop()Stops dumping.
running = false;
|
private synchronized void | waitForStart()Waits until someone starts us.
while (!running) {
try {
wait();
} catch (InterruptedException e) { /* ignore */ }
}
|
private static void | writeHeader(java.io.File file, java.io.OutputStream out)Writes a header for the given file.
String header = "*** " + file.toString() + "\n";
out.write(header.getBytes());
|