]>
git.saurik.com Git - redis.git/blob - src/memtest.c
2 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/ioctl.h>
39 #if (ULONG_MAX == 4294967295UL)
41 #elif (ULONG_MAX == 18446744073709551615ULL)
44 #error "ULONG_MAX value not supported."
48 #define ULONG_ONEZERO 0xaaaaaaaaUL
49 #define ULONG_ZEROONE 0x55555555UL
51 #define ULONG_ONEZERO 0xaaaaaaaaaaaaaaaaUL
52 #define ULONG_ZEROONE 0x5555555555555555UL
55 static struct winsize ws
;
56 size_t progress_printed
; /* Printed chars in screen-wide progress bar. */
57 size_t progress_full
; /* How many chars to write to fill the progress bar. */
59 void memtest_progress_start(char *title
, int pass
) {
62 printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
64 for (j
= 0; j
< ws
.ws_col
*(ws
.ws_row
-2); j
++) printf(".");
65 printf("Please keep the test running several minutes per GB of memory.\n");
66 printf("Also check http://www.memtest86.com/ and http://pyropus.ca/software/memtester/");
67 printf("\x1b[H\x1b[2K"); /* Cursor home, clear current line. */
68 printf("%s [%d]\n", title
, pass
); /* Print title. */
70 progress_full
= ws
.ws_col
*(ws
.ws_row
-3);
74 void memtest_progress_end(void) {
75 printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
78 void memtest_progress_step(size_t curr
, size_t size
, char c
) {
79 size_t chars
= ((unsigned long long)curr
*progress_full
)/size
, j
;
81 for (j
= 0; j
< chars
-progress_printed
; j
++) {
88 /* Test that addressing is fine. Every location is populated with its own
89 * address, and finally verified. This test is very fast but may detect
90 * ASAP big issues with the memory subsystem. */
91 void memtest_addressing(unsigned long *l
, size_t bytes
) {
92 unsigned long words
= bytes
/sizeof(unsigned long);
97 for (j
= 0; j
< words
; j
++) {
98 *p
= (unsigned long)p
;
100 if ((j
& 0xffff) == 0) memtest_progress_step(j
,words
*2,'A');
104 for (j
= 0; j
< words
; j
++) {
105 if (*p
!= (unsigned long)p
) {
106 printf("\n*** MEMORY ADDRESSING ERROR: %p contains %lu\n",
111 if ((j
& 0xffff) == 0) memtest_progress_step(j
+words
,words
*2,'A');
115 /* Fill words stepping a single page at every write, so we continue to
116 * touch all the pages in the smallest amount of time reducing the
117 * effectiveness of caches, and making it hard for the OS to transfer
118 * pages on the swap. */
119 void memtest_fill_random(unsigned long *l
, size_t bytes
) {
120 unsigned long step
= 4096/sizeof(unsigned long);
121 unsigned long words
= bytes
/sizeof(unsigned long)/2;
122 unsigned long iwords
= words
/step
; /* words per iteration */
123 unsigned long off
, w
, *l1
, *l2
;
125 assert((bytes
& 4095) == 0);
126 for (off
= 0; off
< step
; off
++) {
129 for (w
= 0; w
< iwords
; w
++) {
131 *l1
= *l2
= ((unsigned long) (rand()&0xffff)) |
132 (((unsigned long) (rand()&0xffff)) << 16);
134 *l1
= *l2
= ((unsigned long) (rand()&0xffff)) |
135 (((unsigned long) (rand()&0xffff)) << 16) |
136 (((unsigned long) (rand()&0xffff)) << 32) |
137 (((unsigned long) (rand()&0xffff)) << 48);
141 if ((w
& 0xffff) == 0)
142 memtest_progress_step(w
+iwords
*off
,words
,'R');
147 /* Like memtest_fill_random() but uses the two specified values to fill
148 * memory, in an alternated way (v1|v2|v1|v2|...) */
149 void memtest_fill_value(unsigned long *l
, size_t bytes
, unsigned long v1
,
150 unsigned long v2
, char sym
)
152 unsigned long step
= 4096/sizeof(unsigned long);
153 unsigned long words
= bytes
/sizeof(unsigned long)/2;
154 unsigned long iwords
= words
/step
; /* words per iteration */
155 unsigned long off
, w
, *l1
, *l2
, v
;
157 assert((bytes
& 4095) == 0);
158 for (off
= 0; off
< step
; off
++) {
161 v
= (off
& 1) ? v2
: v1
;
162 for (w
= 0; w
< iwords
; w
++) {
164 *l1
= *l2
= ((unsigned long) v
) |
165 (((unsigned long) v
) << 16);
167 *l1
= *l2
= ((unsigned long) v
) |
168 (((unsigned long) v
) << 16) |
169 (((unsigned long) v
) << 32) |
170 (((unsigned long) v
) << 48);
174 if ((w
& 0xffff) == 0)
175 memtest_progress_step(w
+iwords
*off
,words
,sym
);
180 void memtest_compare(unsigned long *l
, size_t bytes
) {
181 unsigned long words
= bytes
/sizeof(unsigned long)/2;
182 unsigned long w
, *l1
, *l2
;
184 assert((bytes
& 4095) == 0);
187 for (w
= 0; w
< words
; w
++) {
189 printf("\n*** MEMORY ERROR DETECTED: %p != %p (%lu vs %lu)\n",
190 (void*)l1
, (void*)l2
, *l1
, *l2
);
195 if ((w
& 0xffff) == 0) memtest_progress_step(w
,words
,'=');
199 void memtest_compare_times(unsigned long *m
, size_t bytes
, int pass
, int times
) {
202 for (j
= 0; j
< times
; j
++) {
203 memtest_progress_start("Compare",pass
);
204 memtest_compare(m
,bytes
);
205 memtest_progress_end();
209 void memtest_test(size_t megabytes
, int passes
) {
210 size_t bytes
= megabytes
*1024*1024;
211 unsigned long *m
= malloc(bytes
);
215 fprintf(stderr
,"Unable to allocate %zu megabytes: %s",
216 megabytes
, strerror(errno
));
219 while (pass
!= passes
) {
222 memtest_progress_start("Addressing test",pass
);
223 memtest_addressing(m
,bytes
);
224 memtest_progress_end();
226 memtest_progress_start("Random fill",pass
);
227 memtest_fill_random(m
,bytes
);
228 memtest_progress_end();
229 memtest_compare_times(m
,bytes
,pass
,4);
231 memtest_progress_start("Solid fill",pass
);
232 memtest_fill_value(m
,bytes
,0,(unsigned long)-1,'S');
233 memtest_progress_end();
234 memtest_compare_times(m
,bytes
,pass
,4);
236 memtest_progress_start("Checkerboard fill",pass
);
237 memtest_fill_value(m
,bytes
,ULONG_ONEZERO
,ULONG_ZEROONE
,'C');
238 memtest_progress_end();
239 memtest_compare_times(m
,bytes
,pass
,4);
243 void memtest(size_t megabytes
, int passes
) {
244 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
248 memtest_test(megabytes
,passes
);
249 printf("\nYour memory passed this test.\n");
250 printf("Please if you are still in doubt use the following two tools:\n");
251 printf("1) memtest86: http://www.memtest86.com/\n");
252 printf("2) memtester: http://pyropus.ca/software/memtester/\n");