Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
ae92e6dc
Commit
ae92e6dc
authored
Mar 16, 2012
by
antirez
Browse files
Memory test function now less boring thanks to screen-wide progress bar.
parent
e306e990
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/memtest.c
View file @
ae92e6dc
...
@@ -4,6 +4,8 @@
...
@@ -4,6 +4,8 @@
#include <assert.h>
#include <assert.h>
#include <limits.h>
#include <limits.h>
#include <errno.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#if (ULONG_MAX == 4294967295UL)
#if (ULONG_MAX == 4294967295UL)
#define MEMTEST_32BIT
#define MEMTEST_32BIT
...
@@ -13,6 +15,37 @@
...
@@ -13,6 +15,37 @@
#error "ULONG_MAX value not supported."
#error "ULONG_MAX value not supported."
#endif
#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
/* 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
* touch all the pages in the smallest amount of time reducing the
* effectiveness of caches, and making it hard for the OS to transfer
* 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) {
...
@@ -39,6 +72,8 @@ void memtest_fill(unsigned long *l, size_t bytes) {
#endif
#endif
l1
+=
step
;
l1
+=
step
;
l2
+=
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) {
...
@@ -58,13 +93,14 @@ void memtest_compare(unsigned long *l, size_t bytes) {
}
}
l1
++
;
l1
++
;
l2
++
;
l2
++
;
if
((
w
&
0xffff
)
==
0
)
memtest_progress_step
(
w
,
words
,
'='
);
}
}
}
}
void
memtest_test
(
size_t
megabytes
,
int
passes
)
{
void
memtest_test
(
size_t
megabytes
,
int
passes
)
{
size_t
bytes
=
megabytes
*
1024
*
1024
;
size_t
bytes
=
megabytes
*
1024
*
1024
;
unsigned
long
*
m
=
malloc
(
bytes
);
unsigned
long
*
m
=
malloc
(
bytes
);
int
pass
=
0
;
int
pass
=
0
,
j
;
if
(
m
==
NULL
)
{
if
(
m
==
NULL
)
{
fprintf
(
stderr
,
"Unable to allocate %zu megabytes: %s"
,
fprintf
(
stderr
,
"Unable to allocate %zu megabytes: %s"
,
...
@@ -73,15 +109,22 @@ void memtest_test(size_t megabytes, int passes) {
...
@@ -73,15 +109,22 @@ void memtest_test(size_t megabytes, int passes) {
}
}
while
(
pass
!=
passes
)
{
while
(
pass
!=
passes
)
{
pass
++
;
pass
++
;
printf
(
"PASS %d... "
,
pass
);
memtest_progress_start
(
"Random fill"
,
pass
);
fflush
(
stdout
);
memtest_fill
(
m
,
bytes
);
memtest_fill
(
m
,
bytes
);
memtest_compare
(
m
,
bytes
);
memtest_progress_end
();
printf
(
"ok
\n
"
);
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
)
{
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
);
memtest_test
(
megabytes
,
passes
);
printf
(
"
\n
Your memory passed this test.
\n
"
);
printf
(
"
\n
Your memory passed this test.
\n
"
);
printf
(
"Please if you are stil in doubt use the following two tools:
\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