summaryrefslogtreecommitdiffstats
path: root/tools/preload
diff options
context:
space:
mode:
authorBob Lee <crazybob@google.com>2009-08-13 14:41:54 -0700
committerBob Lee <crazybob@google.com>2009-08-17 15:36:15 -0700
commit9d2d6e14b0932b6a74e01f393d5efed61458941b (patch)
tree833ec805d5f8eded7819bd98fea6587ed2098501 /tools/preload
parent802527c6a8a91f7da72f2ce9f6cb3cc342582127 (diff)
downloadframeworks_base-9d2d6e14b0932b6a74e01f393d5efed61458941b.zip
frameworks_base-9d2d6e14b0932b6a74e01f393d5efed61458941b.tar.gz
frameworks_base-9d2d6e14b0932b6a74e01f393d5efed61458941b.tar.bz2
Simplified algorithm used to generate the preloaded-classes list. Generated a new preloaded-classes file.
Diffstat (limited to 'tools/preload')
-rw-r--r--tools/preload/20090811.compiledbin15749051 -> 15943336 bytes
-rw-r--r--tools/preload/Android.mk2
-rw-r--r--tools/preload/ClassRank.java53
-rw-r--r--tools/preload/LoadedClass.java51
-rw-r--r--tools/preload/MemoryUsage.java37
-rw-r--r--tools/preload/Policy.java41
-rw-r--r--tools/preload/PrintCsv.java131
-rw-r--r--tools/preload/PrintHtmlDiff.java142
-rw-r--r--tools/preload/Proc.java76
-rw-r--r--tools/preload/Root.java2
-rw-r--r--tools/preload/WritePreloadedClassFile.java98
-rw-r--r--tools/preload/loadclass/LoadClass.java4
-rw-r--r--tools/preload/preload.ipr24
-rw-r--r--tools/preload/sorttable.js493
14 files changed, 867 insertions, 287 deletions
diff --git a/tools/preload/20090811.compiled b/tools/preload/20090811.compiled
index dd61487..6dbeca0 100644
--- a/tools/preload/20090811.compiled
+++ b/tools/preload/20090811.compiled
Binary files differ
diff --git a/tools/preload/Android.mk b/tools/preload/Android.mk
index e6fa103..f325870 100644
--- a/tools/preload/Android.mk
+++ b/tools/preload/Android.mk
@@ -3,13 +3,13 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
- ClassRank.java \
Compile.java \
LoadedClass.java \
MemoryUsage.java \
Operation.java \
Policy.java \
PrintCsv.java \
+ PrintHtmlDiff.java \
PrintPsTree.java \
Proc.java \
Record.java \
diff --git a/tools/preload/ClassRank.java b/tools/preload/ClassRank.java
deleted file mode 100644
index c562d5c..0000000
--- a/tools/preload/ClassRank.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.util.Comparator;
-
-/**
- * Ranks classes for preloading based on how long their operations took
- * and how early the operations happened. Higher ranked classes come first.
- */
-class ClassRank implements Comparator<Operation> {
-
- /**
- * Increase this number to add more weight to classes which were loaded
- * earlier.
- */
- static final int SEQUENCE_WEIGHT = 500; // 0.5ms
-
- static final int BUCKET_SIZE = 5;
-
- public int compare(Operation a, Operation b) {
- // Higher ranked operations should come first.
- int result = rankOf(b) - rankOf(a);
- if (result != 0) {
- return result;
- }
-
- // Make sure we don't drop one of two classes w/ the same rank.
- // If a load and an initialization have the same rank, it's OK
- // to treat the operations equally.
- return a.loadedClass.name.compareTo(b.loadedClass.name);
- }
-
- /** Ranks the given operation. */
- private static int rankOf(Operation o) {
- return o.medianExclusiveTimeMicros()
- + SEQUENCE_WEIGHT / (o.index / BUCKET_SIZE + 1);
- }
-}
-
-
diff --git a/tools/preload/LoadedClass.java b/tools/preload/LoadedClass.java
index 9ef17f5..86e5dfc 100644
--- a/tools/preload/LoadedClass.java
+++ b/tools/preload/LoadedClass.java
@@ -51,7 +51,7 @@ class LoadedClass implements Serializable, Comparable<LoadedClass> {
}
void measureMemoryUsage() {
-// this.memoryUsage = MemoryUsage.forClass(name);
+ this.memoryUsage = MemoryUsage.forClass(name);
}
int mlt = -1;
@@ -76,6 +76,10 @@ class LoadedClass implements Serializable, Comparable<LoadedClass> {
return mit = calculateMedian(initializations);
}
+ int medianTimeMicros() {
+ return medianInitTimeMicros() + medianLoadTimeMicros();
+ }
+
/** Calculates the median duration for a list of operations. */
private static int calculateMedian(List<Operation> operations) {
int size = operations.size();
@@ -99,18 +103,18 @@ class LoadedClass implements Serializable, Comparable<LoadedClass> {
}
}
- /** Returns names of apps that loaded this class. */
- Set<String> applicationNames() {
- Set<String> appNames = new HashSet<String>();
- addProcessNames(loads, appNames);
- addProcessNames(initializations, appNames);
- return appNames;
+ /** Returns names of processes that loaded this class. */
+ Set<String> processNames() {
+ Set<String> names = new HashSet<String>();
+ addProcessNames(loads, names);
+ addProcessNames(initializations, names);
+ return names;
}
- private void addProcessNames(List<Operation> ops, Set<String> appNames) {
+ private void addProcessNames(List<Operation> ops, Set<String> names) {
for (Operation operation : ops) {
- if (operation.process.isApplication()) {
- appNames.add(operation.process.name);
+ if (operation.process.fromZygote()) {
+ names.add(operation.process.name);
}
}
}
@@ -123,31 +127,4 @@ class LoadedClass implements Serializable, Comparable<LoadedClass> {
public String toString() {
return name;
}
-
- /**
- * Returns true if this class's initialization causes the given class to
- * initialize.
- */
- public boolean initializes(LoadedClass clazz, Set<LoadedClass> visited) {
- // Avoid infinite recursion.
- if (!visited.add(this)) {
- return false;
- }
-
- if (clazz == this) {
- return true;
- }
-
- for (Operation initialization : initializations) {
- if (initialization.loadedClass.initializes(clazz, visited)) {
- return true;
- }
- }
-
- return false;
- }
-
- public boolean isPreloadable() {
- return systemClass && Policy.isPreloadableClass(name);
- }
}
diff --git a/tools/preload/MemoryUsage.java b/tools/preload/MemoryUsage.java
index e5dfb2a..bc21b6f 100644
--- a/tools/preload/MemoryUsage.java
+++ b/tools/preload/MemoryUsage.java
@@ -34,8 +34,8 @@ class MemoryUsage implements Serializable {
static final MemoryUsage NOT_AVAILABLE = new MemoryUsage();
static int errorCount = 0;
- static final int MAXIMUM_ERRORS = 10; // give up after this many fails
+ // These values are in 1kB increments (not 4kB like you'd expect).
final int nativeSharedPages;
final int javaSharedPages;
final int otherSharedPages;
@@ -123,15 +123,24 @@ class MemoryUsage implements Serializable {
return allocSize - freedSize;
}
+ int totalHeap() {
+ return javaHeapSize() + (int) nativeHeapSize;
+ }
+
int javaPagesInK() {
- return (javaSharedPages + javaPrivatePages) * 4;
+ return javaSharedPages + javaPrivatePages;
}
int nativePagesInK() {
- return (nativeSharedPages + nativePrivatePages) * 4;
+ return nativeSharedPages + nativePrivatePages;
}
int otherPagesInK() {
- return (otherSharedPages + otherPrivatePages) * 4;
+ return otherSharedPages + otherPrivatePages;
+ }
+
+ int totalPages() {
+ return javaSharedPages + javaPrivatePages + nativeSharedPages +
+ nativePrivatePages + otherSharedPages + otherPrivatePages;
}
/**
@@ -163,13 +172,6 @@ class MemoryUsage implements Serializable {
* Measures memory usage for the given class.
*/
static MemoryUsage forClass(String className) {
-
- // This is a coarse approximation for determining that no device is connected,
- // or that the communication protocol has changed, but we'll keep going and stop whining.
- if (errorCount >= MAXIMUM_ERRORS) {
- return NOT_AVAILABLE;
- }
-
MeasureWithTimeout measurer = new MeasureWithTimeout(className);
new Thread(measurer).start();
@@ -280,4 +282,17 @@ class MemoryUsage implements Serializable {
e.printStackTrace();
}
}
+
+ /** Measures memory usage information and stores it in the model. */
+ public static void main(String[] args) throws IOException,
+ ClassNotFoundException {
+ Root root = Root.fromFile(args[0]);
+ root.baseline = baseline();
+ for (LoadedClass loadedClass : root.loadedClasses.values()) {
+ if (loadedClass.systemClass) {
+ loadedClass.measureMemoryUsage();
+ }
+ }
+ root.toFile(args[0]);
+ }
}
diff --git a/tools/preload/Policy.java b/tools/preload/Policy.java
index ade889e3..7a190ac 100644
--- a/tools/preload/Policy.java
+++ b/tools/preload/Policy.java
@@ -19,10 +19,10 @@ import java.util.HashSet;
import java.util.Set;
/**
- * This is not instantiated - we just provide data for other classes to use
+ * Policy that governs which classes are preloaded.
*/
public class Policy {
-
+
/**
* No constructor - use static methods only
*/
@@ -31,18 +31,24 @@ public class Policy {
/**
* This location (in the build system) of the preloaded-classes file.
*/
- private static final String PRELOADED_CLASS_FILE
+ static final String PRELOADED_CLASS_FILE
= "frameworks/base/preloaded-classes";
/**
* Long running services. These are restricted in their contribution to the
* preloader because their launch time is less critical.
*/
+ // TODO: Generate this automatically from package manager.
private static final Set<String> SERVICES = new HashSet<String>(Arrays.asList(
- "system_server",
- "com.google.process.content",
- "android.process.media",
- "com.google.process.gapps"
+ "system_server",
+ "com.google.process.content",
+ "android.process.media",
+ "com.android.phone",
+ "com.google.android.apps.maps.FriendService",
+ "com.google.android.apps.maps.LocationFriendService",
+ "com.google.android.googleapps",
+ "com.google.process.gapps",
+ "android.tts"
));
/**
@@ -63,24 +69,15 @@ public class Policy {
));
/**
- * Returns the path/file name of the preloaded classes file that will be written
- * by WritePreloadedClassFile.
- */
- public static String getPreloadedClassFileName() {
- return PRELOADED_CLASS_FILE;
- }
-
- /**
- * Reports if the given process name is a "long running" process or service
+ * Returns true if the given process name is a "long running" process or
+ * service.
*/
public static boolean isService(String processName) {
return SERVICES.contains(processName);
}
-
- /**
- * Reports if the given class should never be preloaded
- */
- public static boolean isPreloadableClass(String className) {
- return !EXCLUDED_CLASSES.contains(className);
+
+ /**Reports if the given class should be preloaded. */
+ public static boolean isPreloadable(LoadedClass clazz) {
+ return clazz.systemClass && !EXCLUDED_CLASSES.contains(clazz.name);
}
}
diff --git a/tools/preload/PrintCsv.java b/tools/preload/PrintCsv.java
index 62f4271..1820830 100644
--- a/tools/preload/PrintCsv.java
+++ b/tools/preload/PrintCsv.java
@@ -18,9 +18,12 @@ import java.io.IOException;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.BufferedInputStream;
+import java.io.Writer;
+import java.io.PrintStream;
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
+import java.util.Iterator;
/**
* Prints raw information in CSV format.
@@ -36,71 +39,89 @@ public class PrintCsv {
Root root = Root.fromFile(args[0]);
- System.out.println("Name"
+ printHeaders(System.out);
+
+ MemoryUsage baseline = MemoryUsage.baseline();
+
+ for (LoadedClass loadedClass : root.loadedClasses.values()) {
+ if (!loadedClass.systemClass) {
+ continue;
+ }
+
+ printRow(System.out, baseline, loadedClass);
+ }
+ }
+
+ static void printHeaders(PrintStream out) {
+ out.println("Name"
+ ",Preloaded"
+ ",Median Load Time (us)"
+ ",Median Init Time (us)"
+ ",Process Names"
+ ",Load Count"
- + ",Init Count");
-// + ",Managed Heap (B)"
-// + ",Native Heap (B)"
-// + ",Managed Pages (kB)"
-// + ",Native Pages (kB)"
-// + ",Other Pages (kB)");
+ + ",Init Count"
+ + ",Managed Heap (B)"
+ + ",Native Heap (B)"
+ + ",Managed Pages (kB)"
+ + ",Native Pages (kB)"
+ + ",Other Pages (kB)");
+ }
- MemoryUsage baseline = root.baseline;
+ static void printRow(PrintStream out, MemoryUsage baseline,
+ LoadedClass loadedClass) {
+ out.print(loadedClass.name);
+ out.print(',');
+ out.print(loadedClass.preloaded);
+ out.print(',');
+ out.print(loadedClass.medianLoadTimeMicros());
+ out.print(',');
+ out.print(loadedClass.medianInitTimeMicros());
+ out.print(',');
+ out.print('"');
- for (LoadedClass loadedClass : root.loadedClasses.values()) {
- if (!loadedClass.systemClass) {
- continue;
- }
+ Set<String> procNames = new TreeSet<String>();
+ for (Operation op : loadedClass.loads)
+ procNames.add(op.process.name);
+ for (Operation op : loadedClass.initializations)
+ procNames.add(op.process.name);
- System.out.print(loadedClass.name);
- System.out.print(',');
- System.out.print(loadedClass.preloaded);
- System.out.print(',');
- System.out.print(loadedClass.medianLoadTimeMicros());
- System.out.print(',');
- System.out.print(loadedClass.medianInitTimeMicros());
- System.out.print(',');
- System.out.print('"');
-
- Set<String> procNames = new TreeSet<String>();
- for (Operation op : loadedClass.loads)
- procNames.add(op.process.name);
- for (Operation op : loadedClass.initializations)
- procNames.add(op.process.name);
+ if (procNames.size() <= 3) {
for (String name : procNames) {
- System.out.print(name + "\n");
+ out.print(name + "\n");
}
-
- System.out.print('"');
- System.out.print(',');
- System.out.print(loadedClass.loads.size());
- System.out.print(',');
- System.out.print(loadedClass.initializations.size());
-/*
- if (loadedClass.memoryUsage.isAvailable()) {
- MemoryUsage subtracted
- = loadedClass.memoryUsage.subtract(baseline);
-
- System.out.print(',');
- System.out.print(subtracted.javaHeapSize());
- System.out.print(',');
- System.out.print(subtracted.nativeHeapSize);
- System.out.print(',');
- System.out.print(subtracted.javaPagesInK());
- System.out.print(',');
- System.out.print(subtracted.nativePagesInK());
- System.out.print(',');
- System.out.print(subtracted.otherPagesInK());
-
- } else {
- System.out.print(",n/a,n/a,n/a,n/a,n/a");
- }
-*/
- System.out.println();
+ } else {
+ Iterator<String> i = procNames.iterator();
+ out.print(i.next() + "\n");
+ out.print(i.next() + "\n");
+ out.print("...and " + (procNames.size() - 2)
+ + " others.");
}
+
+ out.print('"');
+ out.print(',');
+ out.print(loadedClass.loads.size());
+ out.print(',');
+ out.print(loadedClass.initializations.size());
+
+ if (loadedClass.memoryUsage.isAvailable()) {
+ MemoryUsage subtracted
+ = loadedClass.memoryUsage.subtract(baseline);
+
+ out.print(',');
+ out.print(subtracted.javaHeapSize());
+ out.print(',');
+ out.print(subtracted.nativeHeapSize);
+ out.print(',');
+ out.print(subtracted.javaPagesInK());
+ out.print(',');
+ out.print(subtracted.nativePagesInK());
+ out.print(',');
+ out.print(subtracted.otherPagesInK());
+
+ } else {
+ out.print(",n/a,n/a,n/a,n/a,n/a");
+ }
+
+ out.println();
}
}
diff --git a/tools/preload/PrintHtmlDiff.java b/tools/preload/PrintHtmlDiff.java
new file mode 100644
index 0000000..b101c85
--- /dev/null
+++ b/tools/preload/PrintHtmlDiff.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+import java.io.FileReader;
+import java.io.BufferedReader;
+import java.io.PrintStream;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.HashSet;
+import java.util.Iterator;
+
+/**
+ * Prints HTML containing removed and added files.
+ */
+public class PrintHtmlDiff {
+
+ private static final String OLD_PRELOADED_CLASSES
+ = "old-preloaded-classes";
+
+ public static void main(String[] args) throws IOException,
+ ClassNotFoundException {
+ Root root = Root.fromFile(args[0]);
+
+ BufferedReader oldClasses = new BufferedReader(
+ new FileReader(OLD_PRELOADED_CLASSES));
+
+ // Classes loaded implicitly by the zygote.
+ Set<LoadedClass> zygote = new HashSet<LoadedClass>();
+ for (Proc proc : root.processes.values()) {
+ if (proc.name.equals("zygote")) {
+ for (Operation op : proc.operations) {
+ zygote.add(op.loadedClass);
+ }
+ break;
+ }
+ }
+
+ Set<LoadedClass> removed = new TreeSet<LoadedClass>();
+ Set<LoadedClass> added = new TreeSet<LoadedClass>();
+
+ for (LoadedClass loadedClass : root.loadedClasses.values()) {
+ if (loadedClass.preloaded && !zygote.contains(loadedClass)) {
+ added.add(loadedClass);
+ }
+ }
+
+ String line;
+ while ((line = oldClasses.readLine()) != null) {
+ line = line.trim();
+ LoadedClass clazz = root.loadedClasses.get(line);
+ if (clazz != null) {
+ added.remove(clazz);
+ if (!clazz.preloaded) removed.add(clazz);
+ }
+ }
+
+ PrintStream out = System.out;
+
+ out.println("<html><body>");
+ out.println("<style>");
+ out.println("a, th, td, h2 { font-family: arial }");
+ out.println("th, td { font-size: small }");
+ out.println("</style>");
+ out.println("<script src=\"sorttable.js\"></script>");
+ out.println("<p><a href=\"#removed\">Removed</a>");
+ out.println("<a name=\"added\"/><h2>Added</h2>");
+ printTable(out, root.baseline, added);
+ out.println("<a name=\"removed\"/><h2>Removed</h2>");
+ printTable(out, root.baseline, removed);
+ out.println("</body></html>");
+ }
+
+ static void printTable(PrintStream out, MemoryUsage baseline,
+ Iterable<LoadedClass> classes) {
+ out.println("<table border=\"1\" cellpadding=\"5\""
+ + " class=\"sortable\">");
+
+ out.println("<thead><tr>");
+ out.println("<th>Name</th>");
+ out.println("<th>Load Time (us)</th>");
+ out.println("<th>Loaded By</th>");
+ out.println("<th>Heap (B)</th>");
+ out.println("<th>Pages</th>");
+ out.println("</tr></thead>");
+
+ for (LoadedClass clazz : classes) {
+ out.println("<tr>");
+ out.println("<td>" + clazz.name + "</td>");
+ out.println("<td>" + clazz.medianTimeMicros() + "</td>");
+
+ out.println("<td>");
+ Set<String> procNames = new TreeSet<String>();
+ for (Operation op : clazz.loads) procNames.add(op.process.name);
+ for (Operation op : clazz.initializations) {
+ procNames.add(op.process.name);
+ }
+ if (procNames.size() <= 3) {
+ for (String name : procNames) {
+ out.print(name + "<br/>");
+ }
+ } else {
+ Iterator<String> i = procNames.iterator();
+ out.print(i.next() + "<br/>");
+ out.print(i.next() + "<br/>");
+ out.print("...and " + (procNames.size() - 2)
+ + " others.");
+ }
+ out.println("</td>");
+
+ if (clazz.memoryUsage.isAvailable()) {
+ MemoryUsage subtracted
+ = clazz.memoryUsage.subtract(baseline);
+
+ out.println("<td>" + (subtracted.javaHeapSize()
+ + subtracted.nativeHeapSize) + "</td>");
+ out.println("<td>" + subtracted.totalPages() + "</td>");
+ } else {
+ for (int i = 0; i < 2; i++) {
+ out.println("<td>n/a</td>");
+ }
+ }
+
+ out.println("</tr>");
+ }
+
+ out.println("</table>");
+ }
+}
diff --git a/tools/preload/Proc.java b/tools/preload/Proc.java
index 66e04dc..2105021 100644
--- a/tools/preload/Proc.java
+++ b/tools/preload/Proc.java
@@ -14,13 +14,11 @@
* limitations under the License.
*/
-import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
-import java.util.Collections;
import java.io.Serializable;
/**
@@ -30,11 +28,6 @@ class Proc implements Serializable {
private static final long serialVersionUID = 0;
- /**
- * Default percentage of time to cut off of app class loading times.
- */
- static final int PERCENTAGE_TO_PRELOAD = 75;
-
/** Parent process. */
final Proc parent;
@@ -80,72 +73,11 @@ class Proc implements Serializable {
}
/**
- * Returns the percentage of time we should cut by preloading for this
- * app.
- */
- int percentageToPreload() {
- return PERCENTAGE_TO_PRELOAD;
- }
-
- /**
- * Returns a list of classes which should be preloaded.
+ * Returns true if this process comes from the zygote.
*/
- List<LoadedClass> highestRankedClasses() {
- if (!isApplication() || Policy.isService(this.name)) {
- return Collections.emptyList();
- }
-
- // Sort by rank.
- Operation[] ranked = new Operation[operations.size()];
- ranked = operations.toArray(ranked);
- Arrays.sort(ranked, new ClassRank());
-
- // The percentage of time to save by preloading.
- int timeToSave = totalTimeMicros() * percentageToPreload() / 100;
- int timeSaved = 0;
-
- int count = 0;
- List<LoadedClass> highest = new ArrayList<LoadedClass>();
- for (Operation operation : ranked) {
- if (timeSaved >= timeToSave || count++ > 100) {
- break;
- }
-
- if (!Policy.isPreloadableClass(operation.loadedClass.name)) {
- continue;
- }
-
- if (!operation.loadedClass.systemClass) {
- continue;
- }
-
- highest.add(operation.loadedClass);
- timeSaved += operation.medianExclusiveTimeMicros();
- }
-
- return highest;
- }
-
- /**
- * Total time spent class loading and initializing.
- */
- int totalTimeMicros() {
- int totalTime = 0;
- for (Operation operation : operations) {
- totalTime += operation.medianExclusiveTimeMicros();
- }
- return totalTime;
- }
-
- /**
- * Returns true if this process is an app.
- */
- public boolean isApplication() {
- if (name.equals("com.android.development")) {
- return false;
- }
-
- return parent != null && parent.name.equals("zygote");
+ public boolean fromZygote() {
+ return parent != null && parent.name.equals("zygote")
+ && !name.equals("com.android.development");
}
/**
diff --git a/tools/preload/Root.java b/tools/preload/Root.java
index 949f9b7..0bc29bf 100644
--- a/tools/preload/Root.java
+++ b/tools/preload/Root.java
@@ -46,7 +46,7 @@ public class Root implements Serializable {
final Map<String, LoadedClass> loadedClasses
= new HashMap<String, LoadedClass>();
- final MemoryUsage baseline = MemoryUsage.baseline();
+ MemoryUsage baseline = MemoryUsage.baseline();
/**
* Records class loads and initializations.
diff --git a/tools/preload/WritePreloadedClassFile.java b/tools/preload/WritePreloadedClassFile.java
index b209af0..96c539b 100644
--- a/tools/preload/WritePreloadedClassFile.java
+++ b/tools/preload/WritePreloadedClassFile.java
@@ -24,12 +24,18 @@ import java.util.Set;
import java.util.TreeSet;
/**
- * Writes /frameworks/base/preloaded-classes. Also updates LoadedClass.preloaded
- * fields and writes over compiled log file.
+ * Writes /frameworks/base/preloaded-classes. Also updates
+ * {@link LoadedClass#preloaded} fields and writes over compiled log file.
*/
public class WritePreloadedClassFile {
- public static void main(String[] args) throws IOException, ClassNotFoundException {
+ /**
+ * Preload any class that take longer to load than MIN_LOAD_TIME_MICROS us.
+ */
+ static final int MIN_LOAD_TIME_MICROS = 1250;
+
+ public static void main(String[] args) throws IOException,
+ ClassNotFoundException {
if (args.length != 1) {
System.err.println("Usage: WritePreloadedClassFile [compiled log]");
System.exit(-1);
@@ -44,48 +50,64 @@ public class WritePreloadedClassFile {
// Open preloaded-classes file for output.
Writer out = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(Policy.getPreloadedClassFileName()),
+ new FileOutputStream(Policy.PRELOADED_CLASS_FILE),
Charset.forName("US-ASCII")));
- out.write("# Classes which are preloaded by com.android.internal.os.ZygoteInit.\n");
- out.write("# Automatically generated by /frameworks/base/tools/preload.\n");
- out.write("# percent=" + Proc.PERCENTAGE_TO_PRELOAD
- + ", weight=" + ClassRank.SEQUENCE_WEIGHT
- + ", bucket_size=" + ClassRank.BUCKET_SIZE
- + "\n");
-
+ out.write("# Classes which are preloaded by"
+ + " com.android.internal.os.ZygoteInit.\n");
+ out.write("# Automatically generated by frameworks/base/tools/preload/"
+ + WritePreloadedClassFile.class.getSimpleName() + ".java.\n");
+ out.write("# MIN_LOAD_TIME_MICROS=" + MIN_LOAD_TIME_MICROS + "\n");
+
+ /*
+ * The set of classes to preload. We preload a class if:
+ *
+ * a) it's loaded in the bootclasspath (i.e., is a system class)
+ * b) it takes > MIN_LOAD_TIME_MICROS us to load, and
+ * c) it's loaded by more than one process, or it's loaded by an
+ * application (i.e., not a long running service)
+ */
Set<LoadedClass> toPreload = new TreeSet<LoadedClass>();
- // Preload all classes that were loaded by at least 2 apps, if both
- // apps run at the same time, they'll share memory.
+ // Preload classes that were loaded by at least 2 processes. Hopefully,
+ // the memory associated with these classes will be shared.
for (LoadedClass loadedClass : root.loadedClasses.values()) {
- if (!loadedClass.isPreloadable()) {
- continue;
- }
-
- Set<String> appNames = loadedClass.applicationNames();
-
- if (appNames.size() > 3) {
+ Set<String> names = loadedClass.processNames();
+ if (shouldPreload(loadedClass) && names.size() > 1) {
toPreload.add(loadedClass);
}
}
- // Try to make individual apps start faster by preloading slowest
- // classes.
+ int initialSize = toPreload.size();
+ System.out.println(initialSize
+ + " classses were loaded by more than one app.");
+
+ // Preload eligable classes from applications (not long-running
+ // services).
for (Proc proc : root.processes.values()) {
- toPreload.addAll(proc.highestRankedClasses());
+ if (proc.fromZygote() && !Policy.isService(proc.name)) {
+ for (Operation operation : proc.operations) {
+ LoadedClass loadedClass = operation.loadedClass;
+ if (shouldPreload(loadedClass)) {
+ toPreload.add(loadedClass);
+ }
+ }
+ }
}
- System.out.println(toPreload.size() + " classes will be preloaded.");
+ System.out.println("Added " + (toPreload.size() - initialSize)
+ + " more to speed up applications.");
- // Make classes that were already loaded by the zygote explicit.
+ System.out.println(toPreload.size()
+ + " total classes will be preloaded.");
+
+ // Make classes that were implicitly loaded by the zygote explicit.
// This adds minimal overhead but avoid confusion about classes not
// appearing in the list.
- addAllClassesFor("zygote", root, toPreload);
+ addAllClassesFrom("zygote", root, toPreload);
for (LoadedClass loadedClass : toPreload) {
- out.write(loadedClass.name);
- out.write('\n');
+ out.write(loadedClass.name + "\n");
}
out.close();
@@ -97,18 +119,26 @@ public class WritePreloadedClassFile {
root.toFile(rootFile);
}
- private static void addAllClassesFor(String packageName, Root root,
- Set<LoadedClass> toPreload) {
+ private static void addAllClassesFrom(String processName, Root root,
+ Set<LoadedClass> toPreload) {
for (Proc proc : root.processes.values()) {
- if (proc.name.equals(packageName)) {
+ if (proc.name.equals(processName)) {
for (Operation operation : proc.operations) {
- // TODO: I'm not sure how the zygote loaded classes that
- // aren't supposed to be preloadable...
- if (operation.loadedClass.isPreloadable()) {
+ boolean preloadable
+ = Policy.isPreloadable(operation.loadedClass);
+ if (preloadable) {
toPreload.add(operation.loadedClass);
}
}
}
}
}
+
+ /**
+ * Returns true if the class should be preloaded.
+ */
+ private static boolean shouldPreload(LoadedClass clazz) {
+ return Policy.isPreloadable(clazz)
+ && clazz.medianTimeMicros() > MIN_LOAD_TIME_MICROS;
+ }
}
diff --git a/tools/preload/loadclass/LoadClass.java b/tools/preload/loadclass/LoadClass.java
index 471cc84..a71b6a8 100644
--- a/tools/preload/loadclass/LoadClass.java
+++ b/tools/preload/loadclass/LoadClass.java
@@ -35,7 +35,11 @@ class LoadClass {
if (args.length > 0) {
try {
+ long start = System.currentTimeMillis();
Class.forName(args[0]);
+ long elapsed = System.currentTimeMillis() - start;
+ Log.i("LoadClass", "Loaded " + args[0] + " in " + elapsed
+ + "ms.");
} catch (ClassNotFoundException e) {
Log.w("LoadClass", e);
return;
diff --git a/tools/preload/preload.ipr b/tools/preload/preload.ipr
index f78bf76..0c9621c 100644
--- a/tools/preload/preload.ipr
+++ b/tools/preload/preload.ipr
@@ -25,7 +25,28 @@
<option name="USE_PROJECT_LEVEL_SETTINGS" value="false" />
</component>
<component name="CodeStyleSettingsManager">
- <option name="PER_PROJECT_SETTINGS" />
+ <option name="PER_PROJECT_SETTINGS">
+ <value>
+ <ADDITIONAL_INDENT_OPTIONS fileType="java">
+ <option name="INDENT_SIZE" value="4" />
+ <option name="CONTINUATION_INDENT_SIZE" value="8" />
+ <option name="TAB_SIZE" value="4" />
+ <option name="USE_TAB_CHARACTER" value="false" />
+ <option name="SMART_TABS" value="false" />
+ <option name="LABEL_INDENT_SIZE" value="0" />
+ <option name="LABEL_INDENT_ABSOLUTE" value="false" />
+ </ADDITIONAL_INDENT_OPTIONS>
+ <ADDITIONAL_INDENT_OPTIONS fileType="xml">
+ <option name="INDENT_SIZE" value="4" />
+ <option name="CONTINUATION_INDENT_SIZE" value="8" />
+ <option name="TAB_SIZE" value="4" />
+ <option name="USE_TAB_CHARACTER" value="false" />
+ <option name="SMART_TABS" value="false" />
+ <option name="LABEL_INDENT_SIZE" value="0" />
+ <option name="LABEL_INDENT_ABSOLUTE" value="false" />
+ </ADDITIONAL_INDENT_OPTIONS>
+ </value>
+ </option>
<option name="USE_PER_PROJECT_SETTINGS" value="false" />
</component>
<component name="CompilerConfiguration">
@@ -405,6 +426,7 @@
</component>
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Perforce" />
+ <mapping directory="/Volumes/Android/donut/frameworks/base" vcs="Git" />
</component>
<component name="VssConfiguration">
<option name="CLIENT_PATH" value="" />
diff --git a/tools/preload/sorttable.js b/tools/preload/sorttable.js
new file mode 100644
index 0000000..25bccb2
--- /dev/null
+++ b/tools/preload/sorttable.js
@@ -0,0 +1,493 @@
+/*
+ SortTable
+ version 2
+ 7th April 2007
+ Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
+
+ Instructions:
+ Download this file
+ Add <script src="sorttable.js"></script> to your HTML
+ Add class="sortable" to any table you'd like to make sortable
+ Click on the headers to sort
+
+ Thanks to many, many people for contributions and suggestions.
+ Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
+ This basically means: do what you want with it.
+*/
+
+
+var stIsIE = /*@cc_on!@*/false;
+
+sorttable = {
+ init: function() {
+ // quit if this function has already been called
+ if (arguments.callee.done) return;
+ // flag this function so we don't do the same thing twice
+ arguments.callee.done = true;
+ // kill the timer
+ if (_timer) clearInterval(_timer);
+
+ if (!document.createElement || !document.getElementsByTagName) return;
+
+ sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
+
+ forEach(document.getElementsByTagName('table'), function(table) {
+ if (table.className.search(/\bsortable\b/) != -1) {
+ sorttable.makeSortable(table);
+ }
+ });
+
+ },
+
+ makeSortable: function(table) {
+ if (table.getElementsByTagName('thead').length == 0) {
+ // table doesn't have a tHead. Since it should have, create one and
+ // put the first table row in it.
+ the = document.createElement('thead');
+ the.appendChild(table.rows[0]);
+ table.insertBefore(the,table.firstChild);
+ }
+ // Safari doesn't support table.tHead, sigh
+ if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
+
+ if (table.tHead.rows.length != 1) return; // can't cope with two header rows
+
+ // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
+ // "total" rows, for example). This is B&R, since what you're supposed
+ // to do is put them in a tfoot. So, if there are sortbottom rows,
+ // for backwards compatibility, move them to tfoot (creating it if needed).
+ sortbottomrows = [];
+ for (var i=0; i<table.rows.length; i++) {
+ if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
+ sortbottomrows[sortbottomrows.length] = table.rows[i];
+ }
+ }
+ if (sortbottomrows) {
+ if (table.tFoot == null) {
+ // table doesn't have a tfoot. Create one.
+ tfo = document.createElement('tfoot');
+ table.appendChild(tfo);
+ }
+ for (var i=0; i<sortbottomrows.length; i++) {
+ tfo.appendChild(sortbottomrows[i]);
+ }
+ delete sortbottomrows;
+ }
+
+ // work through each column and calculate its type
+ headrow = table.tHead.rows[0].cells;
+ for (var i=0; i<headrow.length; i++) {
+ // manually override the type with a sorttable_type attribute
+ if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
+ mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
+ if (mtch) { override = mtch[1]; }
+ if (mtch && typeof sorttable["sort_"+override] == 'function') {
+ headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
+ } else {
+ headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
+ }
+ // make it clickable to sort
+ headrow[i].sorttable_columnindex = i;
+ headrow[i].sorttable_tbody = table.tBodies[0];
+ dean_addEvent(headrow[i],"click", function(e) {
+
+ if (this.className.search(/\bsorttable_sorted\b/) != -1) {
+ // if we're already sorted by this column, just
+ // reverse the table, which is quicker
+ sorttable.reverse(this.sorttable_tbody);
+ this.className = this.className.replace('sorttable_sorted',
+ 'sorttable_sorted_reverse');
+ this.removeChild(document.getElementById('sorttable_sortfwdind'));
+ sortrevind = document.createElement('span');
+ sortrevind.id = "sorttable_sortrevind";
+ sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
+ this.appendChild(sortrevind);
+ return;
+ }
+ if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
+ // if we're already sorted by this column in reverse, just
+ // re-reverse the table, which is quicker
+ sorttable.reverse(this.sorttable_tbody);
+ this.className = this.className.replace('sorttable_sorted_reverse',
+ 'sorttable_sorted');
+ this.removeChild(document.getElementById('sorttable_sortrevind'));
+ sortfwdind = document.createElement('span');
+ sortfwdind.id = "sorttable_sortfwdind";
+ sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
+ this.appendChild(sortfwdind);
+ return;
+ }
+
+ // remove sorttable_sorted classes
+ theadrow = this.parentNode;
+ forEach(theadrow.childNodes, function(cell) {
+ if (cell.nodeType == 1) { // an element
+ cell.className = cell.className.replace('sorttable_sorted_reverse','');
+ cell.className = cell.className.replace('sorttable_sorted','');
+ }
+ });
+ sortfwdind = document.getElementById('sorttable_sortfwdind');
+ if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
+ sortrevind = document.getElementById('sorttable_sortrevind');
+ if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
+
+ this.className += ' sorttable_sorted';
+ sortfwdind = document.createElement('span');
+ sortfwdind.id = "sorttable_sortfwdind";
+ sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
+ this.appendChild(sortfwdind);
+
+ // build an array to sort. This is a Schwartzian transform thing,
+ // i.e., we "decorate" each row with the actual sort key,
+ // sort based on the sort keys, and then put the rows back in order
+ // which is a lot faster because you only do getInnerText once per row
+ row_array = [];
+ col = this.sorttable_columnindex;
+ rows = this.sorttable_tbody.rows;
+ for (var j=0; j<rows.length; j++) {
+ row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
+ }
+ /* If you want a stable sort, uncomment the following line */
+ //sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
+ /* and comment out this one */
+ row_array.sort(this.sorttable_sortfunction);
+
+ tb = this.sorttable_tbody;
+ for (var j=0; j<row_array.length; j++) {
+ tb.appendChild(row_array[j][1]);
+ }
+
+ delete row_array;
+ });
+ }
+ }
+ },
+
+ guessType: function(table, column) {
+ // guess the type of a column based on its first non-blank row
+ sortfn = sorttable.sort_alpha;
+ for (var i=0; i<table.tBodies[0].rows.length; i++) {
+ text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
+ if (text != '') {
+ if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
+ return sorttable.sort_numeric;
+ }
+ // check for a date: dd/mm/yyyy or dd/mm/yy
+ // can have / or . or - as separator
+ // can be mm/dd as well
+ possdate = text.match(sorttable.DATE_RE)
+ if (possdate) {
+ // looks like a date
+ first = parseInt(possdate[1]);
+ second = parseInt(possdate[2]);
+ if (first > 12) {
+ // definitely dd/mm
+ return sorttable.sort_ddmm;
+ } else if (second > 12) {
+ return sorttable.sort_mmdd;
+ } else {
+ // looks like a date, but we can't tell which, so assume
+ // that it's dd/mm (English imperialism!) and keep looking
+ sortfn = sorttable.sort_ddmm;
+ }
+ }
+ }
+ }
+ return sortfn;
+ },
+
+ getInnerText: function(node) {
+ // gets the text we want to use for sorting for a cell.
+ // strips leading and trailing whitespace.
+ // this is *not* a generic getInnerText function; it's special to sorttable.
+ // for example, you can override the cell text with a customkey attribute.
+ // it also gets .value for <input> fields.
+
+ hasInputs = (typeof node.getElementsByTagName == 'function') &&
+ node.getElementsByTagName('input').length;
+
+ if (node.getAttribute("sorttable_customkey") != null) {
+ return node.getAttribute("sorttable_customkey");
+ }
+ else if (typeof node.textContent != 'undefined' && !hasInputs) {
+ return node.textContent.replace(/^\s+|\s+$/g, '');
+ }
+ else if (typeof node.innerText != 'undefined' && !hasInputs) {
+ return node.innerText.replace(/^\s+|\s+$/g, '');
+ }
+ else if (typeof node.text != 'undefined' && !hasInputs) {
+ return node.text.replace(/^\s+|\s+$/g, '');
+ }
+ else {
+ switch (node.nodeType) {
+ case 3:
+ if (node.nodeName.toLowerCase() == 'input') {
+ return node.value.replace(/^\s+|\s+$/g, '');
+ }
+ case 4:
+ return node.nodeValue.replace(/^\s+|\s+$/g, '');
+ break;
+ case 1:
+ case 11:
+ var innerText = '';
+ for (var i = 0; i < node.childNodes.length; i++) {
+ innerText += sorttable.getInnerText(node.childNodes[i]);
+ }
+ return innerText.replace(/^\s+|\s+$/g, '');
+ break;
+ default:
+ return '';
+ }
+ }
+ },
+
+ reverse: function(tbody) {
+ // reverse the rows in a tbody
+ newrows = [];
+ for (var i=0; i<tbody.rows.length; i++) {
+ newrows[newrows.length] = tbody.rows[i];
+ }
+ for (var i=newrows.length-1; i>=0; i--) {
+ tbody.appendChild(newrows[i]);
+ }
+ delete newrows;
+ },
+
+ /* sort functions
+ each sort function takes two parameters, a and b
+ you are comparing a[0] and b[0] */
+ sort_numeric: function(a,b) {
+ aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
+ if (isNaN(aa)) aa = 0;
+ bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
+ if (isNaN(bb)) bb = 0;
+ return aa-bb;
+ },
+ sort_alpha: function(a,b) {
+ if (a[0]==b[0]) return 0;
+ if (a[0]<b[0]) return -1;
+ return 1;
+ },
+ sort_ddmm: function(a,b) {
+ mtch = a[0].match(sorttable.DATE_RE);
+ y = mtch[3]; m = mtch[2]; d = mtch[1];
+ if (m.length == 1) m = '0'+m;
+ if (d.length == 1) d = '0'+d;
+ dt1 = y+m+d;
+ mtch = b[0].match(sorttable.DATE_RE);
+ y = mtch[3]; m = mtch[2]; d = mtch[1];
+ if (m.length == 1) m = '0'+m;
+ if (d.length == 1) d = '0'+d;
+ dt2 = y+m+d;
+ if (dt1==dt2) return 0;
+ if (dt1<dt2) return -1;
+ return 1;
+ },
+ sort_mmdd: function(a,b) {
+ mtch = a[0].match(sorttable.DATE_RE);
+ y = mtch[3]; d = mtch[2]; m = mtch[1];
+ if (m.length == 1) m = '0'+m;
+ if (d.length == 1) d = '0'+d;
+ dt1 = y+m+d;
+ mtch = b[0].match(sorttable.DATE_RE);
+ y = mtch[3]; d = mtch[2]; m = mtch[1];
+ if (m.length == 1) m = '0'+m;
+ if (d.length == 1) d = '0'+d;
+ dt2 = y+m+d;
+ if (dt1==dt2) return 0;
+ if (dt1<dt2) return -1;
+ return 1;
+ },
+
+ shaker_sort: function(list, comp_func) {
+ // A stable sort function to allow multi-level sorting of data
+ // see: http://en.wikipedia.org/wiki/Cocktail_sort
+ // thanks to Joseph Nahmias
+ var b = 0;
+ var t = list.length - 1;
+ var swap = true;
+
+ while(swap) {
+ swap = false;
+ for(var i = b; i < t; ++i) {
+ if ( comp_func(list[i], list[i+1]) > 0 ) {
+ var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
+ swap = true;
+ }
+ } // for
+ t--;
+
+ if (!swap) break;
+
+ for(var i = t; i > b; --i) {
+ if ( comp_func(list[i], list[i-1]) < 0 ) {
+ var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
+ swap = true;
+ }
+ } // for
+ b++;
+
+ } // while(swap)
+ }
+}
+
+/* ******************************************************************
+ Supporting functions: bundled here to avoid depending on a library
+ ****************************************************************** */
+
+// Dean Edwards/Matthias Miller/John Resig
+
+/* for Mozilla/Opera9 */
+if (document.addEventListener) {
+ document.addEventListener("DOMContentLoaded", sorttable.init, false);
+}
+
+/* for Internet Explorer */
+/*@cc_on @*/
+/*@if (@_win32)
+ document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
+ var script = document.getElementById("__ie_onload");
+ script.onreadystatechange = function() {
+ if (this.readyState == "complete") {
+ sorttable.init(); // call the onload handler
+ }
+ };
+/*@end @*/
+
+/* for Safari */
+if (/WebKit/i.test(navigator.userAgent)) { // sniff
+ var _timer = setInterval(function() {
+ if (/loaded|complete/.test(document.readyState)) {
+ sorttable.init(); // call the onload handler
+ }
+ }, 10);
+}
+
+/* for other browsers */
+window.onload = sorttable.init;
+
+// written by Dean Edwards, 2005
+// with input from Tino Zijdel, Matthias Miller, Diego Perini
+
+// http://dean.edwards.name/weblog/2005/10/add-event/
+
+function dean_addEvent(element, type, handler) {
+ if (element.addEventListener) {
+ element.addEventListener(type, handler, false);
+ } else {
+ // assign each event handler a unique ID
+ if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
+ // create a hash table of event types for the element
+ if (!element.events) element.events = {};
+ // create a hash table of event handlers for each element/event pair
+ var handlers = element.events[type];
+ if (!handlers) {
+ handlers = element.events[type] = {};
+ // store the existing event handler (if there is one)
+ if (element["on" + type]) {
+ handlers[0] = element["on" + type];
+ }
+ }
+ // store the event handler in the hash table
+ handlers[handler.$$guid] = handler;
+ // assign a global event handler to do all the work
+ element["on" + type] = handleEvent;
+ }
+};
+// a counter used to create unique IDs
+dean_addEvent.guid = 1;
+
+function removeEvent(element, type, handler) {
+ if (element.removeEventListener) {
+ element.removeEventListener(type, handler, false);
+ } else {
+ // delete the event handler from the hash table
+ if (element.events && element.events[type]) {
+ delete element.events[type][handler.$$guid];
+ }
+ }
+};
+
+function handleEvent(event) {
+ var returnValue = true;
+ // grab the event object (IE uses a global event object)
+ event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
+ // get a reference to the hash table of event handlers
+ var handlers = this.events[event.type];
+ // execute each event handler
+ for (var i in handlers) {
+ this.$$handleEvent = handlers[i];
+ if (this.$$handleEvent(event) === false) {
+ returnValue = false;
+ }
+ }
+ return returnValue;
+};
+
+function fixEvent(event) {
+ // add W3C standard event methods
+ event.preventDefault = fixEvent.preventDefault;
+ event.stopPropagation = fixEvent.stopPropagation;
+ return event;
+};
+fixEvent.preventDefault = function() {
+ this.returnValue = false;
+};
+fixEvent.stopPropagation = function() {
+ this.cancelBubble = true;
+}
+
+// Dean's forEach: http://dean.edwards.name/base/forEach.js
+/*
+ forEach, version 1.0
+ Copyright 2006, Dean Edwards
+ License: http://www.opensource.org/licenses/mit-license.php
+*/
+
+// array-like enumeration
+if (!Array.forEach) { // mozilla already supports this
+ Array.forEach = function(array, block, context) {
+ for (var i = 0; i < array.length; i++) {
+ block.call(context, array[i], i, array);
+ }
+ };
+}
+
+// generic enumeration
+Function.prototype.forEach = function(object, block, context) {
+ for (var key in object) {
+ if (typeof this.prototype[key] == "undefined") {
+ block.call(context, object[key], key, object);
+ }
+ }
+};
+
+// character enumeration
+String.forEach = function(string, block, context) {
+ Array.forEach(string.split(""), function(chr, index) {
+ block.call(context, chr, index, string);
+ });
+};
+
+// globally resolve forEach enumeration
+var forEach = function(object, block, context) {
+ if (object) {
+ var resolve = Object; // default
+ if (object instanceof Function) {
+ // functions have a "length" property
+ resolve = Function;
+ } else if (object.forEach instanceof Function) {
+ // the object implements a custom forEach method so use that
+ object.forEach(block, context);
+ return;
+ } else if (typeof object == "string") {
+ // the object is a string
+ resolve = String;
+ } else if (typeof object.length == "number") {
+ // the object is array-like
+ resolve = Array;
+ }
+ resolve.forEach(object, block, context);
+ }
+};
+