Bug 843361 - Read proc names more efficiently r=mfinkle

This commit is contained in:
Brad Lassey
2013-02-21 11:24:08 -05:00
parent cdbb84be40
commit 0462c1a7f9

View File

@@ -73,6 +73,7 @@ import java.io.BufferedReader;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@@ -87,8 +88,10 @@ import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.concurrent.SynchronousQueue; import java.util.concurrent.SynchronousQueue;
import java.net.ProxySelector; import java.net.ProxySelector;
import java.net.Proxy; import java.net.Proxy;
@@ -124,7 +127,6 @@ public class GeckoAppShell
static private final boolean LOGGING = false; static private final boolean LOGGING = false;
static File sHomeDir = null;
static private int sDensityDpi = 0; static private int sDensityDpi = 0;
private static final EventDispatcher sEventDispatcher = new EventDispatcher(); private static final EventDispatcher sEventDispatcher = new EventDispatcher();
@@ -1558,15 +1560,24 @@ public class GeckoAppShell
} catch (InterruptedException ie) {} } catch (InterruptedException ie) {}
} }
} }
public static String getAppNameByPID(int pid) {
public static String getAppNameByPID(Context context, int pid) { BufferedReader cmdlineReader = null;
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); String path = "/proc/" + pid + "/cmdline";
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { try {
if (processInfo.pid == pid) { File cmdlineFile = new File(path);
return processInfo.processName; if (!cmdlineFile.exists())
}
}
return ""; return "";
cmdlineReader = new BufferedReader(new FileReader(cmdlineFile));
return cmdlineReader.readLine().trim();
} catch (Exception ex) {
return "";
} finally {
if (null != cmdlineReader) {
try {
cmdlineReader.close();
} catch (Exception e) {}
}
}
} }
public static void listOfOpenFiles() { public static void listOfOpenFiles() {
@@ -1574,6 +1585,9 @@ public class GeckoAppShell
int nameColumn = -1; int nameColumn = -1;
try { try {
String filter = GeckoProfile.get(GeckoApp.mAppContext).getDir().toString();
Log.i(LOGTAG, "[OPENFILE] Filter: " + filter);
// run lsof and parse its output // run lsof and parse its output
java.lang.Process lsof = Runtime.getRuntime().exec("lsof"); java.lang.Process lsof = Runtime.getRuntime().exec("lsof");
BufferedReader in = new BufferedReader(new InputStreamReader(lsof.getInputStream()), 2048); BufferedReader in = new BufferedReader(new InputStreamReader(lsof.getInputStream()), 2048);
@@ -1591,14 +1605,21 @@ public class GeckoAppShell
} }
// alright, the rest are open file entries. // alright, the rest are open file entries.
Map<Integer, String> pidNameMap = new TreeMap<Integer, String>();
String output = null; String output = null;
while ((output = in.readLine()) != null) { while ((output = in.readLine()) != null) {
String[] split = output.split("\\s+"); String[] split = output.split("\\s+");
if (split.length <= pidColumn || split.length <= nameColumn) if (split.length <= pidColumn || split.length <= nameColumn)
continue; continue;
String name = getAppNameByPID(GeckoApp.mAppContext, Integer.parseInt(split[pidColumn])); Integer pid = new Integer(split[pidColumn]);
if (!TextUtils.isEmpty(name) && name.startsWith("org.mozilla")) String name = pidNameMap.get(pid);
Log.i(LOGTAG, "[OPENFILE] " + name + "(" + split[pidColumn] + ") : " + split[nameColumn]); if (name == null) {
name = getAppNameByPID(pid.intValue());
pidNameMap.put(pid, name);
}
String file = split[nameColumn];
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(file) && file.startsWith(filter))
Log.i(LOGTAG, "[OPENFILE] " + name + "(" + split[pidColumn] + ") : " + file);
} }
in.close(); in.close();
} catch (Exception e) { } } catch (Exception e) { }