2012-11-08 12:25:23 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-03-16 12:21:49 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2012-03-16 16:19:53 -04:00
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/ioctl.h>
|
2014-12-09 06:59:36 -05:00
|
|
|
#if defined(__sun)
|
2014-12-01 17:22:03 -05:00
|
|
|
#include <stropts.h>
|
2014-12-09 06:59:36 -05:00
|
|
|
#endif
|
2012-11-21 07:19:38 -05:00
|
|
|
#include "config.h"
|
2012-03-16 12:21:49 -04:00
|
|
|
|
|
|
|
#if (ULONG_MAX == 4294967295UL)
|
|
|
|
#define MEMTEST_32BIT
|
|
|
|
#elif (ULONG_MAX == 18446744073709551615ULL)
|
|
|
|
#define MEMTEST_64BIT
|
|
|
|
#else
|
|
|
|
#error "ULONG_MAX value not supported."
|
|
|
|
#endif
|
|
|
|
|
2012-03-18 13:03:27 -04:00
|
|
|
#ifdef MEMTEST_32BIT
|
|
|
|
#define ULONG_ONEZERO 0xaaaaaaaaUL
|
|
|
|
#define ULONG_ZEROONE 0x55555555UL
|
2012-04-11 05:32:22 -04:00
|
|
|
#else
|
|
|
|
#define ULONG_ONEZERO 0xaaaaaaaaaaaaaaaaUL
|
|
|
|
#define ULONG_ZEROONE 0x5555555555555555UL
|
2012-03-18 13:03:27 -04:00
|
|
|
#endif
|
|
|
|
|
2012-03-16 16:19:53 -04:00
|
|
|
static struct winsize ws;
|
|
|
|
size_t progress_printed; /* Printed chars in screen-wide progress bar. */
|
|
|
|
size_t progress_full; /* How many chars to write to fill the progress bar. */
|
|
|
|
|
|
|
|
void memtest_progress_start(char *title, int pass) {
|
|
|
|
int j;
|
|
|
|
|
|
|
|
printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
|
|
|
|
/* Fill with dots. */
|
2012-03-18 12:24:48 -04:00
|
|
|
for (j = 0; j < ws.ws_col*(ws.ws_row-2); j++) printf(".");
|
|
|
|
printf("Please keep the test running several minutes per GB of memory.\n");
|
|
|
|
printf("Also check http://www.memtest86.com/ and http://pyropus.ca/software/memtester/");
|
2012-03-16 16:19:53 -04:00
|
|
|
printf("\x1b[H\x1b[2K"); /* Cursor home, clear current line. */
|
|
|
|
printf("%s [%d]\n", title, pass); /* Print title. */
|
|
|
|
progress_printed = 0;
|
2012-03-18 12:24:48 -04:00
|
|
|
progress_full = ws.ws_col*(ws.ws_row-3);
|
2012-03-16 16:19:53 -04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void memtest_progress_end(void) {
|
|
|
|
printf("\x1b[H\x1b[2J"); /* Cursor home, clear screen. */
|
|
|
|
}
|
|
|
|
|
|
|
|
void memtest_progress_step(size_t curr, size_t size, char c) {
|
2012-04-12 05:49:52 -04:00
|
|
|
size_t chars = ((unsigned long long)curr*progress_full)/size, j;
|
2012-03-16 16:19:53 -04:00
|
|
|
|
2013-01-21 06:34:22 -05:00
|
|
|
for (j = 0; j < chars-progress_printed; j++) printf("%c",c);
|
|
|
|
progress_printed = chars;
|
2012-03-16 16:19:53 -04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2012-03-19 09:02:34 -04:00
|
|
|
/* Test that addressing is fine. Every location is populated with its own
|
|
|
|
* address, and finally verified. This test is very fast but may detect
|
|
|
|
* ASAP big issues with the memory subsystem. */
|
2015-12-16 06:31:42 -05:00
|
|
|
int memtest_addressing(unsigned long *l, size_t bytes, int interactive) {
|
2012-03-19 09:02:34 -04:00
|
|
|
unsigned long words = bytes/sizeof(unsigned long);
|
|
|
|
unsigned long j, *p;
|
|
|
|
|
|
|
|
/* Fill */
|
|
|
|
p = l;
|
|
|
|
for (j = 0; j < words; j++) {
|
|
|
|
*p = (unsigned long)p;
|
|
|
|
p++;
|
2015-12-16 06:31:42 -05:00
|
|
|
if ((j & 0xffff) == 0 && interactive)
|
|
|
|
memtest_progress_step(j,words*2,'A');
|
2012-03-19 09:02:34 -04:00
|
|
|
}
|
|
|
|
/* Test */
|
|
|
|
p = l;
|
|
|
|
for (j = 0; j < words; j++) {
|
|
|
|
if (*p != (unsigned long)p) {
|
2015-12-16 06:31:42 -05:00
|
|
|
if (interactive) {
|
|
|
|
printf("\n*** MEMORY ADDRESSING ERROR: %p contains %lu\n",
|
|
|
|
(void*) p, *p);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return 1;
|
2012-03-19 09:02:34 -04:00
|
|
|
}
|
|
|
|
p++;
|
2015-12-16 06:31:42 -05:00
|
|
|
if ((j & 0xffff) == 0 && interactive)
|
|
|
|
memtest_progress_step(j+words,words*2,'A');
|
2012-03-19 09:02:34 -04:00
|
|
|
}
|
2015-12-16 06:31:42 -05:00
|
|
|
return 0;
|
2012-03-19 09:02:34 -04:00
|
|
|
}
|
|
|
|
|
2012-03-16 12:21:49 -04:00
|
|
|
/* 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. */
|
2015-12-16 06:31:42 -05:00
|
|
|
void memtest_fill_random(unsigned long *l, size_t bytes, int interactive) {
|
2012-03-16 12:21:49 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
l1 += step;
|
|
|
|
l2 += step;
|
2015-12-16 06:31:42 -05:00
|
|
|
if ((w & 0xffff) == 0 && interactive)
|
2012-03-18 13:03:27 -04:00
|
|
|
memtest_progress_step(w+iwords*off,words,'R');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like memtest_fill_random() but uses the two specified values to fill
|
|
|
|
* memory, in an alternated way (v1|v2|v1|v2|...) */
|
|
|
|
void memtest_fill_value(unsigned long *l, size_t bytes, unsigned long v1,
|
2015-12-16 06:31:42 -05:00
|
|
|
unsigned long v2, char sym, int interactive)
|
2012-03-18 13:03:27 -04:00
|
|
|
{
|
|
|
|
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, v;
|
|
|
|
|
|
|
|
assert((bytes & 4095) == 0);
|
|
|
|
for (off = 0; off < step; off++) {
|
|
|
|
l1 = l+off;
|
|
|
|
l2 = l1+words;
|
|
|
|
v = (off & 1) ? v2 : v1;
|
|
|
|
for (w = 0; w < iwords; w++) {
|
|
|
|
#ifdef MEMTEST_32BIT
|
2012-04-27 10:28:31 -04:00
|
|
|
*l1 = *l2 = ((unsigned long) v) |
|
|
|
|
(((unsigned long) v) << 16);
|
2012-03-18 13:03:27 -04:00
|
|
|
#else
|
2012-04-27 10:28:31 -04:00
|
|
|
*l1 = *l2 = ((unsigned long) v) |
|
|
|
|
(((unsigned long) v) << 16) |
|
|
|
|
(((unsigned long) v) << 32) |
|
|
|
|
(((unsigned long) v) << 48);
|
2012-03-18 13:03:27 -04:00
|
|
|
#endif
|
|
|
|
l1 += step;
|
|
|
|
l2 += step;
|
2015-12-16 06:31:42 -05:00
|
|
|
if ((w & 0xffff) == 0 && interactive)
|
2012-03-18 13:03:27 -04:00
|
|
|
memtest_progress_step(w+iwords*off,words,sym);
|
2012-03-16 12:21:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 06:31:42 -05:00
|
|
|
int memtest_compare(unsigned long *l, size_t bytes, int interactive) {
|
2012-03-16 12:21:49 -04:00
|
|
|
unsigned long words = bytes/sizeof(unsigned long)/2;
|
|
|
|
unsigned long w, *l1, *l2;
|
|
|
|
|
|
|
|
assert((bytes & 4095) == 0);
|
|
|
|
l1 = l;
|
|
|
|
l2 = l1+words;
|
|
|
|
for (w = 0; w < words; w++) {
|
|
|
|
if (*l1 != *l2) {
|
2015-12-16 06:31:42 -05:00
|
|
|
if (interactive) {
|
|
|
|
printf("\n*** MEMORY ERROR DETECTED: %p != %p (%lu vs %lu)\n",
|
|
|
|
(void*)l1, (void*)l2, *l1, *l2);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return 1;
|
2012-03-16 12:21:49 -04:00
|
|
|
}
|
|
|
|
l1 ++;
|
|
|
|
l2 ++;
|
2015-12-16 06:31:42 -05:00
|
|
|
if ((w & 0xffff) == 0 && interactive)
|
|
|
|
memtest_progress_step(w,words,'=');
|
2012-03-16 12:21:49 -04:00
|
|
|
}
|
2015-12-16 06:31:42 -05:00
|
|
|
return 0;
|
2012-03-16 12:21:49 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 06:31:42 -05:00
|
|
|
int memtest_compare_times(unsigned long *m, size_t bytes, int pass, int times,
|
|
|
|
int interactive)
|
|
|
|
{
|
2012-03-18 13:03:27 -04:00
|
|
|
int j;
|
2015-12-16 06:31:42 -05:00
|
|
|
int errors = 0;
|
2012-03-18 13:03:27 -04:00
|
|
|
|
|
|
|
for (j = 0; j < times; j++) {
|
2015-12-16 06:31:42 -05:00
|
|
|
if (interactive) memtest_progress_start("Compare",pass);
|
|
|
|
errors += memtest_compare(m,bytes,interactive);
|
|
|
|
if (interactive) memtest_progress_end();
|
2012-03-18 13:03:27 -04:00
|
|
|
}
|
2015-12-16 06:31:42 -05:00
|
|
|
return errors;
|
2012-03-18 13:03:27 -04:00
|
|
|
}
|
|
|
|
|
2015-12-16 06:31:42 -05:00
|
|
|
/* Test the specified memory. The number of bytes must be multiple of 4096.
|
|
|
|
* If interactive is true the program exists with an error and prints
|
|
|
|
* ASCII arts to show progresses. Instead when interactive is 0, it can
|
|
|
|
* be used as an API call, and returns 1 if memory errors were found or
|
|
|
|
* 0 if there were no errors detected. */
|
|
|
|
int memtest_test(unsigned long *m, size_t bytes, int passes, int interactive) {
|
2012-03-18 13:03:27 -04:00
|
|
|
int pass = 0;
|
2015-12-16 06:31:42 -05:00
|
|
|
int errors = 0;
|
2012-03-16 12:21:49 -04:00
|
|
|
|
|
|
|
while (pass != passes) {
|
|
|
|
pass++;
|
2012-03-19 09:02:34 -04:00
|
|
|
|
2015-12-16 06:31:42 -05:00
|
|
|
if (interactive) memtest_progress_start("Addressing test",pass);
|
|
|
|
errors += memtest_addressing(m,bytes,interactive);
|
|
|
|
if (interactive) memtest_progress_end();
|
2012-03-19 09:02:34 -04:00
|
|
|
|
2015-12-16 06:31:42 -05:00
|
|
|
if (interactive) memtest_progress_start("Random fill",pass);
|
|
|
|
memtest_fill_random(m,bytes,interactive);
|
|
|
|
if (interactive) memtest_progress_end();
|
|
|
|
errors += memtest_compare_times(m,bytes,pass,4,interactive);
|
2012-03-18 13:03:27 -04:00
|
|
|
|
2015-12-16 06:31:42 -05:00
|
|
|
if (interactive) memtest_progress_start("Solid fill",pass);
|
|
|
|
memtest_fill_value(m,bytes,0,(unsigned long)-1,'S',interactive);
|
|
|
|
if (interactive) memtest_progress_end();
|
|
|
|
errors += memtest_compare_times(m,bytes,pass,4,interactive);
|
2012-03-18 13:03:27 -04:00
|
|
|
|
2015-12-16 06:31:42 -05:00
|
|
|
if (interactive) memtest_progress_start("Checkerboard fill",pass);
|
|
|
|
memtest_fill_value(m,bytes,ULONG_ONEZERO,ULONG_ZEROONE,'C',interactive);
|
|
|
|
if (interactive) memtest_progress_end();
|
|
|
|
errors += memtest_compare_times(m,bytes,pass,4,interactive);
|
|
|
|
}
|
|
|
|
free(m);
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform an interactive test allocating the specified number of megabytes. */
|
|
|
|
void memtest_alloc_and_test(size_t megabytes, int passes) {
|
|
|
|
size_t bytes = megabytes*1024*1024;
|
|
|
|
unsigned long *m = malloc(bytes);
|
|
|
|
|
|
|
|
if (m == NULL) {
|
|
|
|
fprintf(stderr,"Unable to allocate %zu megabytes: %s",
|
|
|
|
megabytes, strerror(errno));
|
|
|
|
exit(1);
|
2012-03-16 12:21:49 -04:00
|
|
|
}
|
2015-12-16 06:31:42 -05:00
|
|
|
memtest_test(m,bytes,passes,1);
|
2014-03-17 23:37:08 -04:00
|
|
|
free(m);
|
2012-03-16 12:21:49 -04:00
|
|
|
}
|
|
|
|
|
2012-11-25 10:21:21 -05:00
|
|
|
void memtest_non_destructive_invert(void *addr, size_t size) {
|
2012-11-21 07:19:38 -05:00
|
|
|
volatile unsigned long *p = addr;
|
2012-11-25 10:21:21 -05:00
|
|
|
size_t words = size / sizeof(unsigned long);
|
2012-11-21 07:19:38 -05:00
|
|
|
size_t j;
|
|
|
|
|
2012-11-25 10:21:21 -05:00
|
|
|
/* Invert */
|
|
|
|
for (j = 0; j < words; j++)
|
|
|
|
p[j] = ~p[j];
|
|
|
|
}
|
2012-11-21 07:19:38 -05:00
|
|
|
|
2012-11-25 10:21:21 -05:00
|
|
|
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;
|
2012-11-21 07:19:38 -05:00
|
|
|
|
2012-11-25 10:21:21 -05:00
|
|
|
/* 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;
|
|
|
|
}
|
2012-11-21 07:19:38 -05:00
|
|
|
}
|
|
|
|
|
2012-03-16 12:21:49 -04:00
|
|
|
void memtest(size_t megabytes, int passes) {
|
2012-03-16 16:19:53 -04:00
|
|
|
if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
|
|
|
|
ws.ws_col = 80;
|
|
|
|
ws.ws_row = 20;
|
|
|
|
}
|
2015-12-16 06:31:42 -05:00
|
|
|
memtest_alloc_and_test(megabytes,passes);
|
2012-03-16 12:21:49 -04:00
|
|
|
printf("\nYour memory passed this test.\n");
|
2012-03-18 12:27:56 -04:00
|
|
|
printf("Please if you are still in doubt use the following two tools:\n");
|
2012-03-16 12:21:49 -04:00
|
|
|
printf("1) memtest86: http://www.memtest86.com/\n");
|
|
|
|
printf("2) memtester: http://pyropus.ca/software/memtester/\n");
|
|
|
|
exit(0);
|
|
|
|
}
|