Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
d605fdab
Commit
d605fdab
authored
Mar 16, 2012
by
antirez
Browse files
Memory test function now less boring thanks to screen-wide progress bar.
parent
f4df22d1
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/memtest.c
View file @
d605fdab
...
...
@@ -4,6 +4,8 @@
#include <assert.h>
#include <limits.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#if (ULONG_MAX == 4294967295UL)
#define MEMTEST_32BIT
...
...
@@ -13,6 +15,37 @@
#error "ULONG_MAX value not supported."
#endif
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. */
for
(
j
=
0
;
j
<
ws
.
ws_col
*
ws
.
ws_row
;
j
++
)
printf
(
"."
);
printf
(
"
\x1b
[H
\x1b
[2K"
);
/* Cursor home, clear current line. */
printf
(
"%s [%d]
\n
"
,
title
,
pass
);
/* Print title. */
progress_printed
=
0
;
progress_full
=
ws
.
ws_col
*
(
ws
.
ws_row
-
1
);
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
)
{
size_t
chars
=
(
curr
*
progress_full
)
/
size
,
j
;
for
(
j
=
0
;
j
<
chars
-
progress_printed
;
j
++
)
{
printf
(
"%c"
,
c
);
progress_printed
++
;
}
fflush
(
stdout
);
}
/* 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
...
...
@@ -39,6 +72,8 @@ void memtest_fill(unsigned long *l, size_t bytes) {
#endif
l1
+=
step
;
l2
+=
step
;
if
((
w
&
0xffff
)
==
0
)
memtest_progress_step
(
w
+
iwords
*
off
,
words
,
'+'
);
}
}
}
...
...
@@ -58,13 +93,14 @@ void memtest_compare(unsigned long *l, size_t bytes) {
}
l1
++
;
l2
++
;
if
((
w
&
0xffff
)
==
0
)
memtest_progress_step
(
w
,
words
,
'='
);
}
}
void
memtest_test
(
size_t
megabytes
,
int
passes
)
{
size_t
bytes
=
megabytes
*
1024
*
1024
;
unsigned
long
*
m
=
malloc
(
bytes
);
int
pass
=
0
;
int
pass
=
0
,
j
;
if
(
m
==
NULL
)
{
fprintf
(
stderr
,
"Unable to allocate %zu megabytes: %s"
,
...
...
@@ -73,15 +109,22 @@ void memtest_test(size_t megabytes, int passes) {
}
while
(
pass
!=
passes
)
{
pass
++
;
printf
(
"PASS %d... "
,
pass
);
fflush
(
stdout
);
memtest_progress_start
(
"Random fill"
,
pass
);
memtest_fill
(
m
,
bytes
);
memtest_compare
(
m
,
bytes
);
printf
(
"ok
\n
"
);
memtest_progress_end
();
for
(
j
=
0
;
j
<
4
;
j
++
)
{
memtest_progress_start
(
"Compare"
,
pass
);
memtest_compare
(
m
,
bytes
);
memtest_progress_end
();
}
}
}
void
memtest
(
size_t
megabytes
,
int
passes
)
{
if
(
ioctl
(
1
,
TIOCGWINSZ
,
&
ws
)
==
-
1
)
{
ws
.
ws_col
=
80
;
ws
.
ws_row
=
20
;
}
memtest_test
(
megabytes
,
passes
);
printf
(
"
\n
Your memory passed this test.
\n
"
);
printf
(
"Please if you are stil in doubt use the following two tools:
\n
"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment