From 65180976c9963432d166b47a0b692260a69c0d47 Mon Sep 17 00:00:00 2001
From: Qingming Su <qingming.su@windriver.com>
Date: Tue, 19 Aug 2014 05:16:17 -0400
Subject: [PATCH] lmbench: Can't proceed on some targets

lmbench can't proceed on some targets.  The memory check fails because the
memory latency of each page is longer then 10us, which is a time limit set
in the original memsize.c.

The memory latency is very different on different targets due to the
hardware and current system load.  The targets with slower memory
chips or heavy system load need much longer time to read or write
the memory.

This fix changes the fixed time limit of 10us to a specific value calculated
from the runtime target.

Also set an upper limit of memory size used for lmbench testing.  The memory
check sometimes fails if the target has a large amount of memory, for
example more than 4G.

Signed-off-by: Qingming Su <qingming.su@windriver.com>
Signed-off-by: Fupan Li <fupan.li@windriver.com>

Add and reword above comments

Upstream-status: inappropriate [ configuration ]

Signed-off-by: Mark Hatle <mark.hatle@windriver.com>

---
 scripts/config-run | 12 +++++++---
 src/Makefile       |  4 ++--
 src/memsize.c      | 66 +++++++++++++++++++++++++++++++++++-------------------
 3 files changed, 54 insertions(+), 28 deletions(-)

diff --git a/scripts/config-run b/scripts/config-run
index 918cbdf..096ed12 100755
--- a/scripts/config-run
+++ b/scripts/config-run
@@ -197,6 +197,12 @@ The bigger the range, the more accurate the results, but larger sizes
 take somewhat longer to run the benchmark.
 
 EOF
+
+# By default, use 512M memory as the upper limit for lmbench test
+if [ $MB -gt 512 ];then
+MB=512
+fi
+
 echo $ECHON "MB [default $MB]: $ECHOC"
 read TMP
 if [ X$TMP != X ]
@@ -687,10 +693,10 @@ case $MAIL in
 		;;
 esac
 
-INFO=`../scripts/info`
+INFO=`../scripts/hostinfo`
 if [ $MAIL = yes ]
 then	if [ ! -f $INFO ]
-	then	cp ../scripts/info-template $INFO
+	then	cp ../scripts/hostinfo-template $INFO
 		chmod +w $INFO
 		REUSE=no
 	else	
@@ -733,7 +739,7 @@ EOF
 		then	EDITOR=$TMP
 		fi
 		if [ X$EDITOR != "none" ]
-		then	$EDITOR `../scripts/info`
+		then	$EDITOR `../scripts/hostinfo`
 		fi
 	fi
 fi
diff --git a/src/Makefile b/src/Makefile
index c7e4e3c..d9efd54 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -50,7 +50,7 @@ TARGET=`../scripts/target`
 BINDIR=../bin/$(OS)
 CONFIG=../bin/$(OS)/`../scripts/config`
 UTILS=../scripts/target ../scripts/os ../scripts/gnu-os ../scripts/compiler \
-	../scripts/info ../scripts/info-template ../scripts/version \
+	../scripts/hostinfo ../scripts/hostinfo-template ../scripts/version \
 	../scripts/config ../scripts/config-run ../scripts/results \
 	../scripts/lmbench ../scripts/make ../scripts/build
 INSTALL=cp
@@ -240,7 +240,7 @@ $O/getopt.o : getopt.c $(INCS)
 	$(COMPILE) -c getopt.c -o $O/getopt.o
 
 $(UTILS) :
-	-cd ../scripts; make get
+	-cd ../scripts; cp info hostinfo; cp info-template hostinfo-template
 
 # Do not remove the next line, $(MAKE) depend needs it
 # MAKEDEPEND follows
diff --git a/src/memsize.c b/src/memsize.c
index eb25a09..82d7faf 100644
--- a/src/memsize.c
+++ b/src/memsize.c
@@ -14,9 +14,12 @@ char	*id = "$Id$\n";
 
 #define	CHK(x)	if ((x) == -1) { perror("x"); exit(1); }
 
