Skip to contentSkip to conversation

nandtest: use seed argument

PATCH Applied Jan Weitzel 3 participants 10 messages Nov 24, 2011 – Dec 5, 2011 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nandtest: use seed argument

if a seed is provided it is actually not used. First call is "seed = rand()" killing the given seed.

---
 nandtest.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
nandtest.c +1 -0
index db7f427..b3aacaf 100644
@@ -173,6 +173,7 @@ int main(int argc, char **argv)
 
 		case 's':
 			seed = atol(optarg);
+			srand(seed);
 			break;
 
 		case 'p':
Signature · 1 line
1.7.0.4
Tested-by on v1 1/1
#1 lore raw
Quoted 4 lines · Jan Weitzel

if a seed is provided it is actually not used. First call is "seed = rand()" killing the given seed.

You might also note that when a seed isn't provided, nandtest does not produce random results because it just uses the default seed. At least for my system, I see the same results every execution; I believe this is the general C rand() behavior. I'll send a follow-up patch that actually utilizes <time.h> to use time as a random seed.

Brian

#2 lore raw

PATCH 1/1 nandtest: seed random generator properly

This patch fixes two problems in nandtest:

(1) if a seed is provided it is actually not used. First call is "seed = rand()" killing the given seed.
Credit: Jan Weitzel <[email protected]>

(2) if a seed is not provided, we use the default rand() values, which produces the same sequence of values every run. It makes more sense to seed with the time to produce more random sequences.

---
Jan: This is an amendment to your patch. Feel free to add a "Signed-off-by" if this works for you.

 nandtest.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
nandtest.c +5 -1
index dc28d09..8cdc816 100644
@@ -35,7 +35,7 @@ struct mtd_info_user meminfo;
 struct mtd_ecc_stats oldstats, newstats;
 int fd;
 int markbad=0;
