Fixup some of the time (usec) based conversions

Modified by Jens to fixup some of the mtime/utime confusion,
and add a specific ->is_time to options to get rid of future
issues in this area.

Signed-off-by: Jens Axboe <axboe@fb.com>
diff --git a/eta.c b/eta.c
index db08e2a..5be5aed 100644
--- a/eta.c
+++ b/eta.c
@@ -555,8 +555,7 @@
 	if (!eta_new_line_init) {
 		fio_gettime(&disp_eta_new_line, NULL);
 		eta_new_line_init = 1;
-	} else if (eta_new_line &&
-		   mtime_since_now(&disp_eta_new_line) > eta_new_line * 1000) {
+	} else if (eta_new_line && mtime_since_now(&disp_eta_new_line) > eta_new_line) {
 		fio_gettime(&disp_eta_new_line, NULL);
 		eta_new_line_pending = 1;
 	}
diff --git a/exp/expression-parser.l b/exp/expression-parser.l
index 856596a..50bd383 100644
--- a/exp/expression-parser.l
+++ b/exp/expression-parser.l
@@ -42,6 +42,9 @@
 static void __attribute__((unused)) yyunput(int c, char *buf_ptr);
 static int __attribute__((unused)) input(void);
 
+/* set by parser -- this is another thing which makes the parser thread-unsafe :(. */
+int lexer_value_is_time = 0; /* for determining if "m" suffix means mega- or minutes */
+
 #define set_suffix_value(yylval, i_val, d_val, has_d_val) \
 	(yylval).v.dval = (d_val); \
 	(yylval).v.ival = (i_val); \
@@ -57,7 +60,7 @@
 			set_suffix_value(yylval, 1024, 1024.0, 0);
 			return SUFFIX;
 		}
-[Mm]|[Mm][bB]	{
+[Mm][bB]	{
 			set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0);
 			return SUFFIX;
 		}
@@ -103,6 +106,14 @@
 			set_suffix_value(yylval, 1000000LL, 1000000.0 , 0);
 			return SUFFIX;
 		}
+[mM]		{
+			if (!lexer_value_is_time) {
+				set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0);
+			} else {
+				set_suffix_value(yylval, 60LL * 1000000LL, 60.0 * 1000000.0, 0);
+			}
+			return SUFFIX;
+		}
 [dD]		{
 			set_suffix_value(yylval, 60LL * 60LL * 24LL * 1000000LL,
 						60.0 * 60.0 * 24.0 * 1000000.0, 0);
diff --git a/exp/expression-parser.y b/exp/expression-parser.y
index 8ae0c01..d664b8e 100644
--- a/exp/expression-parser.y
+++ b/exp/expression-parser.y
@@ -43,6 +43,7 @@
 
 extern int yylex(void);
 extern void yyrestart(FILE *file);
+extern int lexer_value_is_time;
 
 %}
 
@@ -214,10 +215,11 @@
 }
 
 int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval,
