Skip to contentSkip to conversation

fzsync: don't compare spins_avg (float) against 1 (int)

PATCH New Jan Stancek 2 participants 6 messages Sep 4, 2019 – Sep 7, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fzsync: don't compare spins_avg (float) against 1 (int)

tst_fzsync_pair.spins is increased at least by one on every iteration. If during all calibration loops, both A and B manage to complete at nearly same time, then spins is increased only by one.

spins_avg starts as 0, and tst_exp_moving_avg() will move it closer and closer to 1, but it will never be 1:

        float f = 0; int i;
        for (i = 0; i < 4096; i++)
                f = tst_exp_moving_avg(0.25f, 1, f);
        printf("%.15f %d\n", f, f >= 1);
	$ 0.999999880790710 0

which on rare occasion can cause: "Can't calculate random delay".

Compare against float slightly smaller than 1. Also print stats one more time when we hit the warning.

---
 include/tst_fuzzy_sync.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/tst_fuzzy_sync.h +4 -1
index de0402c9bbe9..2c0389c5d63e 100644
@@ -73,6 +73,8 @@
 /* how much of exec time is sampling allowed to take */
 #define SAMPLING_SLICE 0.5f
 
+#define EPSILON 0.999f
+
 /** Some statistics for a variable */
 struct tst_fzsync_stat {
 	float avg;
@@ -477,7 +479,7 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 			tst_res(TINFO, "Minimum sampling period ended");
 			tst_fzsync_pair_info(pair);
 		}
-	} else if (fabsf(pair->diff_ab.avg) >= 1 && pair->spins_avg.avg >= 1) {
+	} else if (fabsf(pair->diff_ab.avg) > EPSILON && pair->spins_avg.avg > EPSILON) {
 		per_spin_time = fabsf(pair->diff_ab.avg) / pair->spins_avg.avg;
 		time_delay = drand48() * (pair->diff_sa.avg + pair->diff_sb.avg)
 			- pair->diff_sb.avg;
@@ -495,6 +497,7 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 		}
 	} else if (!pair->sampling) {
 		tst_res(TWARN, "Can't calculate random delay");
+		tst_fzsync_pair_info(pair);
 		pair->sampling = -1;
 	}
 
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 fzsync: don't compare spins_avg (float) against 1 (int)

Hello,

Quoted 6 lines · Jan Stancek

tst_fzsync_pair.spins is increased at least by one on every iteration. If during all calibration loops, both A and B manage to complete at nearly same time, then spins is increased only by one.

spins_avg starts as 0, and tst_exp_moving_avg() will move it closer and closer to 1, but it will never be 1:

Quoted 7 lines
        float f = 0; int i;
        for (i = 0; i < 4096; i++)
                f = tst_exp_moving_avg(0.25f, 1, f);
        printf("%.15f %d\n", f, f >= 1);
	$ 0.999999880790710 0

which on rare occasion can cause: "Can't calculate random delay".

I'm surprised that you have seen this. Out of interest, does this happen on a particular setup, or just randomly?

Quoted 23 lines

Compare against float slightly smaller than 1. Also print stats one more time when we hit the warning.

---
 include/tst_fuzzy_sync.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/tst_fuzzy_sync.h +2 -0
index de0402c9bbe9..2c0389c5d63e 100644
@@ -73,6 +73,8 @@
 /* how much of exec time is sampling allowed to take */
 #define SAMPLING_SLICE 0.5f
 
+#define EPSILON 0.999f
+
 /** Some statistics for a variable */
 struct tst_fzsync_stat {
 	float avg;

include/tst_fuzzy_sync.h static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair) · line 483

Quoted 6 lines
include/tst_fuzzy_sync.h +1 -1
@@ -477,7 +479,7 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 			tst_res(TINFO, "Minimum sampling period ended");
 			tst_fzsync_pair_info(pair);
 		}
-	} else if (fabsf(pair->diff_ab.avg) >= 1 && pair->spins_avg.avg >= 1) {
+	} else if (fabsf(pair->diff_ab.avg) > EPSILON && pair->spins_avg.avg > EPSILON) {
 		per_spin_time = fabsf(pair->diff_ab.avg) / pair->spins_avg.avg;

I suppose you could also use MAX(pair->spins_avg.avg, 1f) and drop the check. It might even be OK to only check that it is greater than zero. I'm not sure if the number of delay spins will be sane either way, but it probably can't do any harm.

include/tst_fuzzy_sync.h static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair) · line 502

Quoted 9 lines
include/tst_fuzzy_sync.h +1 -0
 		time_delay = drand48() * (pair->diff_sa.avg + pair->diff_sb.avg)
 			- pair->diff_sb.avg;
@@ -495,6 +497,7 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 		}
 	} else if (!pair->sampling) {
 		tst_res(TWARN, "Can't calculate random delay");
+		tst_fzsync_pair_info(pair);
 		pair->sampling = -1;
 	}

--
Thank you,
Richard.

#2 lore raw

PATCH 1/1 fzsync: don't compare spins_avg (float) against 1 (int)

