memtest.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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.
 */
29 30 31 32
#ifdef _WIN32
#include "win32_Interop/win32_util.h"
#include "win32_Interop/win32_types.h"
#endif
33

A
antirez 已提交
34 35 36 37 38 39
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>
H
Henry Rawas 已提交
40
#ifndef _WIN32
41 42
#include <termios.h>
#include <sys/ioctl.h>
43
#if defined(__sun)
44
#include <stropts.h>
45
#endif
H
Henry Rawas 已提交
46
#else
47
#include "win32_Interop/win32fixes.h"
48
#include "win32_Interop/win32_ANSI.h"
H
Henry Rawas 已提交
49
#endif
A
antirez 已提交
50
#include "config.h"
A
antirez 已提交
51

52 53

#if (PORT_ULONG_MAX == 4294967295UL)
A
antirez 已提交
54
#define MEMTEST_32BIT
55
#elif (PORT_ULONG_MAX == 18446744073709551615ULL)
A
antirez 已提交
56 57
#define MEMTEST_64BIT
#else
58
#error "PORT_ULONG_MAX value not supported."
A
antirez 已提交
59 60
#endif

61 62 63
#ifdef MEMTEST_32BIT
#define ULONG_ONEZERO 0xaaaaaaaaUL
#define ULONG_ZEROONE 0x55555555UL
64 65 66
#else
#define ULONG_ONEZERO 0xaaaaaaaaaaaaaaaaUL
#define ULONG_ZEROONE 0x5555555555555555UL
67 68
#endif

H
Henry Rawas 已提交
69 70 71 72 73 74 75 76
#ifdef _WIN32
typedef struct winsize
{
    unsigned short ws_row;
    unsigned short ws_col;
};
#endif

77 78 79 80 81 82 83 84 85
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. */
86 87 88
    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/");
89 90 91
    printf("\x1b[H\x1b[2K");          /* Cursor home, clear current line.  */
    printf("%s [%d]\n", title, pass); /* Print title. */
    progress_printed = 0;
92
    progress_full = ws.ws_col*(ws.ws_row-3);
93 94 95 96 97 98 99 100
    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) {
101
    size_t chars = ((PORT_ULONGLONG)curr*progress_full)/size, j;
102

103 104
    for (j = 0; j < chars-progress_printed; j++) printf("%c",c);
    progress_printed = chars;
105 106 107
    fflush(stdout);
}

A
antirez 已提交
108 109 110
/* 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. */
111 112 113
void memtest_addressing(PORT_ULONG *l, size_t bytes) {
    PORT_ULONG words = (PORT_ULONG)(bytes / sizeof(PORT_ULONG));
    PORT_ULONG j,*p;
A
antirez 已提交
114 115 116 117

    /* Fill */
    p = l;
    for (j = 0; j < words; j++) {
118
        *p = (PORT_ULONG)p;
A
antirez 已提交
119 120 121 122 123 124
        p++;
        if ((j & 0xffff) == 0) memtest_progress_step(j,words*2,'A');
    }
    /* Test */
    p = l;
    for (j = 0; j < words; j++) {
125
        if (*p != (PORT_ULONG)p) {
A
antirez 已提交
126 127 128 129 130 131 132 133 134
            printf("\n*** MEMORY ADDRESSING ERROR: %p contains %lu\n",
                (void*) p, *p);
            exit(1);
        }
        p++;
        if ((j & 0xffff) == 0) memtest_progress_step(j+words,words*2,'A');
    }
}

A
antirez 已提交
135 136 137 138
/* 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. */
139 140 141 142 143
void memtest_fill_random(PORT_ULONG *l, size_t bytes) {
    PORT_ULONG step = (PORT_ULONG)(4096 / sizeof(PORT_ULONG));
    PORT_ULONG words = (PORT_ULONG)(bytes / sizeof(PORT_ULONG) / 2);
    PORT_ULONG iwords = words / step;  /* words per iteration */
    PORT_ULONG off,w,*l1,*l2;
A
antirez 已提交
144 145 146 147 148 149 150

    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
151 152
            *l1 = *l2 = ((PORT_ULONG)(rand() & 0xffff)) |
                        (((PORT_ULONG)    (rand()&0xffff)) << 16);
A
antirez 已提交
153
#else
154 155 156 157
            *l1 = *l2 = ((PORT_ULONG)     (rand()&0xffff)) |
                        (((PORT_ULONG)    (rand()&0xffff)) << 16) |
                        (((PORT_ULONG)    (rand()&0xffff)) << 32) |
                        (((PORT_ULONG)    (rand()&0xffff)) << 48);
A
antirez 已提交
158 159 160
#endif
            l1 += step;
            l2 += step;
161
            if ((w & 0xffff) == 0)
162 163 164 165 166 167 168
                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|...) */
169 170
void memtest_fill_value(PORT_ULONG *l,size_t bytes,PORT_ULONG v1,
                        PORT_ULONG v2, char sym)