-					double implied_units)
+					double implied_units, int is_time)
 {
 	int rc, units_specified = 0, has_error = 0;
 
+	lexer_value_is_time = is_time;
 	setup_to_parse_string(buffer);
 	rc = yyparse(ival, dval, &has_error, &units_specified);
 	yyrestart(NULL);
diff --git a/exp/test-expression-parser.c b/exp/test-expression-parser.c
index 93c766a..bf3fb3e 100644
--- a/exp/test-expression-parser.c
+++ b/exp/test-expression-parser.c
@@ -25,7 +25,7 @@
 #include "../y.tab.h"
 
 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
-					  double *dval, double implied_units);
+					  double *dval, double implied_units, int is_time);
  
 int main(int argc, char *argv[])
 {
@@ -40,7 +40,7 @@
 		rc = strlen(buffer);
 		if (rc > 0 && buffer[rc - 1] == '\n')
 			buffer[rc - 1] = '\0';
-		rc = evaluate_arithmetic_expression(buffer, &result, &dresult, 1.0);
+		rc = evaluate_arithmetic_expression(buffer, &result, &dresult, 1.0, 0);
 		if (!rc) {
 			printf("%lld (%20.20lf)\n", result, dresult);
 		} else {
diff --git a/init.c b/init.c
index c2c126b..70d5d06 100644
--- a/init.c
+++ b/init.c
@@ -1968,12 +1968,12 @@
 		case 'E': {
 			long long t = 0;
 
-			if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
+			if (check_str_time(optarg, &t, 1)) {
 				log_err("fio: failed parsing eta time %s\n", optarg);
 				exit_val = 1;
 				do_exit++;
 			}
-			eta_new_line = t;
+			eta_new_line = t / 1000;
 			break;
 			}
 		case 'd':
@@ -2175,13 +2175,13 @@
 		case 'L': {
 			long long val;
 
-			if (check_str_time(optarg, &val, 0)) {
+			if (check_str_time(optarg, &val, 1)) {
 				log_err("fio: failed parsing time %s\n", optarg);
 				do_exit++;
 				exit_val = 1;
 				break;
 			}
-			status_interval = val * 1000;
+			status_interval = val / 1000;
 			break;
 			}
 		case '?':
diff --git a/options.c b/options.c
index 529b8f0..ac09480 100644
--- a/options.c
+++ b/options.c
@@ -102,7 +102,7 @@
 		} else
 			perc = -1U;
 
-		if (str_to_decimal(fname, &val, 1, o, 0)) {
+		if (str_to_decimal(fname, &val, 1, o, 0, 0)) {
 			log_err("fio: bssplit conversion failed\n");
 			free(bssplit);
 			return 1;
@@ -342,7 +342,7 @@
 	else {
 		long long val;
 
-		if (str_to_decimal(nr, &val, 1, o, 0)) {
+		if (str_to_decimal(nr, &val, 1, o, 0, 0)) {
 			log_err("fio: rw postfix parsing failed\n");
 			free(nr);
 			return 1;
@@ -738,7 +738,7 @@
 		return 0;
 
 	nr = get_opt_postfix(str);
-	if (nr && !str_to_float(nr, &val)) {
+	if (nr && !str_to_float(nr, &val, 0)) {
 		log_err("fio: random postfix parsing failed\n");
 		free(nr);
 		return 1;
@@ -2177,6 +2177,7 @@
 		.help	= "Only start job when this period has passed",
 		.def	= "0",
 		.is_seconds = 1,
+		.is_time = 1,
 		.category = FIO_OPT_C_GENERAL,
 		.group	= FIO_OPT_G_RUNTIME,
 	},
@@ -2189,6 +2190,7 @@
 		.help	= "Stop workload when this amount of time has passed",
 		.def	= "0",
 		.is_seconds = 1,
+		.is_time = 1,
 		.category = FIO_OPT_C_GENERAL,
 		.group	= FIO_OPT_G_RUNTIME,
 	},
@@ -2217,6 +2219,7 @@
 		.off1	= td_var_offset(ramp_time),
 		.help	= "Ramp up time before measuring performance",
 		.is_seconds = 1,
+		.is_time = 1,
 		.category = FIO_OPT_C_GENERAL,
 		.group	= FIO_OPT_G_RUNTIME,
 	},
@@ -2770,6 +2773,7 @@
 		.off1	= td_var_offset(thinktime),
 		.help	= "Idle time between IO buffers (usec)",
 		.def	= "0",
+		.is_time = 1,
 		.category = FIO_OPT_C_IO,
 		.group	= FIO_OPT_G_THINKTIME,
 	},
@@ -2780,6 +2784,7 @@
 		.off1	= td_var_offset(thinktime_spin),
 		.help	= "Start think time by spinning this amount (usec)",
 		.def	= "0",
+		.is_time = 1,
 		.parent	= "thinktime",
 		.hide	= 1,
 		.category = FIO_OPT_C_IO,
@@ -2863,6 +2868,7 @@
 		.type	= FIO_OPT_INT,
 		.off1	= td_var_offset(max_latency),
 		.help	= "Maximum tolerated IO latency (usec)",
+		.is_time = 1,
 		.category = FIO_OPT_C_IO,
 		.group = FIO_OPT_G_LATPROF,
 	},
@@ -2872,6 +2878,7 @@
 		.type	= FIO_OPT_STR_VAL_TIME,
 		.off1	= td_var_offset(latency_target),
 		.help	= "Ramp to max queue depth supporting this latency",
+		.is_time = 1,
 		.category = FIO_OPT_C_IO,
 		.group	= FIO_OPT_G_LATPROF,
 	},
@@ -2881,6 +2888,7 @@
 		.type	= FIO_OPT_STR_VAL_TIME,
 		.off1	= td_var_offset(latency_window),
 		.help	= "Time to sustain latency_target",
+		.is_time = 1,
 		.category = FIO_OPT_C_IO,
 		.group	= FIO_OPT_G_LATPROF,
 	},
diff --git a/parse.c b/parse.c
index 282baa4..4209647 100644
--- a/parse.c
+++ b/parse.c
@@ -269,7 +269,8 @@
 }
 
 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
-					  double *dval, double implied_units);
+					  double *dval, double implied_units,
+					  int is_time);
 
 #ifdef CONFIG_ARITHMETIC
 /*
@@ -278,7 +279,7 @@
  * original number parsing code.  Once sufficiently sure that the arithmetic
  * code is always getting the right answers, these can be removed.
  */
