[PATCH] lib: fwts_cpu: force compiler to increment i rather than optimize it out

Colin King colin.king at canonical.com
Tue Jan 24 12:22:49 UTC 2017


From: Colin Ian King <colin.king at canonical.com>

Currently the increment of i is being optimized out, so force it to be
volatile (to force stores) and return it too to force the compiler not
to optimize the cycle wasting increment out.

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 src/lib/src/fwts_cpu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/lib/src/fwts_cpu.c b/src/lib/src/fwts_cpu.c
index b4f256e..148857e 100644
--- a/src/lib/src/fwts_cpu.c
+++ b/src/lib/src/fwts_cpu.c
@@ -512,15 +512,16 @@ uint64_t fwts_cpu_benchmark_best_result(fwts_cpu_benchmark_result *res)
  *  fwts_cpu_consume_cycles()
  *	eat up CPU cycles
  */
-static void fwts_cpu_consume_cycles(void)
+static uint64_t fwts_cpu_consume_cycles(void)
 {
 	fwts_sig_handler_set(SIGUSR1, fwts_cpu_consume_sighandler, NULL);
-	uint64_t i = 0;
+	volatile uint64_t i = 0;
 
 	for (;;) {
 		fwts_cpu_burn_cycles();
 		i++;
 	}
+	return i;
 }
 
 /*
@@ -554,7 +555,7 @@ int fwts_cpu_consume_start(void)
 		pid = fork();
 		switch (pid) {
 		case 0: /* Child */
-			fwts_cpu_consume_cycles();
+			(void)fwts_cpu_consume_cycles();
 			break;
 		case -1:
 			/* Went wrong */
-- 
2.10.2




More information about the fwts-devel mailing list