----- Original Message -----

Quoted 16 lines

Hello,

Quoted 12 lines · Jan Stancek

tst_fzsync_pair.spins is increased at least by one on every iteration. If during all calibration loops, both A and B manage to complete at nearly same time, then spins is increased only by one.

spins_avg starts as 0, and tst_exp_moving_avg() will move it closer and closer to 1, but it will never be 1:

        float f = 0; int i;
        for (i = 0; i < 4096; i++)
                f = tst_exp_moving_avg(0.25f, 1, f);
        printf("%.15f %d\n", f, f >= 1);
	$ 0.999999880790710 0
Quoted 4 lines
Quoted 1 line

which on rare occasion can cause: "Can't calculate random delay".

I'm surprised that you have seen this. Out of interest, does this happen on a particular setup, or just randomly?

It happens very rarely on power9 lpar running fedora 30 and cve-2017-2671, where there's only single syscall in fuzzing section.

It takes couple hours to reproduce on demand.

Quoted 29 lines
Quoted 29 lines

Compare against float slightly smaller than 1. Also print stats one more time when we hit the warning.

---
 include/tst_fuzzy_sync.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/tst_fuzzy_sync.h +2 -1
index de0402c9bbe9..2c0389c5d63e 100644
@@ -73,6 +73,8 @@
 /* how much of exec time is sampling allowed to take */
 #define SAMPLING_SLICE 0.5f
 
+#define EPSILON 0.999f
+
 /** Some statistics for a variable */
 struct tst_fzsync_stat {
 	float avg;
@@ -477,7 +479,7 @@ static void tst_fzsync_pair_update(struct
 tst_fzsync_pair *pair)
 			tst_res(TINFO, "Minimum sampling period ended");
 			tst_fzsync_pair_info(pair);
 		}
-	} else if (fabsf(pair->diff_ab.avg) >= 1 && pair->spins_avg.avg >= 1) {
Quoted 8 lines

include/tst_fuzzy_sync.h static void tst_fzsync_pair_update(struct · line 485

Quoted 3 lines
include/tst_fuzzy_sync.h +1 -0
+	} else if (fabsf(pair->diff_ab.avg) > EPSILON && pair->spins_avg.avg >
 EPSILON) {
 		per_spin_time = fabsf(pair->diff_ab.avg) / pair->spins_avg.avg;

I suppose you could also use MAX(pair->spins_avg.avg, 1f) and drop the check. It might even be OK to only check that it is greater than zero. I'm not sure if the number of delay spins will be sane either way, but it probably can't do any harm.

I'll send v2.

Thanks,
Jan

Quoted 17 lines

include/tst_fuzzy_sync.h static void tst_fzsync_pair_update(struct · line 503

Quoted 11 lines
include/tst_fuzzy_sync.h +1 -0
 		time_delay = drand48() * (pair->diff_sa.avg + pair->diff_sb.avg)
 			- pair->diff_sb.avg;
@@ -495,6 +497,7 @@ static void tst_fzsync_pair_update(struct
 tst_fzsync_pair *pair)
 		}
 	} else if (!pair->sampling) {
 		tst_res(TWARN, "Can't calculate random delay");
+		tst_fzsync_pair_info(pair);
 		pair->sampling = -1;
 	}

--
Thank you,
Richard.

#3 lore raw

PATCH v2 1/1 fzsync: drop pair->spins_avg.avg check

In reply to Jan Stancek

tst_fzsync_pair.spins is increased at least by one on every iteration. If during all calibration loops, both A and B manage to complete at nearly same time, then spins is increased only by one.

spins_avg starts as 0, and tst_exp_moving_avg() will move it closer and closer to 1, but it will never be 1:

        float f = 0; int i;
        for (i = 0; i < 4096; i++)
                f = tst_exp_moving_avg(0.25f, 1, f);
        printf("%.15f %d\n", f, f >= 1);
	$ 0.999999880790710 0

which on rare occasion can cause: "Can't calculate random delay".

Drop check and use MAX(pair->spins_avg.avg, 1f) to calculate per_spin_time. Also print stats one more time when we hit the warning.

---
 include/tst_fuzzy_sync.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/tst_fuzzy_sync.h +3 -2
index de0402c9bbe9..f9a1947c7542 100644
@@ -477,8 +477,8 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 			tst_res(TINFO, "Minimum sampling period ended");
 			tst_fzsync_pair_info(pair);
 		}
-	} else if (fabsf(pair->diff_ab.avg) >= 1 && pair->spins_avg.avg >= 1) {
-		per_spin_time = fabsf(pair->diff_ab.avg) / pair->spins_avg.avg;
+	} else if (fabsf(pair->diff_ab.avg) >= 1) {
+		per_spin_time = fabsf(pair->diff_ab.avg) / MAX(pair->spins_avg.avg, 1.0f);
 		time_delay = drand48() * (pair->diff_sa.avg + pair->diff_sb.avg)
 			- pair->diff_sb.avg;
 		pair->delay += (int)(time_delay / per_spin_time);
@@ -495,6 +495,7 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 		}
 	} else if (!pair->sampling) {
 		tst_res(TWARN, "Can't calculate random delay");
+		tst_fzsync_pair_info(pair);
 		pair->sampling = -1;
 	}
 
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v2 1/1 fzsync: drop pair->spins_avg.avg check