-static void verify_exp_parser_float(const char *str, double implied_units)
+static void verify_exp_parser_float(const char *str, double implied_units, int is_time)
 {
 	long long ival;
 	double dval, tmpval;
@@ -286,7 +287,7 @@
 	if (sscanf(str, "%lf", &tmpval) != 1)
 		return;
 
-	if (evaluate_arithmetic_expression(str, &ival, &dval, implied_units) != 0) {
+	if (evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time) != 0) {
 		log_info("Arithmetic failed on '%s'\n", str);
 		return;
 	}
@@ -296,7 +297,8 @@
 	}
 }
 
-static void verify_exp_parser_decimal(const char *str, long long val, int kilo, int is_seconds)
+static void verify_exp_parser_decimal(const char *str, long long val, int kilo, int is_seconds,
+				      int is_time)
 {
 	int rc;
 	long long ival;
@@ -306,7 +308,7 @@
 	if (is_seconds)
 		implied_units = 1000000.0;
 
-	rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units);
+	rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
 	if (!rc) {
 		if (ival != val)
 			log_info("Arithmetic failed on '%s', expected %lld, got %lld\n",
@@ -320,7 +322,7 @@
 /*
  * Convert string into a floating number. Return 1 for success and 0 otherwise.
  */
-int str_to_float(const char *str, double *val)
+int str_to_float(const char *str, double *val, int is_time)
 {
 #ifdef CONFIG_ARITHMETIC
 	int rc;
@@ -328,13 +330,13 @@
 	double dval;
 
 	if (str[0] == '(') {
-		rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0);
+		rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
 		if (!rc) {
 			*val = dval;
 			return 1;
 		}
 	} else {
-		verify_exp_parser_float(str, 1.0);
+		verify_exp_parser_float(str, 1.0, is_time);
 	}
 #endif
 	return 1 == sscanf(str, "%lf", val);
@@ -344,7 +346,7 @@
  * convert string into decimal value, noting any size suffix
  */
 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
-		   int is_seconds)
+		   int is_seconds, int is_time)
 {
 	int len, base;
 	int rc = 1;
@@ -362,7 +364,7 @@
 	if (is_seconds)
 		implied_units = 1000000.0;
 	if (str[0] == '(')
-		rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units);
+		rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
 	if (str[0] == '(' && !rc) {
 		if (!kilo && is_seconds)
 			*val = ival / 1000000LL;
@@ -394,19 +396,19 @@
 	} else
 		*val *= get_mult_time(str, len, is_seconds);
 #ifdef CONFIG_ARITHMETIC
-	verify_exp_parser_decimal(str, *val, kilo, is_seconds);
+	verify_exp_parser_decimal(str, *val, kilo, is_seconds, is_time);
 #endif
 	return 0;
 }
 
 int check_str_bytes(const char *p, long long *val, void *data)
 {
-	return str_to_decimal(p, val, 1, data, 0);
+	return str_to_decimal(p, val, 1, data, 0, 0);
 }
 
 int check_str_time(const char *p, long long *val, int is_seconds)
 {
-	return str_to_decimal(p, val, 0, NULL, is_seconds);
+	return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
 }
 
 void strip_blank_front(char **p)
@@ -448,7 +450,7 @@
 {
 	long long __val;
 
-	if (!str_to_decimal(str, &__val, 1, data, 0)) {
+	if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
 		*val = __val;
 		return 0;
 	}
@@ -552,6 +554,9 @@
 		fio_opt_str_val_fn *fn = o->cb;
 		char tmp[128], *p;
 
+		if (!is_time && o->is_time)
+			is_time = o->is_time;
+
 		strncpy(tmp, ptr, sizeof(tmp) - 1);
 		p = strchr(tmp, ',');
 		if (p)
@@ -655,7 +660,7 @@
 					o->maxlen);
 			return 1;
 		}
-		if (!str_to_float(ptr, &uf)) {
+		if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
 			log_err("not a floating point value: %s\n", ptr);
 			return 1;
 		}
diff --git a/parse.h b/parse.h
index 2a1e06a..a9d726d 100644
--- a/parse.h
+++ b/parse.h
@@ -73,6 +73,7 @@
 	unsigned int group;		/* who to group with */
 	void *gui_data;
 	int is_seconds;			/* time value with seconds base */
+	int is_time;			/* time based value */
 	int no_warn_def;
 };
 
@@ -89,10 +90,10 @@
 
 extern void strip_blank_front(char **);
 extern void strip_blank_end(char *);
-extern int str_to_decimal(const char *, long long *, int, void *, int);
+extern int str_to_decimal(const char *, long long *, int, void *, int, int);
 extern int check_str_bytes(const char *p, long long *val, void *data);
 extern int check_str_time(const char *p, long long *val, int);
-extern int str_to_float(const char *str, double *val);
+extern int str_to_float(const char *str, double *val, int is_time);
 
 /*
  * Handlers for the options