1From 65180976c9963432d166b47a0b692260a69c0d47 Mon Sep 17 00:00:00 2001
2From: Qingming Su <qingming.su@windriver.com>
3Date: Tue, 19 Aug 2014 05:16:17 -0400
4Subject: [PATCH] lmbench: Can't proceed on some targets
5
6lmbench can't proceed on some targets.  The memory check fails because the
7memory latency of each page is longer then 10us, which is a time limit set
8in the original memsize.c.
9
10The memory latency is very different on different targets due to the
11hardware and current system load.  The targets with slower memory
12chips or heavy system load need much longer time to read or write
13the memory.
14
15This fix changes the fixed time limit of 10us to a specific value calculated
16from the runtime target.
17
18Also set an upper limit of memory size used for lmbench testing.  The memory
19check sometimes fails if the target has a large amount of memory, for
20example more than 4G.
21
22Signed-off-by: Qingming Su <qingming.su@windriver.com>
23Signed-off-by: Fupan Li <fupan.li@windriver.com>
24
25Add and reword above comments
26
27Upstream-Status: Inappropriate [ configuration ]
28
29Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
30
31---
32 scripts/config-run | 12 +++++++---
33 src/Makefile       |  4 ++--
34 src/memsize.c      | 66 +++++++++++++++++++++++++++++++++++-------------------
35 3 files changed, 54 insertions(+), 28 deletions(-)
36
37diff --git a/scripts/config-run b/scripts/config-run
38index 918cbdf..096ed12 100755
39--- a/scripts/config-run
40+++ b/scripts/config-run
41@@ -197,6 +197,12 @@ The bigger the range, the more accurate the results, but larger sizes
42 take somewhat longer to run the benchmark.
43
44 EOF
45+
46+# By default, use 512M memory as the upper limit for lmbench test
47+if [ $MB -gt 512 ];then
48+MB=512
49+fi
50+
51 echo $ECHON "MB [default $MB]: $ECHOC"
52 read TMP
53 if [ X$TMP != X ]
54@@ -687,10 +693,10 @@ case $MAIL in
55 		;;
56 esac
57
58-INFO=`../scripts/info`
59+INFO=`../scripts/hostinfo`
60 if [ $MAIL = yes ]
61 then	if [ ! -f $INFO ]
62-	then	cp ../scripts/info-template $INFO
63+	then	cp ../scripts/hostinfo-template $INFO
64 		chmod +w $INFO
65 		REUSE=no
66 	else
67@@ -733,7 +739,7 @@ EOF
68 		then	EDITOR=$TMP
69 		fi
70 		if [ X$EDITOR != "none" ]
71-		then	$EDITOR `../scripts/info`
72+		then	$EDITOR `../scripts/hostinfo`
73 		fi
74 	fi
75 fi
76diff --git a/src/Makefile b/src/Makefile
77index c7e4e3c..d9efd54 100644
78--- a/src/Makefile
79+++ b/src/Makefile
80@@ -50,7 +50,7 @@ TARGET=`../scripts/target`
81 BINDIR=../bin/$(OS)
82 CONFIG=../bin/$(OS)/`../scripts/config`
83 UTILS=../scripts/target ../scripts/os ../scripts/gnu-os ../scripts/compiler \
84-	../scripts/info ../scripts/info-template ../scripts/version \
85+	../scripts/hostinfo ../scripts/hostinfo-template ../scripts/version \
86 	../scripts/config ../scripts/config-run ../scripts/results \
87 	../scripts/lmbench ../scripts/make ../scripts/build
88 INSTALL=cp
89@@ -240,7 +240,7 @@ $O/getopt.o : getopt.c $(INCS)
90 	$(COMPILE) -c getopt.c -o $O/getopt.o
91
92 $(UTILS) :
93-	-cd ../scripts; make get
94+	-cd ../scripts; cp info hostinfo; cp info-template hostinfo-template
95
96 # Do not remove the next line, $(MAKE) depend needs it
97 # MAKEDEPEND follows
98diff --git a/src/memsize.c b/src/memsize.c
99index eb25a09..82d7faf 100644
100--- a/src/memsize.c
101+++ b/src/memsize.c
102@@ -14,9 +14,12 @@ char	*id = "$Id$\n";
103
104 #define	CHK(x)	if ((x) == -1) { perror("x"); exit(1); }
105
106-#ifndef	TOO_LONG
107-#define	TOO_LONG	10	/* usecs */
108-#endif
109+//#ifndef	TOO_LONG
110+//#define	TOO_LONG	10	/* usecs */
111+//#endif
112+
113+#define	MEMORY_SIZE_1MB (1024 * 1024)
114+#define	MEMORY_SIZE_8MB (8 * 1024 * 1024)
115
116 int	alarm_triggered = 0;
117
118@@ -35,10 +38,10 @@ main(int ac, char **av)
119 	size_t	delta;
120
121 	if (ac == 2) {
122-		max = size = bytes(av[1]) * 1024 * 1024;
123+		max = size = bytes(av[1]) * MEMORY_SIZE_1MB;
124 	}
125-	if (max < 1024 * 1024) {
126-		max = size = 1024 * 1024 * 1024;
127+	if (max < MEMORY_SIZE_1MB) {
128+		max = size = 1024 * MEMORY_SIZE_1MB;
129 	}
130 	/*
131 	 * Binary search down and then binary search up
132@@ -48,7 +51,7 @@ main(int ac, char **av)
133 	}
134 	/* delta = size / (2 * 1024 * 1024) */
135 	for (delta = (size >> 21); delta > 0; delta >>= 1) {
136-		uint64 sz = (uint64)size + (uint64)delta * 1024 * 1024;
137+		uint64 sz = (uint64)size + (uint64)delta * MEMORY_SIZE_1MB;
138 		size_t check = sz;
139 		if (max < sz) continue;
140 		if (check < sz || !test_malloc(sz)) break;
141@@ -66,41 +69,58 @@ timeit(char *where, size_t size)
142 {
143 	int	sum = 0;
144 	size_t	n;
145-	size_t	s_prev;
146+	size_t	s_prev = MEMORY_SIZE_8MB;
147 	size_t	range;
148-	size_t	incr = 1024 * 1024;
149+	size_t	incr = MEMORY_SIZE_1MB;
150 	size_t	pagesize = getpagesize();
151-	unsigned long long	s;
152-
153-	if (size < 1024*1024 - 16*1024) {
154-		fprintf(stderr, "Bad size\n");
155-		return;
156-	}
157+	size_t	time_each_page = 0;
158+	size_t	too_long = 0;
159+	unsigned long long      s;
160+
161+	if (pagesize < MEMORY_SIZE_1MB)
162+		range = MEMORY_SIZE_1MB;
163+	else
164+		range = MEMORY_SIZE_8MB;
165+
166+	incr = MEMORY_SIZE_1MB;
167+
168+	if (size < range) {
169+              fprintf(stderr, "Bad size\n");
170+              return;
171+	    }
172+
173+	//Touch range of memory, get the average time (usec) of operating each memory page on this system
174+        start(0);
175+        touchRange(where, range, pagesize);
176+        sum = stop(0, 0);
177+
178+        if ((time_each_page = sum * pagesize / range) < 1)
179+		time_each_page = 1;
180+	//Set the uper limit of time spending on one page
181+        too_long = 10 * time_each_page;
182
183-	range = 1024 * 1024;
184-	incr = 1024 * 1024;
185-	touchRange(where, range, pagesize);
186 	for (range += incr; range <= size; range += incr) {
187 		n = range / pagesize;
188-		set_alarm(n * TOO_LONG);
189+		set_alarm(n * too_long);
190 		touchRange(where + range - incr, incr, pagesize);
191 		clear_alarm();
192-		set_alarm(n * TOO_LONG);
193+		set_alarm(n * too_long);
194 		start(0);
195 		touchRange(where, range, pagesize);
196 		sum = stop(0, 0);
197 		clear_alarm();
198-		if ((sum / n) > TOO_LONG || alarm_triggered) {
199+		if ((sum / n) > too_long || alarm_triggered) {
200 			size = range - incr;
201+			fprintf(stderr, "Error! Memory testing timeout! Touch one page of memory needs more than %d (usecs)\n ", too_long);
202 			break;
203 		}
204-		for (s = 8 * 1024 * 1024; s <= range; s_prev = s, s *= 2)
205+		for (s = s_prev; s <= range; s_prev = s, s *= 2)
206 			if (s < s_prev) break;
207 		incr = s / 8;
208 		if (range < size && size < range + incr) {
209 			incr = size - range;
210 		}
211-		fprintf(stderr, "%dMB OK\r", (int)(range/(1024*1024)));
212+		fprintf(stderr, "%dMB OK\r", (int)(range/MEMORY_SIZE_1MB));
213 	}
214 	fprintf(stderr, "\n");
215 	printf("%d\n", (int)(size>>20));
216