merge from open-source master

Change-Id: Iff982a4bec118c01718056fff079460d365968c3
diff --git a/CleanSpec.mk b/CleanSpec.mk
new file mode 100644
index 0000000..b84e1b6
--- /dev/null
+++ b/CleanSpec.mk
@@ -0,0 +1,49 @@
+# Copyright (C) 2007 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.
+#
+
+# If you don't need to do a full clean build but would like to touch
+# a file or delete some intermediate files, add a clean step to the end
+# of the list.  These steps will only be run once, if they haven't been
+# run before.
+#
+# E.g.:
+#     $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
+#     $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
+#
+# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
+# files that are missing or have been moved.
+#
+# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
+# Use $(OUT_DIR) to refer to the "out" directory.
+#
+# If you need to re-do something that's already mentioned, just copy
+# the command and add it to the bottom of the list.  E.g., if a change
+# that you made last week required touching a file and a change you
+# made today requires touching the same file, just copy the old
+# touch step and add it to the end of the list.
+#
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
+
+# For example:
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
+#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
+#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
+
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
diff --git a/daemon/opd_events.c b/daemon/opd_events.c
index b544fb3..fa017e4 100644
--- a/daemon/opd_events.c
+++ b/daemon/opd_events.c
@@ -142,8 +142,19 @@
 	}
 
 	fprintf(stderr, "Unknown event for counter %lu\n", counter);
+        /*
+         * ANDROID FIXME - from time to time there seems to be 1 phantom event
+         * reported from counter 3 when only counter 0 is enabled. Instead of
+         * crashing the daemon and losing tons of useful samples, we just
+         * charge the erroneous single count to the first event.
+         */
+#ifdef ANDROID
+        return &opd_events[0];
+#else
 	abort();
 	return NULL;
+
+#endif
 }
 
 
diff --git a/opcontrol/Android.mk b/opcontrol/Android.mk
index 8f04f3a..bc538ad 100644
--- a/opcontrol/Android.mk
+++ b/opcontrol/Android.mk
@@ -1,7 +1,7 @@
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
-ifeq ($(TARGET_ARCH_VARIANT), armv7-a)
+ifeq ($(ARCH_ARM_HAVE_ARMV7A), true)
     LOCAL_CFLAGS += -DWITH_ARM_V7_A
 endif
 
diff --git a/opcontrol/opcontrol.cpp b/opcontrol/opcontrol.cpp
index 9f59247..0552c8c 100644
--- a/opcontrol/opcontrol.cpp
+++ b/opcontrol/opcontrol.cpp
@@ -45,7 +45,7 @@
 int min_count[MAX_EVENTS] = {150000, 200000, 250000};
 #else
 #define MAX_EVENTS 5
-int min_count[MAX_EVENTS] = {150000, 200000, 250000, 300000, 350000};
+int min_count[MAX_EVENTS] = {150000, 20000, 25000, 30000, 35000};
 #endif
 
 int verbose_print;
@@ -756,6 +756,7 @@
     }
 
     if (stop) {
+        echo_dev("1", 0, "dump", -1);
         echo_dev("0", 0, "enable", -1);
     }
 }
diff --git a/opimport_pull b/opimport_pull
index bc443ec..ed820f0 100755
--- a/opimport_pull
+++ b/opimport_pull
@@ -5,11 +5,12 @@
 import sys
 
 def PrintUsage():
-    print "Usage:" + sys.argv[0] + " [-r] dir"
+    print "Usage:" + sys.argv[0] + " [-s serial_number] [-r] dir"
+    print "    serial_number: the device being profiled"
     print "    -r : reuse the directory if it already exists"
     print "    dir: directory on the host to store profile results"
 
-if (len(sys.argv) > 3):
+if (len(sys.argv) > 5):
     PrintUsage()
     sys.exit(1)
 
@@ -31,12 +32,22 @@
     print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first"
     sys.exit(1)
 
-if sys.argv[1] == "-r" :
+argv_next = 1
+if sys.argv[1] == "-s":
+  if len(sys.argv) < 4:
+    PrintUsage()
+    sys.exit(1)
+  device = " -s %s" % sys.argv[2]
+  argv_next = argv_next + 2
+else:
+  device = ""
+
+if sys.argv[argv_next] == "-r" :
     replace_dir = 1
-    output_dir = sys.argv[2]
+    output_dir = sys.argv[argv_next+1]
 else:
     replace_dir = 0
-    output_dir = sys.argv[1]
+    output_dir = sys.argv[argv_next]
 
 if (os.path.exists(output_dir) and (replace_dir == 1)):
     os.system("rm -fr " + output_dir)
@@ -52,15 +63,32 @@
     sys.exit(1)
 
 # get the samples off the phone
-result = os.system("adb pull /data/oprofile/samples " + output_dir + \
-                   "/raw_samples > /dev/null 2>&1")
+result = os.system("adb%s pull /data/oprofile/samples %s/raw_samples "
+                   "> /dev/null 2>&1" % (device, output_dir))
 if result != 0:
-    print "adb pull failure, exiting"
+    print "adb%s pull failure, exiting" % device
     sys.exit(1)
 
 # enter the destination directory
 os.chdir(output_dir)
-stream = os.popen("find raw_samples -type f -name \*all")
+
+# We need to replace the " (deleted)" part in the directory names if
+# the region is allocated through ashmem. The post-processing tool doesn't like
+# space and parentheses.
+# Rename each individual directory from the longest first
+# For example, first rename
+# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)
+# to
+# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache
+# then to
+# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache
+deleted_pattern = re.compile(" \(deleted\)$");
+stream = os.popen("find raw_samples -type d -name \*\ \(deleted\)\* | sort -r")
+for line in stream:
+    line = line.rstrip()
+    new_dir = deleted_pattern.sub("", line)
+    cmd = "mv " + "\"" + line + "\" \"" + new_dir + "\""
+    os.system(cmd)
 
 # now all the sample files are on the host, we need to invoke opimport one at a
 # time to convert the content from the ARM abi to x86 ABI
@@ -70,6 +98,8 @@
 # 2: intermediate dirs: "/blah/blah/blah"
 # 3: filename: e.g. "CPU_CYCLES.150000.0.all.all.all"
 pattern = re.compile("(^raw_samples)(.*)/(.*)$")
+
+stream = os.popen("find raw_samples -type f -name \*all")
 for line in stream:
     match = pattern.search(line)
     leading_dir = match.group(1)