Hello,

Quoted 38 lines · Jan Stancek

tst_fzsync_pair.spins is increased at least by one on every iteration. If during all calibration loops, both A and B manage to complete at nearly same time, then spins is increased only by one.

spins_avg starts as 0, and tst_exp_moving_avg() will move it closer and closer to 1, but it will never be 1:

        float f = 0; int i;
        for (i = 0; i < 4096; i++)
                f = tst_exp_moving_avg(0.25f, 1, f);
        printf("%.15f %d\n", f, f >= 1);
	$ 0.999999880790710 0

which on rare occasion can cause: "Can't calculate random delay".

Drop check and use MAX(pair->spins_avg.avg, 1f) to calculate per_spin_time. Also print stats one more time when we hit the warning.

---
 include/tst_fuzzy_sync.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/tst_fuzzy_sync.h +2 -2
index de0402c9bbe9..f9a1947c7542 100644
@@ -477,8 +477,8 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 			tst_res(TINFO, "Minimum sampling period ended");
 			tst_fzsync_pair_info(pair);
 		}
-	} else if (fabsf(pair->diff_ab.avg) >= 1 && pair->spins_avg.avg >= 1) {
-		per_spin_time = fabsf(pair->diff_ab.avg) / pair->spins_avg.avg;
+	} else if (fabsf(pair->diff_ab.avg) >= 1) {
+		per_spin_time = fabsf(pair->diff_ab.avg) / MAX(pair->spins_avg.avg, 1.0f);
 		time_delay = drand48() * (pair->diff_sa.avg + pair->diff_sb.avg)
 			- pair->diff_sb.avg;
 		pair->delay += (int)(time_delay / per_spin_time);

include/tst_fuzzy_sync.h static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair) · line 500

Quoted 6 lines
include/tst_fuzzy_sync.h +1 -0
@@ -495,6 +495,7 @@ static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
 		}
 	} else if (!pair->sampling) {
 		tst_res(TWARN, "Can't calculate random delay");
+		tst_fzsync_pair_info(pair);
 		pair->sampling = -1;
 	}

LGTM

Signature · 2 lines
Thank you,
Richard.
#5 lore raw

PATCH v2 1/1 fzsync: drop pair->spins_avg.avg check

----- Original Message -----

Quoted 45 lines

Hello,

Quoted 41 lines · Jan Stancek

tst_fzsync_pair.spins is increased at least by one on every iteration. If during all calibration loops, both A and B manage to complete at nearly same time, then spins is increased only by one.

spins_avg starts as 0, and tst_exp_moving_avg() will move it closer and closer to 1, but it will never be 1:

        float f = 0; int i;
        for (i = 0; i < 4096; i++)
                f = tst_exp_moving_avg(0.25f, 1, f);
        printf("%.15f %d\n", f, f >= 1);
	$ 0.999999880790710 0

which on rare occasion can cause: "Can't calculate random delay".

Drop check and use MAX(pair->spins_avg.avg, 1f) to calculate per_spin_time. Also print stats one more time when we hit the warning.

---
 include/tst_fuzzy_sync.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/tst_fuzzy_sync.h +2 -2
index de0402c9bbe9..f9a1947c7542 100644
@@ -477,8 +477,8 @@ static void tst_fzsync_pair_update(struct
 tst_fzsync_pair *pair)
 			tst_res(TINFO, "Minimum sampling period ended");
 			tst_fzsync_pair_info(pair);
 		}
-	} else if (fabsf(pair->diff_ab.avg) >= 1 && pair->spins_avg.avg >= 1) {
-		per_spin_time = fabsf(pair->diff_ab.avg) / pair->spins_avg.avg;
+	} else if (fabsf(pair->diff_ab.avg) >= 1) {
+		per_spin_time = fabsf(pair->diff_ab.avg) / MAX(pair->spins_avg.avg,
 1.0f);
 		time_delay = drand48() * (pair->diff_sa.avg + pair->diff_sb.avg)
include/tst_fuzzy_sync.h +0 -0
 			- pair->diff_sb.avg;
 		pair->delay += (int)(time_delay / per_spin_time);
@@ -495,6 +495,7 @@ static void tst_fzsync_pair_update(struct
 tst_fzsync_pair *pair)
Quoted 8 lines

include/tst_fuzzy_sync.h static void tst_fzsync_pair_update(struct tst_fzsync_pair *pair) · line 500

Quoted 6 lines
include/tst_fuzzy_sync.h +1 -0
 		}
 	} else if (!pair->sampling) {
 		tst_res(TWARN, "Can't calculate random delay");
+		tst_fzsync_pair_info(pair);
 		pair->sampling = -1;
 	}

LGTM

Pushed.