]>
Commit | Line | Data |
---|---|---|
4365e5b2 | 1 | /* |
2 | * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
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. | |
16 | * | |
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. | |
28 | */ | |
29 | ||
54e0fa1c | 30 | #include <stdlib.h> |
31 | #include <stdio.h> | |
32 | #include <string.h> | |
33 | #include <assert.h> | |
34 | #include <limits.h> | |
35 | #include <errno.h> | |
fb068dc9 | 36 | #include <termios.h> |
37 | #include <sys/ioctl.h> | |
54e0fa1c | 38 | |
39 | #if (ULONG_MAX == 4294967295UL) | |
40 | #define MEMTEST_32BIT | |
41 | #elif (ULONG_MAX == 18446744073709551615ULL) | |
42 | #define MEMTEST_64BIT | |
43 | #else | |
44 | #error "ULONG_MAX value not supported." | |
45 | #endif | |
46 | ||
d033ccb0 | 47 | #ifdef MEMTEST_32BIT |
d033ccb0 | 48 | #define ULONG_ONEZERO 0xaaaaaaaaUL |
49 | #define ULONG_ZEROONE 0x55555555UL | |
d98b03b7 | 50 | #else |
51 | #define ULONG_ONEZERO 0xaaaaaaaaaaaaaaaaUL | |
52 | #define ULONG_ZEROONE 0x5555555555555555UL | |
d033ccb0 | 53 | #endif |
54 | ||
fb068dc9 | 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. */ | |
58 | ||
59 | void memtest_progress_start(char *title, int pass) { | |
60 | int j; | |
61 | ||
62 | printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */ | |
63 | /* Fill with dots. */ | |
1a197a3c | 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/"); | |
fb068dc9 | 67 | printf("\x1b[H\x1b[2K"); /* Cursor home, clear current line. */ |
68 | printf("%s [%d]\n", title, pass); /* Print title. */ | |
69 | progress_printed = 0; | |
1a197a3c | 70 | progress_full = ws.ws_col*(ws.ws_row-3); |
fb068dc9 | 71 | fflush(stdout); |
72 | } | |
73 | ||
74 | void memtest_progress_end(void) { | |
75 | printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */ | |
76 | } | |
77 | ||
78 | void memtest_progress_step(size_t curr, size_t size, char c) { | |
4c442e9d | 79 | size_t chars = ((unsigned long long)curr*progress_full)/size, j; |
fb068dc9 | 80 | |
81 | for (j = 0; j < chars-progress_printed; j++) { | |
82 | printf("%c",c); | |
83 | progress_printed++; | |
84 | } | |
85 | fflush(stdout); | |
86 | } | |
87 | ||
d4a515c5 | 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); | |
93 | unsigned long j, *p; | |
94 | ||
95 | /* Fill */ | |
96 | p = l; | |
97 | for (j = 0; j < words; j++) { | |
98 | *p = (unsigned long)p; | |
99 | p++; | |
100 | if ((j & 0xffff) == 0) memtest_progress_step(j,words*2,'A'); | |
101 | } | |
102 | /* Test */ | |
103 | p = l; | |
104 | for (j = 0; j < words; j++) { | |
105 | if (*p != (unsigned long)p) { | |
106 | printf("\n*** MEMORY ADDRESSING ERROR: %p contains %lu\n", | |
107 | (void*) p, *p); | |
108 | exit(1); | |
109 | } | |
110 | p++; | |
111 | if ((j & 0xffff) == 0) memtest_progress_step(j+words,words*2,'A'); | |
112 | } | |
113 | } | |
114 | ||
54e0fa1c | 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. */ | |
d033ccb0 | 119 | void memtest_fill_random(unsigned long *l, size_t bytes) { |
54e0fa1c | 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; | |
124 | ||
125 | assert((bytes & 4095) == 0); | |
126 | for (off = 0; off < step; off++) { | |
127 | l1 = l+off; | |
128 | l2 = l1+words; | |
129 | for (w = 0; w < iwords; w++) { | |
130 | #ifdef MEMTEST_32BIT | |
131 | *l1 = *l2 = ((unsigned long) (rand()&0xffff)) | | |
132 | (((unsigned long) (rand()&0xffff)) << 16); | |
133 | #else | |
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); | |
138 | #endif | |
139 | l1 += step; | |
140 | l2 += step; | |
fb068dc9 | 141 | if ((w & 0xffff) == 0) |
d033ccb0 | 142 | memtest_progress_step(w+iwords*off,words,'R'); |
143 | } | |
144 | } | |
145 | } | |
146 | ||
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) | |
151 | { | |
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; | |
156 | ||
157 | assert((bytes & 4095) == 0); | |
158 | for (off = 0; off < step; off++) { | |
159 | l1 = l+off; | |
160 | l2 = l1+words; | |
161 | v = (off & 1) ? v2 : v1; | |
162 | for (w = 0; w < iwords; w++) { | |
163 | #ifdef MEMTEST_32BIT | |
ffe003dc | 164 | *l1 = *l2 = ((unsigned long) v) | |
165 | (((unsigned long) v) << 16); | |
d033ccb0 | 166 | #else |
ffe003dc | 167 | *l1 = *l2 = ((unsigned long) v) | |
168 | (((unsigned long) v) << 16) | | |
169 | (((unsigned long) v) << 32) | | |
170 | (((unsigned long) v) << 48); | |
d033ccb0 | 171 | #endif |
172 | l1 += step; | |
173 | l2 += step; | |
174 | if ((w & 0xffff) == 0) | |
175 | memtest_progress_step(w+iwords*off,words,sym); | |
54e0fa1c | 176 | } |
177 | } | |
178 | } | |
179 | ||
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; | |
183 | ||
184 | assert((bytes & 4095) == 0); | |
185 | l1 = l; | |
186 | l2 = l1+words; | |
187 | for (w = 0; w < words; w++) { | |
188 | if (*l1 != *l2) { | |
189 | printf("\n*** MEMORY ERROR DETECTED: %p != %p (%lu vs %lu)\n", | |
190 | (void*)l1, (void*)l2, *l1, *l2); | |
191 | exit(1); | |
192 | } | |
193 | l1 ++; | |
194 | l2 ++; | |
fb068dc9 | 195 | if ((w & 0xffff) == 0) memtest_progress_step(w,words,'='); |
54e0fa1c | 196 | } |
197 | } | |
198 | ||
d033ccb0 | 199 | void memtest_compare_times(unsigned long *m, size_t bytes, int pass, int times) { |
200 | int j; | |
201 | ||
202 | for (j = 0; j < times; j++) { | |
203 | memtest_progress_start("Compare",pass); | |
204 | memtest_compare(m,bytes); | |
205 | memtest_progress_end(); | |
206 | } | |
207 | } | |
208 | ||
54e0fa1c | 209 | void memtest_test(size_t megabytes, int passes) { |
210 | size_t bytes = megabytes*1024*1024; | |
211 | unsigned long *m = malloc(bytes); | |
d033ccb0 | 212 | int pass = 0; |
54e0fa1c | 213 | |
214 | if (m == NULL) { | |
215 | fprintf(stderr,"Unable to allocate %zu megabytes: %s", | |
216 | megabytes, strerror(errno)); | |
217 | exit(1); | |
218 | } | |
219 | while (pass != passes) { | |
220 | pass++; | |
d4a515c5 | 221 | |
222 | memtest_progress_start("Addressing test",pass); | |
223 | memtest_addressing(m,bytes); | |
224 | memtest_progress_end(); | |
225 | ||
fb068dc9 | 226 | memtest_progress_start("Random fill",pass); |
d033ccb0 | 227 | memtest_fill_random(m,bytes); |
fb068dc9 | 228 | memtest_progress_end(); |
d033ccb0 | 229 | memtest_compare_times(m,bytes,pass,4); |
230 | ||
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); | |
235 | ||
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); | |
54e0fa1c | 240 | } |
241 | } | |
242 | ||
243 | void memtest(size_t megabytes, int passes) { | |
fb068dc9 | 244 | if (ioctl(1, TIOCGWINSZ, &ws) == -1) { |
245 | ws.ws_col = 80; | |
246 | ws.ws_row = 20; | |
247 | } | |
54e0fa1c | 248 | memtest_test(megabytes,passes); |
249 | printf("\nYour memory passed this test.\n"); | |
a5801142 | 250 | printf("Please if you are still in doubt use the following two tools:\n"); |
54e0fa1c | 251 | printf("1) memtest86: http://www.memtest86.com/\n"); |
252 | printf("2) memtester: http://pyropus.ca/software/memtester/\n"); | |
253 | exit(0); | |
254 | } |