diff options
author | antirez <antirez@gmail.com> | 2015-12-16 17:41:20 +0100 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2015-12-16 17:41:22 +0100 |
commit | a1c9c05e17a1d49425ac0d87196fe91869645a08 (patch) | |
tree | 1ff758e4ef80fce92703fc452273d87f7aa7eaf7 /src/memtest.c | |
parent | b9aeb9815684aa9a2652f15ec2c6753919aed2c0 (diff) | |
download | redis-a1c9c05e17a1d49425ac0d87196fe91869645a08.tar.gz |
Hopefully better memory test on crash.
The old test, designed to do a transformation on the bits that was
invertible, in order to avoid touching the original memory content, was
not effective as it was redis-server --test-memory. The former often
reported OK while the latter was able to spot the error.
So the test was substituted with one that may perform better, however
the new one must backup the memory tested, so it tests memory in small
pieces. This limits the effectiveness because of the CPU caches. However
some attempt is made in order to trash the CPU cache between the fill
and the check stages, but not for the addressing test unfortunately.
We'll see if this test will be able to find errors where the old failed.
Diffstat (limited to 'src/memtest.c')
-rw-r--r-- | src/memtest.c | 124 |
1 files changed, 86 insertions, 38 deletions
diff --git a/src/memtest.c b/src/memtest.c index a25290852..a455430f5 100644 --- a/src/memtest.c +++ b/src/memtest.c @@ -26,7 +26,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - +#include <stdint.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -123,27 +123,33 @@ int memtest_addressing(unsigned long *l, size_t bytes, int interactive) { /* Fill words stepping a single page at every write, so we continue to * touch all the pages in the smallest amount of time reducing the * effectiveness of caches, and making it hard for the OS to transfer - * pages on the swap. */ + * pages on the swap. + * + * In this test we can't call rand() since the system may be completely + * unable to handle library calls, so we have to resort to our own + * PRNG that only uses local state. We use an xorshift* PRNG. */ +#define xorshift64star_next() do { \ + rseed ^= rseed >> 12; \ + rseed ^= rseed << 25; \ + rseed ^= rseed >> 27; \ + rout = rseed * UINT64_C(2685821657736338717); \ +} while(0) + void memtest_fill_random(unsigned long *l, size_t bytes, int interactive) { unsigned long step = 4096/sizeof(unsigned long); unsigned long words = bytes/sizeof(unsigned long)/2; unsigned long iwords = words/step; /* words per iteration */ unsigned long off, w, *l1, *l2; + uint64_t rseed = UINT64_C(0xd13133de9afdb566); /* Just a random seed. */ + uint64_t rout = 0; assert((bytes & 4095) == 0); for (off = 0; off < step; off++) { l1 = l+off; l2 = l1+words; for (w = 0; w < iwords; w++) { -#ifdef MEMTEST_32BIT - *l1 = *l2 = ((unsigned long) (rand()&0xffff)) | - (((unsigned long) (rand()&0xffff)) << 16); -#else - *l1 = *l2 = ((unsigned long) (rand()&0xffff)) | - (((unsigned long) (rand()&0xffff)) << 16) | - (((unsigned long) (rand()&0xffff)) << 32) | - (((unsigned long) (rand()&0xffff)) << 48); -#endif + xorshift64star_next(); + *l1 = *l2 = (unsigned long) rout; l1 += step; l2 += step; if ((w & 0xffff) == 0 && interactive) @@ -254,7 +260,75 @@ int memtest_test(unsigned long *m, size_t bytes, int passes, int interactive) { if (interactive) memtest_progress_end(); errors += memtest_compare_times(m,bytes,pass,4,interactive); } - free(m); + return errors; +} + +/* A version of memtest_test() that tests memory in small pieces + * in order to restore the memory content at exit. + * + * One problem we have with this approach, is that the cache can avoid + * real memory accesses, and we can't test big chunks of memory at the + * same time, because we need to backup them on the stack (the allocator + * may not be usable or we may be already in an out of memory condition). + * So what we do is to try to trash the cache with useless memory accesses + * between the fill and compare cycles. */ +#define MEMTEST_BACKUP_WORDS (1024*(1024/sizeof(long))) +/* Random accesses of MEMTEST_DECACHE_SIZE are performed at the start and + * end of the region between fill and compare cycles in order to trash + * the cache. */ +#define MEMTEST_DECACHE_SIZE (1024*8) +int memtest_preserving_test(unsigned long *m, size_t bytes, int passes) { + unsigned long backup[MEMTEST_BACKUP_WORDS]; + unsigned long *p = m; + unsigned long *end = (unsigned long*) (((unsigned char*)m)+(bytes-MEMTEST_DECACHE_SIZE)); + size_t left = bytes; + int errors = 0; + + if (bytes & 4095) return 0; /* Can't test across 4k page boundaries. */ + if (bytes < 4096*2) return 0; /* Can't test a single page. */ + + while(left) { + /* If we have to test a single final page, go back a single page + * so that we can test two pages, since the code can't test a single + * page but at least two. */ + if (left == 4096) { + left += 4096; + p -= 4096/sizeof(unsigned long); + } + + int pass = 0; + size_t len = (left > sizeof(backup)) ? sizeof(backup) : left; + + /* Always test an even number of pages. */ + if (len/4096 % 2) len -= 4096; + + memcpy(backup,p,len); /* Backup. */ + while(pass != passes) { + pass++; + errors += memtest_addressing(p,len,0); + memtest_fill_random(p,len,0); + if (bytes >= MEMTEST_DECACHE_SIZE) { + memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0); + memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0); + } + errors += memtest_compare_times(p,len,pass,4,0); + memtest_fill_value(p,len,0,(unsigned long)-1,'S',0); + if (bytes >= MEMTEST_DECACHE_SIZE) { + memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0); + memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0); + } + errors += memtest_compare_times(p,len,pass,4,0); + memtest_fill_value(p,len,ULONG_ONEZERO,ULONG_ZEROONE,'C',0); + if (bytes >= MEMTEST_DECACHE_SIZE) { + memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0); + memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0); + } + errors += memtest_compare_times(p,len,pass,4,0); + } + memcpy(p,backup,len); /* Restore. */ + left -= len; + p += len/sizeof(unsigned long); + } return errors; } @@ -272,32 +346,6 @@ void memtest_alloc_and_test(size_t megabytes, int passes) { free(m); } -void memtest_non_destructive_invert(void *addr, size_t size) { - volatile unsigned long *p = addr; - size_t words = size / sizeof(unsigned long); - size_t j; - - /* Invert */ - for (j = 0; j < words; j++) - p[j] = ~p[j]; -} - -void memtest_non_destructive_swap(void *addr, size_t size) { - volatile unsigned long *p = addr; - size_t words = size / sizeof(unsigned long); - size_t j; - - /* Swap */ - for (j = 0; j < words; j += 2) { - unsigned long a, b; - - a = p[j]; - b = p[j+1]; - p[j] = b; - p[j+1] = a; - } -} - void memtest(size_t megabytes, int passes) { if (ioctl(1, TIOCGWINSZ, &ws) == -1) { ws.ws_col = 80; |