-#ifndef	TOO_LONG
-#define	TOO_LONG	10	/* usecs */
-#endif
+//#ifndef	TOO_LONG
+//#define	TOO_LONG	10	/* usecs */
+//#endif
+
+#define	MEMORY_SIZE_1MB (1024 * 1024)
+#define	MEMORY_SIZE_8MB (8 * 1024 * 1024)
 
 int	alarm_triggered = 0;
 
@@ -35,10 +38,10 @@ main(int ac, char **av)
 	size_t	delta;
 
 	if (ac == 2) {
-		max = size = bytes(av[1]) * 1024 * 1024;
+		max = size = bytes(av[1]) * MEMORY_SIZE_1MB;
 	}
-	if (max < 1024 * 1024) {
-		max = size = 1024 * 1024 * 1024;
+	if (max < MEMORY_SIZE_1MB) {
+		max = size = 1024 * MEMORY_SIZE_1MB;
 	}
 	/*
 	 * Binary search down and then binary search up
@@ -48,7 +51,7 @@ main(int ac, char **av)
 	}
 	/* delta = size / (2 * 1024 * 1024) */
 	for (delta = (size >> 21); delta > 0; delta >>= 1) {
-		uint64 sz = (uint64)size + (uint64)delta * 1024 * 1024;
+		uint64 sz = (uint64)size + (uint64)delta * MEMORY_SIZE_1MB;
 		size_t check = sz;
 		if (max < sz) continue;
 		if (check < sz || !test_malloc(sz)) break;
@@ -66,41 +69,58 @@ timeit(char *where, size_t size)
 {
 	int	sum = 0;
 	size_t	n;
-	size_t	s_prev;
+	size_t	s_prev = MEMORY_SIZE_8MB;
 	size_t	range;
-	size_t	incr = 1024 * 1024;
+	size_t	incr = MEMORY_SIZE_1MB;
 	size_t	pagesize = getpagesize();
-	unsigned long long	s;
-
-	if (size < 1024*1024 - 16*1024) {
-		fprintf(stderr, "Bad size\n");
-		return;
-	}
+	size_t	time_each_page = 0;
+	size_t	too_long = 0;
+	unsigned long long      s;
+
+	if (pagesize < MEMORY_SIZE_1MB)
+		range = MEMORY_SIZE_1MB;
+	else
+		range = MEMORY_SIZE_8MB;
+
+	incr = MEMORY_SIZE_1MB;
+	
+	if (size < range) {
+              fprintf(stderr, "Bad size\n");
+              return;
+	    }
+
+	//Touch range of memory, get the average time (usec) of operating each memory page on this system
+        start(0);
+        touchRange(where, range, pagesize);
+        sum = stop(0, 0);
+
+        if ((time_each_page = sum * pagesize / range) < 1)
+		time_each_page = 1;
+	//Set the uper limit of time spending on one page
+        too_long = 10 * time_each_page;
 
-	range = 1024 * 1024;
-	incr = 1024 * 1024;
-	touchRange(where, range, pagesize);
 	for (range += incr; range <= size; range += incr) {
 		n = range / pagesize;
-		set_alarm(n * TOO_LONG);
+		set_alarm(n * too_long);
 		touchRange(where + range - incr, incr, pagesize);
 		clear_alarm();
-		set_alarm(n * TOO_LONG);
+		set_alarm(n * too_long);
 		start(0);
 		touchRange(where, range, pagesize);
 		sum = stop(0, 0);
 		clear_alarm();
-		if ((sum / n) > TOO_LONG || alarm_triggered) {
+		if ((sum / n) > too_long || alarm_triggered) {
 			size = range - incr;
+			fprintf(stderr, "Error! Memory testing timeout! Touch one page of memory needs more than %d (usecs)\n ", too_long);
 			break;
 		}
-		for (s = 8 * 1024 * 1024; s <= range; s_prev = s, s *= 2)
+		for (s = s_prev; s <= range; s_prev = s, s *= 2)
 			if (s < s_prev) break;
 		incr = s / 8;
 		if (range < size && size < range + incr) {
 			incr = size - range;
 		}
-		fprintf(stderr, "%dMB OK\r", (int)(range/(1024*1024)));
+		fprintf(stderr, "%dMB OK\r", (int)(range/MEMORY_SIZE_1MB));
 	}
 	fprintf(stderr, "\n");
 	printf("%d\n", (int)(size>>20));