171
{
172 173 174 175
    PORT_ULONG step = (PORT_ULONG)(4096/sizeof(PORT_ULONG));
    PORT_ULONG words = (PORT_ULONG)(bytes/sizeof(PORT_ULONG)/2);
    PORT_ULONG iwords = words/step;  /* words per iteration */
    PORT_ULONG off, w, *l1, *l2, v;
176 177 178 179 180 181 182 183

    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
184 185
            *l1 = *l2 = ((PORT_ULONG)     v) |
                        (((PORT_ULONG)    v) << 16);
186
#else
187 188 189 190
            *l1 = *l2 = ((PORT_ULONG)     v) |
                        (((PORT_ULONG)    v) << 16) |
                        (((PORT_ULONG)    v) << 32) |
                        (((PORT_ULONG)    v) << 48);
191 192 193 194 195
#endif
            l1 += step;
            l2 += step;
            if ((w & 0xffff) == 0)
                memtest_progress_step(w+iwords*off,words,sym);
A
antirez 已提交
196 197 198 199
        }
    }
}

200 201 202
void memtest_compare(PORT_ULONG *l, size_t bytes) {
    PORT_ULONG words = (PORT_ULONG)(bytes/sizeof(PORT_ULONG)/2);
    PORT_ULONG w, *l1, *l2;
A
antirez 已提交
203 204 205 206 207 208 209 210 211 212 213 214

    assert((bytes & 4095) == 0);
    l1 = l;
    l2 = l1+words;
    for (w = 0; w < words; w++) {
        if (*l1 != *l2) {
            printf("\n*** MEMORY ERROR DETECTED: %p != %p (%lu vs %lu)\n",
                (void*)l1, (void*)l2, *l1, *l2);
            exit(1);
        }
        l1 ++;
        l2 ++;
215
        if ((w & 0xffff) == 0) memtest_progress_step(w,words,'=');
A
antirez 已提交
216 217 218
    }
}

219
void memtest_compare_times(PORT_ULONG *m, size_t bytes, int pass, int times) {
220 221 222 223 224 225 226 227 228
    int j;

    for (j = 0; j < times; j++) {
        memtest_progress_start("Compare",pass);
        memtest_compare(m,bytes);
        memtest_progress_end();
    }
}

A
antirez 已提交
229
void memtest_test(size_t megabytes, int passes) {
230 231
    size_t bytes = megabytes * 1024 * 1024;
    PORT_ULONG *m = malloc(bytes);
232
    int pass = 0;
A
antirez 已提交
233 234

    if (m == NULL) {
235 236
        fprintf(stderr,"Unable to allocate %Iu megabytes: %s",                  WIN_PORT_FIX /* %zu -> %Iu */
            megabytes,strerror(errno));
A
antirez 已提交
237 238 239 240
        exit(1);
    }
    while (pass != passes) {
        pass++;
A
antirez 已提交
241 242 243 244 245

        memtest_progress_start("Addressing test",pass);
        memtest_addressing(m,bytes);
        memtest_progress_end();

246
        memtest_progress_start("Random fill",pass);
247
        memtest_fill_random(m,bytes);
248
        memtest_progress_end();
249 250 251
        memtest_compare_times(m,bytes,pass,4);

        memtest_progress_start("Solid fill",pass);
252
        memtest_fill_value(m,bytes,0,(PORT_ULONG)-1,'S');
253 254 255 256 257 258 259
        memtest_progress_end();
        memtest_compare_times(m,bytes,pass,4);

        memtest_progress_start("Checkerboard fill",pass);
        memtest_fill_value(m,bytes,ULONG_ONEZERO,ULONG_ZEROONE,'C');
        memtest_progress_end();
        memtest_compare_times(m,bytes,pass,4);
A
antirez 已提交
260
    }
X
Xian Li 已提交
261
    free(m);
A
antirez 已提交
262 263
}

264
void memtest_non_destructive_invert(void *addr, size_t size) {
265 266
    volatile PORT_ULONG *p = addr;
    size_t words = size / sizeof(PORT_ULONG);
A
antirez 已提交
267 268
    size_t j;

269 270 271 272
    /* Invert */
    for (j = 0; j < words; j++)
        p[j] = ~p[j];
}
A
antirez 已提交
273

274
void memtest_non_destructive_swap(void *addr, size_t size) {
275 276
    volatile PORT_ULONG *p = addr;
    size_t words = size / sizeof(PORT_ULONG);
277
    size_t j;
A
antirez 已提交
278

279 280
    /* Swap */
    for (j = 0; j < words; j += 2) {
281
        PORT_ULONG a,b;
282 283 284 285 286 287

        a = p[j];
        b = p[j+1];
        p[j] = b;
        p[j+1] = a;
    }
A
antirez 已提交
288 289
}

A
antirez 已提交
290
void memtest(size_t megabytes, int passes) {
H
Henry Rawas 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303
#ifdef _WIN32
    HANDLE hOut;
    CONSOLE_SCREEN_BUFFER_INFO b;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleScreenBufferInfo(hOut, &b)) {
        ws.ws_col = b.dwSize.X;
        ws.ws_row = b.dwSize.Y;
    } else {
        ws.ws_col = 80;
        ws.ws_row = 20;
    }
#else
304 305 306 307
    if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
        ws.ws_col = 80;
        ws.ws_row = 20;
    }
H
Henry Rawas 已提交
308
#endif
A
antirez 已提交
309 310
    memtest_test(megabytes,passes);
    printf("\nYour memory passed this test.\n");
A
antirez 已提交
311
    printf("Please if you are still in doubt use the following two tools:\n");
A
antirez 已提交
312 313 314 315
    printf("1) memtest86: http://www.memtest86.com/\n");
    printf("2) memtester: http://pyropus.ca/software/memtester/\n");
    exit(0);
}