release-request-f7cf88d3-9ff3-4602-981e-79c555d8aa91-for-git_oc-mr1-release-4102303 snap-temp-L92600000074342209

Change-Id: I26cc2ce2b6a8f2070f251e4c6aced0ba6700996c
diff --git a/tests/apply_overlay.sh b/tests/apply_overlay.sh
index 55f863c..3da3a64 100755
--- a/tests/apply_overlay.sh
+++ b/tests/apply_overlay.sh
@@ -15,6 +15,12 @@
 
 PROG_NAME=`basename $0`
 
+# Set to true to get more debug information
+DEBUG=false
+# Remove the comment to enable valgrind
+# Require build with $mmma external/valgrind
+#VALGRIND="valgrind --leak-check=yes --show-reachable=yes"
+
 function usage() {
   echo "Usage:"
   echo "  $PROG_NAME (--fdt|--ufdt) (--remote) <Base DTS> <Overlay DTS> <Output DTS>"
@@ -68,19 +74,27 @@
 BASE_DTB_NAME="${BASE_DTS_NAME}-base.dtb"
 BASE_DTB="${TEMP_DIR}/${BASE_DTB_NAME}"
 dtc -@ -qq -O dtb -o "$BASE_DTB" "$BASE_DTS"
+if $DEBUG; then
+  echo "[base.dts]"
+  dtc -O dts "$BASE_DTB"
+fi
 
 # Compile the *-overlay.dts to make *-overlay.dtb
 OVERLAY_DTS_NAME=`basename "$OVERLAY_DTS"`
 OVERLAY_DTB_NAME="${OVERLAY_DTS_NAME}-overlay.dtb"
 OVERLAY_DTB="${TEMP_DIR}/${OVERLAY_DTB_NAME}"
 dtc -@ -qq -O dtb -o "$OVERLAY_DTB" "$OVERLAY_DTS"
+if $DEBUG; then
+  echo "[overlay.dts]"
+  dtc -O dts "$OVERLAY_DTB"
+fi
 
 # Run ufdt_apply_overlay to combine *-base.dtb and *-overlay.dtb
 # into *-merged.dtb
 MERGED_DTB_NAME="${BASE_DTS_NAME}-merged.dtb"
 MERGED_DTB="${TEMP_DIR}/${MERGED_DTB_NAME}"
 if [ -z "$REMOTE_PATH" ]; then
-  "$OVERLAY" "$BASE_DTB" "$OVERLAY_DTB" "$MERGED_DTB"
+  $VALGRIND "$OVERLAY" "$BASE_DTB" "$OVERLAY_DTB" "$MERGED_DTB"
 else
   adb push "$BASE_DTB" "$REMOTE_PATH" > /dev/null
   adb push "$OVERLAY_DTB" "$REMOTE_PATH" > /dev/null
@@ -104,3 +118,7 @@
 
 # Dump
 dtc -s -O dts -o "$OUT_DTS" "$MERGED_DTB"
+if $DEBUG; then
+  echo "[merged.dts]"
+  cat $OUT_DTS
+fi
diff --git a/tests/run_stress_test.sh b/tests/run_stress_test.sh
new file mode 100755
index 0000000..0caf2b4
--- /dev/null
+++ b/tests/run_stress_test.sh
@@ -0,0 +1,101 @@
+#!/bin/bash
+# Copyright (C) 2016 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.
+
+# Include some functions from common.sh.
+SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
+source ${SCRIPT_DIR}/common.sh
+
+OUT_DATA_DIR="test_out"
+
+DEBUG=false
+
+
+apply_overlay() {
+  local overaly="$1"
+  local base_dts="$2"
+  local overlay_dts="$3"
+  local merged_dts="$4"
+
+  ./apply_overlay.sh "--$overaly" "$base_dts" "$overlay_dts" "$merged_dts"
+}
+
+run_stress_test() {
+  # see ufdt_gen_test_dts.c for detail
+  local node_depth="$1"
+  local node_unused="$2"
+  local node_count="$3"
+  local append_count="$4"
+  local override_count="$5"
+
+  mkdir -p "$OUT_DATA_DIR" >& /dev/null
+
+  #
+  # Prepare dtb and dtbo files
+  #
+  local base_dts="${OUT_DATA_DIR}/base.dts"
+  echo "  Base DT: depth=$node_depth unused=$node_unused nodes=$node_count"
+  ufdt_gen_test_dts -n $node_count -d $node_depth -u $node_unused \
+    -o "$base_dts"
+  if $DEBUG; then
+    cat "$base_dts"
+  fi
+
+  local overlay_dts="${OUT_DATA_DIR}/overlay.dts"
+  echo "  Overlay DT: append=$append_count override=$override_count"
+  ufdt_gen_test_dts -p -a $append_count -w $override_count \
+    -o "$overlay_dts"
+  if $DEBUG; then
+    cat "$overlay_dts"
+  fi
+
+  local merged_dts="${OUT_DATA_DIR}/overlay_merged.dts"
+  apply_overlay ufdt $base_dts $overlay_dts $merged_dts
+
+  rm -rf "$OUT_DATA_DIR"
+}
+
+main() {
+  alert "========== Running Stress Tests =========="
+
+  if [ -z "$ANDROID_BUILD_TOP" ]; then
+    die "Run envsetup.sh / lunch yet?"
+  fi
+
+  if ! command_exists ufdt_gen_test_dts ||
+     ! command_exists dtc; then
+    die "Run mmma $(dirname $SCRIPT_DIR) yet?"
+  fi
+
+  (
+
+  # cd to ${SCRIPT_DIR} in a subshell because gen_test.sh uses relative
+  # paths for dependent files.
+  cd "${SCRIPT_DIR}"
+
+  # node_depth      = 2
+  # node_unused     = 0
+  # node_count      = 5000
+  # append_count    = 2500
+  # override_count  = 2500
+  run_stress_test 2 0 5000 2500 2500
+
+  )
+
+  if [ $? -ne 0 ]; then
+    die "Some test cases failed, please check error message..."
+  fi
+}
+
+main "$@"
diff --git a/ufdt_node_pool.c b/ufdt_node_pool.c
index dabb285..8b9c8a9 100644
--- a/ufdt_node_pool.c
+++ b/ufdt_node_pool.c
@@ -19,6 +19,9 @@
 #include "libufdt_sysdeps.h"
 #include "ufdt_types.h"
 
+/* Define DEBUG_DISABLE_POOL to use dto_malloc and dto_free directly */
+/* #define DEBUG_DISABLE_POOL */
+
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 
 #define UFDT_NODE_POOL_ENTRIES_PER_BLOCK 1024
@@ -72,7 +75,7 @@
     if (block->alloc_entry_cnt != 0) is_leak = 1;
 
     struct ufdt_node_pool_block_header *next_block = block->next_block;
-    if (!block->first_free_entry) dto_free(block);
+    dto_free(block);
     block = next_block;
   }
 
@@ -171,6 +174,10 @@
 }
 
 void *ufdt_node_pool_alloc(struct ufdt_node_pool *pool) {
+#ifdef DEBUG_DISABLE_POOL
+  return dto_malloc(UFDT_NODE_POOL_ENTRY_SIZE);
+#endif
+
   // return dto_malloc(UFDT_NODE_POOL_ENTRY_SIZE);
   // If there is no free block, create a new one
   struct ufdt_node_pool_block_header *block = pool->first_block;
@@ -209,6 +216,10 @@
 }
 
 void ufdt_node_pool_free(struct ufdt_node_pool *pool, void *node) {
+#ifdef DEBUG_DISABLE_POOL
+  return dto_free(node);
+#endif
+
   struct ufdt_node_pool_block_header **block_ptr =
       _ufdt_node_pool_search_block(pool, node);
   if (*block_ptr == NULL) {