-int seed;
+int seed = -1;
 
 int erase_and_write(loff_t ofs, unsigned char *data, unsigned char *rbuf)
 {
@@ -192,6 +192,10 @@ int main(int argc, char **argv)
 	if (argc - optind != 1)
 		usage();
 
+	if (seed < 0)
+		seed = time(NULL);
+	srand(seed);
+
 	fd = open(argv[optind], O_RDWR);
 	if (fd < 0) {
 		perror("open");
Signature · 1 line
1.7.5.4
#3 lore raw

Antwort: [PATCH] nandtest: seed random generator properly

Brian Norris <[email protected]> schrieb am 28.11.2011 19:11:52:

Quoted 31 lines

Von: Brian Norris <[email protected]>
An: <[email protected]>
Kopie: Brian Norris <[email protected]>, Artem Bityutskiy <[email protected]>, Jan Weitzel <[email protected]>
Datum: 28.11.2011 19:13
Betreff: [PATCH] nandtest: seed random generator properly

This patch fixes two problems in nandtest:

(1) if a seed is provided it is actually not used. First call is "seed = rand()" killing the given seed.
Credit: Jan Weitzel <[email protected]>

(2) if a seed is not provided, we use the default rand() values, which produces the same sequence of values every run. It makes more sense to seed with the time to produce more random sequences.

---
Jan: This is an amendment to your patch. Feel free to add a "Signed-off-by" if this works for you.

 nandtest.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
nandtest.c +0 -0
index dc28d09..8cdc816 100644

nandtest.c struct mtd_info_user meminfo; · line 40

Quoted 7 lines
nandtest.c +1 -1
@@ -35,7 +35,7 @@ struct mtd_info_user meminfo;
 struct mtd_ecc_stats oldstats, newstats;
 int fd;
 int markbad=0;
-int seed;
+int seed = -1;
 
 int erase_and_write(loff_t ofs, unsigned char *data, unsigned char 

*rbuf)

nandtest.c int main(int argc, char **argv) · line 198

Quoted 9 lines
nandtest.c +4 -0
 {
@@ -192,6 +192,10 @@ int main(int argc, char **argv)
    if (argc - optind != 1)
       usage();
 
+   if (seed < 0)
+      seed = time(NULL);
+   srand(seed);
+

So you loose all negative seeds. What is about

int seed = time(NULL);
...
case 's'

        seed = atol(optarg);

...
}
srand(seed);

Jan

Quoted 6 lines
nandtest.c +0 -0
    fd = open(argv[optind], O_RDWR);
    if (fd < 0) {
       perror("open");
Signature · 1 line
1.7.5.4
#4 lore raw

Re: [PATCH] nandtest: seed random generator properly

Quoted 6 lines · Jan Weitzel

Brian Norris <[email protected]> schrieb am 28.11.2011 19:11:52:

Quoted 3 lines

+   if (seed < 0)
+      seed = time(NULL);
+   srand(seed);

So you loose all negative seeds.

Well, srand() technically takes unsigned ints, so all seeds are technically positive. But that just means we're parsing and storing seeds wrong (not a big deal, really).

Quoted 9 lines

What is about

int seed = time(NULL);
...
case 's'
       seed = atol(optarg);
...
}
srand(seed);

Not sure if I understand the question perfectly (clarify if I'm wrong). My patch was intended to:
(1) use the user-supplied seed (--seed=X) if supplied
(2) use a random seed (based on time()) if the seed is left uninitialized (i.e., -1)

Anyway, I see a problem with my method. How about the following patch, which saves the time first, then overwrites it with the supplied seed if provided.

Quoted 1 line

From 4df0643c4819f5de5db54b855406852da0dc6bf1 Mon Sep 17 00:00:00 2001

From: Brian Norris <[email protected]>
Date: Mon, 28 Nov 2011 10:03:30 -0800
Subject: [PATCH] nandtest: seed random generator properly

This patch fixes two problems in nandtest:

* if a seed is provided it is actually not used. First call is "seed = rand()" killing the given seed.
Credit: Jan Weitzel <[email protected]>

* if a seed is not provided, we use the default rand() values, which produces the same sequence of values every run.r

To solve these problems, we seed the random number generator with either the time() or the supplied seed.

---
 nandtest.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
nandtest.c +4 -0
index dc28d09..7811be2 100644
@@ -140,6 +140,8 @@ int main(int argc, char **argv)
        uint32_t offset = 0;
        uint32_t length = -1;
 
+       seed = time(NULL);
+
        for (;;) {
                static const char *short_options="hkl:mo:p:s:";
                static const struct option long_options[] = {
@@ -243,6 +245,8 @@ int main(int argc, char **argv)
        printf("Bad blocks     : %d\n", oldstats.badblocks);
        printf("BBT blocks     : %d\n", oldstats.bbtblocks);
 
+       srand(seed);
+
        for (pass = 0; pass < nr_passes; pass++) {
                loff_t test_ofs;
 
Signature · 1 line
1.7.5.4
author Acked-by
#5 lore raw

Re: [PATCH] nandtest: seed random generator properly

In reply to Brian Norris

Am Mittwoch, den 30.11.2011, 10:24 -0800 schrieb Brian Norris:

Quoted 28 lines · Jan Weitzel
Quoted 6 lines

Brian Norris <[email protected]> schrieb am 28.11.2011 19:11:52:

Review

Quoted 3 lines
(quoted diff) +3 -0
+   if (seed < 0)
+      seed = time(NULL);
+   srand(seed);

So you loose all negative seeds.

Well, srand() technically takes unsigned ints, so all seeds are technically positive. But that just means we're parsing and storing seeds wrong (not a big deal, really).

Quoted 9 lines

What is about

int seed = time(NULL);
...
case 's'

       seed = atol(optarg);

...
}
srand(seed);

Not sure if I understand the question perfectly (clarify if I'm wrong). My patch was intended to:
(1) use the user-supplied seed (--seed=X) if supplied
(2) use a random seed (based on time()) if the seed is left uninitialized (i.e., -1)

Quoted 3 lines

Anyway, I see a problem with my method. How about the following patch, which saves the time first, then overwrites it with the supplied seed if provided.

Maybe you need to rebase it, because my former patch just got pushed.

Quoted 45 lines

From 4df0643c4819f5de5db54b855406852da0dc6bf1 Mon Sep 17 00:00:00 2001
From: Brian Norris <[email protected]>
Date: Mon, 28 Nov 2011 10:03:30 -0800
Subject: [PATCH] nandtest: seed random generator properly

This patch fixes two problems in nandtest:

* if a seed is provided it is actually not used. First call is "seed = rand()" killing the given seed.
Credit: Jan Weitzel <[email protected]>

* if a seed is not provided, we use the default rand() values, which produces the same sequence of values every run.r

To solve these problems, we seed the random number generator with either the time() or the supplied seed.

---
 nandtest.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
nandtest.c +4 -0
index dc28d09..7811be2 100644
@@ -140,6 +140,8 @@ int main(int argc, char **argv)
        uint32_t offset = 0;
        uint32_t length = -1;
 
+       seed = time(NULL);
+
        for (;;) {
                static const char *short_options="hkl:mo:p:s:";
                static const struct option long_options[] = {
@@ -243,6 +245,8 @@ int main(int argc, char **argv)
        printf("Bad blocks     : %d\n", oldstats.badblocks);
        printf("BBT blocks     : %d\n", oldstats.bbtblocks);
 
+       srand(seed);
+
        for (pass = 0; pass < nr_passes; pass++) {
                loff_t test_ofs;
 
#6 lore raw

Re: [PATCH] nandtest: seed random generator properly

In reply to Brian Norris
Quoted 11 lines · Brian Norris
Quoted 6 lines · Jan Weitzel

Brian Norris <[email protected]> schrieb am 28.11.2011 19:11:52:

Review

Quoted 3 lines
(quoted diff) +3 -0
+   if (seed < 0)
+      seed = time(NULL);
+   srand(seed);

So you loose all negative seeds.

Well, srand() technically takes unsigned ints, so all seeds are technically positive. But that just means we're parsing and storing seeds wrong (not a big deal, really).

Brian, would you please send an incremental patch on top of Jan's which I've pushed?

Artem.

#7 lore raw

PATCH v2 1/1 nandtest: seed random generator with time

If a seed is not provided via --seed, we use the default rand() values, which produces the same sequence of values every run. Since this is undesirable, we should choose a random seed via the current time().

Note that this patch moves the srand() until after all the initial options processing.

---
rebased on top of Jan's patch

 nandtest.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
nandtest.c +4 -1
index b3aacaf..0187b87 100644
@@ -140,6 +140,8 @@ int main(int argc, char **argv)
 	uint32_t offset = 0;
 	uint32_t length = -1;
 
+	seed = time(NULL);
+
 	for (;;) {
 		static const char *short_options="hkl:mo:p:s:";
 		static const struct option long_options[] = {
@@ -173,7 +175,6 @@ int main(int argc, char **argv)
 
 		case 's':
 			seed = atol(optarg);
-			srand(seed);
 			break;
 
 		case 'p':
@@ -244,6 +245,8 @@ int main(int argc, char **argv)
 	printf("Bad blocks     : %d\n", oldstats.badblocks);
 	printf("BBT blocks     : %d\n", oldstats.bbtblocks);
 
+	srand(seed);
+
 	for (pass = 0; pass < nr_passes; pass++) {
 		loff_t test_ofs;
 
Signature · 1 line
1.7.5.4
#8 lore raw

Re: [PATCH v2] nandtest: seed random generator with time

Quoted 6 lines · Brian Norris

If a seed is not provided via --seed, we use the default rand() values, which produces the same sequence of values every run. Since this is undesirable, we should choose a random seed via the current time().

Note that this patch moves the srand() until after all the initial options processing.

Pushed, thanks!

Artem.