Show more debugging info: system memory, VM memory and disk space.

This commit is contained in:
akwizgran
2014-04-10 15:46:53 +01:00
parent ab76b4a9e3
commit 0eaf46209c
7 changed files with 91 additions and 28 deletions

View File

@@ -145,6 +145,13 @@ public class BriarService extends RoboService implements EventListener {
}.start();
}
@Override
public void onLowMemory() {
super.onLowMemory();
LOG.warning("Memory is low");
// FIXME: Work out what to do about it
}
public void eventOccurred(Event e) {
if(e instanceof MessageAddedEvent) {
MessageAddedEvent m = (MessageAddedEvent) e;

View File

@@ -51,8 +51,10 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.plugins.Plugin;
import org.briarproject.api.plugins.PluginManager;
import org.briarproject.api.system.FileUtils;
import org.briarproject.util.StringUtils;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.res.Resources;
@@ -87,6 +89,8 @@ public class TestingActivity extends BriarActivity implements OnClickListener {
private ImageButton refresh = null, share = null;
private File temp = null;
@Inject private volatile FileUtils fileUtils;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
@@ -194,7 +198,8 @@ public class TestingActivity extends BriarActivity implements OnClickListener {
String brand = Build.BRAND;
if(model.startsWith(manufacturer)) deviceType = capitalize(model);
else deviceType = capitalize(manufacturer) + " " + model;
if(!StringUtils.isNullOrEmpty(brand)) deviceType += " (" + brand + ")";
if(!StringUtils.isNullOrEmpty(brand))
deviceType += " (" + capitalize(brand) + ")";
statusMap.put("Device type:", deviceType);
// Android version
@@ -205,8 +210,52 @@ public class TestingActivity extends BriarActivity implements OnClickListener {
// CPU architecture
statusMap.put("Architecture:", Build.CPU_ABI);
// System memory
Object o = getSystemService(ACTIVITY_SERVICE);
ActivityManager am = (ActivityManager) o;
ActivityManager.MemoryInfo mem = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mem);
String systemMemory = (mem.totalMem / 1024 / 1024) + " MiB total, "
+ (mem.availMem / 1024 / 1204) + " MiB free, "
+ (mem.threshold / 1024 / 1024) + " MiB threshold";
statusMap.put("System memory:", systemMemory);
// Virtual machine memory
Runtime runtime = Runtime.getRuntime();
long heap = runtime.totalMemory();
long heapFree = runtime.freeMemory();
long heapMax = runtime.maxMemory();
String vmMemory = (heap / 1024 / 1024) + " MiB allocated, "
+ (heapFree / 1024 / 1024) + " MiB free, "
+ (heapMax / 1024 / 1024) + " MiB maximum";
statusMap.put("Virtual machine memory:", vmMemory);
// Internal storage
try {
File root = Environment.getRootDirectory();
long rootTotal = fileUtils.getTotalSpace(root);
long rootFree = fileUtils.getFreeSpace(root);
String internal = (rootTotal / 1024 / 1024) + " MiB total, "
+ (rootFree / 1024 / 1024) + " MiB free";
statusMap.put("Internal storage:", internal);
} catch(IOException e) {
statusMap.put("Internal storage:", "Unknown");
}
// External storage (SD card)
try {
File sd = Environment.getExternalStorageDirectory();
long sdTotal = fileUtils.getTotalSpace(sd);
long sdFree = fileUtils.getFreeSpace(sd);
String external = (sdTotal / 1024 / 1024) + " MiB total, "
+ (sdFree / 1024 / 1024) + " MiB free";
statusMap.put("External storage:", external);
} catch(IOException e) {
statusMap.put("External storage:", "Unknown");
}
// Is mobile data available?
Object o = getSystemService(CONNECTIVITY_SERVICE);
o = getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) o;
NetworkInfo mobile = cm.getNetworkInfo(TYPE_MOBILE);
boolean mobileAvailable = mobile != null && mobile.isAvailable();
@@ -367,7 +416,6 @@ public class TestingActivity extends BriarActivity implements OnClickListener {
try {
int pid = android.os.Process.myPid();
Pattern pattern = Pattern.compile(".*\\( *" + pid + "\\).*");
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("logcat -d -v time *:I");
Scanner scanner = new Scanner(process.getInputStream());
while(scanner.hasNextLine()) {
@@ -383,22 +431,6 @@ public class TestingActivity extends BriarActivity implements OnClickListener {
}
statusMap.put("Debugging log:", log.toString());
// TorPlugin log output for all processes
StringBuilder torLog = new StringBuilder();
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("logcat -d -v time -s TorPlugin");
Scanner scanner = new Scanner(process.getInputStream());
while(scanner.hasNextLine()) {
torLog.append(scanner.nextLine());
torLog.append('\n');
}
scanner.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
statusMap.put("Tor debugging log:", torLog.toString());
return Collections.unmodifiableMap(statusMap);
}

View File

@@ -10,6 +10,14 @@ import android.os.StatFs;
class AndroidFileUtils implements FileUtils {
@SuppressWarnings("deprecation")
public long getTotalSpace(File f) throws IOException {
if(Build.VERSION.SDK_INT >= 9) return f.getTotalSpace();
StatFs s = new StatFs(f.getAbsolutePath());
// These deprecated methods are the best thing available for SDK < 9
return (long) s.getBlockCount() * s.getBlockSize();
}
@SuppressWarnings("deprecation")
public long getFreeSpace(File f) throws IOException {
if(Build.VERSION.SDK_INT >= 9) return f.getUsableSpace();

View File

@@ -5,5 +5,7 @@ import java.io.IOException;
public interface FileUtils {
long getTotalSpace(File f) throws IOException;
long getFreeSpace(File f) throws IOException;
}

View File

@@ -0,0 +1,17 @@
package org.briarproject.system;
import java.io.File;
import java.io.IOException;
import org.briarproject.api.system.FileUtils;
class DesktopFileUtils implements FileUtils {
public long getTotalSpace(File f) throws IOException {
return f.getTotalSpace(); // Requires Java 1.6
}
public long getFreeSpace(File f) throws IOException {
return f.getUsableSpace(); // Requires Java 1.6
}
}

View File

@@ -1,8 +1,5 @@
package org.briarproject.system;
import java.io.File;
import java.io.IOException;
import org.briarproject.api.system.Clock;
import org.briarproject.api.system.FileUtils;
import org.briarproject.api.system.SeedProvider;
@@ -18,10 +15,6 @@ public class DesktopSystemModule extends AbstractModule {
bind(Timer.class).to(SystemTimer.class);
if(OsUtils.isLinux())
bind(SeedProvider.class).to(LinuxSeedProvider.class);
bind(FileUtils.class).toInstance(new FileUtils() {
public long getFreeSpace(File f) throws IOException {
return f.getFreeSpace();
}
});
bind(FileUtils.class).to(DesktopFileUtils.class);
}
}

View File

@@ -7,7 +7,11 @@ import org.briarproject.api.system.FileUtils;
public class TestFileUtils implements FileUtils {
public long getTotalSpace(File f) throws IOException {
return f.getTotalSpace(); // Requires Java 1.6
}
public long getFreeSpace(File f) throws IOException {
return f.getFreeSpace();
return f.getUsableSpace(); // Requires Java 1.6
}
}